abcl: explicitly use JVM API for thread yield
[bordeaux-threads.git] / src / impl-clisp.lisp
blob9ef8d95f0e0255a98dad801c25a41af587638a8a
1 ;;;; -*- 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 (deftype thread ()
12 'mt:thread)
14 ;;; Thread Creation
15 (defun %make-thread (function name)
16 (mt:make-thread function
17 :name name
18 :initial-bindings mt:*default-special-bindings*))
20 (defun current-thread ()
21 (mt:current-thread))
23 (defun threadp (object)
24 (mt:threadp object))
26 (defun thread-name (thread)
27 (mt:thread-name thread))
29 ;;; Resource contention: locks and recursive locks
31 (defun make-lock (&optional name)
32 (mt:make-mutex :name (or name "Anonymous lock")))
34 (defun acquire-lock (lock &optional (wait-p t))
35 (mt:mutex-lock lock :timeout (if wait-p nil 0)))
37 (defun release-lock (lock)
38 (mt:mutex-unlock lock))
40 (defmacro with-lock-held ((place) &body body)
41 `(mt:with-mutex-lock (,place) ,@body))
43 (defun make-recursive-lock (&optional name)
44 (mt:make-mutex :name (or name "Anonymous recursive lock")
45 :recursive-p t))
47 (defmacro with-recursive-lock-held ((place) &body body)
48 `(mt:with-mutex-lock (,place) ,@body))
50 ;;; Resource contention: condition variables
52 (defun make-condition-variable (&key name)
53 (mt:make-exemption :name (or name "Anonymous condition variable")))
55 (defun condition-wait (condition-variable lock &key timeout)
56 (mt:exemption-wait condition-variable lock :timeout timeout)
59 (defun condition-notify (condition-variable)
60 (mt:exemption-signal condition-variable))
62 (defun thread-yield ()
63 (mt:thread-yield))
65 ;;; Timeouts
67 (defmacro with-timeout ((timeout) &body body)
68 (once-only (timeout)
69 `(mt:with-timeout (,timeout (error 'timeout :length ,timeout))
70 ,@body)))
72 ;;; Introspection/debugging
74 ;;; VTZ: mt:list-threads returns all threads that are not garbage collected.
75 (defun all-threads ()
76 (delete-if-not #'mt:thread-active-p (mt:list-threads)))
78 (defun interrupt-thread (thread function &rest args)
79 (mt:thread-interrupt thread :function function :arguments args))
81 (defun destroy-thread (thread)
82 ;;; VTZ: actually we can kill ourselelf.
83 ;;; suicide is part of our contemporary life :)
84 (signal-error-if-current-thread thread)
85 (mt:thread-interrupt thread :function t))
87 (defun thread-alive-p (thread)
88 (mt:thread-active-p thread))
90 (defun join-thread (thread)
91 (mt:thread-join thread))
93 (mark-supported)