• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* Do simple things with a recursive mutex. */
3 
4 /* Needed for older glibcs (2.3 and older, at least) who don't
5    otherwise "know" about pthread_rwlock_anything or about
6    PTHREAD_MUTEX_RECURSIVE (amongst things). */
7 #define _GNU_SOURCE 1
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <assert.h>
12 #include <pthread.h>
13 
nearly_main(void)14 void nearly_main ( void )
15 {
16    pthread_mutex_t mx1;
17    pthread_mutexattr_t attr;
18    int r;
19 
20    r = pthread_mutexattr_init( &attr );
21    assert(r==0);
22    r = pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
23    assert(r==0);
24    r = pthread_mutex_init( &mx1, &attr );
25    assert(r==0);
26 
27    fprintf(stderr, "before lock #1\n");
28    r = pthread_mutex_lock( &mx1 ); assert(r == 0);
29    fprintf(stderr, "before lock #2\n");
30    r = pthread_mutex_lock( &mx1 ); assert(r == 0);
31    fprintf(stderr, "before lock #3\n");
32    r = pthread_mutex_lock( &mx1 ); assert(r == 0);
33 
34    fprintf(stderr, "before unlock #1\n");
35    r = pthread_mutex_unlock( &mx1 ); assert(r == 0);
36    fprintf(stderr, "before unlock #2\n");
37    r = pthread_mutex_unlock( &mx1 ); assert(r == 0);
38    fprintf(stderr, "before unlock #3\n");
39    r = pthread_mutex_unlock( &mx1 ); assert(r == 0);
40 
41    fprintf(stderr, "before unlock #4\n");
42    r = pthread_mutex_unlock( &mx1 ); /* FAILS: assert(r == 0); */
43 }
44 
main(void)45 int main ( void )
46 {
47    nearly_main();
48    return 0;
49 }
50