Add daydream.scm and re-enable some files in Makefile.am.
[8sync.git] / 8sync / daydream.scm
1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright © 2017 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;;
4 ;;; This file is part of 8sync.
5 ;;;
6 ;;; 8sync is free software: you can redistribute it and/or modify it
7 ;;; under the terms of the GNU Lesser General Public License as
8 ;;; published by the Free Software Foundation, either version 3 of the
9 ;;; License, or (at your option) any later version.
10 ;;;
11 ;;; 8sync is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;; GNU Lesser General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with 8sync.  If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (8sync daydream)
20   #:use-module (8sync)
21   #:use-module (fibers)
22   #:export (daydream))
23
24 ;;; Daydreaming is a lot like sleeping, except it doesn't block other
25 ;;; message correspondence.
26
27 (define-actor <daydreamer> (<actor>)
28   ((dream daydreamer-dream)))
29
30 (define (daydreamer-dream daydreamer message
31                           secs)
32   (sleep secs)
33   ;; We're a single use actor, so we blow up
34   (self-destruct daydreamer)
35   ;; Now reply to the actor's continuation
36   'wake-up)
37
38 ;; TODO: don't require the actor here
39 (define (daydream secs)
40   (define dreamer
41     (create-actor ((@@ (8sync actors) *current-actor*))
42                   <daydreamer>))
43   (<-wait dreamer 'dream secs))