Debian Codegraph @

src/tests/stress-tests/thread-sync-interrupt.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;; Author: Marius Gerbershagen
5 ;;;; Created: Sun Aug 13 20:04:54 CEST 2023
6 ;;;; Contains: Interrupt tests for thread synchronization primitives
7
8 (in-package :cl-test)
9
10 (suite 'thread-sync-interrupt)
11
12 ;;; These tests may fail because in general thread synchronization
13 ;;; primitives provided by the operating system are not guaranteed to
14 ;;; be interrupt safe. There is no way around this, so this cannot
15 ;;; work in every configuration. As of writing this, the
16 ;;; implementations on Linux and Windows are interrupt safe but on
17 ;;; BSDs generally not. Therefore, the main use of these tests is to
18 ;;; check for regressions introduced by changes in ECL.
19
20
21 ;; Locks
22
23 ;;; Date: 04/09/2009
24 ;;; From: Matthew Mondor
25 ;;; Fixed: 05/09/2009 (Juanjo)
26 ;;; Description:
27 ;;;
28 ;;; When a WITH-LOCK is interrupted, it is not able to release
29 ;;; the resulting lock and an error is signaled.
30 ;;;
31 (test-with-timeout mp.mutex.with-lock
32 (let ((flag 0)
33 (lock (mp:make-lock :name "mutex.with-lock" :recursive nil)))
34 (mp:with-lock (lock)
35 (let ((background-process
36 (mp:process-run-function
37 "mutex.with-lock"
38 #'(lambda ()
39 (handler-case
40 (progn
41 (setf flag 1)
42 (mp:with-lock (lock)
43 (setf flag 2)))
44 (error (c)
45 (princ c)(terpri)
46 (setf flag c)))
47 (setf flag 3)))))
48 ;; The background process should not be able to get
49 ;; the lock, and will simply wait. Now we interrupt it
50 ;; and the process should gracefully quit, without
51 ;; signalling any serious condition
52 (loop until (/= flag 0))
53 (sleep 0.1)
54 (is (mp:process-kill background-process))
55 (mp:process-join background-process)
56 (is (= flag 1))))))
57
58 ;;; Date: 12/04/2012
59 ;;; It is possible to kill processes waiting for a lock.
60 ;;;
61 (test-with-timeout mp.mutex.interruptible
62 (let ((mutex (mp:make-lock :name "mutex.interruptible"))
63 (flag 0))
64 (mp:get-lock mutex)
65 (let ((sleeper-thread
66 (mp:process-run-function
67 "mutex.interruptible"
68 #'(lambda ()
69 (setf flag 1)
70 (mp:with-lock (mutex)
71 (setf flag 2))))))
72 (loop until (/= flag 0))
73 (sleep 0.1)
74 (is (mp:process-active-p sleeper-thread))
75 (mp:process-kill sleeper-thread)
76 (mp:process-join sleeper-thread)
77 (is (= flag 1))
78 (is (eq (mp:lock-owner mutex) mp:*current-process*)))
79 (mp:giveup-lock mutex)))
80
81 ;;; Date: 14/04/2012
82 ;;; It is possible to kill processes waiting for a semaphore.
83 ;;;
84 (test-with-timeout mp.sem.interruptible
85 (loop with sem = (mp:make-semaphore :name "sem.interruptible")
86 with flag = nil
87 for count from 1 to 10
88 for all-processes = (loop for i from 1 upto count
89 collect (mp:process-run-function
90 "sem.interruptible"
91 #'(lambda ()
92 (mp:wait-on-semaphore sem)
93 (setf flag t))))
94 do (loop until (= (mp:semaphore-wait-count sem) count))
95 (is (null flag))
96 (is (every #'mp:process-active-p all-processes))
97 (mapc #'mp:process-kill all-processes)
98 (mapc #'mp:process-join all-processes)
99 (is (null flag))
100 ;; Usually, the wait count should be zero at this point. We may
101 ;; get higher values since the interrupt doesn't lock the mutex
102 ;; associated to the semaphore and thus multiple threads may write
103 ;; the wait count at the same time. However, interrupts are provided
104 ;; only for debugging purposes, for which this behaviour is acceptable.
105 (is (<= (mp:semaphore-wait-count sem) count))))
106
107 ;;; Date: 14/04/2012
108 ;;; When we kill a process, it is removed from the wait queue.
109 ;;;
110 (test-with-timeout mp.sem.interrupt-updates-queue
111 (let* ((sem (mp:make-semaphore :name "sem.interrupt-updates-queue"))
112 (process (mp:process-run-function
113 "sem.interrupt-updates-queue"
114 #'(lambda () (mp:wait-on-semaphore sem)))))
115 (loop until (= (mp:semaphore-wait-count sem) 1))
116 (is (mp:process-active-p process))
117 (mp:process-kill process)
118 (mp:process-join process)
119 ;; In contrast to the previous test, if we interrupt only a single thread
120 ;; the wait count must be correct, since only a single thread is writing.
121 (is (zerop (mp:semaphore-wait-count sem)))))
122
123 ;;; Date: 14/04/2012
124 ;;; When we kill a process, it signals another one. This is tricky,
125 ;;; because we need the awake signal to arrive _after_ the process is
126 ;;; killed, but the process must still be in the queue for the semaphore
127 ;;; to awake it.
128 ;;;
129 (test-with-timeout mp.sem.interrupted-resignals
130 (let* ((sem (mp:make-semaphore :name "sem.interrupted-resignals"))
131 (flag1 nil)
132 (flag2 nil)
133 (process1 (mp:process-run-function
134 "sem.interrupted-resignals-1"
135 #'(lambda ()
136 (unwind-protect
137 (mp:wait-on-semaphore sem)
138 (loop repeat (* 60 100) do (sleep 1/100))
139 (setf flag1 t)))))
140 (process2 (mp:process-run-function
141 "sem.interrupted-resignals-2"
142 #'(lambda ()
143 (mp:wait-on-semaphore sem)
144 (setf flag2 t)))))
145 (loop until (= (mp:semaphore-wait-count sem) 2))
146 (is (mp:process-active-p process1))
147 (is (mp:process-active-p process2))
148 ;; We kill the process but ensure it is still running
149 (mp:process-kill process1)
150 (is (mp:process-active-p process1))
151 (is (null flag1))
152 ;; Wait until the process is no longer waiting for the semaphore
153 (loop until (= (mp:semaphore-wait-count sem) 1))
154 ;; ... then awake it and the other process should start working
155 (mp:signal-semaphore sem)
156 (mp:process-join process2)
157 (is (zerop (mp:semaphore-wait-count sem)))
158 (is flag2)
159 ;; Finally we kill the first process (which will by this time be
160 ;; stuck in the unwind-protect call) again.
161 (mp:process-kill process1)
162 (mp:process-join process1)
163 (is (null flag1))))
164
165 (test-with-timeout mp.cv.interruptible
166 (let* ((mutex (mp:make-lock :name "cv.interruptible"))
167 (cv (mp:make-condition-variable))
168 (flag 0)
169 (waiting-process
170 (mp:process-run-function
171 "cv.interruptible"
172 (lambda ()
173 (mp:get-lock mutex)
174 (setf flag 1)
175 (mp:condition-variable-wait cv mutex)
176 (setf flag 2)
177 (error "We shouldn't have gotten to this point")))))
178 (loop until (mp:with-lock (mutex) (/= flag 0)))
179 (sleep 0.1)
180 (is (mp:process-kill waiting-process))
181 (mp:process-join waiting-process)
182 (is (null (mp:lock-owner mutex)))
183 (is (= flag 1))))
184