Jim Donelson wrote: > All that is really required is an atomic exchange. > Suppose 1 means taken, 0 means free. I do an exchange with a 1. If I > got back a zero, it's mine.
Fine in theory. ARM has an instruction like that, called SWP, and I've written mutex code with it. It turns out to be difficult to use efficiently from userspace. Most example locking code makes spinlocks with it. That's easy, but spinlocks are extremely inefficient when preempted inside a critical section. And you can't disable preemption in userspace code. The problem is process A has the lock and is preempted, then process B tries to get the lock and finds it doesn't have it. What does process B do? If it spins, that will waste CPU for a long time. If it calls sched_yield(), process B gets an unfairly small share of the CPU. It can't call FUTEX_WAIT, unless it has a way to signal process A when to call FUTEX_WAKE... In fact you can write an efficient mutex using atomic exchange and futex, but it's quite tricky and it's not any of the usual, documented futex algorithms. The algorithms generally used with futex in NPTL, for things like mutexes, rwlocks and condition varibles, do not work with atomic exchange; they require atomic compare-exchange. You can build the other algorithms on top of a basic mutex, as Michael is doing, but you have to be careful to ensure performance doesn't plummet when preempted. The simple spinlock algorithms aren't suitable. -- Jamie _______________________________________________ uClinux-dev mailing list uClinux-dev@uclinux.org http://mailman.uclinux.org/mailman/listinfo/uclinux-dev This message was resent by uclinux-dev@uclinux.org To unsubscribe see: http://mailman.uclinux.org/mailman/options/uclinux-dev