c - Implementing a simple spin lock given some instruction -


for instance if have following code if set clear , performs atomic in following manner. simple c code.

int ifsetthenclear(int * ptr) {     int actual = *ptr;     if(actual== 1)     *ptr = 0;     return actual; } 

now want use given instruction implement spin lock how achieve it? not looking workign code want learn pesudo code can explain lot new os.

presumably 0 means lock held.

you'd like:

int spinlock_flag; // shared flag  while (ifsetthenclear(&spinlock_flag) == 1)) {} // returns 1 if locked , 0 means hold lock.  do_something();  spinlock_flag = 1; // unlocked. 

you know need use atomic compare , swap operation make sense outside in real life?

this might - code posted code review few months ago when working on toy operating system. code posted works , there great responses.

https://codereview.stackexchange.com/questions/84148/spinlock-for-c-kernel-with-x86-asm


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -