1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28 #define __USE_UNIX98 1 /* necessary to define pthread_mutexattr_set/gettype in Linux GLIBC headers. doh ! */
29 #include <pthread.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <stdlib.h>
34
panic(const char * format,...)35 static void panic( const char* format, ... )
36 {
37 va_list args;
38 va_start(args, format);
39 vfprintf(stderr, format, args);
40 va_end(args);
41 exit(1);
42 }
43
44 #define assert(cond) do { if ( !(cond) ) panic( "%s:%d: assertion failure: %s\n", __FILE__, __LINE__, #cond ); } while (0)
45
46 #define expect(call,result) \
47 do { \
48 int ret = (call); \
49 if (ret != (result)) { \
50 panic( "%s:%d: call returned %d instead of %d: %s\n", \
51 __FILE__, __LINE__, ret, (result), #call ); \
52 } \
53 } while (0)
54
55
main(void)56 int main( void )
57 {
58 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
59 pthread_mutexattr_t attr;
60 int attr_type;
61
62 expect( pthread_mutexattr_init( &attr ), 0 );
63
64 expect( pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_NORMAL ), 0 );
65 expect( pthread_mutexattr_gettype( &attr, &attr_type ), 0 );
66 assert( attr_type == PTHREAD_MUTEX_NORMAL );
67
68 expect( pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK ), 0 );
69 expect( pthread_mutexattr_gettype( &attr, &attr_type ), 0 );
70 assert( attr_type == PTHREAD_MUTEX_ERRORCHECK );
71
72 expect( pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE ), 0 );
73 expect( pthread_mutexattr_gettype( &attr, &attr_type ), 0 );
74 assert( attr_type == PTHREAD_MUTEX_RECURSIVE );
75
76 /* standard mutexes */
77 expect( pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_NORMAL ), 0 );
78 expect( pthread_mutex_init( &lock, &attr ), 0 );
79 expect( pthread_mutex_lock( &lock ), 0 );
80 expect( pthread_mutex_unlock( &lock ), 0 );
81 expect( pthread_mutex_destroy( &lock ), 0 );
82
83 /* error-check mutex */
84 expect( pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK ), 0 );
85 expect( pthread_mutex_init( &lock, &attr ), 0 );
86 expect( pthread_mutex_lock( &lock ), 0 );
87 expect( pthread_mutex_lock( &lock ), EDEADLK );
88 expect( pthread_mutex_unlock( &lock ), 0 );
89 expect( pthread_mutex_trylock( &lock ), 0 );
90 expect( pthread_mutex_trylock( &lock ), EDEADLK );
91 expect( pthread_mutex_unlock( &lock ), 0 );
92 expect( pthread_mutex_unlock( &lock ), EPERM );
93 expect( pthread_mutex_destroy( &lock ), 0 );
94
95 /* recursive mutex */
96 expect( pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE ), 0 );
97 expect( pthread_mutex_init( &lock, &attr ), 0 );
98 expect( pthread_mutex_lock( &lock ), 0 );
99 expect( pthread_mutex_lock( &lock ), 0 );
100 expect( pthread_mutex_unlock( &lock ), 0 );
101 expect( pthread_mutex_unlock( &lock ), 0 );
102 expect( pthread_mutex_trylock( &lock ), 0 );
103 expect( pthread_mutex_unlock( &lock ), 0 );
104 expect( pthread_mutex_unlock( &lock ), EPERM );
105 expect( pthread_mutex_destroy( &lock ), 0 );
106
107 printf( "ok\n" );
108 return 0;
109 }
110