Add 469782 to NEWS
[valgrind.git] / helgrind / tests / tc15_laog_lockdel.c
blob5b2607d39b617566f212501caed551e18d12a637
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <assert.h>
7 /* Test that locks, having entered the lock acquisition tracking
8 machinery, are forgotten by it when the client does
9 pthread_{mutex,rwlock}_destroy. 2008-Nov-10: see comments below. */
11 int main ( void )
13 int r;
14 pthread_mutex_t *mx1, *mx2;
15 assert (sizeof(pthread_mutex_t) <= 120);
16 mx1 = malloc(120 + sizeof(pthread_mutex_t) - sizeof(pthread_mutex_t));
17 mx2 = malloc(120 + sizeof(pthread_mutex_t) - sizeof(pthread_mutex_t));
19 assert(mx1);
20 assert(mx2);
22 r = pthread_mutex_init( mx1, NULL ); assert(r==0);
23 r = pthread_mutex_init( mx2, NULL ); assert(r==0);
25 /* Establish order 1 -> 2 */
26 fprintf(stderr, "Establish order 1 -> 2\n");
27 r = pthread_mutex_lock( mx1 ); assert(r==0);
28 r = pthread_mutex_lock( mx2 ); assert(r==0);
30 r = pthread_mutex_unlock( mx1 ); assert(r==0);
31 r = pthread_mutex_unlock( mx2 ); assert(r==0);
33 /* Try order 2 -> 1. This gives an error. */
34 fprintf(stderr, "Try order 2 -> 1. This gives an error.\n");
35 r = pthread_mutex_lock( mx2 ); assert(r==0); /* error */
36 r = pthread_mutex_lock( mx1 ); assert(r==0);
38 r = pthread_mutex_unlock( mx1 ); assert(r==0);
39 r = pthread_mutex_unlock( mx2 ); assert(r==0);
41 /* De-initialise 2 and re-initialise it. This gives it a new
42 identity, so a second locking sequence 2 -> 1 should now be OK. */
43 fprintf(stderr,
44 "Free 2 and re-allocate it. This gives it a new identity,\n");
45 fprintf(stderr, "so a second locking sequence 2 -> 1 should now be OK.\n");
46 pthread_mutex_destroy( mx2 );
50 r = pthread_mutex_init( mx2, NULL ); assert(r==0);
52 r = pthread_mutex_lock( mx2 ); assert(r==0);
53 r = pthread_mutex_lock( mx1 ); assert(r==0); /* no error */
55 r = pthread_mutex_unlock( mx1 ); assert(r==0);
56 r = pthread_mutex_unlock( mx2 ); assert(r==0);
58 /* done */
60 fprintf(stderr, "done\n");
61 r = pthread_mutex_destroy( mx1 );
62 r = pthread_mutex_destroy( mx2 );
64 free( mx1 );
65 free( mx2 );
67 return 0;
70 /* 2008-Nov-10: I believe this test is flawed and requires further
71 investigation. I don't think it really tests what it claims to
72 test. In particular, it still gives the right results if
73 "pthread_mutex_destroy( mx2 );" at line 46 is commented out. In
74 other words, laog somehow forgets about mx2 so that 2->1 lock
75 sequence at lines 52/3 does not produce a complaint, EVEN WHEN the
76 preceding "pthread_mutex_destroy( mx2 );" is not observed. I don't
77 know why this is, but it seems highly suspicious to me. */