The Computer Language
Benchmarks Game

thread-ring Lisp SBCL #2 program

source code

;;; The Computer Language Benchmarks Game
;;; http://benchmarksgame.alioth.debian.org/
;;;
;;; contributed by Witali Kusnezow 2008-12-19
;;;     using semaphore synchronization

(defconstant  +threads+ 503)
(defparameter *counter* 0)
(defparameter *current* 0)
(defparameter *semaphore* (sb-thread:make-semaphore))
(defparameter *semaphores*
  (make-array +threads+
              :initial-contents
              (loop for i of-type fixnum below +threads+
                 collect (sb-thread:make-semaphore))))

(declaim (type fixnum *counter* *current*))

(defmacro wait   (semaphore)
  `(sb-thread:wait-on-semaphore ,semaphore))
(defmacro wake (semaphore)
  `(sb-thread:signal-semaphore  ,semaphore))
(defmacro kill   (thread)
  `(handler-case (sb-thread:terminate-thread ,thread)
     (sb-thread:interrupt-thread-error () nil)))

(defun thread-body ()
  (let* ((curr (svref *semaphores* *current*))
         (next (svref *semaphores* (if (= (incf *current*) +threads+) 0 *current*)))
         (number *current*))
    (loop do (wait curr)
       until (zerop (decf *counter*))
       do (wake next)
       finally (format t "~d~%" number) (wake *semaphore*))))

(defun start (n)
  (declare (type fixnum n))
  (setq *counter* (1+ n) *current* 0)
  (loop for i of-type fixnum below +threads+
     collect (sb-thread:make-thread #'thread-body) into threads
     finally
       (wake (svref *semaphores* 0))
       (wait *semaphore*)
       (dolist (i threads) (kill i))))

(defun main ()
  (let ((n (parse-integer (or (car (last #+sbcl sb-ext:*posix-argv*
                                         #+cmu  extensions:*command-line-strings*
                                         #+gcl  si::*command-args*)) "1"))))
    (start n)))
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
SBCL 1.4.0


Thu, 26 Oct 2017 17:54:01 GMT

MAKE:
cp: 'threadring.sbcl-2.sbcl' and './threadring.sbcl-2.sbcl' are the same file
SBCL built with: /opt/src/sbcl-1.4.0/bin/sbcl --userinit /dev/null --batch --eval '(load "threadring.sbcl-2.sbcl_compile")'
### START threadring.sbcl-2.sbcl_compile
(handler-bind ((sb-ext:defconstant-uneql      (lambda (c) (abort c))))      (load (compile-file "threadring.sbcl-2.sbcl" ))) (save-lisp-and-die "sbcl.core" :purify t)
### END threadring.sbcl-2.sbcl_compile

; compiling file "/home/dunham/benchmarksgame/bench/threadring/threadring.sbcl-2.sbcl" (written 24 JAN 2013 01:22:34 PM):
; compiling (DEFCONSTANT +THREADS+ ...)
; compiling (DEFPARAMETER *COUNTER* ...)
; compiling (DEFPARAMETER *CURRENT* ...)
; compiling (DEFPARAMETER *SEMAPHORE* ...)
; compiling (DEFPARAMETER *SEMAPHORES* ...)
; compiling (DECLAIM (TYPE FIXNUM ...))
; compiling (DEFMACRO WAIT ...)
; compiling (DEFMACRO WAKE ...)
; compiling (DEFMACRO KILL ...)
; compiling (DEFUN THREAD-BODY ...)
; compiling (DEFUN START ...)
; compiling (DEFUN MAIN ...)

; /home/dunham/benchmarksgame_quadcore/threadring/tmp/threadring.sbcl-2.fasl written
; compilation finished in 0:00:00.026
### START threadring.sbcl-2.sbcl_run
(main) (quit)
### END threadring.sbcl-2.sbcl_run


3.51s to complete and log all make actions

COMMAND LINE:
/opt/src/sbcl-1.4.0/bin/sbcl  --noinform --core sbcl.core --userinit /dev/null --load threadring.sbcl-2.sbcl_run 50000000

PROGRAM OUTPUT:
292