Thread-sleep allow fractional seconds

This commit is contained in:
Justin Ethier 2017-11-13 18:41:10 -05:00
parent 2911c88a7b
commit e465f811f5
10 changed files with 24 additions and 20 deletions

View file

@ -8,6 +8,10 @@ Features
- Allow a program to have macros expand into a top-level `import` expression. - Allow a program to have macros expand into a top-level `import` expression.
- Added continuous integration support thanks to Alex Arslan. - Added continuous integration support thanks to Alex Arslan.
Bug Fixes
- Incorporated a patch from @0-8-15 to pass seconds to `thread-sleep!` instead of milliseconds. Fractional seconds are accepted as well for high-resolution timers.
## 0.6.3 - September 16, 2017 ## 0.6.3 - September 16, 2017
Features Features

View file

@ -74,17 +74,17 @@ Makes thread runnable. The thread must be a new thread. thread-start! returns th
(thread-sleep! timeout) (thread-sleep! timeout)
Block the current thread for `timeout` milliseconds. Block the current thread for `timeout` seconds. Fractional seconds may be provided to sleep for less than one second.
# thread-yield! # thread-yield!
(thread-yield!) (thread-sleep! 1)) (thread-yield!)
The current thread exits the running state as if its quantum had expired. The current thread exits the running state as if its quantum had expired.
# thread-terminate! # thread-terminate!
(thread-terminate! (thread-terminate!)
Immediately abort the current thread. Immediately abort the current thread.

View file

@ -19,10 +19,10 @@
(write "consumer sleeping") (write "consumer sleeping")
(set! sleep? #t))) (set! sleep? #t)))
(mutex-unlock! *lock*) (mutex-unlock! *lock*)
(if sleep? (thread-sleep! 1000)) (if sleep? (thread-sleep! 1))
(loop)))) (loop))))
'()) '())
(write `(,i)) (write `(,i))
(thread-sleep! 5) (thread-sleep! 0.005)
(loop (+ i 1))) (loop (+ i 1)))

View file

@ -176,7 +176,7 @@
(thread-start! t))) (thread-start! t)))
(define (wait-for-all-async) (define (wait-for-all-async)
(thread-sleep! 1) ;; TODO: not good enough, figure out a better solution (thread-sleep! 0) ;; TODO: not good enough, figure out a better solution
(let loop () (let loop ()
(define t #f) (define t #f)
(mutex-lock! m) (mutex-lock! m)

View file

@ -197,7 +197,7 @@
(thread-start! t))) (thread-start! t)))
(define (wait-for-all-async) (define (wait-for-all-async)
(thread-sleep! 1) ;; TODO: not good enough, figure out a better solution (thread-sleep! 0) ;; TODO: not good enough, figure out a better solution
(let loop () (let loop ()
(define t #f) (define t #f)
(mutex-lock! m) (mutex-lock! m)

View file

@ -16,7 +16,7 @@
(thread-start! (thread-start!
(make-thread (make-thread
(lambda () (lambda ()
(thread-sleep! 3000) (thread-sleep! 3)
(set! *done* #t) (set! *done* #t)
(condition-variable-broadcast! cv) (condition-variable-broadcast! cv)
(trace "broadcast thread done")))) (trace "broadcast thread done"))))
@ -42,7 +42,7 @@
(cond (cond
(*done* (*done*
(mutex-unlock! m) (mutex-unlock! m)
(thread-sleep! 500) (thread-sleep! 0.5)
(trace "main thread done")) (trace "main thread done"))
(else (else
(mutex-unlock! m cv) ;; Wait on cv (mutex-unlock! m cv) ;; Wait on cv

View file

@ -12,20 +12,20 @@
(thread-start! (thread-start!
(make-thread (make-thread
(lambda () (lambda ()
(thread-sleep! 1200) (thread-sleep! 1.2)
(display "started thread, this should be written to console") (display "started thread, this should be written to console")
(newline) (newline)
(display "thread done") (display "thread done")
(newline) (newline)
(flush-output-port (current-output-port))))) (flush-output-port (current-output-port)))))
(thread-sleep! 1000) ;; Prevent race condition replacing stdout before thread is spawned (thread-sleep! 1) ;; Prevent race condition replacing stdout before thread is spawned
(write `(1 2 3)) (write `(1 2 3))
(define fp (open-output-file "tmp.txt")) (define fp (open-output-file "tmp.txt"))
(parameterize (parameterize
((current-output-port fp)) ((current-output-port fp))
(write `(4 5 6)) (write `(4 5 6))
(thread-sleep! 3000) (thread-sleep! 3)
) )
(close-port fp) (close-port fp)
(write `(7 8 9)) (write `(7 8 9))

View file

@ -43,7 +43,7 @@
(newline) (newline)
(set! sleep? #t))) (set! sleep? #t)))
(mutex-unlock! *lock*) (mutex-unlock! *lock*)
(if sleep? (thread-sleep! 1000)) (if sleep? (thread-sleep! 1))
(loop))) (loop)))
(thread-start! (make-thread producer)) (thread-start! (make-thread producer))

View file

@ -13,7 +13,7 @@
(lambda () (lambda ()
(display "started thread") (display "started thread")
(newline) (newline)
(thread-sleep! 3000) (thread-sleep! 3)
(display "thread done") (display "thread done")
(newline) (newline)
(condition-variable-broadcast! cv)))) (condition-variable-broadcast! cv))))
@ -23,7 +23,7 @@
(mutex-unlock! m cv) ;; Wait on cv (mutex-unlock! m cv) ;; Wait on cv
(display "main thread done") (display "main thread done")
(newline) (newline)
(thread-sleep! 500) (thread-sleep! 0.5)
;(display "thread join") ;(display "thread join")
;(newline) ;(newline)
@ -31,12 +31,12 @@
; (lambda () ; (lambda ()
; (display "started second thread") ; (display "started second thread")
; (newline) ; (newline)
; (thread-sleep! 3000) ; (thread-sleep! 3)
; (display "thread done") ; (display "thread done")
; (newline) ; (newline)
; 1)))) ; 1))))
; (thread-start! t) ; (thread-start! t)
; (thread-sleep! 1) ; (thread-sleep! 0)
; (display (thread-join! t)) ; (display (thread-join! t))
; (display "main thread done again") ; (display "main thread done again")
; (newline)) ; (newline))

View file

@ -5692,11 +5692,11 @@ void Cyc_exit_thread(gc_thread_data * thd)
object Cyc_thread_sleep(void *data, object timeout) object Cyc_thread_sleep(void *data, object timeout)
{ {
struct timespec tim; struct timespec tim;
long value; double value;
Cyc_check_num(data, timeout); Cyc_check_num(data, timeout);
value = unbox_number(timeout); value = unbox_number(timeout);
tim.tv_sec = value; tim.tv_sec = (long)value;
tim.tv_nsec = (value % 1000000) * NANOSECONDS_PER_MILLISECOND; tim.tv_nsec = (long)((value - tim.tv_sec) * 1000 * NANOSECONDS_PER_MILLISECOND);
nanosleep(&tim, NULL); nanosleep(&tim, NULL);
return boolean_t; return boolean_t;
} }