util.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #elif defined WIN32
  24. #include <ws2tcpip.h>
  25. #include <winsock2.h>
  26. #define SOCKETTYPE SOCKET
  27. #define SOCKETFAIL(a) ((int)(a) == SOCKET_ERROR)
  28. #define INVSOCK INVALID_SOCKET
  29. #define INVINETADDR INADDR_NONE
  30. #define CLOSESOCKET closesocket
  31. extern char *WSAErrorMsg(void);
  32. #define SOCKERRMSG WSAErrorMsg()
  33. static inline bool sock_blocks(void)
  34. {
  35. return (WSAGetLastError() == WSAEWOULDBLOCK);
  36. }
  37. static inline bool sock_timeout(void)
  38. {
  39. return (errno == WSAETIMEDOUT);
  40. }
  41. #ifndef SHUT_RDWR
  42. #define SHUT_RDWR SD_BOTH
  43. #endif
  44. #ifndef in_addr_t
  45. #define in_addr_t uint32_t
  46. #endif
  47. #endif
  48. #if JANSSON_MAJOR_VERSION >= 2
  49. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  50. #else
  51. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  52. #endif
  53. #ifdef HAVE_LIBCURL
  54. typedef curl_proxytype proxytypes_t;
  55. #else
  56. typedef int proxytypes_t;
  57. #endif /* HAVE_LIBCURL */
  58. /* cgminer locks, a write biased variant of rwlocks */
  59. struct cglock {
  60. pthread_mutex_t mutex;
  61. pthread_rwlock_t rwlock;
  62. };
  63. typedef struct cglock cglock_t;
  64. /* cgminer specific unnamed semaphore implementations to cope with osx not
  65. * implementing them. */
  66. #ifdef __APPLE__
  67. struct cgsem {
  68. int pipefd[2];
  69. };
  70. typedef struct cgsem cgsem_t;
  71. #else
  72. typedef sem_t cgsem_t;
  73. #endif
  74. typedef struct timespec cgtimer_t;
  75. struct thr_info;
  76. struct pool;
  77. enum dev_reason;
  78. struct cgpu_info;
  79. int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  80. void thr_info_cancel(struct thr_info *thr);
  81. void cgtime(struct timeval *tv);
  82. void subtime(struct timeval *a, struct timeval *b);
  83. void addtime(struct timeval *a, struct timeval *b);
  84. bool time_more(struct timeval *a, struct timeval *b);
  85. bool time_less(struct timeval *a, struct timeval *b);
  86. void copy_time(struct timeval *dest, const struct timeval *src);
  87. void timespec_to_val(struct timeval *val, const struct timespec *spec);
  88. void timeval_to_spec(struct timespec *spec, const struct timeval *val);
  89. void us_to_timeval(struct timeval *val, int64_t us);
  90. void us_to_timespec(struct timespec *spec, int64_t us);
  91. void ms_to_timespec(struct timespec *spec, int64_t ms);
  92. void timeraddspec(struct timespec *a, const struct timespec *b);
  93. void cgsleep_ms(int ms);
  94. void cgsleep_us(int64_t us);
  95. void cgtimer_time(cgtimer_t *ts_start);
  96. #define cgsleep_prepare_r(ts_start) cgtimer_time(ts_start)
  97. void cgsleep_ms_r(cgtimer_t *ts_start, int ms);
  98. void cgsleep_us_r(cgtimer_t *ts_start, int64_t us);
  99. int cgtimer_to_ms(cgtimer_t *cgt);
  100. void cgtimer_sub(cgtimer_t *a, cgtimer_t *b, cgtimer_t *res);
  101. double us_tdiff(struct timeval *end, struct timeval *start);
  102. int ms_tdiff(struct timeval *end, struct timeval *start);
  103. double tdiff(struct timeval *end, struct timeval *start);
  104. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  105. bool sock_full(struct pool *pool);
  106. char *recv_line(struct pool *pool);
  107. bool parse_method(struct pool *pool, char *s);
  108. bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port);
  109. bool auth_stratum(struct pool *pool);
  110. bool initiate_stratum(struct pool *pool);
  111. bool restart_stratum(struct pool *pool);
  112. void suspend_stratum(struct pool *pool);
  113. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  114. void *realloc_strcat(char *ptr, char *s);
  115. void *str_text(char *ptr);
  116. void RenameThread(const char* name);
  117. void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int line);
  118. void _cgsem_post(cgsem_t *cgsem, const char *file, const char *func, const int line);
  119. void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int line);
  120. int _cgsem_mswait(cgsem_t *cgsem, int ms, const char *file, const char *func, const int line);
  121. void _cgsem_destroy(cgsem_t *cgsem);
  122. #define cgsem_init(_sem) _cgsem_init(_sem, __FILE__, __func__, __LINE__)
  123. #define cgsem_post(_sem) _cgsem_post(_sem, __FILE__, __func__, __LINE__)
  124. #define cgsem_wait(_sem) _cgsem_wait(_sem, __FILE__, __func__, __LINE__)
  125. #define cgsem_mswait(_sem, _timeout) _cgsem_mswait(_sem, _timeout, __FILE__, __func__, __LINE__)
  126. #define cgsem_destroy(_sem) _cgsem_destroy(_sem)
  127. /* Align a size_t to 4 byte boundaries for fussy arches */
  128. static inline void align_len(size_t *len)
  129. {
  130. if (*len % 4)
  131. *len += 4 - (*len % 4);
  132. }
  133. #endif /* __UTIL_H__ */