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 | |
---|
81 | puts( "\n\n*** POSIX TEST 5 ***" ); |
---|
82 | |
---|
83 | /* set the time of day, and print our buffer in multiple ways */ |
---|
84 | |
---|
85 | set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
86 | |
---|
87 | /* get id of this thread */ |
---|
88 | |
---|
89 | Init_id = pthread_self(); |
---|
90 | printf( "Init's ID is 0x%08x\n", Init_id ); |
---|
91 | |
---|
92 | /* basic checkout of mutex attributes */ |
---|
93 | |
---|
94 | printf( "Init: Initializing mutex attributes\n" ); |
---|
95 | status = pthread_mutexattr_init( &attr ); |
---|
96 | assert( !status ); |
---|
97 | |
---|
98 | Print_mutexattr( "Init: ", &attr ); |
---|
99 | |
---|
100 | printf( "Init: Changing mutex attributes\n" ); |
---|
101 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
102 | assert( !status ); |
---|
103 | |
---|
104 | status = pthread_mutexattr_setprioceiling( &attr, 128 ); |
---|
105 | assert( !status ); |
---|
106 | |
---|
107 | status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); |
---|
108 | assert( !status ); |
---|
109 | |
---|
110 | Print_mutexattr( "Init: ", &attr ); |
---|
111 | |
---|
112 | printf( "Init: Resetting mutex attributes\n" ); |
---|
113 | status = pthread_mutexattr_init( &attr ); |
---|
114 | assert( !status ); |
---|
115 | |
---|
116 | /* create a thread */ |
---|
117 | |
---|
118 | status = pthread_create( &Task_id, NULL, Task_1_through_3, NULL ); |
---|
119 | assert( !status ); |
---|
120 | |
---|
121 | /* now try some basic mutex operations */ |
---|
122 | |
---|
123 | empty_line(); |
---|
124 | |
---|
125 | printf( "Init: Creating a mutex\n" ); |
---|
126 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
127 | if ( status ) |
---|
128 | printf( "status = %d\n", status ); |
---|
129 | assert( !status ); |
---|
130 | |
---|
131 | printf( "Init: pthread_mutex_trylock successfully\n" ); |
---|
132 | status = pthread_mutex_trylock( &Mutex_id ); |
---|
133 | if ( status ) |
---|
134 | printf( "status = %d\n", status ); |
---|
135 | assert( !status ); |
---|
136 | |
---|
137 | printf( "Init: pthread_mutex_trylock already locked\n" ); |
---|
138 | status = pthread_mutex_trylock( &Mutex_id ); |
---|
139 | if ( status != EDEADLK ) |
---|
140 | printf( "status = %d\n", status ); |
---|
141 | assert( status == EDEADLK ); |
---|
142 | |
---|
143 | printf( "Init: pthread_mutex_lock already locked\n" ); |
---|
144 | status = pthread_mutex_lock( &Mutex_id ); |
---|
145 | if ( status != EDEADLK ) |
---|
146 | printf( "status = %d\n", status ); |
---|
147 | assert( status == EDEADLK ); |
---|
148 | |
---|
149 | printf( "Init: Sleep 1 second\n" ); |
---|
150 | |
---|
151 | sleep( 1 ); |
---|
152 | |
---|
153 | /* switch to task 1 */ |
---|
154 | |
---|
155 | printf( "Init: pthread_mutex_unlock successfully\n" ); |
---|
156 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
157 | if ( status ) |
---|
158 | printf( "status = %d\n", status ); |
---|
159 | assert( !status ); |
---|
160 | |
---|
161 | printf( "Init: pthread_mutex_unlock not owner\n" ); |
---|
162 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
163 | if ( status != EPERM ) |
---|
164 | printf( "status = %d\n", status ); |
---|
165 | assert( status == EPERM ); |
---|
166 | |
---|
167 | puts( "*** END OF POSIX TEST 5 ***" ); |
---|
168 | exit( 0 ); |
---|
169 | |
---|
170 | return NULL; /* just so the compiler thinks we returned something */ |
---|
171 | } |
---|