Timerlib demo

Discussions related to the code libraries supplied with BB4W & BBCSDL
RichardRussell

Timerlib demo

Post by RichardRussell »

I think I'm right in saying that timerlib is the only library supplied with BBC BASIC for SDL 2.0 (and BBC BASIC for Windows) which does not have an accompanying example program to illustrate its use. Therefore I'm on the lookout for such a program.

If you have used timerlib and you think your program could be suitable as a demo of the library, perhaps with some adaptation, please let me know.
RichardRussell

Re: Timerlib demo

Post by RichardRussell »

RichardRussell wrote: Wed 09 Sep 2020, 15:44 I think I'm right in saying that timerlib is the only library supplied with BBC BASIC for SDL 2.0 (and BBC BASIC for Windows) which does not have an accompanying example program to illustrate its use.
Six weeks later and not a single reply. I'm very much still on the lookout for a program that can demonstrate the use of timerlib, so if nobody has anything suitable perhaps I can ask for ideas that might form the basis for a new program.
KenDown
Posts: 327
Joined: Wed 04 Apr 2018, 06:36

Re: Timerlib demo

Post by KenDown »

I'm afraid that I can't think of any use for a timer that will be exact down to 1,000th of a second. I'm sure there must be some - but how would the casual user tell the difference between a timer that went off in a 100th of a second and one that went off in 327/1000 of a second?
RichardRussell

Re: Timerlib demo

Post by RichardRussell »

KenDown wrote: Thu 05 Nov 2020, 01:18 I'm afraid that I can't think of any use for a timer that will be exact down to 1,000th of a second.
What has that to do with timerlib? No timer in Windows (or any other general-purpose Operating System) is accurate to 1 ms, and timerlib is no exception. Indeed timerlib is no more accurate than the ordinary ON TIME interrupt in BBC BASIC (it uses the same underlying mechanism). You are possibly confusing accuracy with resolution (not an uncommon mistake).

The whole point of timerlib is that it supports multiple timers. I've had a couple of suggestions at the Discussion Group but nothing that really fits the bill. I'm still very much open to ideas.
KenDown
Posts: 327
Joined: Wed 04 Apr 2018, 06:36

Re: Timerlib demo

Post by KenDown »

Hmmmm. I was basing my comment on what is said about timerlib in the Help for BB4W. I noticed the statement that interrupts "may occur at rates up to 1000Hz" and assumed that implied an accuracy of that order.

Unfortunately I cannot see anything in the Help about multiple timers. If that is indeed the chief benefit of timerlib, it's a pity it is not mentioned. Perhaps the SDL help is more forthcoming? Just looked, and it doesn't seem to be.
RichardRussell

Re: Timerlib demo

Post by RichardRussell »

KenDown wrote: Thu 05 Nov 2020, 22:06 I was basing my comment on what is said about timerlib in the Help for BB4W. I noticed the statement that interrupts "may occur at rates up to 1000Hz" and assumed that implied an accuracy of that order.
As I said, you're confusing resolution with accuracy; only a Real-Time Operating System could achieve an accuracy of that sort. The standard ON TIME timer also achieves a resolution of 1 millisecond: "Set the periodicity of the ON TIME interrupt to the specified number of milliseconds".
Unfortunately I cannot see anything in the Help about multiple timers.
"High speed timers" Note: timers plural.
"FN_ontimer sets up a timer" Note: a timer, not the timer!
"PROC_killtimer cancels the timer whose identifier is passed as a parameter" Note: the timer whose identifier is passed as a parameter.

All imply the possibility of having multiple timers. You may remember this old demo which I published way back when timerlib was first introduced; it works in both BB4W and BBCSDL:

Code: Select all

      INSTALL @lib$+"timerlib"

      ON CLOSE PROC_killtimer(tid1%) : PROC_killtimer(tid2%) : PROC_killtimer(tid3%) : QUIT
      ON ERROR PROC_killtimer(tid1%) : PROC_killtimer(tid2%) : PROC_killtimer(tid3%) : PRINT 'REPORT$ : END

      ON TIME PRINT "ON TIME timer" : RETURN

      TIME = 0
      tid1% = FN_ontimer(99, PROCtimer1, 1)
      tid2% = FN_ontimer(101, PROCtimer2, 1)
      tid3% = FN_ontimer(1000, PROCtimer3, 1)
      IF tid1% = 0 ERROR 100, "Failed to start timer1"
      IF tid2% = 0 ERROR 100, "Failed to start timer2"
      IF tid3% = 0 ERROR 100, "Failed to start timer2"

      REPEAT
        WAIT 10
      UNTIL FALSE
      END

      DEF PROCtimer1
      PRINT "Timer 1"
      ENDPROC

      DEF PROCtimer2
      PRINT "Timer 2"
      ENDPROC

      DEF PROCtimer3
      PRIVATE T%
      T% += 100
      COLOUR 1
      PRINT "Timer 3.........................." TIME-T%
      COLOUR 0
      ENDPROC
Hated Moron

Re: Timerlib demo

Post by Hated Moron »

RichardRussell wrote: Mon 19 Oct 2020, 22:01 Six weeks later and not a single reply. I'm very much still on the lookout for a program that can demonstrate the use of timerlib, so if nobody has anything suitable perhaps I can ask for ideas that might form the basis for a new program.
More than three years later and I'm still looking for an example program (or a suggestion for an example program) to demonstrate the timerlib library!

I appreciate that there's something of a circular argument here: if there's no example program, it reduces the likelihood of somebody using the library, and if fewer people use the library the source of potential example programs is reduced!

But being able to do things 'in the background' in BB4W and BBCSDL is a particularly valuable feature (I've mentioned before how much more difficult drawing the header row in Ceefax.bbc would have been without ON TIME) and having multiple timers gives more flexibility.

So can I once again ask people to put their thinking caps on? If anybody has actually used timerlib I would be very interested to know (and if they haven't perhaps it really is a useless library).
User avatar
hellomike
Posts: 184
Joined: Sat 09 Jun 2018, 09:47
Location: Amsterdam

Re: Timerlib demo

Post by hellomike »

In how many of the thousands of programs you wrote yourself, did you use the timer library?
Maybe the Profiler?
Hated Moron

Re: Timerlib demo

Post by Hated Moron »

hellomike wrote: Sun 22 Oct 2023, 09:04 In how many of the thousands of programs you wrote yourself, did you use the timer library?
Maybe the Profiler?
The Profiler does indeed use a fast timer in addition to the standard ON TIME timer (500 Hz in BBCSDL, 1 kHz in BB4W) but because there are large amounts of assembly language code already in that tool it doesn't actually use timerlib as such, the relevant code is embedded. Indeed on a quick search I don't use timerlib in any of my BBCSDL programs.

As far as BB4W is concerned I'm not sure, because we are going back 22 years (or at least until the BB4W TIMERLIB was first developed) and that's way beyond the span of my memory! But I think it is the case that the library was originally developed because I thought it should exist rather than to meet a specific requirement (since the OS supports multiple timers, BBC BASIC should expose them to the programmer).

Indeed that was the motivation behind several of the libraries. I promote BB4W and BBCSDL as 'general purpose' programming languages which expose many of the capabilities of the OS (i.e. SDL 2.0 in the case of BBCSDL) to the BASIC programmer. When that is made significantly easier by supplying a library I have tried to do so, whether or not I've personally had any need for it.
Hated Moron

Re: Timerlib demo

Post by Hated Moron »

On 22/10/2023 13:04, Daniel Bingamon wrote (cross-posted from the Discussion Group):
About five years ago I discovered that one my subroutines was being held up on a really slow computer and the timer service started cascading. That code was also written in VB.net which is considerably slower that C language.
So, I made boolean variable for the beginning of the timer service - if it were busy to drop out and not service the timer. If its not busy then set the busy state then do the work and then turn off the busy state at the end. This prevented any further cascading from occurring.

I don't know if BBC timer lib prevents cascading but I do know, from research and experience, that the Windows operating system does not prevent it. Later I wrote some code to test if it did it in C language and yes, it occurs there as well.
As you are probably aware, timerlib (in both BB4W and BBCSDL) makes use of the asynchronous event queueing mechanism, which is primarily to support the ON MOUSE, ON MOVE, ON SYS and ON TIME 'interrupts'. The queue is quite short (32 events) so that imposes a limit on how many timer interrupts can 'cascade' because they are not being serviced sufficiently often.

But generally that is not the limiting factor, the interpreter can almost always pull events out of the queue as fast as they are put in (the exception may be in code which contains stalling statements like WAIT or GET or INPUT, which should be avoided when real-time interrupts are expected; the nowait library is provided to help with this).

Where the limitation does typically arise is in the BASIC timer handler code itself. If I am concerned about cascading I use the standard technique you described, using a semaphore, to ensure that if another timer interrupt occurs whilst the previous one is still being serviced it is discarded. Typically the code looks something like this:

Code: Select all

      DEF PROCtimerhandler
      PRIVATE semaphore%
      IF semaphore% THEN ENDPROC
      semaphore% = TRUE
      REM the timer handler 'proper' goes here
      semaphore% = FALSE
      ENDPROC