util.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. struct thr_info;
  91. struct pool;
  92. enum dev_reason;
  93. struct cgpu_info;
  94. void b58tobin(unsigned char *b58bin, const char *b58);
  95. void address_to_pubkeyhash(unsigned char *pkh, const char *addr);
  96. int ser_number(unsigned char *s, int32_t val);
  97. unsigned char *ser_string(char *s, int *slen);
  98. int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  99. void thr_info_cancel(struct thr_info *thr);
  100. void cgtime(struct timeval *tv);
  101. void subtime(struct timeval *a, struct timeval *b);
  102. void addtime(struct timeval *a, struct timeval *b);
  103. bool time_more(struct timeval *a, struct timeval *b);
  104. bool time_less(struct timeval *a, struct timeval *b);
  105. void copy_time(struct timeval *dest, const struct timeval *src);
  106. void timespec_to_val(struct timeval *val, const struct timespec *spec);
  107. void timeval_to_spec(struct timespec *spec, const struct timeval *val);
  108. void us_to_timeval(struct timeval *val, int64_t us);
  109. void us_to_timespec(struct timespec *spec, int64_t us);
  110. void ms_to_timespec(struct timespec *spec, int64_t ms);
  111. void timeraddspec(struct timespec *a, const struct timespec *b);
  112. void cgsleep_ms(int ms);
  113. void cgsleep_us(int64_t us);
  114. void cgtimer_time(cgtimer_t *ts_start);
  115. #define cgsleep_prepare_r(ts_start) cgtimer_time(ts_start)
  116. #if defined(WIN32) || defined(__APPLE__) || USE_BITMAIN_SOC
  117. void cgsleep_ms_r(cgtimer_t *ts_start, int ms);
  118. void cgsleep_us_r(cgtimer_t *ts_start, int64_t us);
  119. #else
  120. int cgsleep_ms_r(cgtimer_t *ts_start, int ms);
  121. int64_t cgsleep_us_r(cgtimer_t *ts_start, int64_t us);
  122. #endif
  123. int cgtimer_to_ms(cgtimer_t *cgt);
  124. void cgtimer_sub(cgtimer_t *a, cgtimer_t *b, cgtimer_t *res);
  125. double us_tdiff(struct timeval *end, struct timeval *start);
  126. int ms_tdiff(struct timeval *end, struct timeval *start);
  127. double tdiff(struct timeval *end, struct timeval *start);
  128. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  129. bool sock_full(struct pool *pool);
  130. void _recalloc(void **ptr, size_t old, size_t new, const char *file, const char *func, const int line);
  131. #define recalloc(ptr, old, new) _recalloc((void *)&(ptr), old, new, __FILE__, __func__, __LINE__)
  132. char *recv_line(struct pool *pool);
  133. bool parse_method(struct pool *pool, char *s);
  134. void check_extranonce_option(struct pool *pool, char * url);
  135. bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port);
  136. void extranonce_subscribe_stratum(struct pool *pool);
  137. bool auth_stratum(struct pool *pool);
  138. bool initiate_stratum(struct pool *pool);
  139. bool restart_stratum(struct pool *pool);
  140. void suspend_stratum(struct pool *pool);
  141. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  142. void *realloc_strcat(char *ptr, char *s);
  143. void *str_text(char *ptr);
  144. void RenameThread(const char* name);
  145. void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int line);
  146. void _cgsem_post(cgsem_t *cgsem, const char *file, const char *func, const int line);
  147. void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int line);
  148. int _cgsem_mswait(cgsem_t *cgsem, int ms, const char *file, const char *func, const int line);
  149. void cgsem_reset(cgsem_t *cgsem);
  150. void cgsem_destroy(cgsem_t *cgsem);
  151. bool cg_completion_timeout(void *fn, void *fnarg, int timeout);
  152. void _cg_memcpy(void *dest, const void *src, unsigned int n, const char *file, const char *func, const int line);
  153. #define cgsem_init(_sem) _cgsem_init(_sem, __FILE__, __func__, __LINE__)
  154. #define cgsem_post(_sem) _cgsem_post(_sem, __FILE__, __func__, __LINE__)
  155. #define cgsem_wait(_sem) _cgsem_wait(_sem, __FILE__, __func__, __LINE__)
  156. #define cgsem_mswait(_sem, _timeout) _cgsem_mswait(_sem, _timeout, __FILE__, __func__, __LINE__)
  157. #define cg_memcpy(dest, src, n) _cg_memcpy(dest, src, n, __FILE__, __func__, __LINE__)
  158. /* Align a size_t to 4 byte boundaries for fussy arches */
  159. static inline void align_len(size_t *len)
  160. {
  161. if (*len % 4)
  162. *len += 4 - (*len % 4);
  163. }
  164. #endif /* __UTIL_H__ */