1 | ;;; threads.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2009-2010 Erik Huelsmann <ehuelsmann@common-lisp.net> |
---|
4 | ;;; |
---|
5 | ;;; $Id: threads.lisp 14690 2014-04-22 11:24:50Z mevenson $ |
---|
6 | ;;; |
---|
7 | ;;; This program is free software; you can redistribute it and/or |
---|
8 | ;;; modify it under the terms of the GNU General Public License |
---|
9 | ;;; as published by the Free Software Foundation; either version 2 |
---|
10 | ;;; of the License, or (at your option) any later version. |
---|
11 | ;;; |
---|
12 | ;;; This program is distributed in the hope that it will be useful, |
---|
13 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | ;;; GNU General Public License for more details. |
---|
16 | ;;; |
---|
17 | ;;; You should have received a copy of the GNU General Public License |
---|
18 | ;;; along with this program; if not, write to the Free Software |
---|
19 | ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | ;;; |
---|
21 | ;;; As a special exception, the copyright holders of this library give you |
---|
22 | ;;; permission to link this library with independent modules to produce an |
---|
23 | ;;; executable, regardless of the license terms of these independent |
---|
24 | ;;; modules, and to copy and distribute the resulting executable under |
---|
25 | ;;; terms of your choice, provided that you also meet, for each linked |
---|
26 | ;;; independent module, the terms and conditions of the license of that |
---|
27 | ;;; module. An independent module is a module which is not derived from |
---|
28 | ;;; or based on this library. If you modify this library, you may extend |
---|
29 | ;;; this exception to your version of the library, but you are not |
---|
30 | ;;; obligated to do so. If you do not wish to do so, delete this |
---|
31 | ;;; exception statement from your version. |
---|
32 | |
---|
33 | (in-package #:threads) |
---|
34 | |
---|
35 | (export '(make-mailbox mailbox-send mailbox-empty-p |
---|
36 | mailbox-read mailbox-peek |
---|
37 | make-thread-lock with-thread-lock |
---|
38 | current-thread yield |
---|
39 | make-mutex get-mutex release-mutex with-mutex)) |
---|
40 | ;; |
---|
41 | ;; MAKE-THREAD helper to establish restarts |
---|
42 | ;; |
---|
43 | |
---|
44 | (defun thread-function-wrapper (fun) |
---|
45 | (restart-case |
---|
46 | (funcall fun) |
---|
47 | (abort () :report "Abort thread."))) |
---|
48 | |
---|
49 | ;; |
---|
50 | ;; Mailbox implementation |
---|
51 | ;; |
---|
52 | |
---|
53 | ;; this export statement is also in autoloads.lisp |
---|
54 | (export '(make-mailbox mailbox-send mailbox-empty-p mailbox-read mailbox-peek)) |
---|
55 | |
---|
56 | (defstruct mailbox |
---|
57 | queue) |
---|
58 | |
---|
59 | (defun mailbox-send (mailbox item) |
---|
60 | "Sends an item into the mailbox, notifying 1 waiter |
---|
61 | to wake up for retrieval of that object." |
---|
62 | (threads:synchronized-on mailbox |
---|
63 | (push item (mailbox-queue mailbox)) |
---|
64 | (threads:object-notify mailbox))) |
---|
65 | |
---|
66 | (defun mailbox-empty-p (mailbox) |
---|
67 | "Returns non-NIL if the mailbox can be read from, NIL otherwise." |
---|
68 | ;; Because we're just checking the value of an object reference, |
---|
69 | ;; (which are atomically gotten and set) we don't need to lock |
---|
70 | ;; the mailbox before operating on it. |
---|
71 | (null (mailbox-queue mailbox))) |
---|
72 | |
---|
73 | (defun mailbox-read (mailbox) |
---|
74 | "Blocks on the mailbox until an item is available for reading. |
---|
75 | When an item is available, it is returned." |
---|
76 | (threads:synchronized-on mailbox |
---|
77 | (loop |
---|
78 | (unless (mailbox-empty-p mailbox) |
---|
79 | (return)) |
---|
80 | (object-wait mailbox)) |
---|
81 | (pop (mailbox-queue mailbox)))) |
---|
82 | |
---|
83 | (defun mailbox-peek (mailbox) |
---|
84 | "Returns two values. The second returns non-NIL when the mailbox |
---|
85 | is empty. The first is the next item to be read from the mailbox. |
---|
86 | |
---|
87 | Note that due to multi-threading, the first value returned upon |
---|
88 | peek, may be different from the one returned upon next read in the |
---|
89 | calling thread." |
---|
90 | (threads:synchronized-on mailbox |
---|
91 | (values (car (mailbox-queue mailbox)) |
---|
92 | (null (mailbox-queue mailbox))))) |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | ;; |
---|
97 | ;; Mutex implementation |
---|
98 | ;; |
---|
99 | |
---|
100 | |
---|
101 | ;; this export statement is also in autoloads.lisp |
---|
102 | (export '(make-mutex get-mutex release-mutex)) |
---|
103 | |
---|
104 | (defstruct mutex |
---|
105 | in-use) |
---|
106 | |
---|
107 | (defun get-mutex (mutex) |
---|
108 | "Acquires a lock on the `mutex'." |
---|
109 | (synchronized-on mutex |
---|
110 | (loop |
---|
111 | while (mutex-in-use mutex) |
---|
112 | do (object-wait mutex)) |
---|
113 | (setf (mutex-in-use mutex) T))) |
---|
114 | |
---|
115 | (defun release-mutex (mutex) |
---|
116 | "Releases a lock on the `mutex'." |
---|
117 | (synchronized-on mutex |
---|
118 | (setf (mutex-in-use mutex) NIL) |
---|
119 | (object-notify mutex))) |
---|
120 | |
---|
121 | (defmacro with-mutex ((mutex) &body body) |
---|
122 | "Acquires a lock on `mutex', executes the body |
---|
123 | and releases the lock." |
---|
124 | (let ((m (gensym))) |
---|
125 | `(let ((,m ,mutex)) |
---|
126 | (when (get-mutex ,m) |
---|
127 | (unwind-protect |
---|
128 | (progn |
---|
129 | ,@body) |
---|
130 | (release-mutex ,m)))))) |
---|
131 | |
---|
132 | |
---|
133 | ;; |
---|
134 | ;; Lock implementation |
---|
135 | ;; |
---|
136 | |
---|
137 | (defun make-thread-lock () |
---|
138 | "Returns an object to be used with the `with-thread-lock' macro." |
---|
139 | (gensym)) |
---|
140 | |
---|
141 | (defmacro with-thread-lock ((lock) &body body) |
---|
142 | "Acquires a lock on the `lock', executes `body' and releases the lock." |
---|
143 | (let ((glock (gensym))) |
---|
144 | `(let ((,glock ,lock)) |
---|
145 | (synchronized-on ,glock |
---|
146 | ,@body)))) |
---|
147 | |
---|
148 | (defun yield () |
---|
149 | "A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this hint. |
---|
150 | |
---|
151 | See java.lang.Thread.yield()." |
---|
152 | (java:jcall "yield" (JAVA:jstatic "currentThread" "java.lang.Thread"))) |
---|