aboutsummaryrefslogtreecommitdiff
path: root/include/bh/internal/thread_win.h
blob: 57150f066c93c6a5ac3957751adc667b176a97fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <windows.h>
#include <process.h>

struct bh_thread_s
{
    HANDLE handle;
    int allocated;
};

typedef struct bh_thread_data_s
{
    bh_task_t *task;
    bh_thread_end_cb_t end;
} bh_thread_data_t;

struct bh_mutex_s
{
    CRITICAL_SECTION handle;
};

struct bh_semaphore_s
{
    HANDLE handle;
};

#if WINVER >= _WIN32_WINNT_VISTA
struct bh_cond_s
{
    CONDITION_VARIABLE handle;
};
#else
struct bh_cond_s
{
    bh_mutex_t lock;
    bh_semaphore_t wait;
    bh_semaphore_t done;
    int waiting;
    int signals;
};
#endif

int bh_thread_init_base(bh_thread_t *thread,
                        bh_task_t *task,
                        bh_thread_begin_cb_t begin,
                        bh_thread_end_cb_t end);

#define bh_thread_init(thread, task) \
    bh_thread_init_base((thread), (task), _beginthreadex, _endthreadex)

int bh_mutex_init(bh_mutex_t *mutex);
void bh_mutex_destroy(bh_mutex_t *mutex);

int bh_semaphore_init(bh_semaphore_t *semaphore, 
                      int count);
void bh_semaphore_destroy(bh_semaphore_t *semaphore);

int bh_cond_init(bh_cond_t *cond);
void bh_cond_destroy(bh_cond_t *cond);

int bh_thread_pool_init_base(bh_thread_pool_t *pool,
                             size_t size,
                             bh_thread_begin_cb_t begin,
                             bh_thread_end_cb_t end);

#define bh_thread_pool_init(pool, size) \
    bh_thread_pool_init_base((pool), (size), _beginthreadex, _endthreadex)

void bh_thread_pool_destroy(bh_thread_pool_t *pool);