c - timer_settime calling handler function in pthread on uClinux -
i've got following function gets called pthread_create. function work, sets timer, other work , waits timer expire before doing loop again. however, on first run of timer, after expires program quits , i'm not totally sure why. should never leave infinite while loop. main thread accesses nothing thread , vice versa (for now).
my guess might not have setup correctly thread, or timer not calling handler function correctly. perhaps changing idle global variable thread causes problem.
i call handler without signals, hence use of sigev_thread_id. i'm using sigusrx signals in main thread anyway. thoughts i've started here wrong?
#ifndef sigev_notify_thread_id #define sigev_notify_thread_id _sigev_un._tid #endif volatile sig_atomic_t idle = 0; timer_t timer_id; struct sigevent sev; void handler() { printf("timer expired.\n"); idle = 0; } void *thread_worker() { struct itimerspec ts; /* setup handler timer event */ memset (&sev, 0, sizeof(struct sigevent)); sev.sigev_notify = sigev_thread_id; sev.sigev_value.sival_ptr = null; sev.sigev_notify_function = handler; sev.sigev_notify_attributes = null; sev.sigev_signo = sigrtmin + 1; sev.sigev_notify_thread_id = syscall(sys_gettid); /* setup "idle" timer */ ts.it_value.tv_sec = 55; ts.it_value.tv_nsec = 0; ts.it_interval.tv_sec = 0; ts.it_interval.tv_nsec = 0; if (timer_create(0, &sev, &timer_id) == -1) { printf("timer_create failed: %d: %s\n", errno, strerror(errno)); exit(3); } while (1) { // work here before timer gets started takes 5 seconds while (idle); /* wait here until timer_id expires */ /* setup timer */ if (timer_settime(timer_id, 0, &ts, null) == -1) { printf("timer_settime failed: %d\n", errno); exit(3); } idle = 1; // work here while timer running not take 10 seconds } }
as far can tell, haven't installed signal handler sigusr1
, default action kills process when it's acted upon.
in case, whole thing strikes me extraordinarily bad design:
the while loop give 100% cpu load while waiting timer expire.
this not way use
sigev_thread_id
, , in factsigev_thread_id
isn't setup usable applications. rather it's libc use internally implementingsigev_thread
.you don't want using signals. they're messy.
if have threads, why aren't calling clock_nanosleep
in loop? timers useful when can't this, e.g. when can't use threads.
Comments
Post a Comment