summaryrefslogtreecommitdiff
path: root/2d/mvars.scm
blob: 77461aeb9e9545bbf4c323e1009533af555136ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
;;; Synchronized Mutable Variables

;; Copyright (C) 2013 Mark Weaver

;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
;; License as published by the Free Software Foundation; either
;; version 3 of the License, or (at your option) any later version.
;;
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public
;; License along with this library; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301 USA

;;; Code:

(define-module (2d mvars)
  #:use-module (ice-9 threads)
  #:use-module (srfi srfi-8)            ; receive
  #:use-module (srfi srfi-9)            ; records
  #:use-module (srfi srfi-9 gnu)
  #:export (mvar?
            mvar-empty? new-empty-mvar new-mvar
            take-mvar put-mvar read-mvar swap-mvar
            try-take-mvar try-put-mvar
            with-mvar modify-mvar modify-mvar*))

(define-record-type <mvar>
  (make-mvar contents empty? mutex full-condition empty-condition)
  mvar?
  (contents         %mvar-contents   %set-mvar-contents!)
  (empty?           %mvar-empty?     %set-mvar-empty?!)
  (mutex            mvar-mutex)
  (full-condition   mvar-full-condition)
  (empty-condition  mvar-empty-condition))

(define (mvar-empty? mvar)
  (with-mutex (mvar-mutex mvar)
    (%mvar-empty? mvar)))

(define (new-empty-mvar)
  "Return a freshly allocated mvar that is initially empty."
  (make-mvar #f  ; contents
             #t  ; empty?
             (make-mutex)
             (make-condition-variable)
             (make-condition-variable)))

(define (new-mvar x)
  "Return a freshly allocated mvar with initial contents X."
  (make-mvar x   ; contents
             #f  ; empty?
             (make-mutex)
             (make-condition-variable)
             (make-condition-variable)))

(define (take-mvar mvar)
  "Block until MVAR is full, then atomically remove and return its contents."
  (with-mutex (mvar-mutex mvar)
    (when (%mvar-empty? mvar)
      (wait-condition-variable (mvar-full-condition mvar) (mvar-mutex mvar)))
    (let ((x (%mvar-contents mvar)))
      (%set-mvar-contents! mvar #f)
      (%set-mvar-empty?! mvar #t)
      (signal-condition-variable (mvar-empty-condition mvar))
      x)))

(define (put-mvar mvar x)
  "Block until MVAR is empty, then put X into it."
  (with-mutex (mvar-mutex mvar)
    (unless (%mvar-empty? mvar)
      (wait-condition-variable (mvar-empty-condition mvar) (mvar-mutex mvar)))
    (%set-mvar-contents! mvar x)
    (%set-mvar-empty?! mvar #f)
    (signal-condition-variable (mvar-full-condition mvar))
    *unspecified*))

(define (read-mvar mvar)
  "Take a value x from MVAR, then put it back and return x.  This
procedure is atomic only if there are no other producers for MVAR."
  (let ((x (take-mvar mvar)))
    (put-mvar mvar x)
    x))

(define (swap-mvar mvar y)
  "Take a value x from MVAR, then put Y into MVAR and return x.  This
procedure is atomic only if there are no other producers for MVAR."
  (let ((x (take-mvar mvar)))
    (put-mvar mvar y)
    x))

(define (try-take-mvar mvar)
  "If MVAR is full, return its contents and #t, else return #f and #f."
  (with-mutex (mvar-mutex mvar)
    (if (%mvar-empty? mvar)
        (values #f #f)
        (let ((x (%mvar-contents mvar)))
          (%set-mvar-contents! mvar #f)
          (%set-mvar-empty?! mvar #t)
          (signal-condition-variable (mvar-empty-condition mvar))
          (values x #t)))))

(define (try-put-mvar mvar x)
  "If MVAR is empty, put X into it and return #t, else return #f."
  (with-mutex (mvar-mutex mvar)
    (and (%mvar-empty? mvar)
         (begin
           (%set-mvar-contents! mvar x)
           (%set-mvar-empty?! mvar #f)
           (signal-condition-variable (mvar-full-condition mvar))
           #t))))

(define (with-mvar mvar proc)
  "Take a value from MVAR and apply PROC to it.  If an exception is raised,
the original value is put back into MVAR.  This procedure is atomic only if
there are no other producers for MVAR."
  (let ((x (take-mvar mvar)))
    (catch #t
      (lambda () (proc x))
      (lambda (key . args)
        (put-mvar mvar x)
        (apply throw key args)))))

(define (modify-mvar mvar f)
  "Take a value x from MVAR, and then put back (F x).  If an exception is
raised, the original value is put back into MVAR.  This procedure is
atomic only if there are no other producers for MVAR."
  (let ((old (take-mvar mvar)))
    (catch #t
      (lambda () (put-mvar mvar (f old)))
      (lambda (key . args)
        (put-mvar mvar old)
        (apply throw key args)))))

(define (modify-mvar* mvar f)
  "Take a value x from MVAR, and apply F to it.  (F x) should return one
or more values: the new value to be put back into MVAR, and zero or more
additional values to be returned from MODIFY-MVAR*.  If an exception is
raised, the original value is put back into MVAR.  This procedure is
atomic only if there are no other producers for MVAR."
  (let ((old (take-mvar mvar)))
    (catch #t
      (lambda ()
        (receive (new . results) (f old)
          (put-mvar mvar new)
          (apply values results)))
      (lambda (key . args)
        (put-mvar mvar old)
        (apply throw key args)))))

(set-record-type-printer!
 <mvar>
 (lambda (mvar port)
   (display "#<mvar " port)
   (display (number->string (object-address mvar) 16) port)
   (display " " port)
   (write (with-mutex (mvar-mutex mvar)
            (if (%mvar-empty? mvar)
                '()
                (list (%mvar-contents mvar))))
          port)
   (display ">" port)))