Skip to content

Commit 0ff79d6

Browse files
committed
Use wait-on-semaphore in lieu of spin loops
1 parent fe4cd23 commit 0ff79d6

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

contrib/sb-concurrency/tests/test-frlock.lisp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
(a 0)
2727
(b 0)
2828
(c 0)
29-
(run! nil)
29+
(run! (sb-thread:make-semaphore))
3030
(w-e! (cons :write-oops nil))
3131
(r-e! (cons :read-oops nil)))
3232
(flet ((maybe-pause (pause &optional value)
@@ -41,7 +41,7 @@
4141
collect
4242
(make-thread
4343
(lambda ()
44-
(loop until run! do (thread-yield))
44+
(sb-thread:wait-on-semaphore run!)
4545
(handler-case
4646
(loop repeat read-count
4747
do (multiple-value-bind (a b c)
@@ -56,7 +56,7 @@
5656
(loop repeat writer-count
5757
collect (make-thread
5858
(lambda ()
59-
(loop until run! do (thread-yield))
59+
(sb-thread:wait-on-semaphore run!)
6060
(handler-case
6161
(loop repeat write-count
6262
do (frlock-write (rw)
@@ -76,7 +76,7 @@
7676
(error (e)
7777
(sb-ext:atomic-update (cdr w-e!) #'cons e))))))
7878
(progn
79-
(setf run! t)
79+
(sb-thread:signal-semaphore run! (+ reader-count writer-count))
8080
nil))))
8181
(values (cdr w-e!) (cdr r-e!))))
8282

tests/clos-add-remove-method.impure.lisp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@
129129
,@(to-add 'f))))
130130
(def))
131131

132-
(defvar *run* nil)
132+
(defvar *run* (sb-thread:make-semaphore))
133133

134134
(defun remove-methods (list)
135-
(loop until *run* do (sb-thread:thread-yield))
135+
(sb-thread:wait-on-semaphore *run*)
136136
(dolist (method list)
137137
(remove-method #'foo method)))
138138

139139
(defun add-methods (list)
140-
(loop until *run* do (sb-thread:thread-yield))
140+
(sb-thread:wait-on-semaphore *run*)
141141
(dolist (method list)
142142
(add-method #'foo method)))
143143

@@ -148,7 +148,7 @@
148148
(make-thread (lambda () (add-methods *to-add-d*)))
149149
(make-thread (lambda () (add-methods *to-add-e*)))
150150
(make-thread (lambda () (add-methods *to-add-f*))))))
151-
(setf *run* t)
151+
(sb-thread:signal-semaphore *run* 6)
152152
(mapcar #'join-thread threads))
153153

154154
#-sb-thread

tests/clos-cache.impure.lisp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
(push (find-class ',c) *cache-test-classes*)))))))
6969
(def))
7070

71-
(defvar *run-cache-test* nil)
71+
(defvar *run-cache-test* (sb-thread:make-semaphore))
7272

7373
(let* ((instances (map 'vector #'make-instance *cache-test-classes*))
7474
(limit (length instances)))
@@ -90,7 +90,7 @@
9090
(write-line string)))))
9191

9292
(defun test-loop ()
93-
(loop until *run-cache-test* do (sb-thread:thread-yield))
93+
(sb-thread:wait-on-semaphore *run-cache-test*)
9494
(handler-case
9595
(loop repeat 1024 do (test-cache))
9696
(error (e)
@@ -103,7 +103,7 @@
103103
#+sb-thread
104104
(let ((threads (loop repeat 32
105105
collect (sb-thread:make-thread 'test-loop))))
106-
(setf *run-cache-test* t)
106+
(sb-thread:signal-semaphore *run-cache-test* 32)
107107
(mapcar #'sb-thread:join-thread threads))
108108

109109
#-sb-thread

tests/threads.impure.lisp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,17 @@
112112
`(with-test (:name ,name)
113113
(let* ((n 200000)
114114
(x ,init)
115-
(run nil)
115+
(run (sb-thread:make-semaphore))
116+
(threadcount 10)
116117
(threads
117-
(loop repeat 10
118+
(loop repeat threadcount
118119
collect (make-thread
119120
(lambda ()
120-
(loop until run do (thread-yield))
121+
(sb-thread:wait-on-semaphore run)
121122
(loop repeat n do (,incf x)))))))
122-
(setf run t)
123+
(sb-thread:signal-semaphore run threadcount)
123124
(map nil #'join-thread threads)
124-
(assert (= (,op x) (* 10 n)))))))
125+
(assert (= (,op x) (* threadcount n)))))))
125126

126127
(def (cas car) (cons 0 nil) incf-car car)
127128
(def (cas cdr) (cons nil 0) incf-cdr cdr)

0 commit comments

Comments
 (0)