Newer
Older
libkc / modules / include / kc_threads_win.h
Nomura Kei on 29 May 1 KB update
/**
 * @file kc_windows_thread.h
 * @brief Windows 用スレッドモジュールヘッダファイル
 * @copyright  2020 - 2024  Nomura Kei
 * @depends
 *	kc.h
 */
#ifndef KC_WINDOWS_THREAD_H
#define KC_WINDOWS_THREAD_H

#include <kc.h>
#if (KC_IS_WINDOWS)
#include <process.h>

#ifdef __cplusplus
extern "C"
{
	namespace kc
	{
		using namespace std;
#endif

#define thread_local _Thread_local

		////////////////////////////////////////////////////////////////////////
		//
		// Thread
		//

		enum
		{
			thrd_success = 0, //!< 成功
			thrd_bussy = 1,	  //!< busy
			thrd_error = 2,	  //!< エラー発生
			thrd_nomem = 3,	  //!< メモリ不足
			thrd_timedout = 4 //!< タイムアウト発生
		};

		typedef struct
		{
			HANDLE handle;
			DWORD thread_id;
		} thrd_t;

		typedef int (*thrd_start_t)(void *);
		int thrd_create(thrd_t *thr, thrd_start_t func, void *arg);
		int thrd_join(thrd_t thr, int *res);
		int thrd_detach(thrd_t thr);
		thrd_t thrd_current(void);
		int thrd_equal(thrd_t lhs, thrd_t rhs);
		int thrd_sleep(const struct timespec *duration, struct timespec *remaining);

		////////////////////////////////////////////////////////////////////////
		//
		// mutex
		//

		enum
		{
			mtx_plain = 0,	   //!< 通常
			mtx_recursive = 1, //!< 再帰
			mtx_timed = 2	   //!< timed
		};

		typedef struct
		{
			CRITICAL_SECTION cs;
			int type;
		} mtx_t;

		int mtx_init(mtx_t *mtx, int type);
		void mtx_destroy(mtx_t *mtx);
		int mtx_lock(mtx_t *mtx);
		int mtx_unlock(mtx_t *mtx);

		////////////////////////////////////////////////////////////////////////
		//
		// cond
		//

		typedef struct
		{
			CONDITION_VARIABLE cond;
		} cnd_t;

		int cnd_init(cnd_t *cond);
		int cnd_signal(cnd_t *cond);
		int cnd_broadcast(cnd_t *cond);
		int cnd_wait(cnd_t *cond, mtx_t *mtx);
		int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts);
		void cnd_destroy(cnd_t *cond);

#endif // (KC_IS_WINDOWS)
#ifdef __cplusplus
	} // namespace kc
} // extern "C"
#endif
#endif // KC_SOCKET_H