• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* Check that an error is reported for various kinds of bogus
3    pthread_mutex_unlock calls. */
4 
5 #include <pthread.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 
child_fn(void * arg)9 void* child_fn ( void* arg )
10 {
11    pthread_mutex_unlock( (pthread_mutex_t*)arg ); /* ERROR */
12    return NULL;
13 }
14 
nearly_main(void)15 void nearly_main ( void )
16 {
17    pthread_t child;
18    pthread_mutex_t mx1, mx2;
19    int bogus[100], i;
20    /* fill bogus with values which will cause glibc's pth_mx_unlock to fail */
21    for (i = 0; i < 100; i++) bogus[i] = 0xFFFFFFFF;
22    /* Unlocking a lock that is already unlocked */
23    pthread_mutex_init( &mx1, NULL );
24    pthread_mutex_lock( &mx1 );
25    pthread_mutex_unlock( &mx1 );
26 
27    pthread_mutex_unlock( &mx1 ); /* ERROR */
28 
29    /* Unlocking a lock that is held by a different thread */
30 
31    pthread_mutex_init( &mx2, NULL );
32    pthread_mutex_lock( &mx2 );
33    // start child and get it to unlock this lock
34 
35    pthread_create( &child, NULL, child_fn, (void*)&mx2 );
36       /* child runs and attempts to unlock our lock.  Error
37          is reported in child_fn. */
38    pthread_join(child, NULL );
39 
40    /* Unlocking a totally bogus lock. */
41    pthread_mutex_unlock( (pthread_mutex_t*) &bogus[50] ); /* ERROR */
42 
43    /* Now we get a freeing-locked-lock error, since the stack
44       frame is removed whilst mx2 is still locked. */
45 }
46 
main(void)47 int main ( void )
48 {
49    nearly_main(); fprintf(stderr, "---------------------\n" );
50    nearly_main();
51    return 0;
52 }
53