util.h 4.3 KB

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