Untabify bordeaux-threads-test.lisp
[bordeaux-threads.git] / src / impl-sbcl.lisp
blobee17d7e44d661b1f36bb256944374a06cbd76169
1 ;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; 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 SBCL Threads interface can be found at
12 ;;; http://www.sbcl.org/manual/Threading.html
14 ;;; Thread Creation
16 (defun %make-thread (function name)
17 (sb-thread:make-thread function :name name))
19 (defun current-thread ()
20 sb-thread:*current-thread*)
22 (defun threadp (object)
23 (typep object 'sb-thread:thread))
25 (defun thread-name (thread)
26 (sb-thread:thread-name thread))
28 ;;; Resource contention: locks and recursive locks
30 (defun make-lock (&optional name)
31 (sb-thread:make-mutex :name (or name "Anonymous lock")))
33 (defun acquire-lock (lock &optional (wait-p t))
34 (sb-thread:get-mutex lock nil wait-p))
36 (defun release-lock (lock)
37 (sb-thread:release-mutex lock))
39 (defmacro with-lock-held ((place) &body body)
40 `(sb-thread:with-mutex (,place) ,@body))
42 (defun make-recursive-lock (&optional name)
43 (sb-thread:make-mutex :name (or name "Anonymous recursive lock")))
45 ;;; XXX acquire-recursive-lock and release-recursive-lock are actually
46 ;;; complicated because we can't use control stack tricks. We need to
47 ;;; actually count something to check that the acquire/releases are
48 ;;; balanced
50 (defmacro with-recursive-lock-held ((place) &body body)
51 `(sb-thread:with-recursive-lock (,place)
52 ,@body))
54 ;;; Resource contention: condition variables
56 (defun make-condition-variable (&key name)
57 (sb-thread:make-waitqueue :name (or name "Anonymous condition variable")))
59 (defun condition-wait (condition-variable lock)
60 (sb-thread:condition-wait condition-variable lock))
62 (defun condition-notify (condition-variable)
63 (sb-thread:condition-notify condition-variable))
65 (defun thread-yield ()
66 (sb-thread:release-foreground))
68 ;;; Timeouts
70 (deftype timeout () 'sb-ext:timeout)
72 (defmacro with-timeout ((timeout) &body body)
73 `(sb-ext:with-timeout ,timeout
74 ,@body))
76 ;;; Introspection/debugging
78 (defun all-threads ()
79 (sb-thread:list-all-threads))
81 (defun interrupt-thread (thread function)
82 (sb-thread:interrupt-thread thread function))
84 (defun destroy-thread (thread)
85 (signal-error-if-current-thread thread)
86 (sb-thread:terminate-thread thread))
88 (defun thread-alive-p (thread)
89 (sb-thread:thread-alive-p thread))
91 (defun join-thread (thread)
92 (sb-thread:join-thread thread))
94 (mark-supported)