threads.scm 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ;;;; Copyright (C) 1996, 1998 Free Software Foundation, Inc.
  2. ;;;;
  3. ;;;; This program is free software; you can redistribute it and/or modify
  4. ;;;; it under the terms of the GNU General Public License as published by
  5. ;;;; the Free Software Foundation; either version 2, or (at your option)
  6. ;;;; any later version.
  7. ;;;;
  8. ;;;; This program is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;;;; GNU General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU General Public License
  14. ;;;; along with this software; see the file COPYING. If not, write to
  15. ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. ;;;; Boston, MA 02111-1307 USA
  17. ;;;;
  18. ;;;; ----------------------------------------------------------------
  19. ;;;; threads.scm -- User-level interface to Guile's thread system
  20. ;;;; 4 March 1996, Anthony Green <green@cygnus.com>
  21. ;;;; Modified 5 October 1996, MDJ <djurfeldt@nada.kth.se>
  22. ;;;; ----------------------------------------------------------------
  23. ;;;;
  24. (define-module (ice-9 threads))
  25. ; --- MACROS -------------------------------------------------------
  26. (define-public (%thread-handler tag . args)
  27. (fluid-set! the-last-stack #f)
  28. (unmask-signals)
  29. (let ((n (length args))
  30. (p (current-error-port)))
  31. (display "In thread:" p)
  32. (newline p)
  33. (if (>= n 3)
  34. (display-error #f
  35. p
  36. (car args)
  37. (cadr args)
  38. (caddr args)
  39. (if (= n 4)
  40. (cadddr args)
  41. '()))
  42. (begin
  43. (display "uncaught throw to " p)
  44. (display tag p)
  45. (display ": " p)
  46. (display args p)
  47. (newline p)))))
  48. (defmacro-public make-thread (fn . args)
  49. `(call-with-new-thread
  50. (lambda ()
  51. (,fn ,@args))
  52. %thread-handler))
  53. (defmacro-public begin-thread (first . thunk)
  54. `(call-with-new-thread
  55. (lambda ()
  56. (begin
  57. ,first ,@thunk))
  58. %thread-handler))
  59. (defmacro-public with-mutex (m . thunk)
  60. `(dynamic-wind
  61. (lambda () (lock-mutex ,m))
  62. (lambda () (begin ,@thunk))
  63. (lambda () (unlock-mutex ,m))))
  64. (defmacro-public monitor (first . thunk)
  65. `(with-mutex ,(make-mutex)
  66. (begin
  67. ,first ,@thunk)))