Newer
Older
project / modules / libsc / include / sc_threads.h
Nomura Kei on 26 Sep 2022 1 KB UPDATE
/**
 * @file      sc_thread.h
 * @bried     スレッド用ヘッダ
 * @author    Nomura Kei
 * @copyright 2003 - 2022  Nomura Kei
 */
#ifndef SC_THREAD_H
#define SC_THREAD_H

#include <sc.h>

#ifdef __STDC_NO_THREADS__
// スレッドの完全サポートがない


////////////////////////////////////////////////////////////////////////////////
//
// スレッド
//

#define thread_local
#define _Thread_local

enum
{
	thrd_success,		//< 成功を示します。
	thrd_nomem,			//< メモリ不足で失敗したことを示します。
	thrd_timedout,		//< タイムアウト
	thrd_busy,			//< リソースがいい知事的に利用できない。
	thrd_error			//< エラー発生
};

typedef int thrd_t;
typedef int (*thrd_start_t)(void*);

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



////////////////////////////////////////////////////////////////////////////////
//
// ミューテックス
//

enum
{
	mtx_plain,			//< シンプルで非再帰的なミューテックスが作成されます。
	mtx_recursive,		//< 再帰的ミューテックスが作成されます。
	mtx_timed			//< タイムアウトをサポートする非再帰的ミューテックスが作成されます。
};

#define ONCE_FLAG_INIT			(false)
typedef bool mtx_t;
#define call_once(flag, func) \
	do { if (!*(flag)) { func(); *(flag) = true; } while (0)

int mtx_init(mtx_t* mutex, int type);
int mtx_lock(mtx_t* mutex);
int mtx_timedlock(mtx_t* restrict mutex, const struct timespec time_point);
int mtx_trylock(mtx_t* mutex);
int mtx_unlock(mtx_t* mutex);
void mtx_unlock(mtx_t* mutex);


#else
// スレッドを完全サポートしている
#include <threads.h>

#endif	// __STDC_NO_THREADS___

#endif	// SC_THREAD_H