23
24 status = pthread_mutex_lock (&mutex);
25 if (status != 0)
26 err_abort (status, 'Lock mutex');
27 if (++counter >= 5) {
28 status = pthread_cond_signal (&cond);
29 if (status != 0)
30 err_abort (status, 'Signal condition');
31 }
32 status = pthread_mutex_unlock (&mutex);
33 if (status != 0)
34 err_abort (status, 'Unlock mutex');
35
36 printf ('Timer %d
', counter);
37 }
38
39 main( )
40 {
41 int status;
42 struct itimerspec ts;
43 struct sigevent se;
44
45 #ifdef sun
46 fprintf (
47 stderr,
48 'This program cannot compile on Solaris 2.5.
'
49 'To build and run on Solaris 2.6, remove the
'
50 ''#ifdef sun' block in main().
');
51 #else
52 /*
53 * Set the sigevent structure to cause the signal to be
54 * delivered by creating a new thread.
55 */
56 se.sigev_notify = SIGEV_THREAD;
57 se.sigev_value.sival_ptr = &timer_id;
58 se.sigev_notify_function = timer_thread;
59 se.sigev_notify_attributes = NULL;
60
61 /*
62 * Specify a repeating timer that fires every 5 seconds.
63 */
64 ts.it_value.tv_sec = 5;
65 ts.it_value.tv_nsec = 0;
66 ts.it_interval.tv_sec = 5;
67 ts.it_interval.tv_nsec = 0;
68
69 DPRINTF (('Creating timer
'));
70 status = timer_create(CLOCK_REALTIME, &se, &timer_id);
71 if (status == -1)
72 errno_abort ('Create timer');
73
74 DPRINTF ((
75 'Setting timer %d for 5-second expiration...
', timer_id));
76 status = timer_settime(timer_id, 0, &ts, 0);
77 if (status == -1)
78 errno_abort ('Set timer');
79
80 status = pthread_mutex_lock (&mutex);
81 if (status != 0)
82 err_abort (status, 'Lock mutex');
83 while (counter < 5) {
84 status = pthread_cond_wait (&cond, &mutex);
85 if (status != 0)
86 err_abort (status, 'Wait on condition');
87 }
88 status = pthread_mutex_unlock (&mutex);
89 if (status != 0)
90 err_abort (status, 'Unlock mutex');
91
92 #endif /* Sun */
93 return 0;
94 }
6.6.6 Semaphores: synchronizing with a signal-catching function
#ifdef _POSIX_SEMAPHORES
int sem_init (sem_t *sem, int pshared, unsigned int value);
int sem_destroy (sem_t *sem);
int sem_wait (sem_t*sem);
int sem_trywake (sem_t *sem);
int sem_post (sem_t *sem);
int sem_getvalue (sem_t *sem, int *sval);
#endif
Although mutexes and condition variables provide an ideal solution to most synchronization needs, they cannot meet all needs. One example of this is a need to communicate between a POSIX signal-catching function and a thread waiting for some asynchronous event. In new code, it is best to use sigwait or sigwaitinfo rather than relying on a signal-catching function, and this neatly avoids this problem. However, the use of asynchronous POSIX signal- catching functions is well established and widespread, and most programmers working with threads and existing code will probably encounter this situation.
To awaken a thread from a POSIX signal-catching function, you need a mechanism that's reentrant with respect to POSIX signals (async-signal safe). POSIX provides relatively few of these functions, and none of the Pthreads functions is included. That's primarily because an async-signal safe mutex lock operation would be many times slower than one that isn't async-signal safe. Outside of the kernel, making a function async-signal safe usually requires that the function mask (block) signals while it runs — and that is expensive.
In case you're curious, here is the full list of POSIX 1003.1-1996 functions that are async-signal safe (some of these functions exist only when certain POSIX options are defined, such as _POSIX_ASYNCHRONOUS_IO or _POSIX_TIMERS):