Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934127AbXEERpt (ORCPT ); Sat, 5 May 2007 13:45:49 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S934139AbXEERpt (ORCPT ); Sat, 5 May 2007 13:45:49 -0400 Received: from smtp1.linux-foundation.org ([65.172.181.25]:36662 "EHLO smtp1.linux-foundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934127AbXEERps (ORCPT ); Sat, 5 May 2007 13:45:48 -0400 Date: Sat, 5 May 2007 10:44:28 -0700 (PDT) From: Linus Torvalds To: Esben Nielsen cc: Ingo Molnar , Balbir Singh , linux-kernel@vger.kernel.org, Andrew Morton , Con Kolivas , Nick Piggin , Mike Galbraith , Arjan van de Ven , Peter Williams , Thomas Gleixner , caglar@pardus.org.tr, Willy Tarreau , Gene Heskett , Mark Lord , Zach Carter , buddabrod Subject: Re: [patch] CFS scheduler, -v8 In-Reply-To: Message-ID: References: <20070501212223.GA29867@elte.hu> <463854F3.3020403@linux.vnet.ibm.com> <20070502100545.GA6857@elte.hu> <46386F2B.9050307@linux.vnet.ibm.com> <20070502111742.GA18132@elte.hu> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=us-ascii Sender: linux-kernel-owner@vger.kernel.org X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1323 Lines: 36 On Sat, 5 May 2007, Esben Nielsen wrote: > > I have been wondering why you use usigned for timers anyway. It is also like > that in hrtimers. Why not use signed and avoid (almost) all worries about wrap > around issues. The trick is that when all > a < b > is be replaced by > a - b < 0 > the code will work on all 2-complement machines even if the (signed!) integers > a and b wrap around. No. BOTH of the above are buggy. The C language definition doesn't allow signed integers to wrap (ie it's undefined behaviour), so "a-b < 0" can be rewritten by the compiler as a simple signed "a < b". And the unsigned (or signed) "a < b" is just broken wrt any kind of wrap-around (whether wrapping around zero or the sign bit). So the _only_ valid way to handle timers is to - either not allow wrapping at all (in which case "unsigned" is better, since it is bigger) - or use wrapping explicitly, and use unsigned arithmetic (which is well-defined in C) and do something like "(long)(a-b) > 0". Notice? The signed variant is basically _never_ correct. Linus - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/