Page 1 of 1
Timer question
Posted: Tue May 31, 2005 8:27 pm
by pjot
Hi,
Experimenting with the 'timer' function it seemed to be interesting to use it as a kind of thread.
Code: Select all
#!/usr/bin/newlisp
(define (ticker)
(println (date)) (timer 'ticker 1.0))
(ticker)
(while true (println (read-key)))
However, in this program the ticker is only executed once, while you would expect that the 'while' is interrupted with a 'println (date)' every second. According to the manpage of 'setitimer':
The system provides each process with three interval timers, each decrementing in a distinct time domain. When any timer expires, a signal is sent to the process, and the timer (potentially) restarts.
Probably the signal is being sent (I trust Unix!), but obviously, newLisp is too busy with other things to capture that signal and carry out the appropriate function.
In other words: the timer function only works when newLisp is not doing anything else? So only in interactive mode?
Peter
Posted: Tue May 31, 2005 9:25 pm
by pjot
The 'timer' keyword does not appear in the VIM syntax file. Nor the keyword 'semaphore'.
In the meantime I found out that during a 'sleep' newLisp will respond to a timer signal.
Peter
Posted: Tue May 31, 2005 11:02 pm
by Lutz
Yes, SIGALRM kills sleep and I think SIGCHLD does too, that's the way it is on UNIX.
I added 'timer', 'share' and 'semaphore', it sait 'share' and 'semaphore' where added, but in reality they were not.
Lutz
Posted: Tue May 31, 2005 11:15 pm
by Lutz
>> > in other words: the timer function only works when newLisp is not >>> doing anything else? So only in interactive mode?
Oh no, absolutely not. 'timer' can interrupt any newLISP code, not only interactively. I have to run now. But I post you an example later tonight.
Lutz
Posted: Wed Jun 01, 2005 1:43 am
by Lutz
Here is an example showing the timer interrupting another function in progress, run this program:
Code: Select all
; timer demo
define (alarm)
(set 'done true)
(println "ring ..."))
(timer 'alarm 2.0)
(until done
(println ".")
(sleep 300))
(println "finished")
(exit)
Lutz
Posted: Wed Jun 01, 2005 8:23 am
by pjot
Yes, it interrupts the function because there is a 'sleep' in there. If you remove the sleep then it does not work.
Posted: Wed Jun 01, 2005 1:15 pm
by Lutz
This is not true, the timer can interrupt other operations too:
Code: Select all
(define (mysleep n)
(set 'start (time-of-day))
(while (> (+ start n) (time-of-day))))
(define (alarm)
(set 'done true)
(println "ring ..."))
(timer 'alarm 2.0)
(until done
(println ".")
(mysleep 300))
(println "finished")
(exit)
Lutz
Posted: Wed Jun 01, 2005 3:26 pm
by pjot
OK indeed it works. But this one doesn't, and I was blinded by it:
Code: Select all
#!/usr/bin/newlisp
(define (ticker)
(println (date)) (timer 'ticker 1.0))
(timer 'ticker 1.0)
(while true (println (read-key)))
But I already choose another way for my program. :-)
Posted: Wed Jun 01, 2005 3:42 pm
by Lutz
You only can interrupt inbetween the evaluation of 2 newLISP expressions. The timer signal is received and registered by newLISP, but it must come out of the 'atomic' read-key first to execute the handler. The same is true for all I/O operations. I.e. 'read-buffer' or 'read-line' operations would have to finish first, before the 'timer' or any other 'signal' handler can run.
In the case of network functions you can work with 'net-select' and 'net-peek' before you go into a read operation:
Code: Select all
(define (interrupt)
(set 'timeout true))
(timer 'interrupt 5.0)
(until (or timeout done)
(if (net-select socket "read" 100000)
(begin
(read-buffer socket 'buffer 1024)
(set 'done true)))
)
This code would wait for information on 'socket' for 5 seconds and then time out.
Lutz
Posted: Wed Jun 01, 2005 6:37 pm
by pjot
Well, you are ahead of me, indeed I was about to ask stuff regarding READ/WRITE operations.
Thanks for the clarifications.
Peter