<div dir="ltr">Hi<div><br></div><div>Sorry for the errors; it was not my main concern it should be like this:</div><div><br></div><div><div style="font-family:arial,sans-serif;font-size:13px"><div>(defmacro with-critical-section (addr &body body)</div>
<div> "lock on addr"</div><div> `(unwind-protect</div><div><span style="white-space:pre-wrap"> </span>(progn</div><div> (acquire-lock-xchg ,addr) ; this will block</div><div> ,@body)<br>
</div><div> (free-lock-xchg ,addr)))</div><div><br></div><div><div>(deflapfunction acquire-lock-xchg (addr) </div><div> :again</div><div> mov ($ 1) (% reg)</div><div> xchg (% reg) (@ addr)</div><div> jz @win</div>
<div> (pause)</div><div> jmp @again</div><div> :win</div><div> ret (% reg) )</div></div><div><br></div><div>Now, I am not reinventing the wheel, I used to use semaphores but they are very slow for my needs</div>
<div>and would like to use LOCK XCHG to implement a lightweight locking</div><div><br></div><div>PS: to get the address of a cons I use ccl::%address-of, but in my program I need to get the address of the</div><div>lock bit (ex. (let ((a '(x . 0))) (with-critical-section (cdr a) do-something))); as you can see I need to get the address of 0</div>
<div>to be able to xchg it with 1 atomically in acquire-lock-xchg.</div><div><br></div><div>Kind regards</div><div>Taoufik</div><div><br></div></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Jan 9, 2014 at 10:48 AM, Taoufik Dachraoui <span dir="ltr"><<a href="mailto:dachraoui.taoufik@gmail.com" target="_blank">dachraoui.taoufik@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi<div><br></div><div>Please I need help with creating 2 simple lapfunctions: acquire-lock-xchg and free-lock-xchg</div>
<div><br></div><div>assembler pseudocode for both functions follow:</div><div><br></div>
<div><div>(deflapfunction acquire-lock-xchg (addr) </div><div> mov 1 reg</div><div> xchg addr reg</div><div> ret reg )</div><div> </div><div>(deflapfunction free-lock-xchg (addr) </div><div> mov 0 reg</div>
<div> xchg addr reg</div><div> ret</div><div>)</div><div><br></div><div>(defmacro with-critical-section (addr &body body)</div><div> "lock on addr"</div><div> `((loop when (= 1 (the fixnumm (acquire-lock-xchg ,addr))) return nil) ; spin-wait (unwind-protect</div>
<div><span style="white-space:pre-wrap"> </span> (progn</div><div> ,@body)</div><div> (free-lock-xchg ,addr))))</div></div><div><br></div><div>Usage:</div><div> (let ((x '(1 nil . 0))) (with-critical-section (cddr x) (push 2 (cadr x))))</div>
<div><br></div><div>Do you think that such code is robust and may be used in production? (I am developing a new algorithm for persistent hash tables for multiprocessors systems)</div><div><br></div><div>Kind regards</div>
<span class="HOEnZb"><font color="#888888">
<div>Taoufik</div><div><br></div></font></span></div>
</blockquote></div><br></div>