Re: EVFILT_TIMER just broken
Re: EVFILT_TIMER just broken
- Subject: Re: EVFILT_TIMER just broken
- From: "A.M." <email@hidden>
- Date: Fri, 12 Mar 2010 09:56:38 -0500
On Mar 11, 2010, at 6:15 AM, Godfrey van der Linden wrote:
> I've now explored every variant I can think of for using kevent with EVFILT_TIMER. If I read kevent(2) properly the default implementation for the TIMER is to set a repeating timeout that can fire every ident timeperiods and delivers a EVFILT_TIMER event.
>
> Yet only ever get one hit, immediately and the event never fires again. Is this a documentation problem or am I missing something?
>
> Here is the code that demonstrates the problem, not that it never returns after the initial immediate and incorrect kevents.
>
> I'm running a vanilla x86_64 macbook pro with 10.6.2.
>
> Please help, this problem has been screwing me over for the last week.
>
> Godfrey
>
> /*
> gcc -std=c99 -Wall -Wextra -Werror -g -o kevent_timer kevent_timer.c
> */
>
> #include <sys/cdefs.h>
>
> #include <assert.h>
> #include <errno.h>
> #include <stdio.h>
> #include <stdbool.h>
> #include <strings.h>
> #include <sys/time.h>
>
> #include <sys/event.h>
>
> #define kLongTimer_ms (1000)
> #define kShortTimer_ms ( 10)
>
> static struct timeval sStart;
> static int sKQ;
>
> static void
> logKEvent(const struct kevent64_s *kep, int ind, const char * const pfx);
>
> int main(int argc __unused, char *argv[] __unused)
> {
> sKQ = kqueue();
>
> gettimeofday(&sStart, NULL);
>
> uintptr_t nch = 0;
> struct kevent64_s timers[2];
>
> EV_SET64(&timers[nch], kLongTimer_ms,
> EVFILT_TIMER, EV_ADD, 0, 0, nch, 0, 0); nch++;
> EV_SET64(&timers[nch], kShortTimer_ms,
> EVFILT_TIMER, EV_ADD, 0, 0, nch, 0, 0); nch++;
>
Your code is broken here. The "ident" field of the struct is supposed to contain the timer number, not the time interval- that's put in the "data" field with fflags holding the time units. This is all in the man page. Here is some sample code that works:
#include <sys/cdefs.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include <strings.h>
#include <sys/time.h>
#include <sys/event.h>
#include <stdlib.h>
int main()
{
int akq=kqueue();
struct kevent64_s aTimerEvent;
EV_SET64(&aTimerEvent,1,EVFILT_TIMER,EV_ADD|EV_ENABLE,NOTE_SECONDS,1,0,0,0);
//add the event
int aSuccess=kevent64(akq,&aTimerEvent,1,NULL,0,0,NULL);
if(aSuccess<0)
abort();
while(1)
{
struct kevent64_s anEvent;
int anEventCount=kevent64(akq,NULL,0,&anEvent,1,0,NULL);
if(anEventCount<0)
abort();
if(anEventCount==1)
{
printf("timer!\n");
}
}
}
Cheers,
M _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden