Skip to content
impl-clozure.lisp 2.23 KiB
Newer Older
;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-


Distributed under the MIT license (see LICENSE file)
|#

(in-package #:bordeaux-threads)

;;; documentation on the OpenMCL Threads interface can be found at
;;; http://openmcl.clozure.com/Doc/Programming-with-Threads.html

Stelian Ionescu's avatar
Stelian Ionescu committed
(deftype thread ()
  'ccl:process)

;;; Thread Creation
(defun %make-thread (function name)
  (ccl:process-run-function name function))
  ccl:*current-process*)

(defun threadp (object)
  (typep object 'ccl:process))
  (ccl:process-name thread))

;;; Resource contention: locks and recursive locks

(defun make-lock (&optional name)
Stelian Ionescu's avatar
Stelian Ionescu committed
  (ccl:make-lock (or name "Anonymous lock")))
(defun acquire-lock (lock &optional (wait-p t))
  (if wait-p
      (ccl:grab-lock lock)
      (ccl:try-lock lock)))

  (ccl:release-lock lock))

(defmacro with-lock-held ((place) &body body)
  `(ccl:with-lock-grabbed (,place)
     ,@body))

(defun make-recursive-lock (&optional name)
Stelian Ionescu's avatar
Stelian Ionescu committed
  (ccl:make-lock (or name "Anonymous recursive lock")))
  (ccl:grab-lock lock))

  (ccl:release-lock lock))

(defmacro with-recursive-lock-held ((place) &body body)
  `(ccl:with-lock-grabbed (,place)
     ,@body))

;;; Resource contention: condition variables

(defun make-condition-variable (&key name)
  (declare (ignore name))
  (ccl:make-semaphore))

(defun condition-wait (condition-variable lock)
  (release-lock lock)
  (unwind-protect
       (ccl:wait-on-semaphore condition-variable)
    (acquire-lock lock t)))

(defun condition-notify (condition-variable)
  (ccl:signal-semaphore condition-variable))

  (ccl:process-allow-schedule))

;;; Introspection/debugging

  (ccl:all-processes))

(defun interrupt-thread (thread function &rest args)
  (apply #'ccl:process-interrupt thread function args))
(defun destroy-thread (thread)
  (signal-error-if-current-thread thread)
  (ccl:process-kill thread))

Greg Pfeil's avatar
Greg Pfeil committed
  (ccl::process-active-p thread))

(defun join-thread (thread)
  (ccl:join-process thread))