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: mutex-lock! mutex [timeout [thread]]
SRFI-18 function: 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 function: with-lock mutex thunk

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)
   (mutex-lock! mutex)
   (unwind-protect
      (thunk)
      (mutex-unlock! mutex)))

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

This document was generated on October 23, 2011 using texi2html 5.0.

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