- 论坛徽章:
- 59
|
@zylthinking
your code is similar to the implement of spinlock.
but in logically, the "system call" spinlock is more effective than you implemented.
to notice that you lock the bus in a loop
- while(!__sync_bool_compare_and_swap(lkp, 0, 1)){...}
复制代码 as i know, linux kernel imp it in such as the following code
*NOTE* the following code is pusudo code
- spinlock{
- volatile long lock;
- valatile long wait;
- };
- volatile spinlock spin={0,1};
- void reqspinlock(volatile spinlock &){
- long myid=interlock_add(&spinlock.lock);
- while(myid+1!=spinlock.wait){} // Wait for the lock owner to release spinlock
- spinlock.wait++; // release spinlock
- }
复制代码 the above code assume that interlock_add function return the previous value that before added.
and
- while(myid+1!=spinlock.wait){...}
复制代码 do not lock the bus at all. |
|