util.h 4.0 KB

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