-> 3.17.0.RC2
[valgrind.git] / helgrind / tests / safe-semaphore.h
blob3aa0dc4e4ceeee3d557b0660ad1408ae3e543ef0
1 #include <semaphore.h>
2 #include <signal.h>
3 #include <setjmp.h>
4 #include <errno.h>
5 #include <assert.h>
7 static sigjmp_buf env;
9 /*
10 * Newer glibc crashes on really bogus semaphors.
11 * Catch a SIGABRT and turn it into a EINVAL.
13 static void abrt_handler( int signum, siginfo_t *siginfo, void *sigcontext ) {
14 siglongjmp( env, EINVAL );
17 static int safe_sem_post( sem_t *sem ) {
18 struct sigaction sa;
19 struct sigaction oldsa;
20 int r, e;
22 sa.sa_handler = NULL;
23 sa.sa_sigaction = abrt_handler;
24 sigemptyset( &sa.sa_mask );
25 sa.sa_flags = SA_SIGINFO;
27 sigaction( SIGABRT, &sa, &oldsa );
29 if ( ( e = sigsetjmp( env, 1 ) ) == 0 ) {
30 r = sem_post( sem );
31 } else {
32 r = -1;
34 errno = e;
36 sigaction( SIGABRT, &oldsa, NULL );
38 return r;
41 #define sem_post safe_sem_post