2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * This file is licensed under the GPL license. For the full content
4 * of this license, see the COPYING file at the top level of this
6 * Test pthread_rwlock_wrlock(pthread_rwlock_t * rwlock)
8 * If a signal is delivered to a thread waiting for a read-write lock for writing, upon
9 * return from the signal handler the thread resumes waiting for the read-write lock for
10 * writing as if it was not interrupted.
13 * 1. main thread create read-write lock 'rwlock', and lock it for writing
14 * 2. main thread create a thread sig_thread, the thread is set to handle SIGUSR1
15 * 3. sig_thread try to lock 'rwlock' for writing but blocked
16 * 4. main thread send SIGUSR1 to sig_thread via pthread_kill, while sig_thread is blocking
17 * 5. test that thread handler is called
18 * 6. check that when thread handler returns, sig_thread resume block
19 * 7. main thread unlock 'rwlock', sig_thread should get the lock
22 #define _XOPEN_SOURCE 600
29 #include "posixtest.h"
31 static pthread_t sig_thread;
32 static pthread_rwlock_t rwlock;
34 /* thread_state indicates child thread state:
35 1: not in child thread yet;
36 2: just enter child thread ;
37 3: just before child thread exit;
40 #define NOT_CREATED_THREAD 1
41 #define ENTERED_THREAD 2
42 #define EXITING_THREAD 3
44 static int thread_state;
45 static int handler_called;
47 static void sig_handler() {
48 if(pthread_equal(pthread_self(), sig_thread))
50 printf("sig_handler: handled signal SIGUSR1\n");
55 printf("signal was not handled by sig_thread\n");
60 static void * th_fn(void *arg)
65 /* Set up signal handler for SIGUSR1 */
67 act.sa_handler = sig_handler;
68 /* block all the signal while handling SIGUSR1 */
69 sigfillset(&act.sa_mask);
70 sigaction(SIGUSR1, &act, NULL);
72 thread_state = ENTERED_THREAD;
73 printf("sig_thread: attempt write lock\n");
74 rc = pthread_rwlock_wrlock(&rwlock);
77 printf("Test FAILED: sig_thread: Error at pthread_rwlock_wrlock(), error code:%d\n", rc);
81 printf("sig_thread: acquired write lock\n");
82 printf("sig_thread: unlock write lock\n");
83 if(pthread_rwlock_unlock(&rwlock) != 0)
85 printf("sig_thread: Error at pthread_rwlock_unlock()\n");
88 thread_state = EXITING_THREAD;
99 if(pthread_rwlock_init(&rwlock, NULL) != 0)
101 printf("Error at pthread_rwlock_init()\n");
102 return PTS_UNRESOLVED;
105 printf("main: attempt write lock\n");
106 rc = pthread_rwlock_wrlock(&rwlock);
109 printf("main: Error at pthread_rwlock_wrlock(), error code:%d\n", rc);
110 return PTS_UNRESOLVED;
113 thread_state = NOT_CREATED_THREAD;
114 if(pthread_create(&sig_thread, NULL, th_fn, NULL) != 0)
116 printf("Error at pthread_create()\n");
117 return PTS_UNRESOLVED;
120 /* wait at most 3 seconds for sig_thread to block*/
124 }while(thread_state != EXITING_THREAD && cnt++ < 3);
126 if(thread_state == EXITING_THREAD)
128 /* the sig_thread is not blocking*/
129 printf("Test FAILED: the thread should block when getting write lock\n");
132 else if(thread_state != ENTERED_THREAD)
134 printf("sig_thread in unexpected state %d\n", thread_state);
135 exit(PTS_UNRESOLVED);
138 /* sig_thread is blocking */
139 printf("main: fire SIGUSR1 to sig_thread\n");
140 if(pthread_kill(sig_thread, SIGUSR1) != 0)
142 printf("Error at pthread_kill()\n");
143 exit(PTS_UNRESOLVED);
146 /* wait at most 3 seconds for the singal to be handled */
150 }while(handler_called == 0 && cnt++ < 3);
152 if(handler_called != 1)
154 printf("The signal handler did not get called.\n");
155 exit(PTS_UNRESOLVED);
158 /* sig_thread resume to block? */
162 }while(thread_state != EXITING_THREAD && cnt++ < 3);
164 if(thread_state == 3)
166 printf("Test FAILED: upon return from signal handler, sig_thread does not resume to wait\n");
170 printf("sig_thread: correctly still blocking after signal handler returns\n");
171 printf("main: unlock write lock\n");
172 if(pthread_rwlock_unlock(&rwlock) != 0)
174 printf("main: Error releasing write lock\n");
175 exit(PTS_UNRESOLVED);
178 /* sig_thread should get write lock */
182 }while(thread_state != EXITING_THREAD && cnt++ < 3);
184 if(thread_state != EXITING_THREAD)
186 /* sig_thread does not unblock */
187 printf("Test FAILED: sig_thread should get the write lock and exit\n");
191 if(pthread_join(sig_thread, NULL) != 0)
193 printf("Error at pthread_join()\n");
194 exit(PTS_UNRESOLVED);
197 if(pthread_rwlock_destroy(&rwlock) != 0)
199 printf("pthread_rwlock_destroy()\n");
200 exit(PTS_UNRESOLVED);
203 printf("Test PASSED\n");