• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * mutex8.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  * Test the default (type not set) 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_mutex_lock()
33  *	pthread_mutex_timedlock()
34  *	pthread_mutex_unlock()
35  */
36 
37 #include "test.h"
38 #include <sys/timeb.h>
39 
40 static int lockCount = 0;
41 
42 static pthread_mutex_t mutex;
43 
locker(void * arg)44 void * locker(void * arg)
45 {
46   struct timespec abstime = { 0, 0 };
47   struct _timeb currSysTime;
48   const DWORD NANOSEC_PER_MILLISEC = 1000000;
49 
50   _ftime(&currSysTime);
51 
52   abstime.tv_sec = currSysTime.time;
53   abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
54 
55   abstime.tv_sec += 1;
56 
57   assert(pthread_mutex_timedlock(&mutex, &abstime) == ETIMEDOUT);
58 
59   lockCount++;
60 
61   return 0;
62 }
63 
64 int
main()65 main()
66 {
67   pthread_t t;
68 
69   assert(pthread_mutex_init(&mutex, NULL) == 0);
70 
71   assert(pthread_mutex_lock(&mutex) == 0);
72 
73   assert(pthread_create(&t, NULL, locker, NULL) == 0);
74 
75   Sleep(2000);
76 
77   assert(lockCount == 1);
78 
79   assert(pthread_mutex_unlock(&mutex) == 0);
80 
81   return 0;
82 }
83