Style fixes
[bordeaux-threads.git] / src / impl-allegro.lisp
blob9218641384642181062ee48b3c5444c01b6400a0
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 Allegro Multiprocessing interface can be found at
12 ;;; http://www.franz.com/support/documentation/8.1/doc/multiprocessing.htm
14 ;;; Resource contention: locks and recursive locks
16 (defun make-lock (&optional name)
17 (mp:make-process-lock :name (or name "Anonymous lock")))
19 (defun make-recursive-lock (&optional name)
20 (mp:make-process-lock :name (or name "Anonymous recursive lock")))
22 (defun acquire-lock (lock &optional (wait-p t))
23 (mp:process-lock lock mp:*current-process* "Lock" (if wait-p nil 0)))
25 (defun release-lock (lock)
26 (mp:process-unlock lock))
28 (defmacro with-lock-held ((place) &body body)
29 `(mp:with-process-lock (,place :norecursive t)
30 ,@body))
32 (defmacro with-recursive-lock-held ((place &key timeout) &body body)
33 `(mp:with-process-lock (,place :timeout ,timeout)
34 ,@body))
36 ;;; Resource contention: condition variables
38 (defun make-condition-variable (&key name)
39 (declare (ignorable name))
40 #-(version>= 9)
41 (mp:make-gate nil)
42 #+(version>= 9)
43 (mp:make-condition-variable :name name))
45 (defun condition-wait (condition-variable lock)
46 #-(version>= 9)
47 (progn
48 (release-lock lock)
49 (mp:process-wait "wait for message" #'mp:gate-open-p condition-variable)
50 (acquire-lock lock)
51 (mp:close-gate condition-variable))
52 #+(version>= 9)
53 (mp:condition-variable-wait condition-variable lock))
55 (defun condition-notify (condition-variable)
56 #-(version>= 9)
57 (mp:open-gate condition-variable)
58 #+(version>= 9)
59 (mp:condition-variable-signal condition-variable))
61 (defun thread-yield ()
62 (mp:process-allow-schedule))
64 (deftype thread ()
65 'mp:process)
67 ;;; Thread Creation
69 (defun start-multiprocessing ()
70 (mp:start-scheduler))
72 (defun %make-thread (function name)
73 #+smp
74 (mp:process-run-function name function)
75 #-smp
76 (mp:process-run-function
77 name
78 (lambda ()
79 (let ((return-values
80 (multiple-value-list (funcall function))))
81 (setf (getf (mp:process-property-list mp:*current-process*)
82 'return-values)
83 return-values)
84 (values-list return-values)))))
86 (defun current-thread ()
87 mp:*current-process*)
89 (defun threadp (object)
90 (typep object 'mp:process))
92 (defun thread-name (thread)
93 (mp:process-name thread))
95 ;;; Timeouts
97 (defmacro with-timeout ((timeout) &body body)
98 (once-only (timeout)
99 `(mp:with-timeout (,timeout (error 'timeout :length ,timeout))
100 ,@body)))
102 ;;; Introspection/debugging
104 (defun all-threads ()
105 mp:*all-processes*)
107 (defun interrupt-thread (thread function &rest args)
108 (apply #'mp:process-interrupt thread function args))
110 (defun destroy-thread (thread)
111 (signal-error-if-current-thread thread)
112 (mp:process-kill thread))
114 (defun thread-alive-p (thread)
115 (mp:process-alive-p thread))
117 (defun join-thread (thread)
118 #+smp
119 (values-list (mp:process-join thread))
120 #-smp
121 (progn
122 (mp:process-wait (format nil "Waiting for thread ~A to complete" thread)
123 (complement #'mp:process-alive-p)
124 thread)
125 (let ((return-values
126 (getf (mp:process-property-list thread) 'return-values)))
127 (values-list return-values))))
129 (mark-supported)