9 Aug 2012 18:13
swapcontext() around pthreads
Emmanuel Dreyfus <manu <at> netbsd.org>
2012-08-09 16:13:43 GMT
2012-08-09 16:13:43 GMT
Hi I encountered a funny portability problem when working on glusterfs. In its 3.3. branch, it makes heavy use of swapcontext() and pthreads to get better performance. Unfortunately the code assumes a Linux specific behavior : a thread calling swapcontext() should not affect other threads. Only the calling thread context should be changed. NetBSD has a much more rich/messy behavior. Typical usage us to call getcontext() and makecontext() prior calling swapcontext(). If they all happen in the same thread, everything works like on Linux. However, if getcontext() was called in thread A and swapcontext() is called in thread B, then swapcontext causes the context of thread A to be changed, preempting the code being executed. thread B seems to terminate, though I suspect that could be changed with makecontext() and uc_link. Attached is a test case that exhibit the behavior, and here is the output: netbsd# ./tss before swapcontext self = 0xbb600000 after swapcontext self = 0xbfa00000 linux# ./tss before swapcontext self = 0x4002 after swapcontext self = 0x4002 This seems to fall into a grey area of unspecified behavior. Standards cannot help much since that functions were removed from POSIX.1.(Continue reading)


RSS Feed