2001-04-06 09:51:25 +00:00
|
|
|
|
;;;; Copyright (C) 1996, 1998, 2001 Free Software Foundation, Inc.
|
|
|
|
|
|
;;;;
|
1996-10-05 17:48:22 +00:00
|
|
|
|
;;;; This program is free software; you can redistribute it and/or modify
|
|
|
|
|
|
;;;; it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
;;;; the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
|
|
;;;; any later version.
|
2001-04-06 09:51:25 +00:00
|
|
|
|
;;;;
|
1996-10-05 17:48:22 +00:00
|
|
|
|
;;;; This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
;;;; GNU General Public License for more details.
|
2001-04-06 09:51:25 +00:00
|
|
|
|
;;;;
|
1996-10-05 17:48:22 +00:00
|
|
|
|
;;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
|
;;;; along with this software; see the file COPYING. If not, write to
|
* COPYING, boot-9.scm, debug.scm, emacs.scm, expect.scm, gtcl.scm,
gwish.scm, hcons.scm, lineio.scm, mapping.scm, nonblocking.scm,
oldprint.scm, poe.scm, r4rs.scm, source.scm, tags.scm, test.scm,
threads.scm: New address for FSF.
1997-05-26 22:26:48 +00:00
|
|
|
|
;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
|
|
|
|
;;;; Boston, MA 02111-1307 USA
|
1996-10-05 17:48:22 +00:00
|
|
|
|
;;;;
|
|
|
|
|
|
;;;; ----------------------------------------------------------------
|
|
|
|
|
|
;;;; threads.scm -- User-level interface to Guile's thread system
|
|
|
|
|
|
;;;; 4 March 1996, Anthony Green <green@cygnus.com>
|
|
|
|
|
|
;;;; Modified 5 October 1996, MDJ <djurfeldt@nada.kth.se>
|
2001-04-06 09:51:25 +00:00
|
|
|
|
;;;; Modified 6 April 2001, ttn
|
1996-10-05 17:48:22 +00:00
|
|
|
|
;;;; ----------------------------------------------------------------
|
|
|
|
|
|
;;;;
|
|
|
|
|
|
|
2001-04-28 18:54:16 +00:00
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
|
|
;; This module is documented in the Guile Reference Manual.
|
|
|
|
|
|
;; Briefly, one procedure is exported: `%thread-handler';
|
|
|
|
|
|
;; as well as four macros: `make-thread', `begin-thread',
|
|
|
|
|
|
;; `with-mutex' and `monitor'.
|
|
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
1996-10-05 17:48:22 +00:00
|
|
|
|
|
1997-06-24 16:26:27 +00:00
|
|
|
|
(define-module (ice-9 threads))
|
1996-10-05 17:48:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-04-06 09:51:25 +00:00
|
|
|
|
(define (%thread-handler tag . args)
|
1998-01-30 21:05:53 +00:00
|
|
|
|
(fluid-set! the-last-stack #f)
|
|
|
|
|
|
(unmask-signals)
|
|
|
|
|
|
(let ((n (length args))
|
|
|
|
|
|
(p (current-error-port)))
|
2001-04-06 09:51:25 +00:00
|
|
|
|
(display "In thread:" p)
|
|
|
|
|
|
(newline p)
|
|
|
|
|
|
(if (>= n 3)
|
|
|
|
|
|
(display-error #f
|
|
|
|
|
|
p
|
|
|
|
|
|
(car args)
|
|
|
|
|
|
(cadr args)
|
|
|
|
|
|
(caddr args)
|
|
|
|
|
|
(if (= n 4)
|
|
|
|
|
|
(cadddr args)
|
|
|
|
|
|
'()))
|
|
|
|
|
|
(begin
|
|
|
|
|
|
(display "uncaught throw to " p)
|
|
|
|
|
|
(display tag p)
|
|
|
|
|
|
(display ": " p)
|
|
|
|
|
|
(display args p)
|
|
|
|
|
|
(newline p)))))
|
1998-01-30 21:05:53 +00:00
|
|
|
|
|
2001-04-06 09:51:25 +00:00
|
|
|
|
; --- MACROS -------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
(defmacro make-thread (proc . args)
|
1996-10-05 17:48:22 +00:00
|
|
|
|
`(call-with-new-thread
|
|
|
|
|
|
(lambda ()
|
2001-04-06 09:51:25 +00:00
|
|
|
|
(,proc ,@args))
|
1998-01-30 21:05:53 +00:00
|
|
|
|
%thread-handler))
|
1996-10-05 17:48:22 +00:00
|
|
|
|
|
2001-04-06 09:51:25 +00:00
|
|
|
|
(defmacro begin-thread (first . rest)
|
1996-10-05 17:48:22 +00:00
|
|
|
|
`(call-with-new-thread
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
(begin
|
2001-04-06 09:51:25 +00:00
|
|
|
|
,first ,@rest))
|
1998-01-30 21:05:53 +00:00
|
|
|
|
%thread-handler))
|
1996-10-05 17:48:22 +00:00
|
|
|
|
|
2001-04-06 09:51:25 +00:00
|
|
|
|
(defmacro with-mutex (m . body)
|
1996-10-05 17:48:22 +00:00
|
|
|
|
`(dynamic-wind
|
2001-04-06 09:51:25 +00:00
|
|
|
|
(lambda () (lock-mutex ,m))
|
|
|
|
|
|
(lambda () (begin ,@body))
|
|
|
|
|
|
(lambda () (unlock-mutex ,m))))
|
1996-10-05 17:48:22 +00:00
|
|
|
|
|
2001-04-06 09:51:25 +00:00
|
|
|
|
(defmacro monitor (first . rest)
|
1996-10-05 17:48:22 +00:00
|
|
|
|
`(with-mutex ,(make-mutex)
|
2001-04-06 09:51:25 +00:00
|
|
|
|
(begin
|
|
|
|
|
|
,first ,@rest)))
|
|
|
|
|
|
|
|
|
|
|
|
;; export
|
|
|
|
|
|
|
|
|
|
|
|
(export %thread-handler)
|
|
|
|
|
|
|
|
|
|
|
|
(export-syntax make-thread
|
|
|
|
|
|
begin-thread
|
|
|
|
|
|
with-mutex
|
|
|
|
|
|
monitor)
|
|
|
|
|
|
|
|
|
|
|
|
;;; threads.scm ends here
|