util.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. struct thr_info;
  56. struct pool;
  57. enum dev_reason;
  58. struct cgpu_info;
  59. int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  60. void thr_info_freeze(struct thr_info *thr);
  61. void thr_info_cancel(struct thr_info *thr);
  62. void nmsleep(unsigned int msecs);
  63. void nusleep(unsigned int usecs);
  64. void cgtime(struct timeval *tv);
  65. void subtime(struct timeval *a, struct timeval *b);
  66. void addtime(struct timeval *a, struct timeval *b);
  67. bool time_more(struct timeval *a, struct timeval *b);
  68. bool time_less(struct timeval *a, struct timeval *b);
  69. void copy_time(struct timeval *dest, const struct timeval *src);
  70. double us_tdiff(struct timeval *end, struct timeval *start);
  71. double tdiff(struct timeval *end, struct timeval *start);
  72. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  73. bool sock_full(struct pool *pool);
  74. char *recv_line(struct pool *pool);
  75. bool parse_method(struct pool *pool, char *s);
  76. bool extract_sockaddr(struct pool *pool, char *url);
  77. bool auth_stratum(struct pool *pool);
  78. bool initiate_stratum(struct pool *pool);
  79. bool restart_stratum(struct pool *pool);
  80. void suspend_stratum(struct pool *pool);
  81. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  82. void *realloc_strcat(char *ptr, char *s);
  83. void *str_text(char *ptr);
  84. void RenameThread(const char* name);
  85. void cgsem_init(cgsem_t *cgsem);
  86. void cgsem_post(cgsem_t *cgsem);
  87. void cgsem_wait(cgsem_t *cgsem);
  88. void cgsem_destroy(cgsem_t *cgsem);
  89. /* Align a size_t to 4 byte boundaries for fussy arches */
  90. static inline void align_len(size_t *len)
  91. {
  92. if (*len % 4)
  93. *len += 4 - (*len % 4);
  94. }
  95. #endif /* __UTIL_H__ */