1 /*
2 Copyright (c) 2011-2016 mingw-w64 project
3
4 Permission is hereby granted, free of charge, to any person obtaining a
5 copy of this software and associated documentation files (the "Software"),
6 to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 DEALINGS IN THE SOFTWARE.
21 */
22
23 #ifndef WIN_PTHREADS_MISC_H
24 #define WIN_PTHREADS_MISC_H
25
26 #include "pthread_compat.h"
27
28 #ifndef assert
29
30 #ifndef ASSERT_TRACE
31 # define ASSERT_TRACE 0
32 #else
33 # undef ASSERT_TRACE
34 # define ASSERT_TRACE 0
35 #endif
36
37 # define assert(e) \
38 ((e) ? ((ASSERT_TRACE) ? fprintf(stderr, \
39 "Assertion succeeded: (%s), file %s, line %d\n", \
40 #e, __FILE__, (int) __LINE__), \
41 fflush(stderr) : \
42 0) : \
43 (fprintf(stderr, "Assertion failed: (%s), file %s, line %d\n", \
44 #e, __FILE__, (int) __LINE__), exit(1), 0))
45
46 # define fixme(e) \
47 ((e) ? ((ASSERT_TRACE) ? fprintf(stderr, \
48 "Assertion succeeded: (%s), file %s, line %d\n", \
49 #e, __FILE__, (int) __LINE__), \
50 fflush(stderr) : \
51 0) : \
52 (fprintf(stderr, "FIXME: (%s), file %s, line %d\n", \
53 #e, __FILE__, (int) __LINE__), 0, 0))
54
55 #endif
56
57 #define PTR2INT(x) ((int)(uintptr_t)(x))
58
59 #if SIZE_MAX>UINT_MAX
60 typedef long long LONGBAG;
61 #else
62 typedef long LONGBAG;
63 #endif
64
65 #define CHECK_HANDLE(h) { DWORD dwFlags; \
66 if (!(h) || ((h) == INVALID_HANDLE_VALUE) || !GetHandleInformation((h), &dwFlags)) \
67 return EINVAL; }
68
69 #define CHECK_PTR(p) if (!(p)) return EINVAL;
70
71 #define UPD_RESULT(x,r) { int _r=(x); r = r ? r : _r; }
72
73 #define CHECK_THREAD(t) { \
74 CHECK_PTR(t); \
75 CHECK_HANDLE(t->h); }
76
77 #define CHECK_OBJECT(o, e) { DWORD dwFlags; \
78 if (!(o)) return e; \
79 if (!((o)->h) || (((o)->h) == INVALID_HANDLE_VALUE) || !GetHandleInformation(((o)->h), &dwFlags)) \
80 return e; }
81
82 #define VALID(x) if (!(p)) return EINVAL;
83
84 /* ms can be 64 bit, solve wrap-around issues: */
dwMilliSecs(unsigned long long ms)85 static WINPTHREADS_INLINE unsigned long dwMilliSecs(unsigned long long ms)
86 {
87 if (ms >= 0xffffffffULL) return 0xfffffffful;
88 return (unsigned long) ms;
89 }
90
91 #ifndef _mm_pause
92 #define _mm_pause() {__asm__ __volatile__("pause");}
93 #endif
94
95 #ifndef _ReadWriteBarrier
96 #define _ReadWriteBarrier __sync_synchronize
97 #endif
98
99 #ifndef YieldProcessor
100 #define YieldProcessor _mm_pause
101 #endif
102
103 unsigned long long _pthread_time_in_ms(void);
104 unsigned long long _pthread_time_in_ms_from_timespec(const struct timespec *ts);
105 unsigned long long _pthread_rel_time_in_ms(const struct timespec *ts);
106 unsigned long _pthread_wait_for_single_object (void *handle, unsigned long timeout);
107 unsigned long _pthread_wait_for_multiple_objects (unsigned long count, void **handles, unsigned int all, unsigned long timeout);
108
109 #endif
110