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
9
* Contact Email: rpj@ise.canberra.edu.au
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.
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.
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
25
* --------------------------------------------------------------------------
27
* Tests PTHREAD_MUTEX_RECURSIVE mutex type exercising timedlock.
28
* Thread locks mutex, another thread timedlocks the mutex.
29
* Timed thread should timeout.
31
* Depends on API functions:
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()
45
#include <sys/timeb.h>
47
static int lockCount = 0;
49
static pthread_mutex_t mutex;
50
static pthread_mutexattr_t mxAttr;
52
void * locker(void * arg)
54
struct timespec abstime = { 0, 0 };
55
struct _timeb currSysTime;
56
const DWORD NANOSEC_PER_MILLISEC = 1000000;
60
abstime.tv_sec = currSysTime.time;
61
abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
65
assert(pthread_mutex_timedlock(&mutex, &abstime) == ETIMEDOUT);
78
assert(pthread_mutexattr_init(&mxAttr) == 0);
79
assert(pthread_mutexattr_settype(&mxAttr, PTHREAD_MUTEX_RECURSIVE) == 0);
80
assert(pthread_mutexattr_gettype(&mxAttr, &mxType) == 0);
81
assert(mxType == PTHREAD_MUTEX_RECURSIVE);
83
assert(pthread_mutex_init(&mutex, &mxAttr) == 0);
85
assert(pthread_mutex_lock(&mutex) == 0);
87
assert(pthread_create(&t, NULL, locker, NULL) == 0);
91
assert(lockCount == 1);
93
assert(pthread_mutex_unlock(&mutex) == 0);