1/* $OpenBSD: sigdeliver.c,v 1.1 2002/10/12 03:39:21 marc Exp $ */
2/* PUBLIC DOMAIN Oct 2002 <marc@snafu.org> */
3
4/*
5 * test signal delivery of pending signals
6 */
7
8#include <signal.h>
9#include <stdio.h>
10#include <unistd.h>
11
12#include "test.h"
13
14static pthread_mutex_t	sync_mutex;
15
16volatile sig_atomic_t	got_signal;
17
18/*
19 * sigusr1 signal handler.
20 */
21static void
22sighandler(int signo)
23{
24	got_signal += 1;
25}
26
27/*
28 * Install a signal handler for sigusr1 and then wait for it to
29 * occur.
30 */
31static void *
32do_nothing (void *arg)
33{
34	SET_NAME("nothing");
35
36	ASSERT(signal(SIGUSR1, sighandler) != SIG_ERR);
37	CHECKr(pthread_mutex_lock(&sync_mutex));
38	ASSERT(got_signal != 0);
39	CHECKr(pthread_mutex_unlock(&sync_mutex));
40	return 0;
41}
42
43int
44main (int argc, char *argv[])
45{
46	pthread_t pthread;
47
48	/* Initialize and lock a mutex. */
49	CHECKr(pthread_mutex_init(&sync_mutex, NULL));
50	CHECKr(pthread_mutex_lock(&sync_mutex));
51
52	/* start a thread that will wait on the mutex we now own */
53	CHECKr(pthread_create(&pthread, NULL, do_nothing, NULL));
54
55	/*
56	 * Give the thread time to run and install its signal handler.
57	 * The thread should be blocked waiting for the mutex we own.
58	 * Give it a signal and then release the mutex and see if the
59	 * signal is ever processed.
60	 */
61	sleep(2);
62	CHECKr(pthread_kill(pthread, SIGUSR1));
63	CHECKr(pthread_mutex_unlock(&sync_mutex));
64	CHECKr(pthread_join(pthread, NULL));
65	SUCCEED;
66}
67