• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * mutex4.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  * Thread A locks mutex - thread B tries to unlock.
37  *
38  * Depends on API functions:
39  *	pthread_mutex_lock()
40  *	pthread_mutex_trylock()
41  *	pthread_mutex_unlock()
42  */
43 
44 #include "test.h"
45 
46 static int wasHere = 0;
47 
48 static pthread_mutex_t mutex1;
49 
unlocker(void * arg)50 void * unlocker(void * arg)
51 {
52   int expectedResult = (int) (size_t) arg;
53   int h;
54   wasHere++;
55   h = pthread_mutex_unlock(&mutex1);
56   printf("*** %d==%d\n\n", h, expectedResult); fflush(stdout);
57   assert(h == expectedResult);
58   wasHere++;
59   return NULL;
60 }
61 
62 int
main()63 main()
64 {
65   pthread_t t;
66   pthread_mutexattr_t ma;
67 
68   assert(pthread_mutexattr_init(&ma) == 0);
69 
70   wasHere = 0;
71   assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_DEFAULT) == 0);
72   assert(pthread_mutex_init(&mutex1, &ma) == 0);
73   assert(pthread_mutex_lock(&mutex1) == 0);
74   /*
75    * NORMAL (fast) mutexes don't check ownership.
76    */
77   assert(pthread_create(&t, NULL, unlocker, (void *) 0) == 0);
78   assert(pthread_join(t, NULL) == 0);
79   assert(pthread_mutex_unlock(&mutex1) == EPERM);
80   assert(wasHere == 2);
81 
82   wasHere = 0;
83   assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_NORMAL) == 0);
84   assert(pthread_mutex_destroy(&mutex1) == 0);
85   assert(pthread_mutex_init(&mutex1, &ma) == 0);
86   assert(pthread_mutex_lock(&mutex1) == 0);
87   /*
88    * NORMAL (fast) mutexes don't check ownership.
89    */
90   assert(pthread_create(&t, NULL, unlocker, (void *) 0) == 0);
91   assert(pthread_join(t, NULL) == 0);
92   assert(pthread_mutex_unlock(&mutex1) == EPERM);
93   assert(wasHere == 2);
94 
95   wasHere = 0;
96   assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK) == 0);
97   assert(pthread_mutex_destroy(&mutex1) == 0);
98   assert(pthread_mutex_init(&mutex1, &ma) == 0);
99   assert(pthread_mutex_lock(&mutex1) == 0);
100   assert(pthread_create(&t, NULL, unlocker, (void *) EPERM) == 0);
101   assert(pthread_join(t, NULL) == 0);
102   assert(pthread_mutex_unlock(&mutex1) == 0);
103   assert(wasHere == 2);
104 
105   wasHere = 0;
106   assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE) == 0);
107   assert(pthread_mutex_destroy(&mutex1) == 0);
108   assert(pthread_mutex_init(&mutex1, &ma) == 0);
109   assert(pthread_mutex_lock(&mutex1) == 0);
110   assert(pthread_create(&t, NULL, unlocker, (void *) EPERM) == 0);
111   assert(pthread_join(t, NULL) == 0);
112   assert(pthread_mutex_unlock(&mutex1) == 0);
113   assert(wasHere == 2);
114 
115   return 0;
116 }
117