aboutsummaryrefslogtreecommitdiff
path: root/semestr-2/racket/l15/kacp.bak
diff options
context:
space:
mode:
authorFranciszek Malinka <franciszek.malinka@gmail.com>2021-10-05 21:49:54 +0200
committerFranciszek Malinka <franciszek.malinka@gmail.com>2021-10-05 21:49:54 +0200
commitc5fcf7179a83ef65c86c6a4a390029149e518649 (patch)
treed29ffc5b86a0d257453cedcf87d91a13d8bf3b0d /semestr-2/racket/l15/kacp.bak
parentf8a88b6a4aba1f66d04711a9330eaba49a50c463 (diff)
Duzy commit ze smieciami
Diffstat (limited to 'semestr-2/racket/l15/kacp.bak')
-rw-r--r--semestr-2/racket/l15/kacp.bak55
1 files changed, 55 insertions, 0 deletions
diff --git a/semestr-2/racket/l15/kacp.bak b/semestr-2/racket/l15/kacp.bak
new file mode 100644
index 0000000..ff2a2bc
--- /dev/null
+++ b/semestr-2/racket/l15/kacp.bak
@@ -0,0 +1,55 @@
+#lang racket
+
+(define (run-concurrent . thunks)
+ (define threads (map thread thunks))
+ (for-each thread-wait threads))
+
+(define (random-sleep)
+ (sleep (/ (random) 100)))
+
+(define (with-random-sleep proc)
+ (lambda args
+ (random-sleep)
+ (apply proc args)))
+
+(define (make-serializer)
+ (define sem (make-semaphore 1))
+ (lambda (proc)
+ (lambda args
+ (semaphore-wait sem)
+ (define ret (apply proc args))
+ (semaphore-post sem)
+ ret)))
+
+(define (table)
+ (random-sleep)
+ (define forks (list (make-semaphore 1)
+ (make-semaphore 1)
+ (make-semaphore 1)
+ (make-semaphore 1)
+ (make-semaphore 1)))
+ (define (pick-fork i)
+ (random-sleep)
+ (semaphore-wait (list-ref forks i)))
+ (define (put-fork i)
+ (random-sleep)
+ (semaphore-post (list-ref forks i)))
+ (define (dispatch m)
+ (cond [(eq? m 'pick-fork) pick-fork]
+ [(eq? m 'put-fork) put-fork]
+ [else (error "Unknown request -- TABLE"
+ m)]))
+ dispatch)
+
+(define dtable (table))
+
+(define (philosopher dining-table number)
+ (define my-turn (make-serializer))
+ (define (eat)
+ (display number)
+ (newline)
+ ((dining-table 'pick-fork) number)
+ ((dining-table 'put-fork) number)
+ ((dining-table 'pick-fork) (modulo (+ number 1) 5))
+ ((dining-table 'put-fork) (modulo (+ number 1) 5)))
+ (my-turn eat)) \ No newline at end of file