/* then the wrappers themselves */ int timer_create( clockid_t clockid, struct sigevent* evp, timer_t *ptimerid) { /* if not a SIGEV_THREAD timer, direct creation by the kernel */ if (__likely(evp == NULL || evp->sigev_notify != SIGEV_THREAD)) return __timer_create( clockid, evp, ptimerid ); // check arguments if (evp->sigev_notify_function == NULL) { errno = EINVAL; return -1; } { struct timespec dummy; /* check that the clock id is supported by the kernel */ if (clock_gettime( clockid, &dummy ) < 0 && errno == EINVAL ) return -1; } /* create a new timer and its thread */ { thr_timer_table_t* table = __timer_table_get(); thr_timer_t* timer = thr_timer_table_alloc( table ); struct sigevent evp0; if (timer == NULL) { errno = ENOMEM; return -1; } /* copy the thread attributes */ if (evp->sigev_notify_attributes == NULL) { pthread_attr_init(&timer->attributes); } else { timer->attributes = ((pthread_attr_t*)evp->sigev_notify_attributes)[0]; } /* Posix says that the default is PTHREAD_CREATE_DETACHED and * that PTHREAD_CREATE_JOINABLE has undefined behaviour. * So simply always use DETACHED :-) */ pthread_attr_setdetachstate(&timer->attributes, PTHREAD_CREATE_DETACHED); timer->callback = evp->sigev_notify_function; timer->value = evp->sigev_value; timer->clock = clockid; pthread_mutex_init( &timer->mutex, NULL ); pthread_cond_init( &timer->cond, NULL ); timer->done = 0; timer->stopped = 0; timer->expires.tv_sec = timer->expires.tv_nsec = 0; timer->period.tv_sec = timer->period.tv_nsec = 0; timer->overruns = 0; /* create the thread */ if (pthread_create( &timer->thread, &timer->attributes, timer_thread_start, timer ) < 0) { thr_timer_table_free( __timer_table, timer ); errno = ENOMEM; return -1; } *ptimerid = timer->id; return 0; } }
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html int timer_create(clockid_t clock_id, sigevent* evp, timer_t* timer_id) { PosixTimer* timer = reinterpret_cast<PosixTimer*>(malloc(sizeof(PosixTimer))); if (timer == NULL) { return -1; } timer->sigev_notify = (evp == NULL) ? SIGEV_SIGNAL : evp->sigev_notify; // If not a SIGEV_THREAD timer, the kernel can handle it without our help. if (timer->sigev_notify != SIGEV_THREAD) { if (__timer_create(clock_id, evp, &timer->kernel_timer_id) == -1) { free(timer); return -1; } *timer_id = timer; return 0; } // Otherwise, this must be SIGEV_THREAD timer... timer->callback = evp->sigev_notify_function; timer->callback_argument = evp->sigev_value; // Check arguments that the kernel doesn't care about but we do. if (timer->callback == NULL) { free(timer); errno = EINVAL; return -1; } // Create this timer's thread. pthread_attr_t thread_attributes; if (evp->sigev_notify_attributes == NULL) { pthread_attr_init(&thread_attributes); } else { thread_attributes = *reinterpret_cast<pthread_attr_t*>(evp->sigev_notify_attributes); } pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED); // We start the thread with TIMER_SIGNAL blocked by blocking the signal here and letting it // inherit. If it tried to block the signal itself, there would be a race. kernel_sigset_t sigset; sigaddset(sigset.get(), TIMER_SIGNAL); kernel_sigset_t old_sigset; pthread_sigmask(SIG_BLOCK, sigset.get(), old_sigset.get()); int rc = pthread_create(&timer->callback_thread, &thread_attributes, __timer_thread_start, timer); pthread_sigmask(SIG_SETMASK, old_sigset.get(), NULL); if (rc != 0) { free(timer); errno = rc; return -1; } sigevent se = *evp; se.sigev_signo = TIMER_SIGNAL; se.sigev_notify = SIGEV_THREAD_ID; se.sigev_notify_thread_id = __pthread_gettid(timer->callback_thread); if (__timer_create(clock_id, &se, &timer->kernel_timer_id) == -1) { __timer_thread_stop(timer); return -1; } // Give the thread a meaningful name. // It can't do this itself because the kernel timer isn't created until after it's running. char name[32]; snprintf(name, sizeof(name), "POSIX interval timer %d", to_kernel_timer_id(timer)); pthread_setname_np(timer->callback_thread, name); *timer_id = timer; return 0; }
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html int timer_create(clockid_t clock_id, sigevent* evp, timer_t* timer_id) { PosixTimer* timer = reinterpret_cast<PosixTimer*>(malloc(sizeof(PosixTimer))); if (timer == nullptr) { return -1; } timer->sigev_notify = (evp == nullptr) ? SIGEV_SIGNAL : evp->sigev_notify; // If not a SIGEV_THREAD timer, the kernel can handle it without our help. if (timer->sigev_notify != SIGEV_THREAD) { if (__timer_create(clock_id, evp, &timer->kernel_timer_id) == -1) { free(timer); return -1; } *timer_id = timer; return 0; } // Otherwise, this must be SIGEV_THREAD timer... timer->callback = evp->sigev_notify_function; timer->callback_argument = evp->sigev_value; atomic_init(&timer->deleted, false); // Check arguments that the kernel doesn't care about but we do. if (timer->callback == nullptr) { free(timer); errno = EINVAL; return -1; } // Create this timer's thread. pthread_attr_t thread_attributes; if (evp->sigev_notify_attributes == nullptr) { pthread_attr_init(&thread_attributes); } else { thread_attributes = *reinterpret_cast<pthread_attr_t*>(evp->sigev_notify_attributes); } pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED); // We start the thread with TIMER_SIGNAL blocked by blocking the signal here and letting it // inherit. If it tried to block the signal itself, there would be a race. sigset64_t sigset = {}; sigaddset64(&sigset, TIMER_SIGNAL); sigset64_t old_sigset; // Use __rt_sigprocmask instead of sigprocmask64 to avoid filtering out TIMER_SIGNAL. __rt_sigprocmask(SIG_BLOCK, &sigset, &old_sigset, sizeof(sigset)); int rc = pthread_create(&timer->callback_thread, &thread_attributes, __timer_thread_start, timer); __rt_sigprocmask(SIG_SETMASK, &old_sigset, nullptr, sizeof(old_sigset)); if (rc != 0) { free(timer); errno = rc; return -1; } sigevent se = *evp; se.sigev_signo = TIMER_SIGNAL; se.sigev_notify = SIGEV_THREAD_ID; se.sigev_notify_thread_id = pthread_gettid_np(timer->callback_thread); if (__timer_create(clock_id, &se, &timer->kernel_timer_id) == -1) { __timer_thread_stop(timer); return -1; } // Give the thread a specific meaningful name. // It can't do this itself because the kernel timer isn't created until after it's running. char name[16]; // 16 is the kernel-imposed limit. snprintf(name, sizeof(name), "POSIX timer %d", to_kernel_timer_id(timer)); pthread_setname_np(timer->callback_thread, name); *timer_id = timer; return 0; }