1 /*
2 * test.h
3 *
4 * Useful definitions and declarations for tests.
5 *
6 *
7 * --------------------------------------------------------------------------
8 *
9 * Pthreads-win32 - POSIX Threads Library for Win32
10 * Copyright(C) 1998 John E. Bossom
11 * Copyright(C) 1999,2005 Pthreads-win32 contributors
12 *
13 * Contact Email: rpj@callisto.canberra.edu.au
14 *
15 * The current list of contributors is contained
16 * in the file CONTRIBUTORS included with the source
17 * code distribution. The list can also be seen at the
18 * following World Wide Web location:
19 * http://sources.redhat.com/pthreads-win32/contributors.html
20 *
21 * This library is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU Lesser General Public
23 * License as published by the Free Software Foundation; either
24 * version 2 of the License, or (at your option) any later version.
25 *
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
30 *
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library in the file COPYING.LIB;
33 * if not, write to the Free Software Foundation, Inc.,
34 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
35 *
36 */
37
38 #ifndef _PTHREAD_TEST_H_
39 #define _PTHREAD_TEST_H_
40
41 #include <windows.h>
42 #include <stdio.h>
43 #include <time.h>
44
45 #include "../include/pthread.h"
46 #include "../include/semaphore.h"
47 #include "../src/misc.h"
48 #include "../src/mutex.h"
49 #include "../src/cond.h"
50 #include "../src/rwlock.h"
51 #include "../src/thread.h"
52 #include "../src/barrier.h"
53
54 #define PTW32_THREAD_NULL_ID {NULL,0}
55
56 #include <stdint.h>
57
58 const char * error_string[] = {
59 "ZERO_or_EOK",
60 "EPERM",
61 "ENOFILE_or_ENOENT",
62 "ESRCH",
63 "EINTR",
64 "EIO",
65 "ENXIO",
66 "E2BIG",
67 "ENOEXEC",
68 "EBADF",
69 "ECHILD",
70 "EAGAIN",
71 "ENOMEM",
72 "EACCES",
73 "EFAULT",
74 "UNKNOWN_15",
75 "EBUSY",
76 "EEXIST",
77 "EXDEV",
78 "ENODEV",
79 "ENOTDIR",
80 "EISDIR",
81 "EINVAL",
82 "ENFILE",
83 "EMFILE",
84 "ENOTTY",
85 "UNKNOWN_26",
86 "EFBIG",
87 "ENOSPC",
88 "ESPIPE",
89 "EROFS",
90 "EMLINK",
91 "EPIPE",
92 "EDOM",
93 "ERANGE",
94 "UNKNOWN_35",
95 "EDEADLOCK_or_EDEADLK",
96 "UNKNOWN_37",
97 "ENAMETOOLONG",
98 "ENOLCK",
99 "ENOSYS",
100 "ENOTEMPTY",
101 "EILSEQ"
102 };
103
104 /*
105 * The Mingw32 assert macro calls the CRTDLL _assert function
106 * which pops up a dialog. We want to run in batch mode so
107 * we define our own assert macro.
108 */
109 #ifdef assert
110 # undef assert
111 #endif
112
113 #ifndef ASSERT_TRACE
114 # define ASSERT_TRACE 0
115 #else
116 # undef ASSERT_TRACE
117 # define ASSERT_TRACE 0
118 #endif
119
120 # define assert(e) \
121 ((e) ? ((ASSERT_TRACE) ? fprintf(stderr, \
122 "Assertion succeeded: (%s), file %s, line %d\n", \
123 #e, __FILE__, (int) __LINE__), \
124 fflush(stderr) : \
125 0) : \
126 (fprintf(stderr, "Assertion failed: (%s), file %s, line %d\n", \
127 #e, __FILE__, (int) __LINE__), exit(1), 0))
128
129 int assertE;
130 # define assert_e(e, o, r) \
131 (((assertE = e) o (r)) ? ((ASSERT_TRACE) ? fprintf(stderr, \
132 "Assertion succeeded: (%s), file %s, line %d\n", \
133 #e, __FILE__, (int) __LINE__), \
134 fflush(stderr) : \
135 0) : \
136 (fprintf(stderr, "Assertion failed: (%s %s %s), file %s, line %d, error %s\n", \
137 #e,#o,#r, __FILE__, (int) __LINE__, error_string[assertE]), exit(1), 0))
138
139 #ifndef PTW32_VERSION
140 /* Extensions for winpthread to make more w32 tests happy (non posix): */
141
142 /*
143 * gmtime(tm) and localtime(tm) return 0 if tm represents
144 * a time prior to 1/1/1970.
145 */
146 #ifndef gmtime_r
147 #define gmtime_r( _clock, _result ) \
148 ( gmtime( (_clock) ) \
149 ? (*(_result) = *gmtime( (_clock) ), (_result) ) \
150 : (0) )
151 #endif
152 #ifndef localtime_r
153 #define localtime_r( _clock, _result ) \
154 ( localtime( (_clock) ) \
155 ? (*(_result) = *localtime( (_clock) ), (_result) ) \
156 : (0) )
157 #endif
158 #ifndef rand_r
159 #define rand_r( _seed ) \
160 ( _seed == _seed? rand() : rand() )
161 #endif
162
163 enum ptw32_features {
164 PTW32_SYSTEM_INTERLOCKED_COMPARE_EXCHANGE = 0x0001, /* System provides it. */
165 PTW32_ALERTABLE_ASYNC_CANCEL = 0x0002 /* Can cancel blocked threads. */
166 };
167
168 #define pthread_getw32threadhandle_np(self) ((HANDLE) pthread_gethandle (self))
169
170 /* half-stubbed version */
pthread_win32_test_features_np(int mask)171 int pthread_win32_test_features_np(int mask)
172 {
173 int r = 0;
174
175 r = (mask & PTW32_SYSTEM_INTERLOCKED_COMPARE_EXCHANGE); /* assume Win32 */
176 /* Not supporting PTW32_ALERTABLE_ASYNC_CANCEL for now, just say it isn't there */
177 return r;
178 }
179
180 #endif
181
182 #endif
183