Timer question

Q&A's, tips, howto's
Locked
pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Timer question

Post 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

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Yes, it interrupts the function because there is a 'sleep' in there. If you remove the sleep then it does not work.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post 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. :-)

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Well, you are ahead of me, indeed I was about to ask stuff regarding READ/WRITE operations.

Thanks for the clarifications.

Peter

Locked