spinlock.C
上传用户:shtangtang
上传日期:2007-01-04
资源大小:167k
文件大小:1k
- //
- // struct spinlock
- //
- // this structure is a primitive semaphore. It simple controls
- // the situation of a single integer... with an on/off value.
- #include <thread_spinlock.h>
- extern "C" {
- # include <sched.h>
- };
- //
- // testandset
- //
- // test if the spinlock is clear, and set it in one atomic
- // instruction. Ensuring it is owned by only one process.
- int spinlock::testandset()
- {
- int ret;
- __asm__ __volatile__("xchgl %0, %1"
- : "=r"(ret), "=m"(s_spinlock)
- : "0"(1), "m"(s_spinlock));
- return ret;
- }
- //
- // acquire
- //
- // continue to try and set the spinlock, until successfull. Yielding
- // to other processes in the scheduling while waiting.
- void spinlock::acquire()
- {
- while (testandset())
- __sched_yield();
- }
- //
- // release
- //
- // release the spinlock, by setting it to 0.
- void spinlock::release()
- {
- #ifndef RELEASE
- s_spinlock = 0;
- #else
- RELEASE(&s_spinlock);
- #endif
- }