-
Notifications
You must be signed in to change notification settings - Fork 27
/
compat.h
144 lines (120 loc) · 2.62 KB
/
compat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef COMPAT_H
#define COMPAT_H
#ifdef __MINGW32__
#include <winsock2.h>
#endif
#ifdef WIN32
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <sys/time.h>
#include "miner.h" // for timersub
#include "util.h"
#include <windows.h>
#if !defined S_ISDIR && defined S_IFDIR
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
#if !S_IRUSR && S_IREAD
# define S_IRUSR S_IREAD
#endif
#if !S_IRUSR
# define S_IRUSR 00400
#endif
#if !S_IWUSR && S_IWRITE
# define S_IWUSR S_IWRITE
#endif
#if !S_IWUSR
# define S_IWUSR 00200
#endif
#if !S_IXUSR && S_IEXEC
# define S_IXUSR S_IEXEC
#endif
#if !S_IXUSR
# define S_IXUSR 00100
#endif
#ifndef HAVE_LIBWINPTHREAD
static inline int nanosleep(const struct timespec *req, struct timespec *rem)
{
struct timeval tstart;
DWORD msecs;
cgtime(&tstart);
msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
if (rem) {
struct timeval tdone, tnow, tleft;
tdone.tv_sec = tstart.tv_sec + req->tv_sec;
tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
if (tdone.tv_usec > 1000000) {
tdone.tv_usec -= 1000000;
++tdone.tv_sec;
}
cgtime(&tnow);
if (timercmp(&tnow, &tdone, >))
return 0;
timersub(&tdone, &tnow, &tleft);
rem->tv_sec = tleft.tv_sec;
rem->tv_nsec = tleft.tv_usec * 1000;
}
errno = EINTR;
return -1;
}
return 0;
}
#endif
#if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
// Reported unneded in https://github.com/veox/sgminer/issues/37 */
#if 0
static inline int sleep(unsigned int secs)
{
struct timespec req, rem;
req.tv_sec = secs;
req.tv_nsec = 0;
if (!nanosleep(&req, &rem))
return 0;
return rem.tv_sec + (rem.tv_nsec ? 1 : 0);
}
#endif
#endif
enum {
PRIO_PROCESS = 0,
};
static inline int setpriority(__maybe_unused int which, __maybe_unused int who, __maybe_unused int prio)
{
/* FIXME - actually do something */
return 0;
}
#ifndef HAVE_STRSEP
static inline char *strsep(char **stringp, const char *delim)
{
char *res;
if (!stringp || !*stringp || !**stringp) {
return NULL;
}
res = *stringp;
while(**stringp && !strchr(delim, **stringp)) {
++(*stringp);
}
if (**stringp) {
**stringp = '\0';
++(*stringp);
}
return res;
}
#endif
typedef unsigned long int ulong;
typedef unsigned short int ushort;
typedef unsigned int uint;
#ifndef __SUSECONDS_T_TYPE
typedef long suseconds_t;
#endif
#ifdef HAVE_LIBWINPTHREAD
#define PTH(thr) ((thr)->pth)
#else
#define PTH(thr) ((thr)->pth.p)
#endif
#else
#define PTH(thr) ((thr)->pth)
#endif /* WIN32 */
#endif /* COMPAT_H */