Add semaphores abstraction to bordeaux-threads
[bordeaux-threads.git] / src / impl-sbcl.lisp
bloba1b88504f3e419380bb5ac0e24c50402930cf4b4
1 ;;;; -*- indent-tabs-mode: nil -*-
3 #|
4 Copyright 2006, 2007 Greg Pfeil
6 Distributed under the MIT license (see LICENSE file)
7 |#
9 (in-package #:bordeaux-threads)
11 ;;; documentation on the SBCL Threads interface can be found at
12 ;;; http://www.sbcl.org/manual/Threading.html
14 (deftype thread ()
15 'sb-thread:thread)
17 ;;; Thread Creation
19 (defun %make-thread (function name)
20 (sb-thread:make-thread function :name name))
22 (defun current-thread ()
23 sb-thread:*current-thread*)
25 (defun threadp (object)
26 (typep object 'sb-thread:thread))
28 (defun thread-name (thread)
29 (sb-thread:thread-name thread))
31 ;;; Resource contention: locks and recursive locks
33 (deftype lock () 'sb-thread:mutex)
35 (deftype recursive-lock () 'sb-thread:mutex)
37 (defun lock-p (object)
38 (typep object 'sb-thread:mutex))
40 (defun recursive-lock-p (object)
41 (typep object 'sb-thread:mutex))
43 (defun make-lock (&optional name)
44 (sb-thread:make-mutex :name (or name "Anonymous lock")))
46 (defun acquire-lock (lock &optional (wait-p t))
47 #+#.(cl:if (cl:find-symbol (cl:string '#:grab-mutex) :sb-thread) '(and) '(or))
48 (sb-thread:grab-mutex lock :waitp wait-p)
49 #-#.(cl:if (cl:find-symbol (cl:string '#:grab-mutex) :sb-thread) '(and) '(or))
50 (sb-thread:get-mutex lock nil wait-p))
52 (defun release-lock (lock)
53 (sb-thread:release-mutex lock))
55 (defmacro with-lock-held ((place) &body body)
56 `(sb-thread:with-mutex (,place) ,@body))
58 (defun make-recursive-lock (&optional name)
59 (sb-thread:make-mutex :name (or name "Anonymous recursive lock")))
61 ;;; XXX acquire-recursive-lock and release-recursive-lock are actually
62 ;;; complicated because we can't use control stack tricks. We need to
63 ;;; actually count something to check that the acquire/releases are
64 ;;; balanced
66 (defmacro with-recursive-lock-held ((place) &body body)
67 `(sb-thread:with-recursive-lock (,place)
68 ,@body))
70 ;;; Resource contention: condition variables
72 (defun make-condition-variable (&key name)
73 (sb-thread:make-waitqueue :name (or name "Anonymous condition variable")))
75 (defun condition-wait (condition-variable lock &key timeout)
76 (sb-thread:condition-wait condition-variable lock :timeout timeout))
78 (defun condition-notify (condition-variable)
79 (sb-thread:condition-notify condition-variable))
81 (defun thread-yield ()
82 (sb-thread:release-foreground))
84 ;;; Timeouts
86 (deftype timeout ()
87 'sb-ext:timeout)
89 (defmacro with-timeout ((timeout) &body body)
90 `(sb-ext:with-timeout ,timeout
91 ,@body))
93 ;;; Semaphores
95 (deftype semaphore ()
96 'sb-thread:semaphore)
98 (defun make-semaphore (&key name (count 0))
99 (sb-thread:make-semaphore :name name :count count))
101 (defun signal-semaphore (semaphore &key (count 1))
102 (sb-thread:signal-semaphore semaphore count))
104 (defun wait-on-semaphore (semaphore &key timeout)
105 (sb-thread:wait-on-semaphore semaphore :timeout timeout))
107 ;;; Introspection/debugging
109 (defun all-threads ()
110 (sb-thread:list-all-threads))
112 (defun interrupt-thread (thread function &rest args)
113 (flet ((apply-function ()
114 (if args
115 (lambda () (apply function args))
116 function)))
117 (declare (dynamic-extent #'apply-function))
118 (sb-thread:interrupt-thread thread (apply-function))))
120 (defun destroy-thread (thread)
121 (signal-error-if-current-thread thread)
122 (sb-thread:terminate-thread thread))
124 (defun thread-alive-p (thread)
125 (sb-thread:thread-alive-p thread))
127 (defun join-thread (thread)
128 (sb-thread:join-thread thread))
130 (mark-supported)