util.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <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. int Inet_Pton(int af, const char *src, void *dst);
  37. #define INET_PTON Inet_Pton
  38. extern char *WSAErrorMsg(void);
  39. #define SOCKERRMSG WSAErrorMsg()
  40. /* Check for windows variants of the errors as well as when ming
  41. * decides to wrap the error into the errno equivalent. */
  42. static inline bool sock_blocks(void)
  43. {
  44. return (WSAGetLastError() == WSAEWOULDBLOCK || errno == EAGAIN);
  45. }
  46. static inline bool sock_timeout(void)
  47. {
  48. return (WSAGetLastError() == WSAETIMEDOUT || errno == ETIMEDOUT);
  49. }
  50. static inline bool interrupted(void)
  51. {
  52. return (WSAGetLastError() == WSAEINTR || errno == EINTR);
  53. }
  54. #ifndef SHUT_RDWR
  55. #define SHUT_RDWR SD_BOTH
  56. #endif
  57. #ifndef in_addr_t
  58. #define in_addr_t uint32_t
  59. #endif
  60. #endif
  61. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  62. #ifdef HAVE_LIBCURL
  63. #include <curl/curl.h>
  64. typedef curl_proxytype proxytypes_t;
  65. #else
  66. typedef int proxytypes_t;
  67. #endif /* HAVE_LIBCURL */
  68. /* cgminer locks, a write biased variant of rwlocks */
  69. struct cglock {
  70. pthread_mutex_t mutex;
  71. pthread_rwlock_t rwlock;
  72. };
  73. typedef struct cglock cglock_t;
  74. /* cgminer specific unnamed semaphore implementations to cope with osx not
  75. * implementing them. */
  76. #ifdef __APPLE__
  77. struct cgsem {
  78. int pipefd[2];
  79. };
  80. typedef struct cgsem cgsem_t;
  81. #else
  82. typedef sem_t cgsem_t;
  83. #endif
  84. #ifdef WIN32
  85. typedef LARGE_INTEGER cgtimer_t;
  86. #else
  87. typedef struct timespec cgtimer_t;
  88. #endif
  89. int no_yield(void);
  90. int (*selective_yield)(void);
  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. char *Strcasestr(char *haystack, const char *needle);
  114. char *Strsep(char **stringp, const char *delim);
  115. void cgsleep_ms(int ms);
  116. void cgsleep_us(int64_t us);
  117. void cgtimer_time(cgtimer_t *ts_start);
  118. #define cgsleep_prepare_r(ts_start) cgtimer_time(ts_start)
  119. void cgsleep_ms_r(cgtimer_t *ts_start, int ms);
  120. void cgsleep_us_r(cgtimer_t *ts_start, int64_t us);
  121. int cgtimer_to_ms(cgtimer_t *cgt);
  122. void cgtimer_sub(cgtimer_t *a, cgtimer_t *b, cgtimer_t *res);
  123. double us_tdiff(struct timeval *end, struct timeval *start);
  124. int ms_tdiff(struct timeval *end, struct timeval *start);
  125. double tdiff(struct timeval *end, struct timeval *start);
  126. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  127. bool sock_full(struct pool *pool);
  128. void _recalloc(void **ptr, size_t old, size_t new, const char *file, const char *func, const int line);
  129. #define recalloc(ptr, old, new) _recalloc((void *)&(ptr), old, new, __FILE__, __func__, __LINE__)
  130. char *recv_line(struct pool *pool);
  131. bool parse_method(struct pool *pool, char *s);
  132. bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port);
  133. bool subscribe_extranonce(struct pool *pool);
  134. //void extranonce_subscribe_stratum(struct pool *pool);
  135. bool auth_stratum(struct pool *pool);
  136. bool initiate_stratum(struct pool *pool);
  137. bool restart_stratum(struct pool *pool);
  138. void suspend_stratum(struct pool *pool);
  139. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  140. void *realloc_strcat(char *ptr, char *s);
  141. void *str_text(char *ptr);
  142. void RenameThread(const char* name);
  143. void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int line);
  144. void _cgsem_post(cgsem_t *cgsem, const char *file, const char *func, const int line);
  145. void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int line);
  146. int _cgsem_mswait(cgsem_t *cgsem, int ms, const char *file, const char *func, const int line);
  147. void cgsem_reset(cgsem_t *cgsem);
  148. void cgsem_destroy(cgsem_t *cgsem);
  149. bool cg_completion_timeout(void *fn, void *fnarg, int timeout);
  150. void _cg_memcpy(void *dest, const void *src, unsigned int n, const char *file, const char *func, const int line);
  151. #define cgsem_init(_sem) _cgsem_init(_sem, __FILE__, __func__, __LINE__)
  152. #define cgsem_post(_sem) _cgsem_post(_sem, __FILE__, __func__, __LINE__)
  153. #define cgsem_wait(_sem) _cgsem_wait(_sem, __FILE__, __func__, __LINE__)
  154. #define cgsem_mswait(_sem, _timeout) _cgsem_mswait(_sem, _timeout, __FILE__, __func__, __LINE__)
  155. #define cg_memcpy(dest, src, n) _cg_memcpy(dest, src, n, __FILE__, __func__, __LINE__)
  156. /* Align a size_t to 4 byte boundaries for fussy arches */
  157. static inline void align_len(size_t *len)
  158. {
  159. if (*len % 4)
  160. *len += 4 - (*len % 4);
  161. }
  162. #endif /* __UTIL_H__ */