source: project/release/4/synch/trunk/tests/synch-test.scm @ 35091

Last change on this file since 35091 was 35091, checked in by kon, 8 months ago

add format-synch ex , growl idents , dep slashers

File size: 1.6 KB
Line 
1;;;; synch test
2
3(use synch srfi-18 srfi-69 miscmacros)
4
5;;
6
7(define-syntax define-thread
8        (syntax-rules ()
9          ((_ ?ident ?body ...)
10            (define ?ident
11        (make-thread
12          (lambda () ?body ...)
13          '?ident) ) ) ) )
14
15;;;
16
17(define-record-type <foo>
18  (make-<foo> x y mtx)
19  <foo>?
20  (x <foo>-x)
21  (y <foo>-y)
22  (mtx <foo>-mutex))
23
24(define tfoo (make-<foo> 1 2 (make-mutex)))
25(print "*** prints 1 2 ***")
26(record-synch <foo> tfoo (print (<foo>-x tfoo) " " (<foo>-y tfoo)))
27(newline)
28
29;;; Synchronize thread access to an object
30
31;;
32
33(define (hash-table-count ht)
34  (##sys#check-structure ht 'hash-table 'hash-table-count)
35  (hash-table-fold ht (lambda (k v a) (fx+ a 1)) 0) )
36
37;;
38
39(define-constructor-synch make-hash-table hash-table-synch:)
40(define-predicate-synch hash-table?)
41(define-operation-synch hash-table-count)
42(define-operation-synch hash-table-set!)
43
44;;
45
46(define +tht+ (make-hash-table-synch = number-hash))
47
48(define-constant READER-THREAD-LIMIT 20)
49
50(define-constant THREAD-SLEEP-MS 0 #;2000)
51
52;; Greedy Reader
53
54(define-thread reader-thread
55  (do ((n (hash-table-count-synch +tht+) (hash-table-count-synch +tht+)))
56      ((fx= READER-THREAD-LIMIT n)
57        (print "test hash-table count = " n " so quit"))
58    (print "test hash-table count = " n)
59    (thread-sleep! THREAD-SLEEP-MS) ) )
60
61;; Cooperative Writer
62
63(define-thread writer-thread
64  (repeat* 10
65    (hash-table-set!-synch +tht+ it (number->string it))
66    (hash-table-set!-synch +tht+ (* it 11) (number->string it))
67    (thread-sleep! THREAD-SLEEP-MS)
68    (thread-yield!) ) )
69
70;;
71
72(thread-start! writer-thread)
73(thread-start! reader-thread)
74
75(thread-join! writer-thread)
76(thread-join! reader-thread)
Note: See TracBrowser for help on using the repository browser.