1 /* 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 5 * source tree. 6 7 * Test that pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock) 8 * 9 * Under no circumstances shall the function fail with a timeout if the lock can be 10 * acquired immediately. The abs_timeout parameter need not be checked if the lock 11 * can be immediately acquired. 12 * 13 * Steps:n 14 * 1. Main thread create a thread. 15 * 2. Child thread lock 'rwlock' for reading with pthread_rwlock_timedrdlock(), 16 * should not fail with timeout 17 * 3. The child thread unlocks the 'rwlock' and exits. 18 * 4. Main thread create another thread. 19 * 4. The child thread lock 'rwlock' for reading, with pthread_rwlock_timedrdlock(), 20 * specifying a 'abs_timeout'. The thread sleeps until 'abs_timeout' expires. 21 * 5. The thread call pthread_rwlock_timedrdlock(). Should _NOT_ get ETIMEDOUT. 22 */ 23 24 #define _XOPEN_SOURCE 600 25 #include <pthread.h> 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <unistd.h> 29 #include <errno.h> 30 #include "posixtest.h" 31 32 #define NOT_CREATED_THREAD 1 33 #define ENTERED_THREAD 2 34 #define EXITING_THREAD 3 35 36 #define TIMEOUT 1 37 static int thread_state; 38 static int currsec1; 39 static int expired; 40 41 static void *fn_rd_1(void *arg) 42 { 43 thread_state = ENTERED_THREAD; 44 struct timespec abs_timeout; 45 int rc; 46 pthread_rwlock_t rwlock; 47 48 if (pthread_rwlock_init(&rwlock, NULL) != 0) { 49 printf("thread1: Error at pthread_rwlock_init\n"); 50 exit(PTS_UNRESOLVED); 51 } 52 53 currsec1 = time(NULL); 54 55 /* Absolute time, not relative. */ 56 abs_timeout.tv_sec = currsec1 + TIMEOUT; 57 abs_timeout.tv_nsec = 0; 58 59 printf("thread1: attempt timed read-lock\n"); 60 rc = pthread_rwlock_timedrdlock(&rwlock, &abs_timeout); 61 if (rc == ETIMEDOUT) { 62 printf("thread1: timed read-lock expired\n"); 63 expired = 1; 64 } else if (rc == 0) { 65 printf("thread1: acquired read lock\n"); 66 expired = 0; 67 printf("thread1: unlock read lock\n"); 68 if (pthread_rwlock_unlock(&rwlock) != 0) { 69 printf("thread1: failed to release read lock\n"); 70 exit(PTS_UNRESOLVED); 71 } 72 } else { 73 printf("thread1: Error in pthread_rwlock_timedrdlock().\n"); 74 exit(PTS_UNRESOLVED); 75 } 76 77 if (pthread_rwlock_destroy(&rwlock) != 0) { 78 printf("thread1: Error at pthread_rwlockattr_destroy()"); 79 exit(PTS_UNRESOLVED); 80 } 81 thread_state = EXITING_THREAD; 82 pthread_exit(0); 83 return NULL; 84 } 85 86 static void *fn_rd_2(void *arg) 87 { 88 thread_state = ENTERED_THREAD; 89 struct timespec abs_timeout; 90 int rc; 91 pthread_rwlock_t rwlock; 92 93 if (pthread_rwlock_init(&rwlock, NULL) != 0) { 94 printf("thread2: Error at pthread_rwlock_init\n"); 95 exit(PTS_UNRESOLVED); 96 } 97 currsec1 = time(NULL); 98 99 /* Ensure that the abs_timeout has passed by _subtracting_ the timeout value of 1 100 * from the current time. */ 101 abs_timeout.tv_sec = currsec1 - TIMEOUT; 102 abs_timeout.tv_nsec = 0; 103 104 printf("thread2: attempt timed read-lock\n"); 105 rc = pthread_rwlock_timedrdlock(&rwlock, &abs_timeout); 106 if (rc == ETIMEDOUT) { 107 printf("thread2: timed read-lock expired\n"); 108 expired = 1; 109 } else if (rc == 0) { 110 printf("thread2: acquired read lock\n"); 111 expired = 0; 112 printf("thread2: unlock read lock\n"); 113 if (pthread_rwlock_unlock(&rwlock) != 0) { 114 printf("thread2: failed to release read lock\n"); 115 exit(PTS_UNRESOLVED); 116 } 117 } else { 118 printf("thread2: Error in pthread_rwlock_timedrdlock().\n"); 119 exit(PTS_UNRESOLVED); 120 } 121 122 if (pthread_rwlock_destroy(&rwlock) != 0) { 123 printf("thread2: Error at pthread_rwlockattr_destroy()\n"); 124 exit(PTS_UNRESOLVED); 125 } 126 thread_state = EXITING_THREAD; 127 pthread_exit(0); 128 return NULL; 129 } 130 131 int main(void) 132 { 133 int cnt = 0; 134 135 pthread_t thread1, thread2; 136 137 thread_state = NOT_CREATED_THREAD; 138 printf("main: create thread1\n"); 139 if (pthread_create(&thread1, NULL, fn_rd_1, NULL) != 0) { 140 printf("Error when creating thread1\n"); 141 return PTS_UNRESOLVED; 142 } 143 144 /* If the shared data is not altered by child after 5 seconds, 145 we regard it as blocked */ 146 147 /* we expect thread1 NOT to block, but rather for the timed read-lock to expire */ 148 cnt = 0; 149 do { 150 sleep(1); 151 } while (thread_state != EXITING_THREAD && cnt++ < 5); 152 153 if (thread_state == EXITING_THREAD) { 154 /* the child thread does not block, check the time expired or not */ 155 if (expired == 1) { 156 printf 157 ("Test FAILED: thread1 incorrectly received ETIMEDOUT\n"); 158 return PTS_FAIL; 159 } 160 } else if (thread_state == ENTERED_THREAD) { 161 printf 162 ("Test FAILED: thread1 incorrectly blocked for reading rwlock\n"); 163 return PTS_FAIL; 164 } else { 165 printf("Unexpected state for thread1 %d\n", thread_state); 166 return PTS_UNRESOLVED; 167 } 168 169 if (pthread_join(thread1, NULL) != 0) { 170 printf("Error when joining thread1\n"); 171 return PTS_UNRESOLVED; 172 } 173 174 printf("main: create thread2\n"); 175 thread_state = NOT_CREATED_THREAD; 176 if (pthread_create(&thread2, NULL, fn_rd_2, NULL) != 0) { 177 printf("Error when creating thread2\n"); 178 return PTS_UNRESOLVED; 179 } 180 181 /* If the shared data is not altered by child after 5 seconds, 182 we regard it as blocked */ 183 184 /* we expect thread2 NOT to block */ 185 cnt = 0; 186 do { 187 sleep(1); 188 } while (thread_state != EXITING_THREAD && cnt++ < 5); 189 190 if (thread_state == EXITING_THREAD) { 191 /* the child thread does not block, check the time expired or not */ 192 if (expired == 1) { 193 printf 194 ("Test FAILED: thread2 incorrectly received ETIMEDOUT\n"); 195 return PTS_FAIL; 196 } 197 } else if (thread_state == ENTERED_THREAD) { 198 printf 199 ("Test FAILED: thread2 incorrectly blocked for reading rwlock\n"); 200 return PTS_FAIL; 201 } else { 202 printf("Unexpected state for thread2 %d\n", thread_state); 203 return PTS_UNRESOLVED; 204 } 205 206 if (pthread_join(thread2, NULL) != 0) { 207 printf("Error when join thread2\n"); 208 return PTS_UNRESOLVED; 209 } 210 211 printf("Test PASSED\n"); 212 return PTS_PASS; 213 } 214