#define RES_SIGNAL SIGUSR2 int gs_signal_createtimer(void) { struct sigevent sev; /* * Set up per thread timer. * Upon timeout, a SIGUSR2 will be dilivered to current thread itself. * The signal handler can check sival_ptr for the reason. We don't want * to use SIGEV_THREAD callback method, because it needs certain environment * set up to work correctly. Meanwhile, it creates a new thread for the * callback, which is inefficiently. */ sev.sigev_notify = SIGEV_SIGNAL | SIGEV_THREAD_ID; sev.sigev_signo = RES_SIGNAL; sev.sigev_value.sival_ptr = (void*)(SIGALRM + gs_thread_self()); sev._sigev_un._tid = syscall(SYS_gettid); if (timer_create(CLOCK_REALTIME, &sev, &t_thrd.utils_cxt.sigTimerId) == -1) { ereport(FATAL, (errmsg("failed to create timer for thread"))); return -1; } return 0; }