/ compat.h
compat.h
 1  #ifndef __COMPAT_H__
 2  #define __COMPAT_H__
 3  
 4  #ifdef WIN32
 5  #include "config.h"
 6  #include <errno.h>
 7  #include <time.h>
 8  #include <pthread.h>
 9  #include <sys/time.h>
10  
11  #include "miner.h"  // for timersub
12  #include "util.h"
13  
14  #include <winsock2.h>
15  #include <windows.h>
16  
17  #ifndef HAVE_LIBWINPTHREAD
18  static inline int nanosleep(const struct timespec *req, struct timespec *rem)
19  {
20  	struct timeval tstart;
21  	DWORD msecs;
22  
23  	cgtime(&tstart);
24  	msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
25  
26  	if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
27  		if (rem) {
28  			struct timeval tdone, tnow, tleft;
29  			tdone.tv_sec = tstart.tv_sec + req->tv_sec;
30  			tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
31  			if (tdone.tv_usec > 1000000) {
32  				tdone.tv_usec -= 1000000;
33  				++tdone.tv_sec;
34  			}
35  
36  			cgtime(&tnow);
37  			if (timercmp(&tnow, &tdone, >))
38  				return 0;
39  			timersub(&tdone, &tnow, &tleft);
40  
41  			rem->tv_sec = tleft.tv_sec;
42  			rem->tv_nsec = tleft.tv_usec * 1000;
43  		}
44  		errno = EINTR;
45  		return -1;
46  	}
47  	return 0;
48  }
49  #endif
50  #if 0
51  static inline int sleep(unsigned int secs)
52  {
53  	struct timespec req, rem;
54  	req.tv_sec = secs;
55  	req.tv_nsec = 0;
56  	if (!nanosleep(&req, &rem))
57  		return 0;
58  	return rem.tv_sec + (rem.tv_nsec ? 1 : 0);
59  }
60  #endif
61  enum {
62  	PRIO_PROCESS		= 0,
63  };
64  
65  static inline int setpriority(__maybe_unused int which, __maybe_unused int who, __maybe_unused int prio)
66  {
67  	/* FIXME - actually do something */
68  	return 0;
69  }
70  
71  typedef unsigned long int ulong;
72  typedef unsigned short int ushort;
73  typedef unsigned int uint;
74  
75  #ifndef __SUSECONDS_T_TYPE
76  typedef long suseconds_t;
77  #endif
78  
79  #ifdef HAVE_LIBWINPTHREAD
80  #define PTH(thr) ((thr)->pth)
81  #else
82  #define PTH(thr) ((thr)->pth.p)
83  #endif
84  
85  #else
86  #define PTH(thr) ((thr)->pth)
87  #endif /* WIN32 */
88  
89  #endif /* __COMPAT_H__ */