Skip to content

Commit

Permalink
In thread related routines, tidy up naming stuff and export the API.
Browse files Browse the repository at this point in the history
  • Loading branch information
vy committed Jun 7, 2009
1 parent 34e6f6d commit 7ae8b59
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
13 changes: 7 additions & 6 deletions README
Expand Up @@ -38,24 +38,24 @@ thread pools in minutes.

(defvar *stream* *standard-output*)

(defvar *stream-lock* (bt:make-lock))
(defvar *stream-lock* (patron:make-lock))

(defun safe-format (fmt &rest args)
(bt:with-lock-held (*stream-lock*)
(patron:with-lock *stream-lock*
(apply #'format *stream* fmt args)))

(defun thread-stats (patron)
(safe-format
"Keepers: ~{~A~^ ~}~%Workers: ~{~A~^ ~}~%"
(map 'list #'patron::thread-alive-p (patron::keepers-of patron))
(map 'list #'patron::thread-alive-p (patron::workers-of patron))))
(map 'list #'patron:thread-alive-p (patron::keepers-of patron))
(map 'list #'patron:thread-alive-p (patron::workers-of patron))))

(let ((state (make-random-state)))
(defun job ()
(let ((duration (random 5 state)))
(safe-format " ~S => Sleeping... [~A]~%" (bt:current-thread) duration)
(safe-format " ~S => Sleeping... [~A]~%" (patron:current-thread) duration)
(sleep duration)
(safe-format " ~S => Done!~%" (bt:current-thread)))))
(safe-format " ~S => Done!~%" (patron:current-thread)))))

(defun report-result (job)
(safe-format "RESULT: JOB: ~S~%" job))
Expand Down Expand Up @@ -84,6 +84,7 @@ thread pools in minutes.
(patron:stop-patron patron :wait t)
(safe-format "Stopped.~%")
(thread-stats patron)))

; Starting...
; Keepers: T T
; Workers: T T T
Expand Down
12 changes: 12 additions & 0 deletions src/packages.lisp
Expand Up @@ -35,6 +35,18 @@
(defpackage :patron
(:use :cl)
(:export
;; Threading Utilities
:thread
:id-of
:function-of
:make-lock
:with-lock
:thread-start
:current-thread
:thread-alive-p
:thread-interrupt
:thread-join
:without-interrupts
;; Specials
:timeout-duration
:duration-of
Expand Down
4 changes: 2 additions & 2 deletions src/queue.lisp
Expand Up @@ -43,9 +43,9 @@
(prog1 queue
(setf (head-of queue) (list t)
(tail-of queue) (head-of queue)
(pop-lock-of queue) (lock-make)
(pop-lock-of queue) (make-lock)
(pop-semaphore-of queue) (semaphore-make)
(push-lock-of queue) (lock-make)
(push-lock-of queue) (make-lock)
(push-semaphore-of queue) (semaphore-make (size-of queue)))))

(defun %queue-pop (queue &optional default)
Expand Down
2 changes: 1 addition & 1 deletion src/specials.lisp
Expand Up @@ -297,7 +297,7 @@
:type keyword
:documentation "State of the patron; either `ACTIVE', or `INACTIVE'.")
(state-lock
:initform (lock-make)
:initform (make-lock)
:reader state-lock-of
:documentation "Synchronization primitive for `STATE' slot.")
(error-report-function
Expand Down
4 changes: 2 additions & 2 deletions src/thread.lisp
Expand Up @@ -32,7 +32,7 @@
;;; Thread & Locking Abstraction Layer
;;;

(defun lock-make ()
(defun make-lock ()
(bt:make-lock))

(defmacro with-lock (lock &body body)
Expand All @@ -48,7 +48,7 @@
(funcall (function-of thread))))
(semaphore-wait semaphore))))

(defun thread-current ()
(defun current-thread ()
(bt:current-thread))

(defun thread-alive-p (thread)
Expand Down

0 comments on commit 7ae8b59

Please sign in to comment.