source: branches/0.19.x/abcl/src/org/armedbear/lisp/threads.lisp

Last change on this file was 12213, checked in by ehuelsmann, 16 years ago

Move the implementation of the Mutex functionality to the THREADS package
*and* move the implementation to Lisp.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1;;; threads.lisp
2;;;
3;;; Copyright (C) 2009 Erik Huelsmann <ehuelsmann@common-lisp.net>
4;;;
5;;; $Id: threads.lisp 12213 2009-10-23 20:03:55Z ehuelsmann $
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
36;;
37;; Mailbox implementation
38;;
39
40;; this export statement is also in autoloads.lisp
41(export '(make-mailbox mailbox-send mailbox-empty-p mailbox-read mailbox-peek))
42
43(defstruct mailbox
44  queue)
45
46(defun mailbox-send (mailbox item)
47  "Sends an item into the mailbox, notifying 1 waiter
48to wake up for retrieval of that object."
49  (threads:synchronized-on mailbox
50     (push (mailbox-queue mailbox) item)
51     (threads:object-notify mailbox)))
52
53(defun mailbox-empty-p (mailbox)
54  "Returns non-NIL if the mailbox can be read from, NIL otherwise."
55  ;; Because we're just checking the value of an object reference,
56  ;; (which are atomically gotten and set) we don't need to lock
57  ;; the mailbox before operating on it.
58  (null (mailbox-queue mailbox)))
59
60(defun mailbox-read (mailbox)
61  "Blocks on the mailbox until an item is available for reading.
62When an item is available, it is returned."
63  (threads:synchronized-on mailbox
64     (loop
65        (unless (mailbox-empty-p mailbox)
66          (return))
67        (object-wait mailbox))
68     (pop (mailbox-queue mailbox))))
69
70(defun mailbox-peek (mailbox)
71  "Returns two values. The second returns non-NIL when the mailbox
72is empty. The first is the next item to be read from the mailbox
73if the first is NIL.
74
75Note that due to multi-threading, the first value returned upon
76peek, may be different from the one returned upon next read in the
77calling thread."
78  (threads:synchronized-on mailbox
79     (values (car (mailbox-queue mailbox))
80             (null (mailbox-queue mailbox)))))
81
82
83
84;;
85;; Mutex implementation
86;;
87
88
89;; this export statement is also in autoloads.lisp
90(export '(make-mutex get-mutex release-mutex))
91
92(defstruct mutex
93  in-use)
94
95(defun get-mutex (mutex)
96  "Acquires a lock on the `mutex'."
97  (synchronized-on mutex
98    (loop
99       while (mutex-in-use mutex)
100       do (object-wait mutex))
101    (setf (mutex-in-use mutex) T)))
102
103(defun release-mutex (mutex)
104  "Releases a lock on the `mutex'."
105  (synchronized-on mutex
106    (setf (mutex-in-use mutex) NIL)
107    (object-notify mutex)))
108
109(defmacro with-mutex ((mutex) &body body)
110  "Acquires a lock on `mutex', executes the body
111and releases the lock."
112  (let ((m (gensym)))
113    `(let ((,m ,mutex))
114       (when (get-mutex ,m)
115         (unwind-protect
116          (progn
117            ,@body)
118          (release-mutex ,m))))))
119
120
121;;
122;; Lock implementation
123;;
124
125(defun make-thread-lock ()
126  "Returns an object to be used with the `with-thread-lock' macro."
127  (gensym))
128
129(defmacro with-thread-lock ((lock) &body body)
130  "Acquires a lock on the `lock', executes `body' and releases the lock."
131  (let ((glock (gensym)))
132    `(let ((,glock ,lock))
133       (synchronized-on ,glock
134          ,@body))))
135
136(defun thread-lock (lock)
137  "Deprecated; due for removal in 0.22"
138  (declare (ignore lock)))
139(defun thread-unlock (lock)
140  "Deprecated; due for removal in 0.22"
141  (declare (ignore lock)))
Note: See TracBrowser for help on using the repository browser.