From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6655 invoked by alias); 22 Aug 2007 23:17:26 -0000 Received: (qmail 6079 invoked by uid 22791); 22 Aug 2007 23:17:22 -0000 X-Spam-Check-By: sourceware.org Received: from mail87.messagelabs.com (HELO mail87.messagelabs.com) (216.82.250.19) by sourceware.org (qpsmtpd/0.31) with SMTP; Wed, 22 Aug 2007 23:17:18 +0000 X-VirusChecked: Checked X-Env-Sender: John.Bossom@Cognos.COM X-Msg-Ref: server-14.tower-87.messagelabs.com!1187824636!11424845!1 X-StarScan-Version: 5.5.12.14.2; banners=-,-,- Received: (qmail 23666 invoked from network); 22 Aug 2007 23:17:16 -0000 Received: from nomad.cognos.com (HELO nomad.cognos.com) (205.210.232.62) by server-14.tower-87.messagelabs.com with SMTP; 22 Aug 2007 23:17:16 -0000 Received: from sottemail2.ent.ad.cognos.com ([10.67.10.28]) by nomad.cognos.com with Microsoft SMTPSVC(6.0.3790.1830); Wed, 22 Aug 2007 19:17:15 -0400 Content-Class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Subject: RE: patch for sched_yield() Date: Sat, 29 Sep 2007 22:29:00 -0000 Message-ID: <4ABB890A1781774888DB6505E6F0E3B30D6AF0@sottemail2.ent.ad.cognos.com> In-Reply-To: <353903.10622.qm@web32615.mail.mud.yahoo.com> From: "Bossom, John" To: "Virgilio Alexandre Fornazin" , X-IsSubscribed: yes Mailing-List: contact pthreads-win32-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: pthreads-win32-owner@sourceware.org X-SW-Source: 2007/txt/msg00043.txt.bz2 Sorry, but I do not believe this patch is thread safe... You have used a static variable (i.e. read a private global variable) decla= red within the function but did not provide any locking strategy around the= initial assignment. On a true multi-processor machine where multiple threa= ds call sched_yield() at precisely the same time, you may end up with garba= ge in the variable. 1) there may be a race condition to actually assign the initial value of th= e variable, depending on how the compiler generates the assignment to zero. If it is= deferred until the method is actually called, then it would have generated a hidd= en test to see if this is the first time the method is called in order to perfor= m the assignment once. This "hidden" test is not thread safe, if generated. Recommendation: move the static to the global scope so that the code gen= eration is performed either when the object file was compiled, o= r when the code is initially loaded, but before user functions = are declared. 2) Your test for zero should be followed by a lock to protect the variable = and then=20 re-test for zero and then perform the assignment. i.e.) if( variable =3D=3D 0 ){ LOCK( m ); if( variable =3D=3D 0 ){ variable =3D value; } UNLOCK( m ) } John.=20 -----Original Message----- From: pthreads-win32-owner@sourceware.org [mailto:pthreads-win32-owner@sour= ceware.org] On Behalf Of Virgilio Alexandre Fornazin Sent: Wednesday, August 22, 2007 4:39 PM To: pthreads-win32@sourceware.org Subject: patch for sched_yield() Hi I did a change in sched_yield() to support multi-processor systems after ch= ecking the OS (NT style oses). I managed to compile it under VS2005 SP1. I'm putting the code here to share with pthreads project. I changed the implementation from: { Sleep (0); return 0; } To: { static OSVERSIONINFO vs =3D {0}; if (vs.dwOSVersionInfoSize =3D=3D 0) { vs.dwOSVersionInfoSize =3D sizeof(vs); =20=20 GetVersionEx(&vs); } if (vs.dwPlatformId =3D=3D VER_PLATFORM_WIN32_NT) { SwitchToThread (); } else { Sleep (0); } return 0; } I saw in another mailing list that Sleep(0) sometimes doesn't yield the thr= ead and performs nothing on some Windows versions. I hope this help somebody and could be integrated to main code after more t= esting with other cpu/configurations. Flickr agora em portugu=EAs. Voc=EA clica, todo mundo v=EA. http://www.flickr.com.br/ =20 This message may contain privileged and/or confidential information. = If you have received this e-mail in error or are not the intended recipient= , you may not use, copy, disseminate or distribute it; do not open any atta= chments, delete it immediately from your system and notify the sender promp= tly by e-mail that you have done so. Thank you.