summaryrefslogtreecommitdiff
path: root/src/util/thread/thread_windows.c
blob: 3ea4d4aadff1fc5c8a27dd5aca45cd80861e91dd (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <al/lib.h>

#include "thread.h"

NTSTATUS(__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval);

void nn_thread_sleep(nn_os_tstamp delay)
{
    NtDelayExecution(false, &((LARGE_INTEGER){ .QuadPart = -(LONGLONG)(delay * 1e7L) }));
}

void nn_thread_create(struct nn_thread *thread, nn_thread_func func, void *userdata)
{
    thread->thread = CreateThread(NULL, 0, func, userdata, 0, NULL);
}

void nn_thread_set_priority(s32 policy, s32 priority)
{
    (void)policy;
    (void)priority;
}

void nn_thread_set_name(const char *name)
{
    (void)name;
}

void nn_thread_join(struct nn_thread *thread)
{
    WaitForSingleObject(thread->thread, INFINITE);
    CloseHandle(thread->thread);
}

#ifdef NAUNET_HAS_THREAD_CANCEL
void nn_thread_cancel(struct nn_thread *thread)
{
    (void)thread;
}

void nn_thread_setcanceltype(s32 type)
{
    (void)type;
}
#endif

/*
void nn_thread_detach(struct nn_thread *thread)
{
}
*/

/*
void nn_mutex_init(struct nn_mutex *mutex)
{
    mutex->mutex = CreateMutex(NULL, FALSE, NULL);
}

void nn_mutex_lock(struct nn_mutex *mutex)
{
    WaitForSingleObject(mutex->mutex, INFINITE);
}

void nn_mutex_unlock(struct nn_mutex *mutex)
{
    ReleaseMutex(mutex->mutex);
}

void nn_mutex_destroy(struct nn_mutex *mutex)
{
    CloseHandle(mutex->mutex);
}
*/

void nn_mutex_init(struct nn_mutex *mutex)
{
    InitializeCriticalSection(&mutex->mutex);
}

s32 nn_mutex_lock(struct nn_mutex *mutex)
{
    EnterCriticalSection(&mutex->mutex);
    return 0;
}

s32 nn_mutex_unlock(struct nn_mutex *mutex)
{
    LeaveCriticalSection(&mutex->mutex);
    return 0;
}

void nn_mutex_destroy(struct nn_mutex *mutex)
{
    DeleteCriticalSection(&mutex->mutex);
}

#ifdef NAUNET_WIN32_COMPAT_MODE
void nn_cond_init(struct nn_cond *cond)
{
    cond->condition = true;
    cond->event = CreateEvent(NULL, TRUE, FALSE, NULL);
}

void nn_cond_wait(struct nn_cond *cond, struct nn_mutex *mutex)
{
    al_assert(cond->condition);
    cond->condition = false;
    nn_mutex_unlock(mutex);
    WaitForSingleObject(cond->event, INFINITE);
    ResetEvent(cond->event);
    nn_mutex_lock(mutex);
}

bool nn_cond_is_waiting(struct nn_cond *cond)
{
    return !cond->condition;
}

void nn_cond_signal(struct nn_cond *cond)
{
    al_assert(!cond->condition);
    cond->condition = true;
    SetEvent(cond->event);
}

void nn_cond_destroy(struct nn_cond *cond)
{
    CloseHandle(cond->event);
}
#else
void nn_cond_init(struct nn_cond *cond)
{
    cond->condition = true;
    InitializeConditionVariable(&cond->cond);
}

void nn_cond_wait(struct nn_cond *cond, struct nn_mutex *mutex)
{
    cond->condition = false;
    while (!cond->condition) {
        SleepConditionVariableCS(&cond->cond, &mutex->mutex, INFINITE);
    }
}

bool nn_cond_is_waiting(struct nn_cond *cond)
{
    return !cond->condition;
}

void nn_cond_signal(struct nn_cond *cond)
{
    cond->condition = true;
    WakeConditionVariable(&cond->cond);
}

void nn_cond_destroy(struct nn_cond *cond)
{
    (void)cond;
}
#endif