1 /* $OpenBSD: pthread_mutex_lock.c,v 1.1 2011/10/01 10:26:59 fgsch Exp $ */ 2 /* 3 * Federico G. Schwindt <fgsch@openbsd.org>, 2011. Public Domain. 4 */ 5 #include <pthread.h> 6 #include <signal.h> 7 #include <unistd.h> 8 #include "test.h" 9 10 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; 11 volatile sig_atomic_t hits = 0; 12 13 void 14 handler(int sig) 15 { 16 hits++; 17 } 18 19 void * 20 thr_lock(void *arg) 21 { 22 CHECKr(pthread_mutex_lock(&lock)); 23 return (NULL); 24 } 25 26 int 27 main(int argc, char **argv) 28 { 29 struct sigaction sa; 30 pthread_t tid; 31 32 bzero(&sa, sizeof(sa)); 33 sa.sa_handler = handler; 34 CHECKe(sigaction(SIGUSR1, &sa, NULL)); 35 36 CHECKr(pthread_mutex_lock(&lock)); 37 CHECKr(pthread_create(&tid, NULL, thr_lock, NULL)); 38 sleep(2); 39 40 CHECKr(pthread_kill(tid, SIGUSR1)); 41 sleep(1); 42 43 CHECKr(pthread_mutex_unlock(&lock)); 44 ASSERT(hits == 1); 45 SUCCEED; 46 } 47