www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - event based timer

reply maarten van damme <maartenvd1994 gmail.com> writes:
--00504502c6d3ce790f04a8697365
Content-Type: text/plain; charset=ISO-8859-1

Hi everyone,
for getting to know d a little bit I'm writing a simple pingpong game using
gtk.
for it to work I need to be able to do something every 0.1 seconds so I was
wondering if there was some kind of timer in the phobos library.
I've looked everywhere but couldn't find one. Is it missing and do i have to
write my own or have I overlooked it?

maarten

--00504502c6d3ce790f04a8697365
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi everyone,<div>for getting to know d a little bit I&#39;m writing a simpl=
e pingpong game using gtk.</div><div>for it to work I need to be able to do=
 something every 0.1 seconds so I was wondering if there was some kind of t=
imer in the phobos library.</div>
<div>I&#39;ve looked everywhere but couldn&#39;t find one. Is it missing an=
d do i have to write my own or have I overlooked it?</div><div><br></div><d=
iv>maarten</div>

--00504502c6d3ce790f04a8697365--
Jul 19 2011
next sibling parent reply Piotr Szturmaj <bncrbme jadamspam.pl> writes:
maarten van damme wrote:
 Hi everyone,
 for getting to know d a little bit I'm writing a simple pingpong game
 using gtk.
 for it to work I need to be able to do something every 0.1 seconds so I
 was wondering if there was some kind of timer in the phobos library.
 I've looked everywhere but couldn't find one. Is it missing and do i
 have to write my own or have I overlooked it?

 maarten

There is no callback timer in Phobos. But here's my implementation of timer wheel I used in one of my projects: http://pastebin.com/dRRZtPVW. Feel free to use it if you want.
Jul 19 2011
next sibling parent David Nadlinger <see klickverbot.at> writes:
On 7/20/11 12:05 PM, Martin Nowak wrote:
 It would be really better if phobos had some kind of timer. The thread
 solution is really suboptimal.

I think a major problem when trying to implement a general-purpose solution is that, by its very nature, a timer depends on the event handling scheme used. For example, if you wanted to use a timer on Posix to interrupt your potentially blocking system calls, you would use alarm() to set a signal-based timeout and register a signal handler for it. On the other hand, a typical GUI application has its own event loop to integrate with, for an application using non-blocking I/O, you would like to transparently map your timers on select()/… timeout arguments, etc. David
Jul 20 2011
prev sibling parent Piotr Szturmaj <bncrbme jadamspam.pl> writes:
Martin Nowak wrote:
 You are aware though that in your timer, the callback is executed from
 within a different thread.

Yes. User should be aware of this different thread and should perform synchronization manually. Anyway, it is possible to relay callbacks to a queue and then one can wait for events on that queue. (This timer class was designed to handle thousands of concurrent timers.)
Jul 20 2011
prev sibling next sibling parent reply maarten van damme <maartenvd1994 gmail.com> writes:
--000e0cd32c28d423a304a87b43db
Content-Type: text/plain; charset=ISO-8859-1

Thanks but it seems way more complex then I need, I tried writing my own but
I get an acces violation error. here is my try :
http://dl.dropbox.com/u/15024434/Timer.d . Can someone spot the error? keep
in mind I don't really understand the threading model and pointers.


2011/7/19 Piotr Szturmaj <bncrbme jadamspam.pl>

 maarten van damme wrote:

 Hi everyone,
 for getting to know d a little bit I'm writing a simple pingpong game
 using gtk.
 for it to work I need to be able to do something every 0.1 seconds so I
 was wondering if there was some kind of timer in the phobos library.
 I've looked everywhere but couldn't find one. Is it missing and do i
 have to write my own or have I overlooked it?

 maarten

There is no callback timer in Phobos. But here's my implementation of timer wheel I used in one of my projects: http://pastebin.com/dRRZtPVW. Feel free to use it if you want.

--000e0cd32c28d423a304a87b43db Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thanks but it seems way more complex then I need, I tried writing my own bu= t I get an acces violation error. here is my try : <a href=3D"http://dl.dro= pbox.com/u/15024434/Timer.d">http://dl.dropbox.com/u/15024434/Timer.d</a> .= Can someone spot the error? keep in mind I don&#39;t really understand the= threading model and pointers.<br> <br><div><br><div><div><div class=3D"gmail_quote">2011/7/19 Piotr Szturmaj = <span dir=3D"ltr">&lt;<a href=3D"mailto:bncrbme jadamspam.pl">bncrbme jadam= spam.pl</a>&gt;</span><br><blockquote class=3D"gmail_quote" style=3D"margin= :0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <div><div></div><div class=3D"h5">maarten van damme wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Hi everyone,<br> for getting to know d a little bit I&#39;m writing a simple pingpong game<b= r> using gtk.<br> for it to work I need to be able to do something every 0.1 seconds so I<br> was wondering if there was some kind of timer in the phobos library.<br> I&#39;ve looked everywhere but couldn&#39;t find one. Is it missing and do = i<br> have to write my own or have I overlooked it?<br> <br> maarten<br> </blockquote> <br></div></div> There is no callback timer in Phobos. But here&#39;s my implementation of t= imer wheel I used in one of my projects: <a href=3D"http://pastebin.com/dRR= ZtPVW" target=3D"_blank">http://pastebin.com/dRRZtPVW</a>. Feel free to use= it if you want.<br> </blockquote></div><br></div></div></div> --000e0cd32c28d423a304a87b43db--
Jul 20 2011
parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
private void  function() *  callBack;

should be private void  function() callBack;

void function() is a function pointer, void function()* is a pointer to a 
function pointer.

On lines 23 and 41, you shouldn't be dereferencing callBack.  Just use 
callBack() to call the function and 'this.callBack = callBack' to set it. 
Jul 20 2011
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
--00504502cd050a61e404a87cbe07
Content-Type: text/plain; charset=ISO-8859-1

Thanks a lot, now those errors are gone. Still it refuses to work
properly...
I have a method clockTick in Main.d and that should be called every 0.1
seconds, when I place a call to that method in a onmousenotify event(when
the mouse moves) It runs correctly apart from the odd fact I need to move my
mouse to play the game.
When I use that method in the timerclass it reaches only the lines where I
print something in the console but doesn't go any further without giving any
errors.

The complete code can be seen at:
http://dl.dropbox.com/u/15024434/d/Main.d
http://dl.dropbox.com/u/15024434/d/Timer.d
http://dl.dropbox.com/u/15024434/d/Sprite.d
http://dl.dropbox.com/u/15024434/d/Bain.d
http://dl.dropbox.com/u/15024434/d/PingPongBox.d

The code for the timer in Main.d is at line 63
clockTick method in Main.d is at line 93

2011/7/20 Daniel Murphy <yebblies nospamgmail.com>

 private void  function() *  callBack;

 should be private void  function() callBack;

 void function() is a function pointer, void function()* is a pointer to a
 function pointer.

 On lines 23 and 41, you shouldn't be dereferencing callBack.  Just use
 callBack() to call the function and 'this.callBack = callBack' to set it.

--00504502cd050a61e404a87cbe07 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thanks a lot, now those errors are gone. Still it refuses to work properly.= ..<div>I have a method clockTick in Main.d and that should be called every = 0.1 seconds, when I place a call to that method in a onmousenotify event(wh= en the mouse moves) It runs correctly apart from the odd fact I need to mov= e my mouse to play the game.=A0</div> <div>When I use that method in the timerclass it reaches only the lines whe= re I print something in the console but doesn&#39;t go any further without = giving any errors.=A0</div><div><br></div><div>The complete code can be see= n at:</div> <div><a href=3D"http://dl.dropbox.com/u/15024434/d/Main.d">http://dl.dropbo= x.com/u/15024434/d/Main.d</a></div><div><a href=3D"http://dl.dropbox.com/u/= 15024434/d/Timer.d">http://dl.dropbox.com/u/15024434/d/Timer.d</a></div><di= v> <a href=3D"http://dl.dropbox.com/u/15024434/d/Sprite.d">http://dl.dropbox.c= om/u/15024434/d/Sprite.d</a></div><div><a href=3D"http://dl.dropbox.com/u/1= 5024434/d/Bain.d">http://dl.dropbox.com/u/15024434/d/Bain.d</a></div><div><= a href=3D"http://dl.dropbox.com/u/15024434/d/PingPongBox.d">http://dl.dropb= ox.com/u/15024434/d/PingPongBox.d</a></div> <div><br></div><div>The code for the timer in Main.d is at line 63</div><di= v>clockTick method in Main.d is at line 93</div><div><br><div class=3D"gmai= l_quote">2011/7/20 Daniel Murphy <span dir=3D"ltr">&lt;<a href=3D"mailto:ye= bblies nospamgmail.com">yebblies nospamgmail.com</a>&gt;</span><br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;">private void =A0function() * =A0callBack;<b= r> <br> should be private void =A0function() callBack;<br> <br> void function() is a function pointer, void function()* is a pointer to a<b= r> function pointer.<br> <br> On lines 23 and 41, you shouldn&#39;t be dereferencing callBack. =A0Just us= e<br> callBack() to call the function and &#39;this.callBack =3D callBack&#39; to= set it.<br> <br> <br> </blockquote></div><br></div> --00504502cd050a61e404a87cbe07--
Jul 20 2011
prev sibling next sibling parent "Martin Nowak" <dawg dawgfoto.de> writes:
You are aware though that in your timer, the callback is executed from  
within a different thread.
You could let the timer thread send messages as clock ticks and wait on  
them, that way risking less issues
with implicit sharing.

Martin

----

import std.concurrency, std.stdio;
import core.thread;

struct Timer {
     private Tid clock;
     private bool running;
     enum Terminate { _ };
     enum Ping { _ };

     ~this() {
         stop();
     }

     void start(Duration duration) {
         avoidMailboxSpam();
         stop();
         clock = spawnLinked(&runClock, thisTid, duration);
         running = true;
     }

     void stop() {
         if (running)
             clock.send(Terminate._);
         running = false;
     }

     void wait() {
         receiveOnly!Ping();
     }

     private static void avoidMailboxSpam() {
         setMaxMailboxSize(thisTid, 5, OnCrowding.throwException);
     }

     private static void runClock(Tid tick, Duration duration) {
         bool cont = true;
         size_t cnt;
         try {
             while (cont) {
                 auto timedOut = !receiveTimeout(
                     duration,
                     (Terminate) { cont = false; },
                 );
                 if (timedOut) {
                     writefln("  #%-5d   Ping  ", cnt++);
                     tick.send(Ping._);
                 }
             }
         } catch (Exception ex) {
             stderr.writeln(ex.msg);
             throw ex;
         }
     }
}

void main() {
     Timer timer;
     auto interval = dur!"msecs"(500);
     timer.start(interval);

     foreach(cnt; 0 .. 200) {
         timer.wait();

         // Play some pong, if it takes too long our mailbox gets flooded
         Thread.sleep(interval / 2);
         writefln("  Pong     #%-5d", cnt);
     }
     timer.stop();
}


On Wed, 20 Jul 2011 09:34:39 +0200, maarten van damme  
<maartenvd1994 gmail.com> wrote:

 Thanks but it seems way more complex then I need, I tried writing my own  
 but
 I get an acces violation error. here is my try :
 http://dl.dropbox.com/u/15024434/Timer.d . Can someone spot the error?  
 keep
 in mind I don't really understand the threading model and pointers.


 2011/7/19 Piotr Szturmaj <bncrbme jadamspam.pl>

 maarten van damme wrote:


 There is no callback timer in Phobos. But here's my implementation of  
 timer
 wheel I used in one of my projects: http://pastebin.com/dRRZtPVW. Feel
 free to use it if you want.


Jul 20 2011
prev sibling next sibling parent "Martin Nowak" <dawg dawgfoto.de> writes:
Well, I guessed right I think. In you're main file you are using global  
//VARIABLES but in fact they are thread local.
That is each thread has it's own copy of them, so that different threads  
don't mess up each others data.
You can try the other timer approach or solve your sharing.

It would be really better if phobos had some kind of timer. The thread  
solution is really suboptimal.

On Wed, 20 Jul 2011 11:20:24 +0200, maarten van damme  
<maartenvd1994 gmail.com> wrote:

 Thanks a lot, now those errors are gone. Still it refuses to work
 properly...
 I have a method clockTick in Main.d and that should be called every 0.1
 seconds, when I place a call to that method in a onmousenotify event(when
 the mouse moves) It runs correctly apart from the odd fact I need to  
 move my
 mouse to play the game.
 When I use that method in the timerclass it reaches only the lines where  
 I
 print something in the console but doesn't go any further without giving  
 any
 errors.

 The complete code can be seen at:
 http://dl.dropbox.com/u/15024434/d/Main.d
 http://dl.dropbox.com/u/15024434/d/Timer.d
 http://dl.dropbox.com/u/15024434/d/Sprite.d
 http://dl.dropbox.com/u/15024434/d/Bain.d
 http://dl.dropbox.com/u/15024434/d/PingPongBox.d

 The code for the timer in Main.d is at line 63
 clockTick method in Main.d is at line 93

 2011/7/20 Daniel Murphy <yebblies nospamgmail.com>

 private void  function() *  callBack;

 should be private void  function() callBack;

 void function() is a function pointer, void function()* is a pointer to  
 a
 function pointer.

 On lines 23 and 41, you shouldn't be dereferencing callBack.  Just use
 callBack() to call the function and 'this.callBack = callBack' to set  
 it.


Jul 20 2011
prev sibling parent Chris Molozian <chris cmoz.me> writes:
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

As you're creating a Gtk app, have you considered using glib.Timer 
<http://gtkd.mikewey.eu/src/glib/Timer.html> or glib.Timeout 
<http://gtkd.mikewey.eu/src/glib/Timeout.html> (depending on your 
needs)? I do almost all my Gtk development in Vala these days, so I 
haven't used them from D (so not sure if you've encountered problems 
with them).

Cheers,

Chris


On 07/20/11 10:20, maarten van damme wrote:
 Thanks a lot, now those errors are gone. Still it refuses to work 
 properly...
 I have a method clockTick in Main.d and that should be called every 
 0.1 seconds, when I place a call to that method in a onmousenotify 
 event(when the mouse moves) It runs correctly apart from the odd fact 
 I need to move my mouse to play the game.
 When I use that method in the timerclass it reaches only the lines 
 where I print something in the console but doesn't go any further 
 without giving any errors.

 The complete code can be seen at:
 http://dl.dropbox.com/u/15024434/d/Main.d
 http://dl.dropbox.com/u/15024434/d/Timer.d
 http://dl.dropbox.com/u/15024434/d/Sprite.d
 http://dl.dropbox.com/u/15024434/d/Bain.d
 http://dl.dropbox.com/u/15024434/d/PingPongBox.d

 The code for the timer in Main.d is at line 63
 clockTick method in Main.d is at line 93

 2011/7/20 Daniel Murphy <yebblies nospamgmail.com 
 <mailto:yebblies nospamgmail.com>>

     private void  function() *  callBack;

     should be private void  function() callBack;

     void function() is a function pointer, void function()* is a
     pointer to a
     function pointer.

     On lines 23 and 41, you shouldn't be dereferencing callBack.  Just use
     callBack() to call the function and 'this.callBack = callBack' to
     set it.

Jul 20 2011