1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/*
* Copyright (c) 2002, Intel Corporation. All rights reserved.
* This file is licensed under the GPL license. For the full content
* of this license, see the COPYING file at the top level of this
* source tree.
*
* Test pthread_rwlock_init().
*
* pthread_rwlock_init() function shall allocate any resources
* required to use the read-write lock referenced by rwlock and
* initializes the lock to an unlocked state with attributes referenced
* by attr.
*
* Steps:
* 1. Initialize a pthread_rwlock_t object 'rwlock' with pthread_rwlock_init()
* 2. Create a child thread, the thread lock 'rwlock' for reading, shall not block.
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "posixtest.h"
static pthread_rwlock_t rwlock;
static int thread_state;
static void *fn_rd(void *arg)
{
thread_state = 2;
int rc;
printf("child: lock for reading\n");
rc = pthread_rwlock_rdlock(&rwlock);
if (rc == 0) {
printf("child: get read lock\n");
printf("child: unlock\n");
if (pthread_rwlock_unlock(&rwlock) != 0) {
printf("child: release read lock\n");
exit(PTS_UNRESOLVED);
}
} else {
printf("Error in pthread_rwlock_rdlock().\n");
exit(PTS_FAIL);
}
thread_state = 3;
pthread_exit(0);
return NULL;
}
int main(void)
{
int cnt = 0;
int rc = 0;
thread_state = 0;
pthread_t thread;
pthread_rwlockattr_t rwlockattr;
if (pthread_rwlockattr_init(&rwlockattr) != 0) {
printf("main: Error at pthread_rwlockattr_init()\n");
return PTS_UNRESOLVED;
}
rc = pthread_rwlock_init(&rwlock, &rwlockattr);
if (rc != 0) {
printf
("Test FAILED: Error at pthread_rwlock_init(), returns %d\n",
rc);
return PTS_FAIL;
}
thread_state = 1;
printf("main: create thread\n");
if (pthread_create(&thread, NULL, fn_rd, NULL) != 0) {
printf("main: failed to create thread\n");
return PTS_UNRESOLVED;
}
/* If the shared data is not altered by child after 3 seconds,
we regard it as blocked */
/* We expect the thread not to block */
cnt = 0;
do {
sleep(1);
} while (thread_state != 3 && cnt++ < 3);
if (thread_state == 2) {
printf("Test FAILED: thread blocked on read lock\n");
exit(PTS_FAIL);
} else if (thread_state != 3) {
printf("main: Unexpected thread state\n");
exit(PTS_UNRESOLVED);
}
if (pthread_join(thread, NULL) != 0) {
printf("main: Error at pthread_join()\n");
exit(PTS_UNRESOLVED);
}
/* Cleanup */
if (pthread_rwlock_destroy(&rwlock) != 0) {
printf("Error at pthread_rwlock_destroy()\n");
return PTS_UNRESOLVED;
}
if (pthread_rwlockattr_destroy(&rwlockattr) != 0) {
printf("Error at pthread_rwlockattr_destroy()\n");
return PTS_UNRESOLVED;
}
printf("Test PASSED\n");
return PTS_PASS;
}
|