summaryrefslogtreecommitdiff
path: root/modules/language/python/module/threading.scm
blob: 73d6cba546ec73638bda051acddc1235a79a984f (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(define-module (language python module threading)
  #:use-module (ice-9 threads)
  #:use-module (oop pf-objects)
  #:use-module (language python def)  
  #:export (RLock))

(define-python-class RLock ()
  (define __init__
    (lambda (self)
      (set self '_lock (make-mutex 'recursive))))
  
  (define __enter__
    (lambda (self)
      (lock-mutex (ref self '_lock))))

  (define __leave__
    (lambda (self)
      (unlock-mutex (ref self '_lock))))

  (define acquire
    (lam (self (= blocking #t) (= timeout -1))
	 (if blocking
	     (if (< timeout 0)
		 (lock-mutex (ref self '_lock))
		 (let* ((cur (gettimeofday))
			(x   (+ (car cur) (/ (cdr cur) 1000000.0)))
			(y   (+ x timeout))
			(s   (floor y))
			(us  (floor (* (- y s) 1000000))))
		   (lock-mutex (ref self '_lock) (cons s us))))
	     (try-mutex (ref self '_lock)))))
  
  (define release __leave__))
    
(define allocate_lock
  (lambda () (RLock)))