1 | /* |
---|
2 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * All rights assigned to U.S. Government, 1994. |
---|
5 | * |
---|
6 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
7 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
8 | * notice must appear in all copies of this file and its derivatives. |
---|
9 | * |
---|
10 | * $Id$ |
---|
11 | */ |
---|
12 | |
---|
13 | #define CONFIGURE_INIT |
---|
14 | #include "system.h" |
---|
15 | #include <errno.h> |
---|
16 | |
---|
17 | void Print_mutexattr( |
---|
18 | char *msg, |
---|
19 | pthread_mutexattr_t *attr |
---|
20 | ) |
---|
21 | { |
---|
22 | int status; |
---|
23 | int protocol; |
---|
24 | int prioceiling; |
---|
25 | int pshared; |
---|
26 | |
---|
27 | /* protocol */ |
---|
28 | |
---|
29 | status = pthread_mutexattr_getprotocol( attr, &protocol ); |
---|
30 | assert( !status ); |
---|
31 | |
---|
32 | printf( "%smutex protocol is (%d) -- ", msg, protocol ); |
---|
33 | switch ( protocol ) { |
---|
34 | case PTHREAD_PRIO_NONE: |
---|
35 | printf( "PTHREAD_PRIO_NONE\n" ); |
---|
36 | break; |
---|
37 | case PTHREAD_PRIO_INHERIT: |
---|
38 | printf( "PTHREAD_PRIO_INHERIT\n" ); |
---|
39 | break; |
---|
40 | case PTHREAD_PRIO_PROTECT: |
---|
41 | printf( "PTHREAD_PRIO_PROTECT\n" ); |
---|
42 | break; |
---|
43 | default: |
---|
44 | printf( "UNKNOWN\n" ); |
---|
45 | assert( 0 ); |
---|
46 | break; |
---|
47 | } |
---|
48 | |
---|
49 | /* priority ceiling */ |
---|
50 | |
---|
51 | status = pthread_mutexattr_getprioceiling( attr, &prioceiling ); |
---|
52 | assert( !status ); |
---|
53 | printf( "%smutex priority ceiling is %d\n", msg, prioceiling ); |
---|
54 | |
---|
55 | /* process shared */ |
---|
56 | |
---|
57 | status = pthread_mutexattr_getpshared( attr, &pshared ); |
---|
58 | assert( !status ); |
---|
59 | printf( "%smutex process shared is (%d) -- ", msg, pshared ); |
---|
60 | switch ( pshared ) { |
---|
61 | case PTHREAD_PROCESS_PRIVATE: |
---|
62 | printf( "PTHREAD_PROCESS_PRIVATE\n" ); |
---|
63 | break; |
---|
64 | case PTHREAD_PROCESS_SHARED: |
---|
65 | printf( "PTHREAD_PROCESS_SHARED\n" ); |
---|
66 | break; |
---|
67 | default: |
---|
68 | printf( "UNKNOWN\n" ); |
---|
69 | assert( 0 ); |
---|
70 | break; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | void *POSIX_Init( |
---|
75 | void *argument |
---|
76 | ) |
---|
77 | { |
---|
78 | int status; |
---|
79 | pthread_mutexattr_t attr; |
---|
80 | struct timespec times; |
---|
81 | |
---|
82 | puts( "\n\n*** POSIX TEST 5 ***" ); |
---|
83 | |
---|
84 | /* set the time of day, and print our buffer in multiple ways */ |
---|
85 | |
---|
86 | set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
87 | |
---|
88 | /* get id of this thread */ |
---|
89 | |
---|
90 | Init_id = pthread_self(); |
---|
91 | printf( "Init's ID is 0x%08x\n", Init_id ); |
---|
92 | |
---|
93 | /* basic checkout of mutex attributes */ |
---|
94 | |
---|
95 | printf( "Init: Initializing mutex attributes\n" ); |
---|
96 | status = pthread_mutexattr_init( &attr ); |
---|
97 | assert( !status ); |
---|
98 | |
---|
99 | Print_mutexattr( "Init: ", &attr ); |
---|
100 | |
---|
101 | printf( "Init: Changing mutex attributes\n" ); |
---|
102 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
103 | assert( !status ); |
---|
104 | |
---|
105 | status = pthread_mutexattr_setprioceiling( &attr, 128 ); |
---|
106 | assert( !status ); |
---|
107 | |
---|
108 | status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); |
---|
109 | assert( !status ); |
---|
110 | |
---|
111 | Print_mutexattr( "Init: ", &attr ); |
---|
112 | |
---|
113 | printf( "Init: Resetting mutex attributes\n" ); |
---|
114 | status = pthread_mutexattr_init( &attr ); |
---|
115 | assert( !status ); |
---|
116 | |
---|
117 | /* create a thread */ |
---|
118 | |
---|
119 | status = pthread_create( &Task_id, NULL, Task_1_through_3, NULL ); |
---|
120 | assert( !status ); |
---|
121 | |
---|
122 | /* now try some basic mutex operations */ |
---|
123 | |
---|
124 | empty_line(); |
---|
125 | |
---|
126 | printf( "Init: Creating a mutex\n" ); |
---|
127 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
128 | if ( status ) |
---|
129 | printf( "status = %d\n", status ); |
---|
130 | assert( !status ); |
---|
131 | |
---|
132 | printf( "Init: pthread_mutex_trylock successfully\n" ); |
---|
133 | status = pthread_mutex_trylock( &Mutex_id ); |
---|
134 | if ( status ) |
---|
135 | printf( "status = %d\n", status ); |
---|
136 | assert( !status ); |
---|
137 | |
---|
138 | printf( "Init: pthread_mutex_trylock already locked\n" ); |
---|
139 | status = pthread_mutex_trylock( &Mutex_id ); |
---|
140 | if ( status != EDEADLK ) |
---|
141 | printf( "status = %d\n", status ); |
---|
142 | assert( status == EDEADLK ); |
---|
143 | |
---|
144 | printf( "Init: pthread_mutex_lock already locked\n" ); |
---|
145 | status = pthread_mutex_lock( &Mutex_id ); |
---|
146 | if ( status != EDEADLK ) |
---|
147 | printf( "status = %d\n", status ); |
---|
148 | assert( status == EDEADLK ); |
---|
149 | |
---|
150 | printf( "Init: Sleep 1 second\n" ); |
---|
151 | |
---|
152 | sleep( 1 ); |
---|
153 | |
---|
154 | /* switch to task 1 */ |
---|
155 | |
---|
156 | printf( "Init: pthread_mutex_unlock successfully\n" ); |
---|
157 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
158 | if ( status ) |
---|
159 | printf( "status = %d\n", status ); |
---|
160 | assert( !status ); |
---|
161 | |
---|
162 | printf( "Init: pthread_mutex_unlock not owner\n" ); |
---|
163 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
164 | if ( status != EPERM ) |
---|
165 | printf( "status = %d\n", status ); |
---|
166 | assert( status == EPERM ); |
---|
167 | |
---|
168 | times.tv_sec = 0; |
---|
169 | times.tv_nsec = 500000000; |
---|
170 | printf( "Init: pthread_mutex_timedlock time out in 1/2 second\n" ); |
---|
171 | status = pthread_mutex_timedlock( &Mutex_id, × ); |
---|
172 | if ( status != EAGAIN ) |
---|
173 | printf( "status = %d\n", status ); |
---|
174 | assert( status == EAGAIN ); |
---|
175 | |
---|
176 | /* switch to idle */ |
---|
177 | |
---|
178 | printf( "Init: correctly timed out waiting for mutex\n" ); |
---|
179 | |
---|
180 | puts( "*** END OF POSIX TEST 5 ***" ); |
---|
181 | exit( 0 ); |
---|
182 | |
---|
183 | return NULL; /* just so the compiler thinks we returned something */ |
---|
184 | } |
---|