killing lutexes, adding timeouts
[sbcl.git] / src / code / thread.lisp
index dc92196..4c688bf 100644 (file)
 
 (in-package "SB!THREAD")
 
+(def!type thread-name ()
+  'simple-string)
+
 (def!struct (thread (:constructor %make-thread))
   #!+sb-doc
   "Thread type. Do not rely on threads being structs as it may change
 in future versions."
-  name
-  %alive-p
-  os-thread
-  interruptions
-  (interruptions-lock (make-mutex :name "thread interruptions lock"))
-  result
-  (result-lock (make-mutex :name "thread result lock")))
+  (name          nil :type (or thread-name null))
+  (%alive-p      nil :type boolean)
+  (os-thread     nil :type (or integer null))
+  (interruptions nil :type list)
+  (result        nil :type list)
+  (interruptions-lock
+   (make-mutex :name "thread interruptions lock")
+   :type mutex)
+  (result-lock
+   (make-mutex :name "thread result lock")
+   :type mutex)
+  waiting-for)
 
 (def!struct mutex
   #!+sb-doc
   "Mutex type."
-  (name nil :type (or null simple-string))
+  (name   nil :type (or null thread-name))
   (%owner nil :type (or null thread))
-  #!+(and (not sb-lutex) sb-thread)
-  (state 0 :type fixnum)
-  #!+(and sb-lutex sb-thread)
-  (lutex (make-lutex)))
+  #!+(and sb-thread sb-futex)
+  (state    0 :type fixnum))
 
 (defun mutex-value (mutex)
   "Current owner of the mutex, NIL if the mutex is free. May return a
@@ -60,9 +66,29 @@ stale value, use MUTEX-OWNER instead."
 (def!struct spinlock
   #!+sb-doc
   "Spinlock type."
-  (name nil :type (or null simple-string))
+  (name  nil :type (or null thread-name))
   (value nil))
 
+(sb!xc:defmacro without-thread-waiting-for ((&key already-without-interrupts) &body body)
+  (with-unique-names (thread prev)
+    (let ((without (if already-without-interrupts
+                       'progn
+                       'without-interrupts))
+          (with (if already-without-interrupts
+                    'progn
+                    'with-local-interrupts)))
+      `(let* ((,thread *current-thread*)
+              (,prev (thread-waiting-for ,thread)))
+         (flet ((exec () ,@body))
+           (if ,prev
+               (,without
+                (unwind-protect
+                     (progn
+                       (setf (thread-waiting-for ,thread) nil)
+                       (,with (exec)))
+                  (setf (thread-waiting-for ,thread) ,prev)))
+               (exec)))))))
+
 (sb!xc:defmacro with-mutex ((mutex &key (value '*current-thread*) (wait-p t))
                             &body body)
   #!+sb-doc