• Advertisement
Sign in to follow this  

timeSetEvent()/CreateTimerQueueTimer() functionality with timer_create()

This topic is 464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

To cut straight to the chase here, what I need is the same functionality of timeSetEvent() or CreateTimerQueueTimer() (Windows) with timer_create() (Linux/Mac).  What exactly do these windows functions do?  They allow functions or events to be triggered either once after a timer expires, or every N milliseconds call whatever function you need it to call.  If you need to, take a look at what MSDN says:

 

CreateTimerQueueTimer: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682485(v=vs.85).aspx

timeSetEvent: https://msdn.microsoft.com/en-us/library/windows/desktop/dd757634(v=vs.85).aspx

 

Personally, I like timeSetEvent better then the former, but Microsoft is deprecating it even though it appears to work just fine according to myself and others....

 

Now, I wanted to reference a bit of code (courtesy of Bacterius): https://github.com/TomCrypto/Ordo/blob/master/extra/samples/src/utils/timer.c#L124-L194

#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

static timer_t timer_id;
static struct sigaction timer_old;
static volatile sig_atomic_t timer_elapsed;

static void timer_handler(int unused)
{
    timer_elapsed = 1;
}

void timer_init(double seconds)
{
    struct sigevent evp = {0};
    struct sigaction sig;
    struct itimerspec tm;

    tm.it_interval.tv_nsec = (long)(seconds - (long)seconds) * 1000000000;
    tm.it_interval.tv_sec = (time_t)seconds;
    tm.it_value = tm.it_interval;
    timer_elapsed = 0;

    sig.sa_handler = timer_handler;
    sigemptyset(&sig.sa_mask);
    sig.sa_flags = 0;

    evp.sigev_value.sival_ptr = &timer_id;
    evp.sigev_notify = SIGEV_SIGNAL;
    evp.sigev_signo = SIGALRM;

    if (timer_create(CLOCK_MONOTONIC, &evp, &timer_id))
    {
        perror("timer_create");
        exit(EXIT_FAILURE);
    }

    if (timer_settime(timer_id, 0, &tm, 0))
    {
        perror("timer_settime");
        exit(EXIT_FAILURE);
    }

    if (sigaction(SIGALRM, &sig, &timer_old))
    {
        perror("sigaction");
        exit(EXIT_FAILURE);
    }
}

int timer_has_elapsed(void)
{
    return timer_elapsed;
}

double timer_now(void)
{
    struct timespec tv;
    clock_gettime(CLOCK_MONOTONIC, &tv);
    return tv.tv_sec + tv.tv_nsec / 1000000000.0;
}

void timer_free(void)
{
    sigaction(SIGALRM, &timer_old, 0);
    timer_delete(timer_id);
}

This is a great example on how to use it, but is there any way to cause the timer to repeatedly call this timer_handler function every N milliseconds?  Or does it only work once, and you have to create a new timer every time?  

 

For my purpose, this has to be called every 17 milliseconds (every theoretical vertical blank; 1000ms/60hz) because I'm considering replacing my old audio streaming method.  I used timeSetEvent in a previous game and never had issues with it.  Plus I have other future uses for it in my engine.

 

Any insights would be very helpful.  Thanks.

 

Shogun

Share this post


Link to post
Share on other sites
Advertisement

Did you try reading the man page for timer_create()?

I did, but I didn't see exactly what I was looking for (that or I didn't have a clear understanding of what I was reading).  

 

Also, I found what I was looking for a moment ago.  Turns out I was not using the right search criteria.

 

http://devlib.symbian.slions.net/s3/GUID-B4039418-6499-555D-AC24-9B49161299F2.html

 

That was tougher than I thought.  Several other threads on other forums that never got the same answer I was looking for.

 

Shogun.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement