Newer
Older
libkc / modules / include / kc_threads_win.h
Nomura Kei on 29 May 2024 1 KB update
  1. /**
  2. * @file kc_windows_thread.h
  3. * @brief Windows 用スレッドモジュールヘッダファイル
  4. * @copyright 2020 - 2024 Nomura Kei
  5. * @depends
  6. * kc.h
  7. */
  8. #ifndef KC_WINDOWS_THREAD_H
  9. #define KC_WINDOWS_THREAD_H
  10.  
  11. #include <kc.h>
  12. #if (KC_IS_WINDOWS)
  13. #include <process.h>
  14.  
  15. #ifdef __cplusplus
  16. extern "C"
  17. {
  18. namespace kc
  19. {
  20. using namespace std;
  21. #endif
  22.  
  23. #define thread_local _Thread_local
  24.  
  25. ////////////////////////////////////////////////////////////////////////
  26. //
  27. // Thread
  28. //
  29.  
  30. enum
  31. {
  32. thrd_success = 0, //!< 成功
  33. thrd_bussy = 1, //!< busy
  34. thrd_error = 2, //!< エラー発生
  35. thrd_nomem = 3, //!< メモリ不足
  36. thrd_timedout = 4 //!< タイムアウト発生
  37. };
  38.  
  39. typedef struct
  40. {
  41. HANDLE handle;
  42. DWORD thread_id;
  43. } thrd_t;
  44.  
  45. typedef int (*thrd_start_t)(void *);
  46. int thrd_create(thrd_t *thr, thrd_start_t func, void *arg);
  47. int thrd_join(thrd_t thr, int *res);
  48. int thrd_detach(thrd_t thr);
  49. thrd_t thrd_current(void);
  50. int thrd_equal(thrd_t lhs, thrd_t rhs);
  51. int thrd_sleep(const struct timespec *duration, struct timespec *remaining);
  52.  
  53. ////////////////////////////////////////////////////////////////////////
  54. //
  55. // mutex
  56. //
  57.  
  58. enum
  59. {
  60. mtx_plain = 0, //!< 通常
  61. mtx_recursive = 1, //!< 再帰
  62. mtx_timed = 2 //!< timed
  63. };
  64.  
  65. typedef struct
  66. {
  67. CRITICAL_SECTION cs;
  68. int type;
  69. } mtx_t;
  70.  
  71. int mtx_init(mtx_t *mtx, int type);
  72. void mtx_destroy(mtx_t *mtx);
  73. int mtx_lock(mtx_t *mtx);
  74. int mtx_unlock(mtx_t *mtx);
  75.  
  76. ////////////////////////////////////////////////////////////////////////
  77. //
  78. // cond
  79. //
  80.  
  81. typedef struct
  82. {
  83. CONDITION_VARIABLE cond;
  84. } cnd_t;
  85.  
  86. int cnd_init(cnd_t *cond);
  87. int cnd_signal(cnd_t *cond);
  88. int cnd_broadcast(cnd_t *cond);
  89. int cnd_wait(cnd_t *cond, mtx_t *mtx);
  90. int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts);
  91. void cnd_destroy(cnd_t *cond);
  92.  
  93. #endif // (KC_IS_WINDOWS)
  94. #ifdef __cplusplus
  95. } // namespace kc
  96. } // extern "C"
  97. #endif
  98. #endif // KC_SOCKET_H