util.h.bak 6.0 KB

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