util.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /* cgminer specific unnamed semaphore implementations to cope with osx not
  46. * implementing them. */
  47. #ifdef __APPLE__
  48. struct cgsem {
  49. int pipefd[2];
  50. };
  51. typedef struct cgsem cgsem_t;
  52. #else
  53. typedef sem_t cgsem_t;
  54. #endif
  55. typedef struct timespec cgtimer_t;
  56. struct thr_info;
  57. struct pool;
  58. enum dev_reason;
  59. struct cgpu_info;
  60. int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  61. void thr_info_cancel(struct thr_info *thr);
  62. void cgtime(struct timeval *tv);
  63. void subtime(struct timeval *a, struct timeval *b);
  64. void addtime(struct timeval *a, struct timeval *b);
  65. bool time_more(struct timeval *a, struct timeval *b);
  66. bool time_less(struct timeval *a, struct timeval *b);
  67. void copy_time(struct timeval *dest, const struct timeval *src);
  68. void timespec_to_val(struct timeval *val, const struct timespec *spec);
  69. void timeval_to_spec(struct timespec *spec, const struct timeval *val);
  70. void us_to_timeval(struct timeval *val, int64_t us);
  71. void us_to_timespec(struct timespec *spec, int64_t us);
  72. void ms_to_timespec(struct timespec *spec, int64_t ms);
  73. void timeraddspec(struct timespec *a, const struct timespec *b);
  74. void cgsleep_ms(int ms);
  75. void cgsleep_us(int64_t us);
  76. void cgtimer_time(cgtimer_t *ts_start);
  77. #define cgsleep_prepare_r(ts_start) cgtimer_time(ts_start)
  78. void cgsleep_ms_r(cgtimer_t *ts_start, int ms);
  79. void cgsleep_us_r(cgtimer_t *ts_start, int64_t us);
  80. int cgtimer_to_ms(cgtimer_t *cgt);
  81. void cgtimer_sub(cgtimer_t *a, cgtimer_t *b, cgtimer_t *res);
  82. double us_tdiff(struct timeval *end, struct timeval *start);
  83. double tdiff(struct timeval *end, struct timeval *start);
  84. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  85. bool sock_full(struct pool *pool);
  86. char *recv_line(struct pool *pool);
  87. bool parse_method(struct pool *pool, char *s);
  88. bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port);
  89. bool auth_stratum(struct pool *pool);
  90. bool initiate_stratum(struct pool *pool);
  91. bool restart_stratum(struct pool *pool);
  92. void suspend_stratum(struct pool *pool);
  93. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  94. void *realloc_strcat(char *ptr, char *s);
  95. void *str_text(char *ptr);
  96. void RenameThread(const char* name);
  97. void _cgsem_init(cgsem_t *cgsem, const char *file, const char *func, const int line);
  98. void _cgsem_post(cgsem_t *cgsem, const char *file, const char *func, const int line);
  99. void _cgsem_wait(cgsem_t *cgsem, const char *file, const char *func, const int line);
  100. void _cgsem_destroy(cgsem_t *cgsem);
  101. #define cgsem_init(_sem) _cgsem_init(_sem, __FILE__, __func__, __LINE__)
  102. #define cgsem_post(_sem) _cgsem_post(_sem, __FILE__, __func__, __LINE__)
  103. #define cgsem_wait(_sem) _cgsem_wait(_sem, __FILE__, __func__, __LINE__)
  104. #define cgsem_destroy(_sem) _cgsem_destroy(_sem)
  105. /* Align a size_t to 4 byte boundaries for fussy arches */
  106. static inline void align_len(size_t *len)
  107. {
  108. if (*len % 4)
  109. *len += 4 - (*len % 4);
  110. }
  111. #endif /* __UTIL_H__ */