manpagez: man pages & more
info bigloo
Home | html | info | man
[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

17.1.2 Mutexes

Thread locking mechanism is common to Fair Threads and Posix Threads.

SRFI-18 function: mutex? obj
SRFI-18 function: make-mutex [name]
SRFI-18 function: mutex-name mutex
SRFI-18 function: mutex-specific mutex
SRFI-18 function: mutex-specific-set! mutex obj
SRFI-18 function: mutex-state mutex
SRFI-18 function, deprecated: mutex-lock! mutex [timeout [thread]]
SRFI-18 function, deprecated: mutex-unlock! mutex
(let ((m (make-mutex)))
   (thread-start!
    (instantiate::thread
       (body (lambda ()
                (let loop ()
                   (if (mutex-lock! m 0)
                       (begin
                          (display "locked")
                          (mutex-unlock! m))
                       (begin
                          (thread-yield!)
                          (loop)))))))))
  -| locked

(let ((res '()))
   (define (mutex-lock-recursively! mutex)
      (if (eq? (mutex-state mutex) (current-thread))
          (let ((n (mutex-specific mutex)))
             (mutex-specific-set! mutex (+ n 1)))
          (begin
             (mutex-lock! mutex)
             (mutex-specific-set! mutex 0))))
   (define (mutex-unlock-recursively! mutex)
      (let ((n (mutex-specific mutex)))
         (if (= n 0)
             (mutex-unlock! mutex)
             (mutex-specific-set! mutex (- n 1)))))
   (thread-start!
    (instantiate::thread
       (body (lambda ()
                (let ((m (make-mutex)))
                   (mutex-lock-recursively! m)
                   (mutex-lock-recursively! m)
                   (mutex-lock-recursively! m)
                   (set! res (cons (mutex-specific m) res))
                   (mutex-unlock-recursively! m)
                   (mutex-unlock-recursively! m)
                   (mutex-unlock-recursively! m)
                   (set! res (cons (mutex-specific m) res)))))))
   res)
  ⇒ (0 2)
Bigloo form: synchronize mutex exp1 exp2 ...

The function synchronize evaluates the expressions exp1, exp2, etc. The mutex mutex is acquired and released before exp1 gets evaluated. Its value is the value of the evaluated expression. The form synchronize ensures that however the form returns, the mutex mutex is always unlocked.

(synchronize mutex
   (print "before read...")
   (read p))
Bigloo function, deprecated: with-lock mutex thunk

The form with-lock is similar to synchronize into which it is expanded.

The function with-lock evaluates the body of the thunk. The mutex mutex is acquired and released before thunk gets invoked. The function with-lock might be implemented as:

(define (with-lock mutex thunk)
   (synchronize mutex
      (thunk)))

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on March 31, 2014 using texi2html 5.0.

© manpagez.com 2000-2024
Individual documents may contain additional copyright information.