[M3devel] race condition setting act.handle?

Jay K jay.krell at cornell.edu
Mon Dec 7 13:48:15 CET 2009


I think I resolved this in a simple efficient manner. Please take a look. Perhaps pthread_create is guaranteed to write back the pthread_t before the code runs though?

 

Win32 isn't as amenable to the efficiency here due to the division between handles and thread ids.

pthread I'm not really so familiar with, not sure of the efficiency.

In particular, where pthreads has pthread_self(), Win32 has three analogs:

 GetCurrentThreadId() 

 GetCurrentThread() 

 DuplicateHandle(GetCurrentThread()...) 

 

GetCurrentThread and GetCurrentThreadId are both fast -- no kernel call.

However you can't do all that much with their results, e.g. you can't Suspend/Resume/Close/Wait.

  GetCurrentThread returns a special /constant/ that always means the current thread, relative to the caller.

DuplicateHandle(GetCurrentThread()...) gives you something you can Suspend/Resume/Close/Wait but of course is slower, a kernel call.

I have to pay for either the DuplicateHandle or ResumeThread, a wash.

 

pthreads just has the one pthread_self. Fast? I don't know. A kernel call? I don't know. Usable with suspend/resume (np)? Yes.

 

Well on Solaris it is fast:

-bash-3.00$ /usr/ccs/bin/dis -F pthread_self /usr/lib/libc.so
disassembly for /usr/lib/libc.so

thr_self()
    thr_self:               81 c3 e0 08  retl
    thr_self+0x4:           d0 01 e0 98  ld        [%g7 + 0x98], %o0

 

 

Recent Linux/x86 is fast:

 

objdump -d /usr/lib/libpthread_a


00001020 <__pthread_self>:
    1020:       55                      push   %ebp
    1021:       89 e5                   mov    %esp,%ebp
    1023:       65 a1 50 00 00 00       mov    %gs:0x50,%eax
    1029:       5d                      pop    %ebp
    102a:       c3                      ret
    102b:       90                      nop
    102c:       8d 74 26 00             lea    0x0(%esi),%esi


 

(geez though, six instructions where only two are needed, crazy..imagine if all code was unoptimal by that factor...)

 

 - Jay

 


From: jay.krell at cornell.edu
To: hosking at cs.purdue.edu; m3devel at elegosoft.com
Subject: race condition setting act.handle?
Date: Mon, 7 Dec 2009 06:55:08 +0000



pthread_create(act.handle) followed at some point by use of act.handle in the new thread.
  Or possibly getting on the list of threads in the new thread and then a third thread looking at that list.


What is to cause act.handle to be stored before the new thread starts and uses act.handle?


For this reason in Win32 I create the thread suspended, store the handle, resume the thread.
Apparently there is no direct equivalent in pthreads, you have to use your own mutex+condition variable.

 - Jay

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20091207/313e0a2b/attachment-0002.html>


More information about the M3devel mailing list