Synchronization

Among Solaris operating systems four synchronization primitives for multithread programs, EusLisp provides mutex locks, conditional variables, and semaphores. Reader-writer lock is not available now.

Based on these primitives, higher level synchronization mechanisms, such as synchronized memory port and barrier synchronization, are realized.



sys:make-mutex-lock [function]

makes a mutex-lock and returns it. A mutex-lock is represented by an integer-vector of six elements.


sys:mutex-lock mlock [function]
locks the mutex lock mlock. If the mlock is already locked by another thread, mutex-lock waits for the lock to be released.


sys:mutex-unlock mlock [function]

releases mlock and let one of other threads waiting for this lock resume running.


sys:mutex mlock &rest forms [macro]

Mutex-lock and mutex-unlock have to be used as a pair. Mutex is a macro that brackets a critical section. Mlock is locked before evaluating forms are evaluated, and the lock is released when the evaluation finishes. This macro expands to the following progn form. Note that unwind-protect is used to ensure unlocking even an error occurs during the evaluation of forms.


  (progn
      (sys:mutex-lock mlock)
      (unwind-protect
          (progn . forms)
          (sys:mutex-unlock mlock)))

sys:make-cond [function]

makes a condition variable object which is an integer vector of four elements. The returned condition variable is in unlocked state.


sys:cond-wait condvar mlock [function]

waits for condvar to be signaled. If condvar has already been acquired by another thread, it releases mlock and waits for condvar to be signaled.


sys:cond-signal condvar [function]
signals the condvar condition variable.


sys:make-semaphore [function]
makes a semaphore object which is represented by an integer vector of twelve elements.


sys:sema-post sem [function]
signals sem.


sys:sema-wait sem [function]
waits for the sem semaphore to be posted.



sys:barrier-synch [class]


  :super   propertied-object 

:slots threads n-threads count barrier-cond threads-lock count-lock


represents a structure for barrier-synchronization. Threads waiting for the synchronization are put in threads which is mutually excluded by threads-lock. When a barrier-synch object is created, count is initialized to zero. Synchronizing threads are put in the threads list by sending :add message. Sending :wait to this barrier-sync object causes count to be incremented, and the sending thread is put in the wait state. When all the threads in threads send the :wait message, the waits are unblocked and all threads resume execution. The synchronization is implemented by the combination of the count-lock mutex-lock and the barrier-cond condition-variable.


:init [method]

initializes this barrier-synch object. Two mutex-lock and one condition-variable are created.


:add thr [method]
adds the thr thread in the threads list.


:remove thr [method]
removes the thr thread of the threads list.


:wait [method]
waits for all threads in the threads list to issue :wait.



sys:synch-memory-port [class]


  :super   propertied-object 

:slots sema-in sema-out buf empty lock


realizes the one-directional synchronized memory port, which synchronizes for two threads to transfer datum via this object. Control transfer is implemented by using semaphores.


:read [method]

reads datum buffered in this synch-memory-port. If it has not been written yet, the :read blocks.


:write datum [method]
writes datum in the buffer. Since only one word of buffer is available, if another datum has already been written and not yet read out, :write waits for the datum to be transferred by :read.


:init [method]
initializes this synch-memory-port where two semaphores are created and :write is made acceptable.


k-okada 2013-05-21