mschnell at lumino.de
Tue Jan 26 12:13:43 CET 2010
On 01/25/2010 08:46 PM, Martin Schreiber wrote:
> Possible, I don't touch such code if it works - or if I think it works. ;-)
> Do you think a futex is cheaper than an interlockedincrement()?
Other that System V "sema", Futex ("Fast Userspace mUTEX") does not do a
system call if a program tries to gain the semaphore (= "MUTEX" =
"MUTual EXclusion") but just (with an atomic instruction) writes to a
word in the userspace to mark the semaphore as taken. Only if the
semaphore already is taken by another thread, the FUTEX library call
does a system call to have the process wait and queue multiple threads
that request for the semaphore. When releasing the appropriate happens:
if no other process is waiting for the semaphore, no system call is done
but the code stays in user space.
Thus with the in most applications by far most common case of "waiting
not necessary", no system calls are done (thus no MMU reprogramming
etc.) and thus the code might run up to some hundred times faster.
Of course this is (usually) only done with multiple threads (sharing the
same user space memory) but not with multiple processes (each having
it's own user space memory mapping, unless they explicitly map a shared
If a threaded program uses the PThread ("Posix Thread") library, same
provides "pthread mutex" functions that automatically use FUTEX if the
system provides this (all PC systems do that, only some embedded
architectures lack the FUTEX implementation), otherwise PThread falls
back to using the sema provided by the OS.
I don't know if FPC RTL uses the PThread library to implement TThread
(but it would be the straight forward thing to do) and if it uses
pthread mutex to implement things like WaitFor (straight forward again).
I'll check this ASAP.
More information about the fpc-devel