summaryrefslogtreecommitdiffstats
path: root/osdep/threads-win32.h
blob: ec90fe938728251ed606a640d460cd882361995e (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * This file is part of mpv.
 *
 * mpv is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * mpv is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <errno.h>
#include <process.h>
#include <windows.h>

#include "common/common.h"
#include "timer.h"

typedef struct {
    char use_cs;
    union {
        CRITICAL_SECTION cs;
        SRWLOCK srw;
    };
} mp_mutex;

typedef CONDITION_VARIABLE mp_cond;
typedef INIT_ONCE          mp_once;
typedef mp_mutex           mp_static_mutex;
typedef HANDLE             mp_thread;
typedef DWORD              mp_thread_id;

#define MP_STATIC_COND_INITIALIZER CONDITION_VARIABLE_INIT
#define MP_STATIC_MUTEX_INITIALIZER { .srw = SRWLOCK_INIT }
#define MP_STATIC_ONCE_INITIALIZER INIT_ONCE_STATIC_INIT

static inline int mp_mutex_init_type_internal(mp_mutex *mutex, enum mp_mutex_type mtype)
{
    mutex->use_cs = mtype == MP_MUTEX_RECURSIVE;
    if (mutex->use_cs)
        return !InitializeCriticalSectionEx(&mutex->cs, 0, 0);
    InitializeSRWLock(&mutex->srw);
    return 0;
}

static inline int mp_mutex_destroy(mp_mutex *mutex)
{
    if (mutex->use_cs)
        DeleteCriticalSection(&mutex->cs);
    return 0;
}

static inline int mp_mutex_lock(mp_mutex *mutex)
{
    if (mutex->use_cs) {
        EnterCriticalSection(&mutex->cs);
    } else {
        AcquireSRWLockExclusive(&mutex->srw);
    }
    return 0;
}

static inline int mp_mutex_trylock(mp_mutex *mutex)
{
    if (mutex->use_cs)
        return !TryEnterCriticalSection(&mutex->cs);
    return !TryAcquireSRWLockExclusive(&mutex->srw);
}

static inline int mp_mutex_unlock(mp_mutex *mutex)
{
    if (mutex->use_cs) {
        LeaveCriticalSection(&mutex->cs);
    } else {
        ReleaseSRWLockExclusive(&mutex->srw);
    }
    return 0;
}

static inline int mp_cond_init(mp_cond *cond)
{
    InitializeConditionVariable(cond);
    return 0;
}

static inline int mp_cond_destroy(mp_cond *cond)
{
    // condition variables are not destroyed
    (void) cond;
    return 0;
}

static inline int mp_cond_broadcast(mp_cond *cond)
{
    WakeAllConditionVariable(cond);
    return 0;
}

static inline int mp_cond_signal(mp_cond *cond)
{
    WakeConditionVariable(cond);
    return 0;
}

static inline int mp_cond_timedwait(mp_cond *cond, mp_mutex *mutex, int64_t timeout)
{
    timeout = MPCLAMP(timeout, 0, MP_TIME_MS_TO_NS(INFINITE)) / MP_TIME_MS_TO_NS(1);

    int ret = 0;
    int64_t hrt = mp_start_hires_timers(MP_TIME_MS_TO_NS(timeout));
    BOOL bRet;

    if (mutex->use_cs) {
        bRet = SleepConditionVariableCS(cond, &mutex->cs, timeout);
    } else {
        bRet = SleepConditionVariableSRW(cond, &mutex->srw, timeout, 0);
    }
    if (bRet == FALSE)
        ret = GetLastError() == ERROR_TIMEOUT ? ETIMEDOUT : EINVAL;

    mp_end_hires_timers(hrt);
    return ret;
}

static inline int mp_cond_wait(mp_cond *cond, mp_mutex *mutex)
{
    return mp_cond_timedwait(cond, mutex, MP_TIME_MS_TO_NS(INFINITE));
}

static inline int mp_cond_timedwait_until(mp_cond *cond, mp_mutex *mutex, int64_t until)
{
    return mp_cond_timedwait(cond, mutex, until - mp_time_ns());
}

static inline int mp_exec_once(mp_once *once, void (*init_routine)(void))
{
    BOOL pending;

    if (!InitOnceBeginInitialize(once, 0, &pending, NULL))
        abort();

    if (pending) {
        init_routine();
        InitOnceComplete(once, 0, NULL);
    }

    return 0;
}

#define MP_THREAD_VOID unsigned __stdcall
#define MP_THREAD_RETURN() return 0

static inline int mp_thread_create(mp_thread *thread,
                                   MP_THREAD_VOID (*fun)(void *),
                                   void *__restrict arg)
{
    *thread = (HANDLE) _beginthreadex(NULL, 0, fun, arg, 0, NULL);
    return *thread ? 0 : -1;
}

static inline int mp_thread_join(mp_thread thread)
{
    DWORD ret = WaitForSingleObject(thread, INFINITE);
    if (ret != WAIT_OBJECT_0)
        return ret == WAIT_ABANDONED ? EINVAL : EDEADLK;
    CloseHandle(thread);
    return 0;
}

static inline int mp_thread_join_id(mp_thread_id id)
{
    mp_thread thread = OpenThread(SYNCHRONIZE, FALSE, id);
    if (!thread)
        return ESRCH;
    int ret = mp_thread_join(thread);
    if (ret)
        CloseHandle(thread);
    return ret;
}

static inline int mp_thread_detach(mp_thread thread)
{
    return CloseHandle(thread) ? 0 : EINVAL;
}

#define mp_thread_current_id GetCurrentThreadId
#define mp_thread_id_equal(a, b) ((a) == (b))
#define mp_thread_get_id(thread) GetThreadId(thread)

// declared in io.h, which we don't want to pull in everywhere
wchar_t *mp_from_utf8(void *talloc_ctx, const char *s);
static inline void mp_thread_set_name(const char *name)
{
    HRESULT (WINAPI *pSetThreadDescription)(HANDLE, PCWSTR);
#if !HAVE_UWP
    HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
    if (!kernel32)
        return;
    pSetThreadDescription = (void *) GetProcAddress(kernel32, "SetThreadDescription");
    if (!pSetThreadDescription)
        return;
#else
    WINBASEAPI HRESULT WINAPI
    SetThreadDescription(HANDLE hThread, PCWSTR lpThreadDescription);
    pSetThreadDescription = &SetThreadDescription;
#endif
    wchar_t *wname = mp_from_utf8(NULL, name);
    pSetThreadDescription(GetCurrentThread(), wname);
    talloc_free(wname);
}

static inline int64_t mp_thread_cpu_time_ns(mp_thread_id thread)
{
    (void) thread;
    return 0;
}