1 ;;;; -*- indent-tabs-mode: nil -*-
4 Copyright
2006, 2007 Greg Pfeil
6 Distributed under the MIT license
(see LICENSE file
)
9 (in-package #:bordeaux-threads
)
11 ;;; documentation on the OpenMCL Threads interface can be found at
12 ;;; http://openmcl.clozure.com/Doc/Programming-with-Threads.html
19 (defun %make-thread
(function name
)
20 (ccl:process-run-function name function
))
22 (defun current-thread ()
23 ccl
:*current-process
*)
25 (defun threadp (object)
26 (typep object
'ccl
:process
))
28 (defun thread-name (thread)
29 (ccl:process-name thread
))
31 ;;; Resource contention: locks and recursive locks
33 (deftype lock
() 'ccl
:lock
)
35 (deftype recursive-lock
() 'ccl
:lock
)
37 (defun lock-p (object)
38 (typep object
'ccl
:lock
))
40 (defun recursive-lock-p (object)
41 (typep object
'ccl
:lock
))
43 (defun make-lock (&optional name
)
44 (ccl:make-lock
(or name
"Anonymous lock")))
46 (defun acquire-lock (lock &optional
(wait-p t
))
51 (defun release-lock (lock)
52 (ccl:release-lock lock
))
54 (defmacro with-lock-held
((place) &body body
)
55 `(ccl:with-lock-grabbed
(,place
)
58 (defun make-recursive-lock (&optional name
)
59 (ccl:make-lock
(or name
"Anonymous recursive lock")))
61 (defun acquire-recursive-lock (lock)
64 (defun release-recursive-lock (lock)
65 (ccl:release-lock lock
))
67 (defmacro with-recursive-lock-held
((place) &body body
)
68 `(ccl:with-lock-grabbed
(,place
)
71 ;;; Resource contention: condition variables
73 (defun make-condition-variable (&key name
)
74 (declare (ignore name
))
77 (defun condition-wait (condition-variable lock
&key timeout
)
81 (ccl:timed-wait-on-semaphore condition-variable timeout
)
82 (ccl:wait-on-semaphore condition-variable
))
83 (acquire-lock lock t
))
86 (defun condition-notify (condition-variable)
87 (ccl:signal-semaphore condition-variable
))
89 (defun thread-yield ()
90 (ccl:process-allow-schedule
))
97 (defun make-semaphore (&key name
(count 0))
98 (declare (ignore name
))
99 (let ((semaphore (ccl:make-semaphore
)))
100 (dotimes (c count
) (ccl:signal-semaphore semaphore
))
103 (defun signal-semaphore (semaphore &key
(count 1))
104 (dotimes (c count
) (ccl:signal-semaphore semaphore
)))
106 (defun wait-on-semaphore (semaphore &key timeout
)
108 (ccl:timed-wait-on-semaphore semaphore timeout
)
109 (ccl:wait-on-semaphore semaphore
)))
111 ;;; Introspection/debugging
113 (defun all-threads ()
116 (defun interrupt-thread (thread function
&rest args
)
117 (declare (dynamic-extent args
))
118 (apply #'ccl
:process-interrupt thread function args
))
120 (defun destroy-thread (thread)
121 (signal-error-if-current-thread thread
)
122 (ccl:process-kill thread
))
124 (defun thread-alive-p (thread)
125 (not (ccl:process-exhausted-p thread
)))
127 (defun join-thread (thread)
128 (ccl:join-process thread
))