1 /*
2 * rwlock6.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 locking
37 *
38 * Depends on API functions:
39 * pthread_rwlock_rdlock()
40 * pthread_rwlock_wrlock()
41 * pthread_rwlock_unlock()
42 */
43
44 #include "test.h"
45
46 static pthread_rwlock_t rwlock1 = PTHREAD_RWLOCK_INITIALIZER;
47
48 static volatile int bankAccount = 0;
49
wrfunc(void * arg)50 void * wrfunc(void * arg)
51 {
52 int ba;
53
54 assert(pthread_rwlock_wrlock(&rwlock1) == 0);
55 Sleep(2000);
56 bankAccount += 10;
57 ba = bankAccount;
58 assert(pthread_rwlock_unlock(&rwlock1) == 0);
59
60 return ((void *) (size_t) ba);
61 }
62
rdfunc(void * arg)63 void * rdfunc(void * arg)
64 {
65 int ba;
66
67 assert(pthread_rwlock_rdlock(&rwlock1) == 0);
68 ba = bankAccount;
69 assert(pthread_rwlock_unlock(&rwlock1) == 0);
70
71 return ((void *) (size_t) ba);
72 }
73
74 int
main()75 main()
76 {
77 pthread_t wrt1;
78 pthread_t wrt2;
79 pthread_t rdt;
80 volatile intptr_t wr1Result = 0;
81 volatile intptr_t wr2Result = 0;
82 volatile intptr_t rdResult = 0;
83
84 bankAccount = 0;
85
86 assert(pthread_create(&wrt1, NULL, wrfunc, NULL) == 0);
87 Sleep(1000);
88 assert(pthread_create(&rdt, NULL, rdfunc, NULL) == 0);
89 Sleep(500);
90 assert(pthread_create(&wrt2, NULL, wrfunc, NULL) == 0);
91
92 assert(pthread_join(wrt1, (void **) &wr1Result) == 0);
93 assert(pthread_join(rdt, (void **) &rdResult) == 0);
94 assert(pthread_join(wrt2, (void **) &wr2Result) == 0);
95
96 printf ("%d %d %d\n", (int)wr1Result, (int)rdResult, (int)wr2Result);
97 assert(wr1Result == 10);
98 assert(rdResult == 10);
99 assert(wr2Result == 20);
100
101 return 0;
102 }
103