1 /*
2  * mutex8e.c
3  *
4  *
5  * Pthreads-win32 - POSIX Threads Library for Win32
6  * Copyright (C) 1998 Ben Elliston and Ross Johnson
7  * Copyright (C) 1999,2000,2001 Ross Johnson
8  *
9  * Contact Email: rpj@ise.canberra.edu.au
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  *
25  * --------------------------------------------------------------------------
26  *
27  * Tests PTHREAD_MUTEX_ERRORCHECK mutex type exercising timedlock.
28  * Thread locks mutex, another thread timedlocks the mutex.
29  * Timed thread should timeout.
30  *
31  * Depends on API functions:
32  *	pthread_create()
33  *	pthread_mutexattr_init()
34  *	pthread_mutexattr_destroy()
35  *	pthread_mutexattr_settype()
36  *	pthread_mutexattr_gettype()
37  *	pthread_mutex_init()
38  *	pthread_mutex_destroy()
39  *	pthread_mutex_lock()
40  *	pthread_mutex_timedlock()
41  *	pthread_mutex_unlock()
42  */
43 
44 #include "test.h"
45 #include <sys/timeb.h>
46 
47 static int lockCount = 0;
48 
49 static pthread_mutex_t mutex;
50 static pthread_mutexattr_t mxAttr;
51 
locker(void * arg)52 void * locker(void * arg)
53 {
54   struct timespec abstime = { 0, 0 };
55   struct _timeb currSysTime;
56   const DWORD NANOSEC_PER_MILLISEC = 1000000;
57 
58   _ftime(&currSysTime);
59 
60   abstime.tv_sec = currSysTime.time;
61   abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
62 
63   abstime.tv_sec += 1;
64 
65   assert(pthread_mutex_timedlock(&mutex, &abstime) == ETIMEDOUT);
66 
67   lockCount++;
68 
69   return 0;
70 }
71 
72 int
main()73 main()
74 {
75   pthread_t t;
76   int mxType = -1;
77 
78   assert(pthread_mutexattr_init(&mxAttr) == 0);
79   assert(pthread_mutexattr_settype(&mxAttr, PTHREAD_MUTEX_ERRORCHECK) == 0);
80   assert(pthread_mutexattr_gettype(&mxAttr, &mxType) == 0);
81   assert(mxType == PTHREAD_MUTEX_ERRORCHECK);
82 
83   assert(pthread_mutex_init(&mutex, &mxAttr) == 0);
84 
85   assert(pthread_mutex_lock(&mutex) == 0);
86 
87   assert(pthread_create(&t, NULL, locker, NULL) == 0);
88 
89   Sleep(2000);
90 
91   assert(lockCount == 1);
92 
93   assert(pthread_mutex_unlock(&mutex) == 0);
94 
95   return 0;
96 }
97 
98