From owner-freebsd-hackers@FreeBSD.ORG Wed Sep 26 08:14:52 2012 Return-Path: Delivered-To: freebsd-hackers@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id BDC41106566B for ; Wed, 26 Sep 2012 08:14:52 +0000 (UTC) (envelope-from avg@FreeBSD.org) Received: from citadel.icyb.net.ua (citadel.icyb.net.ua [212.40.38.140]) by mx1.freebsd.org (Postfix) with ESMTP id 14F928FC15 for ; Wed, 26 Sep 2012 08:14:51 +0000 (UTC) Received: from porto.starpoint.kiev.ua (porto-e.starpoint.kiev.ua [212.40.38.100]) by citadel.icyb.net.ua (8.8.8p3/ICyb-2.3exp) with ESMTP id LAA18651 for ; Wed, 26 Sep 2012 11:14:44 +0300 (EEST) (envelope-from avg@FreeBSD.org) Received: from localhost ([127.0.0.1]) by porto.starpoint.kiev.ua with esmtp (Exim 4.34 (FreeBSD)) id 1TGmlX-000CQY-Sn for freebsd-hackers@freebsd.org; Wed, 26 Sep 2012 11:14:43 +0300 Message-ID: <5062B971.30203@FreeBSD.org> Date: Wed, 26 Sep 2012 11:14:41 +0300 From: Andriy Gapon User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:15.0) Gecko/20120913 Thunderbird/15.0.1 MIME-Version: 1.0 To: freebsd-hackers X-Enigmail-Version: 1.4.3 Content-Type: text/plain; charset=X-VIET-VPS Content-Transfer-Encoding: 7bit Cc: Subject: monitor+mwait and volatile-ish X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Sep 2012 08:14:52 -0000 Typical x86 MONITOR+MWAIT is like this (taken from Intel manual): EAX = Logical Address(Trigger) ECX = 0 (*Hints *) EDX = 0 (* Hints *) IF ( !trigger_store_happened) { MONITOR EAX, ECX, EDX IF ( !trigger_store_happened ) { MWAIT EAX, ECX } } In FreeBSD we have this helper function for MONITOR: static __inline void cpu_monitor(const void *addr, u_long extensions, u_int hints) { __asm __volatile("monitor" : : "a" (addr), "c" (extensions), "d" (hints)); } Now, let's assume that 'Trigger' is a global variable and 'trigger_store_happened' is a check for a particular value in this variable. Then, with current state of matters, we must either declare the global variable to be volatile or use a volatile cast in the second IF. Otherwise, a compiler is free to assume that the variable doesn't change between the first and the second IF and drop the second IF altogether. And that would make MONITOR+MWAIT to miss an event if it happens "between" the first check and MONITOR. So what's my point. - using volatile variable with cpu_monitor requires DEVOLATILE to silence compiler warning about discarding volatile; this is unnecessary code bloat - adding volatile cast in the checks is easy to forget and adds code bloat Possible improvements: - make the argument of cpu_monitor be 'const volatile void *', the most permissive type; this would also be a hint that variable should be volatile - add some magic dust to cpu_monitor that would tell compiler to not cache the variable; right now I can only think of the "memory" constraint, but it seems to be too big of a hummer What do you think about this? -- Andriy Gapon