~ubuntu-branches/ubuntu/trusty/librep/trusty

« back to all changes in this revision

Viewing changes to lisp/rep/threads/mutex.jl

  • Committer: Bazaar Package Importer
  • Author(s): Christian Marillat
  • Date: 2001-11-13 15:06:22 UTC
  • Revision ID: james.westby@ubuntu.com-20011113150622-vgmgmk6srj3kldr3
Tags: upstream-0.15.2
ImportĀ upstreamĀ versionĀ 0.15.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
;; mutex.jl -- thread mutex devices
 
2
;; Copyright (C) 2000 John Harper <john@dcs.warwick.ac.uk>
 
3
 
 
4
;; $Id: mutex.jl,v 1.7 2001/08/03 03:13:08 jsh Exp $
 
5
 
 
6
;; This file is part of librep.
 
7
 
 
8
;; librep is free software; you can redistribute it and/or modify it
 
9
;; under the terms of the GNU General Public License as published by
 
10
;; the Free Software Foundation; either version 2, or (at your option)
 
11
;; any later version.
 
12
 
 
13
;; librep is distributed in the hope that it will be useful, but
 
14
;; WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
;; GNU General Public License for more details.
 
17
 
 
18
;; You should have received a copy of the GNU General Public License
 
19
;; along with librep; see the file COPYING.  If not, write to
 
20
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 
21
 
 
22
(define-structure rep.threads.mutex
 
23
 
 
24
    (export make-mutex
 
25
            mutexp
 
26
            obtain-mutex
 
27
            maybe-obtain-mutex
 
28
            release-mutex)
 
29
 
 
30
    (open rep
 
31
          rep.threads
 
32
          rep.threads.utils)
 
33
 
 
34
  (define-structure-alias mutex rep.threads.mutex)
 
35
 
 
36
  ;; Each mutex is (mutex [OWNING-THREAD [BLOCKED-THREADS...]])
 
37
 
 
38
  (defun make-mutex ()
 
39
    "Create and return a mutex object. No thread will own the new mutex."
 
40
    (list 'mutex))
 
41
 
 
42
  (defun mutexp (arg)
 
43
    "Returns true if ARG is a mutex object."
 
44
    (eq (car arg) 'mutex))
 
45
 
 
46
  (defun obtain-mutex (mtx #!optional timeout)
 
47
    "Obtain the mutex MTX for the current thread. Will suspend the current
 
48
thread until the mutex is available. Returns false if the timeout expired."
 
49
    (without-interrupts
 
50
     (if (null (cdr mtx))
 
51
         (rplacd mtx (list (current-thread)))
 
52
       (rplacd mtx (nconc (cdr mtx) (list (current-thread))))
 
53
       (not (thread-suspend (current-thread) timeout)))))
 
54
 
 
55
  (defun maybe-obtain-mutex (mtx)
 
56
    "Attempt to obtain mutex MTX for the current thread without blocking.
 
57
Returns true if able to obtain the mutex, false otherwise."
 
58
    (without-interrupts
 
59
     (if (cdr mtx)
 
60
         nil
 
61
       (obtain-mutex mtx)
 
62
       t)))
 
63
 
 
64
  (defun release-mutex (mtx)
 
65
    "Release the mutex object MTX (which should have previously been obtained
 
66
by the current thread). Returns true if the mutex has no new owner."
 
67
    (or (eq (cadr mtx) (current-thread))
 
68
        (error "Not owner of mutex: %S" mtx))
 
69
    (without-interrupts
 
70
     (rplacd mtx (cddr mtx))
 
71
     (if (cdr mtx)
 
72
         (progn
 
73
           (thread-wake (cadr mtx))
 
74
           nil)
 
75
       t))))