Passing user data using the timer_create function

I use timer_createto create a timer in Linux. Callback prototype:

static void TimerHandlerCB(int sig, siginfo_t *extra, void *cruft)

How can I pass user data so that I can get the same in the callback called after the timer expires.

Here is my sample code:

int RegisterTimer(iDiffInTime)
{
    struct sigaction sa; 
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_SIGINFO;   /*call our handler*/
    sa.sa_sigaction = TimerHandlerCB;/*Event handler to be called after timer expires*/ 
    if(sigaction(SIGRTMAX, &sa, NULL) < 0)
    {
        perror("sigaction");
        return 1;
    }
    // Setup the timer
    sigevent sigev;
    timer_t timerid = 0;

    memset(&sigev, 0, sizeof(sigev));

    sigev.sigev_notify          = SIGEV_SIGNAL;
    sigev.sigev_signo           = SIGRTMAX;
    sigev.sigev_value.sival_ptr = &timerid;

    timer_t timerid;

    if (timer_create(CLOCK_REALTIME, &sigev, &timerid) == 0)
    {
        printf("Timer created\n");
        struct itimerspec itval, oitval;

        itval.it_value.tv_sec = iDiffInTime * 1000;
        itval.it_value.tv_nsec = 0;
        itval.it_interval.tv_sec = 0;
        itval.it_interval.tv_nsec = 0;

              //Populate handles required in TimerCallback
              Display_Handle hDisplay = ......//
              Buffer_Handle hBuf = .....//
        if (timer_settime(timerid, 0, &itval, &oitval) != 0)
        {
            perror("time_settime error!");
            return 1;
        }
    } 
    else 
    {
        perror("timer_create error!");
    }
    return 0
}

Where can I go hDisplayand hBufget them back to TimerHandlerCB?

+3
source share
2 answers

You are already doing this:

sigev.sigev_value.sival_ptr = &timerid;

You can specify something there (an indication &timeridis common to distinguish between multiple timers, but not required). Now in your handler:

static void timer_handler(int signo, siginfo_t *si, void *uc)
{
    /* si->si_value.sival_ptr */
}

Quote from TLPI

timer_create() evp.sigev_value.sival_ptr , . , evp.sigev_value.sival_ptr , timer_create()

+3
+1

All Articles