util.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef __UTIL_H__
  2. #define __UTIL_H__
  3. #include <semaphore.h>
  4. #if defined(unix) || defined(__APPLE__)
  5. #include <errno.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #define SOCKETTYPE long
  10. #define SOCKETFAIL(a) ((a) < 0)
  11. #define INVSOCK -1
  12. #define INVINETADDR -1
  13. #define CLOSESOCKET close
  14. #define INET_PTON inet_pton
  15. #define SOCKERRMSG strerror(errno)
  16. static inline bool sock_blocks(void)
  17. {
  18. return (errno == EAGAIN || errno == EWOULDBLOCK);
  19. }
  20. static inline bool sock_timeout(void)
  21. {
  22. return (errno == ETIMEDOUT);
  23. }
  24. static inline bool interrupted(void)
  25. {
  26. return (errno == EINTR);
  27. }
  28. #elif defined WIN32
  29. #include <winsock2.h>
  30. #include <windows.h>
  31. #include <ws2tcpip.h>
  32. #define SOCKETTYPE SOCKET
  33. #define SOCKETFAIL(a) ((int)(a) == SOCKET_ERROR)
  34. #define INVSOCK INVALID_SOCKET
  35. #define INVINETADDR INADDR_NONE
  36. #define CLOSESOCKET closesocket
  37. int Inet_Pton(int af, const char *src, void *dst);
  38. #define INET_PTON Inet_Pton
  39. extern char *WSAErrorMsg(void);
  40. #define SOCKERRMSG WSAErrorMsg()
  41. /* Check for windows variants of the errors as well as when ming
  42. * decides to wrap the error into the errno equivalent. */
  43. static inline bool sock_blocks(void)
  44. {
  45. return (WSAGetLastError() == WSAEWOULDBLOCK || errno == EAGAIN);
  46. }
  47. static inline bool sock_timeout(void)
  48. {
  49. return (WSAGetLastError() == WSAETIMEDOUT || errno == ETIMEDOUT);
  50. }
  51. static inline bool interrupted(void)
  52. {
  53. return (WSAGetLastError() == WSAEINTR || errno == EINTR);
  54. }
  55. #ifndef SHUT_RDWR
  56. #define SHUT_RDWR SD_BOTH
  57. #endif
  58. #ifndef in_addr_t
  59. #define in_addr_t uint32_t
  60. #endif
  61. #endif
  62. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  63. #ifdef HAVE_LIBCURL
  64. #include <curl/curl.h>
  65. typedef curl_proxytype proxytypes_t;
  66. #else
  67. typedef int proxytypes_t;
  68. #endif /* HAVE_LIBCURL */
  69. /* cgminer locks, a write biased variant of rwlocks */
  70. struct cglock {
  71. pthread_mutex_t mutex;
  72. pthread_rwlock_t rwlock;
  73. };
  74. typedef struct cglock cglock_t;
  75. /* cgminer specific unnamed semaphore implementations to cope with osx not
  76. * implementing them. */
  77. #ifdef __APPLE__
  78. struct cgsem {
  79. int pipefd[2];
  80. };
  81. typedef struct cgsem cgsem_t;
  82. #else
  83. typedef sem_t cgsem_t;
  84. #endif
  85. #ifdef WIN32
  86. typedef LARGE_INTEGER cgtimer_t;
  87. #else
  88. typedef struct timespec cgtimer_t;
  89. #endif
  90. extern int no_yield(void);
  91. extern int (*selective_yield)(void);
  92. void *_cgmalloc(size_t size, const char *file, const char *func, const int line);
  93. void *_cgcalloc(const size_t memb, size_t size, const char *file, const char *func, const int line);
  94. void *_cgrealloc(void *ptr, size_t size, const char *file, const char *func, const int line);
  95. #define cgmalloc(_size) _cgmalloc(_size, __FILE__, __func__, __LINE__)
  96. #define cgcalloc(_memb, _size) _cgcalloc(_memb, _size, __FILE__, __func__, __LINE__)
  97. #define cgrealloc(_ptr, _size) _cgrealloc(_ptr, _size, __FILE__, __func__, __LINE__)
  98. struct thr_info;
  99. struct pool;
  100. enum dev_reason;
  101. struct cgpu_info;
  102. void b58tobin(unsigned char *b58bin, const char *b58);
  103. void address_to_pubkeyhash(unsigned char *pkh, const char *addr);
  104. int ser_number(unsigned char *s, int32_t val);
  105. unsigned char *ser_string(char *s, int *slen);
  106. int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  107. void thr_info_cancel(struct thr_info *thr);
  108. void cgcond_time(struct timespec *abstime);
  109. #ifdef USE_GEKKO
  110. void cgtime_real(struct timeval *tv);
  111. #endif
  112. void cgtime(struct timeval *tv);
  113. void subtime(struct timeval *a, struct timeval *b);
  114. void addtime(struct timeval *a, struct timeval *b);
  115. bool time_more(struct timeval *a, struct timeval *b);
  116. bool time_less(struct timeval *a, struct timeval *b);
  117. void copy_time(struct timeval *dest, const struct timeval *src);
  118. void timespec_to_val(struct timeval *val, const struct timespec *spec);
  119. void timeval_to_spec(struct timespec *spec, const struct timeval *val);
  120. void us_to_timeval(struct timeval *val, int64_t us);
  121. void us_to_timespec(struct timespec *spec, int64_t us);
  122. void ms_to_timespec(struct timespec *spec, int64_t ms);
  123. void timeraddspec(struct timespec *a, const struct timespec *b);
  124. char *Strcasestr(char *haystack, const char *needle);
  125. char *Strsep(char **stringp, const char *delim);
  126. void cgsleep_ms(int ms);
  127. void cgsleep_us(int64_t us);
  128. void cgtimer_time(cgtimer_t *ts_start);
  129. #define cgsleep_prepare_r(ts_start) cgtimer_time(ts_start)
  130. #if defined(WIN32) || defined(__APPLE__) || USE_BITMAIN_SOC
  131. void cgsleep_ms_r(cgtimer_t *ts_start, int ms);
  132. void cgsleep_us_r(cgtimer_t *ts_start, int64_t us);
  133. #else
  134. int cgsleep_ms_r(cgtimer_t *ts_start, int ms);
  135. int64_t cgsleep_us_r(cgtimer_t *ts_start, int64_t us);
  136. #endif
  137. int cgtimer_to_ms(cgtimer_t *cgt);
  138. void cgtimer_sub(cgtimer_t *a, cgtimer_t *b, cgtimer_t *res);
  139. double us_tdiff(struct timeval *end, struct timeval *start);
  140. int ms_tdiff(struct timeval *end, struct timeval *start);
  141. double tdiff(struct timeval *end, struct timeval *start);
  142. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  143. bool sock_full(struct pool *pool);
  144. void ckrecalloc(void **ptr, size_t old, size_t new, const char *file, const char *func, const int line);
  145. #define recalloc(ptr, old, new) ckrecalloc((void *)&(ptr), old, new, __FILE__, __func__, __LINE__)
  146. char *recv_line(struct pool *pool);
  147. bool parse_method(struct pool *pool, char *s);
  148. bool subscribe_extranonce(struct pool *pool);
  149. bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port);
  150. bool auth_stratum(struct pool *pool);
  151. bool initiate_stratum(struct pool *pool);
  152. bool restart_stratum(struct pool *pool);
  153. void suspend_stratum(struct pool *pool);
  154. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  155. void *realloc_strcat(char *ptr, char *s);
  156. void *str_text(char *ptr);
  157. void RenameThread(const char* name);
  158. void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int line);
  159. void _cgsem_post(cgsem_t *cgsem, const char *file, const char *func, const int line);
  160. void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int line);
  161. int _cgsem_mswait(cgsem_t *cgsem, int ms, const char *file, const char *func, const int line);
  162. void cgsem_reset(cgsem_t *cgsem);
  163. void cgsem_destroy(cgsem_t *cgsem);
  164. bool cg_completion_timeout(void *fn, void *fnarg, int timeout);
  165. void _cg_memcpy(void *dest, const void *src, unsigned int n, const char *file, const char *func, const int line);
  166. #define cgsem_init(_sem) _cgsem_init(_sem, __FILE__, __func__, __LINE__)
  167. #define cgsem_post(_sem) _cgsem_post(_sem, __FILE__, __func__, __LINE__)
  168. #define cgsem_wait(_sem) _cgsem_wait(_sem, __FILE__, __func__, __LINE__)
  169. #define cgsem_mswait(_sem, _timeout) _cgsem_mswait(_sem, _timeout, __FILE__, __func__, __LINE__)
  170. #define cg_memcpy(dest, src, n) _cg_memcpy(dest, src, n, __FILE__, __func__, __LINE__)
  171. #endif /* __UTIL_H__ */