1 /*
2 * rwlock6_t2.c
3 *
4 *
5 * --------------------------------------------------------------------------
6 *
7 * Pthreads-win32 - POSIX Threads Library for Win32
8 * Copyright(C) 1998 John E. Bossom
9 * Copyright(C) 1999,2005 Pthreads-win32 contributors
10 *
11 * Contact Email: rpj@callisto.canberra.edu.au
12 *
13 * The current list of contributors is contained
14 * in the file CONTRIBUTORS included with the source
15 * code distribution. The list can also be seen at the
16 * following World Wide Web location:
17 * http://sources.redhat.com/pthreads-win32/contributors.html
18 *
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2 of the License, or (at your option) any later version.
23 *
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
28 *
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library in the file COPYING.LIB;
31 * if not, write to the Free Software Foundation, Inc.,
32 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
33 *
34 * --------------------------------------------------------------------------
35 *
36 * Check writer and reader timeouts.
37 *
38 * Depends on API functions:
39 * pthread_rwlock_timedrdlock()
40 * pthread_rwlock_timedwrlock()
41 * pthread_rwlock_unlock()
42 */
43
44 #include "test.h"
45 #include <sys/timeb.h>
46
47 static pthread_rwlock_t rwlock1 = PTHREAD_RWLOCK_INITIALIZER;
48
49 static int bankAccount = 0;
50 struct timespec abstime = { 0, 0 };
51
wrfunc(void * arg)52 void * wrfunc(void * arg)
53 {
54 int result;
55
56 result = pthread_rwlock_timedwrlock(&rwlock1, &abstime);
57 if ((int) (size_t) arg == 1)
58 {
59 assert(result == 0);
60 Sleep(2000);
61 bankAccount += 10;
62 assert(pthread_rwlock_unlock(&rwlock1) == 0);
63 return ((void *) (size_t) bankAccount);
64 }
65 else if ((int) (size_t) arg == 2)
66 {
67 assert(result == ETIMEDOUT);
68 return ((void *) (size_t) 100);
69 }
70
71 return ((void *) (size_t) -1);
72 }
73
rdfunc(void * arg)74 void * rdfunc(void * arg)
75 {
76 int ba = 0;
77
78 assert(pthread_rwlock_timedrdlock(&rwlock1, &abstime) == ETIMEDOUT);
79
80 return ((void *) (size_t) ba);
81 }
82
83 int
main()84 main()
85 {
86 pthread_t wrt1;
87 pthread_t wrt2;
88 pthread_t rdt;
89 intptr_t wr1Result = 0;
90 intptr_t wr2Result = 0;
91 intptr_t rdResult = 0;
92 struct _timeb currSysTime;
93 const DWORD NANOSEC_PER_MILLISEC = 1000000;
94
95 _ftime(&currSysTime);
96
97 abstime.tv_sec = currSysTime.time;
98 abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
99
100 abstime.tv_sec += 1;
101
102 bankAccount = 0;
103
104 assert(pthread_create(&wrt1, NULL, wrfunc, (void *) 1) == 0);
105 Sleep(100);
106 assert(pthread_create(&rdt, NULL, rdfunc, NULL) == 0);
107 Sleep(100);
108 assert(pthread_create(&wrt2, NULL, wrfunc, (void *) 2) == 0);
109
110 assert(pthread_join(wrt1, (void **) &wr1Result) == 0);
111 assert(pthread_join(rdt, (void **) &rdResult) == 0);
112 assert(pthread_join(wrt2, (void **) &wr2Result) == 0);
113
114 assert(wr1Result == 10);
115 assert(rdResult == 0);
116 assert(wr2Result == 100);
117
118 return 0;
119 }
120