Finished conversion from methods to functions with defaults (Stelian Ionescu <sionesc...
[bordeaux-threads.git] / src / cmu.lisp
blob659db32a2d23b8a5a4235a2000ad784baa757652
1 #|
2 Copyright 2006, 2007 Greg Pfeil
4 Distributed under the MIT license (see LICENSE file)
5 |#
7 (in-package #:bordeaux-threads)
9 ;;; Thread Creation
11 (defun make-thread (function &key name)
12 (mp:make-process function :name name))
14 (defun current-thread ()
15 mp:*current-process*)
17 (defmethod threadp (object)
18 (mp:processp object))
20 (defun thread-name (thread)
21 (mp:process-name thread))
23 ;;; Resource contention: locks and recursive locks
25 (defun make-lock (&optional name)
26 (mp:make-lock name))
28 (defun acquire-lock (lock &optional (wait-p t))
29 (if wait-p
30 (mp::lock-wait lock "Lock")
31 (mp::lock-wait-with-timeout lock "Lock" 0)))
33 (defun release-lock (lock)
34 (setf (mp::lock-process lock) nil))
36 (defmacro with-lock-held ((place) &body body)
37 `(mp:with-lock-held (,place) ,@body))
39 (defmacro with-recursive-lock-held ((place &key timeout) &body body)
40 `(mp:with-lock-held (,place "Lock Wait" :timeout ,timeout) ,@body))
42 ;;; Note that the locks _are_ recursive, but not "balanced", and only
43 ;;; checked if they are being held by the same process by with-lock-held.
44 ;;; The default with-lock-held in bordeaux-mp.lisp sort of works, in that
45 ;;; it will wait for recursive locks by the same process as well.
47 ;;; Resource contention: condition variables
49 ;;; There's some stuff in x86-vm.lisp that might be worth investigating
50 ;;; whether to build on. There's also process-wait and friends.
52 (defstruct condition-var
53 "CMUCL doesn't have conditions, so we need to create our own type."
54 lock
55 active)
57 (defun make-condition-variable ()
58 (make-condition-var :lock (make-lock)))
60 (defun condition-wait (condition-variable lock)
61 (check-type condition-variable condition-var)
62 (with-lock-held ((condition-var-lock condition-variable))
63 (setf (condition-var-active condition-variable) nil))
64 (release-lock lock)
65 (mp:process-wait "Condition Wait"
66 #'(lambda () (condition-var-active condition-variable)))
67 (acquire-lock lock)
70 (defun condition-notify (condition-variable)
71 (check-type condition-variable condition-var)
72 (with-lock-held ((condition-var-lock condition-variable))
73 (setf (condition-var-active condition-variable) t))
74 (thread-yield))
76 (defun thread-yield ()
77 (mp:process-yield))
79 ;;; Introspection/debugging
81 (defun all-threads ()
82 (mp:all-processes))
84 (defun interrupt-thread (thread function)
85 (mp:process-interrupt thread function))
87 (defun destroy-thread (thread)
88 (signal-error-if-current-thread thread)
89 (mp:destroy-process thread))
91 (defun thread-alive-p (thread)
92 (mp:process-active-p thread))
94 (mark-supported)