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

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

Lisp-side implementation for ThreadLock? and Mailbox,
both put in threads.lisp.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1;;; threads.lisp
2;;;
3;;; Copyright (C) 2009 Erik Huelsmann <ehuelsmann@common-lisp.net>
4;;;
5;;; $Id: threads.lisp 12059 2009-07-24 22:05:31Z 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 '(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;; Lock implementation
86;;
87
88(defun make-thread-lock ()
89  "Returns an object to be used with the `with-thread-lock' macro."
90  (gensym))
91
92(defmacro with-thread-lock ((lock) &body body)
93  (let ((glock (gensym)))
94    `(let ((,glock ,lock))
95       (synchronized-on ,glock
96          ,@body))))
97
98(defun thread-lock (lock)
99  "Deprecated; due for removal in 0.22"
100  (declare (ignore lock)))
101(defun thread-unlock (lock)
102  "Deprecated; due for removal in 0.22"
103  (declare (ignore lock)))
Note: See TracBrowser for help on using the repository browser.