-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmp-compat-sbcl.lisp
More file actions
211 lines (163 loc) · 6.43 KB
/
mp-compat-sbcl.lisp
File metadata and controls
211 lines (163 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
;; mp-compatibility.lisp
;; --------------------------------------------------------------------------------------
;; Compatibility layer for Lispworks, Allegro, OS X, and Win32, Mulit-Processing Primitives
;;
;; Copyright (C) 2008 by SpectroDynamics, LLC. All rights reserved.
;;
;; DM/SD 08/08
;; --------------------------------------------------------------------------------------
;; --------------------------------------------------
(in-package #:mp-compatibility)
;; --------------------------------------------------
;; Compatibility Layer
(defun current-process ()
"Get the current Lisp process."
sb-thread:*current-thread*)
;; --------------------------------------------------------------------------
(defun process-name (proc)
(sb-thread:thread-name proc))
(defun set-process-name (proc name)
(setf (sb-thread:thread-name proc) name))
;; --------------------------------------------------------------------------
(defvar *process-plists* (make-hash-table :weakness :key :test 'eq))
(defun process-plist (proc)
"Return the property list for the indicated Lisp process."
(gethash proc *process-plists*))
(defun set-process-plist-entry (proc key val)
(um:if-let (lst (process-plist proc))
(setf (getf lst key) val)
(setf (gethash proc *process-plists*) (list key val))))
;; --------------------------------------------------------------------------
(defun process-run-function (name flags proc &rest args)
"Spawn a new Lisp thread and run the indicated function with inital args."
(declare (ignore flags))
(sb-thread:make-thread (lambda ()
(apply proc args))
:name name))
;; --------------------------------------------------------------------------
(defun process-kill (proc)
"Kill the indicated Lisp process."
(sb-thread:terminate-thread proc))
;; --------------------------------------------------------------------------
(defun process-interrupt (proc fn &rest args)
"Interrupt the indicated Lisp process to have it perform a function."
(sb-thread:interrupt-thread proc (lambda ()
(apply fn args))))
;; --------------------------------------------------------------------------
(defmacro without-preemption (&body body)
"Perform the body forms without preemption."
`(sb-sys:without-interrupts
,@body))
;; --------------------------------------------------------------------------
;; --------------------------------------------------------------------------
(defun make-lock (&key name important-p (safep t))
"Make a Lisp lock."
(declare (ignorable important-p safep))
(sb-thread:make-mutex :name name))
;; --------------------------------------------------------------------------
(defun do-with-lock (lock timeout fn)
(cond ((eq sb-thread:*current-thread* (sb-thread:mutex-owner lock))
(funcall fn))
(timeout
(tagbody
(sb-sys:without-interrupts
(handler-case
(sb-ext:with-timeout timeout
(sb-sys:allow-with-interrupts
(sb-thread:get-mutex lock nil t))
(go have-lock))
(timeout (cx)
(declare (ignore cx))
(go beyond))))
have-lock
(unwind-protect
(funcall fn)
(sb-sys:without-interrupts
(sb-thread:release-mutex lock)))
beyond))
(t (sb-thread:with-mutex (lock)
(funcall fn)))
))
(defmacro with-spin-lock ((lock) &body body)
`(with-lock (,lock) ,@body))
(defmacro with-lock ((lock &optional whostate timeout) &body body)
"Wait for lock available, then execute the body while holding the lock."
(declare (ignore whostate))
`(do-with-lock ,lock ,timeout (lambda () ,@body)))
;; --------------------------------------------------------------------------
(defun lock-owner (lock)
(sb-thread:mutex-owner lock))
;; --------------------------------------------------------------------------
(defun process-lock (lock &optional whostate timeout)
(declare (ignore whostate))
(cond ((eq sb-thread:*current-thread* (sb-thread:mutex-owner lock))
(funcall fn))
(timeout
(tagbody
(sb-sys:without-interrupts
(handler-case
(sb-ext:with-timeout timeout
(sb-sys:allow-with-interrupts
(sb-thread:get-mutex lock nil t))
(go have-lock))
(timeout (cx)
(declare (ignore cx))
(return-from process-lock nil))
))
have-lock
(unwind-protect
(funcall fn)
(sb-sys:without-interrupts
(sb-thread:release-mutex lock)))
beyond))
(t (sb-thread:with-mutex (lock)
(funcall fn)))
))
;; --------------------------------------------------------------------------
(defun process-unlock (lock)
(sb-sys:without-interrupts
(sb-thread:release-mutex lock)))
;; --------------------------------------------------------------------------
(defun make-mailbox (&key size)
"Make a Lisp mailbox."
(declare (ignorable size))
(mrmb:create))
;; --------------------------------------------------------------------------
(defun mailbox-send (mbox msg)
"Send a message to a Lisp mailbox."
(mrmb:send msg mbox))
;; --------------------------------------------------------------------------
(defun mailbox-read (mbox &optional timeout)
"Wait with timeout for a message to arrive at the Lisp mailbox and return it.
A null timeout means wait forever."
(mrmb:receive mbox timeout))
;; --------------------------------------------------------------------------
(defun mailbox-empty? (mbox)
"Check if the Lisp mailbox is empty. Return generalized T/F."
(mrmb:is-empty mbox))
;; --------------------------------------------------------------------------
(defun process-wait (wait-reason wait-fn &rest wait-args)
#+:LISPWORKS
(apply #'sb-thread:process-wait wait-reason wait-fn wait-args)
#+:ALLEGRO
(apply #'sb-thread:process-wait wait-reason wait-fn wait-args)
#+:CLOZURE
(apply #'sb-thread:process-wait wait-reason wait-fn wait-args))
;; --------------------------------------------------------------------------
(defun process-wait-with-timeout (wait-reason timeout
&optional wait-fn &rest wait-args)
#+:LISPWORKS
(apply #'sb-thread:process-wait-with-timeout wait-reason timeout wait-fn wait-args)
#+:ALLEGRO
(if timeout
(apply #'sb-thread:process-wait-with-timeout wait-reason timeout wait-fn wait-args)
(progn
(apply #'sb-thread:process-wait wait-reason wait-fn wait-args)
t))
#+:CLOZURE
(apply #'sb-thread:process-wait-with-timeout wait-reason
(round (* timeout sb-thread*ticks-per-second*))
wait-fn wait-args))
;; --------------------------------------------------------------------------
(defun generate-uuid ()
(uuid:make-v4-uuid))