The custom LW condvar implementation is only for version 4 and 5
[bordeaux-threads.git] / src / impl-clozure.lisp
blobf9dc61bd6306c468ddb5c09a3b670b822af2b211
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 OpenMCL Threads interface can be found at
12 ;;; http://openmcl.clozure.com/Doc/Programming-with-Threads.html
14 (deftype thread ()
15 'ccl:process)
17 ;;; Thread Creation
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))
47 (if wait-p
48 (ccl:grab-lock lock)
49 (ccl:try-lock lock)))
51 (defun release-lock (lock)
52 (ccl:release-lock lock))
54 (defmacro with-lock-held ((place) &body body)
55 `(ccl:with-lock-grabbed (,place)
56 ,@body))
58 (defun make-recursive-lock (&optional name)
59 (ccl:make-lock (or name "Anonymous recursive lock")))
61 (defun acquire-recursive-lock (lock)
62 (ccl:grab-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)
69 ,@body))
71 ;;; Resource contention: condition variables
73 (defun make-condition-variable (&key name)
74 (declare (ignore name))
75 (ccl:make-semaphore))
77 (defun condition-wait (condition-variable lock &key timeout)
78 (release-lock lock)
79 (unwind-protect
80 (if 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))
92 ;;; Semaphores
94 (deftype semaphore ()
95 'ccl:semaphore)
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))
101 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)
107 (if timeout
108 (ccl:timed-wait-on-semaphore semaphore timeout)
109 (ccl:wait-on-semaphore semaphore)))
111 ;;; Introspection/debugging
113 (defun all-threads ()
114 (ccl:all-processes))
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))
130 (mark-supported)