2017-04-15 14:20:00 +00:00
|
|
|
/* Copyright (C) 2017 the mpv developers
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2015-01-01 14:10:42 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
2017-04-15 14:20:00 +00:00
|
|
|
|
2015-01-01 14:10:42 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
2017-04-10 11:16:15 +00:00
|
|
|
#include <assert.h>
|
2021-11-07 19:28:16 +00:00
|
|
|
#include <windows.h>
|
2015-01-01 14:10:42 +00:00
|
|
|
|
2023-09-15 14:52:27 +00:00
|
|
|
#include "common/common.h"
|
2021-09-15 14:04:17 +00:00
|
|
|
#include "osdep/timer.h" // mp_{start,end}_hires_timers
|
|
|
|
|
2015-01-01 14:10:42 +00:00
|
|
|
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
|
|
|
|
{
|
|
|
|
BOOL pending;
|
|
|
|
if (!InitOnceBeginInitialize(once_control, 0, &pending, NULL))
|
|
|
|
abort();
|
|
|
|
if (pending) {
|
|
|
|
init_routine();
|
|
|
|
InitOnceComplete(once_control, 0, NULL);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_destroy(pthread_mutex_t *mutex)
|
|
|
|
{
|
2016-05-24 16:45:40 +00:00
|
|
|
if (mutex->use_cs)
|
|
|
|
DeleteCriticalSection(&mutex->lock.cs);
|
2015-01-01 14:10:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_init(pthread_mutex_t *restrict mutex,
|
|
|
|
const pthread_mutexattr_t *restrict attr)
|
|
|
|
{
|
2016-05-24 16:45:40 +00:00
|
|
|
mutex->use_cs = attr && (*attr & PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
if (mutex->use_cs) {
|
|
|
|
InitializeCriticalSection(&mutex->lock.cs);
|
|
|
|
} else {
|
|
|
|
InitializeSRWLock(&mutex->lock.srw);
|
|
|
|
}
|
2015-01-01 14:10:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_lock(pthread_mutex_t *mutex)
|
|
|
|
{
|
2016-05-24 16:45:40 +00:00
|
|
|
if (mutex->use_cs) {
|
|
|
|
EnterCriticalSection(&mutex->lock.cs);
|
|
|
|
} else {
|
|
|
|
AcquireSRWLockExclusive(&mutex->lock.srw);
|
2015-01-01 14:10:42 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_unlock(pthread_mutex_t *mutex)
|
|
|
|
{
|
2016-05-24 16:45:40 +00:00
|
|
|
if (mutex->use_cs) {
|
|
|
|
LeaveCriticalSection(&mutex->lock.cs);
|
|
|
|
} else {
|
|
|
|
ReleaseSRWLockExclusive(&mutex->lock.srw);
|
|
|
|
}
|
2015-01-01 14:10:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cond_wait(pthread_cond_t *restrict cond,
|
|
|
|
pthread_mutex_t *restrict mutex,
|
|
|
|
DWORD ms)
|
|
|
|
{
|
2016-05-24 16:45:40 +00:00
|
|
|
BOOL res;
|
2021-09-15 14:04:17 +00:00
|
|
|
int hrt = mp_start_hires_timers(ms);
|
2016-05-24 16:45:40 +00:00
|
|
|
if (mutex->use_cs) {
|
|
|
|
res = SleepConditionVariableCS(cond, &mutex->lock.cs, ms);
|
|
|
|
} else {
|
|
|
|
res = SleepConditionVariableSRW(cond, &mutex->lock.srw, ms, 0);
|
|
|
|
}
|
2021-09-15 14:04:17 +00:00
|
|
|
mp_end_hires_timers(hrt);
|
2016-05-24 16:45:40 +00:00
|
|
|
return res ? 0 : ETIMEDOUT;
|
2015-01-01 14:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_cond_timedwait(pthread_cond_t *restrict cond,
|
|
|
|
pthread_mutex_t *restrict mutex,
|
|
|
|
const struct timespec *restrict abstime)
|
|
|
|
{
|
2023-09-15 14:52:27 +00:00
|
|
|
// mp time is not converted to realtime if internal pthread impl is used
|
|
|
|
int64_t now = mp_time_ns();
|
|
|
|
int64_t time_ns = abstime->tv_sec * UINT64_C(1000000000) + abstime->tv_nsec;
|
|
|
|
int64_t timeout_ms = (time_ns - now) / INT64_C(1000000);
|
|
|
|
return cond_wait(cond, mutex, MPCLAMP(timeout_ms, 0, INFINITE));
|
2015-01-01 14:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_cond_wait(pthread_cond_t *restrict cond,
|
|
|
|
pthread_mutex_t *restrict mutex)
|
|
|
|
{
|
|
|
|
return cond_wait(cond, mutex, INFINITE);
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:16:15 +00:00
|
|
|
static pthread_mutex_t pthread_table_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static struct m_thread_info *pthread_table;
|
|
|
|
size_t pthread_table_num;
|
|
|
|
|
2015-01-01 14:10:42 +00:00
|
|
|
struct m_thread_info {
|
2017-04-10 11:16:15 +00:00
|
|
|
DWORD id;
|
2015-01-01 14:10:42 +00:00
|
|
|
HANDLE handle;
|
|
|
|
void *(*user_fn)(void *);
|
|
|
|
void *user_arg;
|
|
|
|
void *res;
|
|
|
|
};
|
|
|
|
|
2017-04-10 11:16:15 +00:00
|
|
|
static struct m_thread_info *find_thread_info(DWORD id)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < pthread_table_num; n++) {
|
|
|
|
if (id == pthread_table[n].id)
|
|
|
|
return &pthread_table[n];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-01 14:10:42 +00:00
|
|
|
|
2017-04-10 11:16:15 +00:00
|
|
|
static void remove_thread_info(struct m_thread_info *info)
|
2015-01-01 14:10:42 +00:00
|
|
|
{
|
2017-04-10 11:16:15 +00:00
|
|
|
assert(pthread_table_num);
|
|
|
|
assert(info >= &pthread_table[0] && info < &pthread_table[pthread_table_num]);
|
|
|
|
|
|
|
|
pthread_table[info - pthread_table] = pthread_table[pthread_table_num - 1];
|
|
|
|
pthread_table_num -= 1;
|
|
|
|
|
|
|
|
// Avoid upsetting leak detectors.
|
|
|
|
if (pthread_table_num == 0) {
|
|
|
|
free(pthread_table);
|
|
|
|
pthread_table = NULL;
|
|
|
|
}
|
2015-01-01 14:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pthread_exit(void *retval)
|
|
|
|
{
|
2017-04-10 11:16:15 +00:00
|
|
|
pthread_mutex_lock(&pthread_table_lock);
|
|
|
|
struct m_thread_info *info = find_thread_info(pthread_self());
|
|
|
|
assert(info); // not started with pthread_create, or pthread_join() race
|
|
|
|
info->res = retval;
|
|
|
|
if (!info->handle)
|
|
|
|
remove_thread_info(info); // detached case
|
|
|
|
pthread_mutex_unlock(&pthread_table_lock);
|
|
|
|
|
2015-01-01 14:10:42 +00:00
|
|
|
ExitThread(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_join(pthread_t thread, void **retval)
|
|
|
|
{
|
2017-04-10 11:16:15 +00:00
|
|
|
pthread_mutex_lock(&pthread_table_lock);
|
|
|
|
struct m_thread_info *info = find_thread_info(thread);
|
|
|
|
assert(info); // not started with pthread_create, or pthread_join() race
|
|
|
|
HANDLE h = info->handle;
|
|
|
|
assert(h); // thread was detached
|
|
|
|
pthread_mutex_unlock(&pthread_table_lock);
|
|
|
|
|
2015-01-01 14:10:42 +00:00
|
|
|
WaitForSingleObject(h, INFINITE);
|
2017-04-10 11:16:15 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&pthread_table_lock);
|
|
|
|
info = find_thread_info(thread);
|
|
|
|
assert(info);
|
|
|
|
assert(info->handle == h);
|
2015-01-01 14:10:42 +00:00
|
|
|
CloseHandle(h);
|
|
|
|
if (retval)
|
2017-04-10 11:16:15 +00:00
|
|
|
*retval = info->res;
|
|
|
|
remove_thread_info(info);
|
|
|
|
pthread_mutex_unlock(&pthread_table_lock);
|
|
|
|
|
2015-01-01 14:10:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_detach(pthread_t thread)
|
|
|
|
{
|
|
|
|
if (!pthread_equal(thread, pthread_self()))
|
|
|
|
abort(); // restriction of this wrapper
|
2017-04-10 11:16:15 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&pthread_table_lock);
|
|
|
|
struct m_thread_info *info = find_thread_info(thread);
|
|
|
|
assert(info); // not started with pthread_create
|
|
|
|
assert(info->handle); // already detached
|
|
|
|
CloseHandle(info->handle);
|
|
|
|
info->handle = NULL;
|
|
|
|
pthread_mutex_unlock(&pthread_table_lock);
|
|
|
|
|
2015-01-01 14:10:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI run_thread(LPVOID lpParameter)
|
|
|
|
{
|
2017-08-21 15:34:27 +00:00
|
|
|
pthread_mutex_lock(&pthread_table_lock);
|
2017-08-21 16:40:52 +00:00
|
|
|
struct m_thread_info *pinfo = find_thread_info(pthread_self());
|
|
|
|
assert(pinfo);
|
|
|
|
struct m_thread_info info = *pinfo;
|
2017-08-21 15:34:27 +00:00
|
|
|
pthread_mutex_unlock(&pthread_table_lock);
|
|
|
|
|
2017-08-21 16:40:52 +00:00
|
|
|
pthread_exit(info.user_fn(info.user_arg));
|
2015-01-01 14:10:42 +00:00
|
|
|
abort(); // not reached
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
|
|
|
void *(*start_routine) (void *), void *arg)
|
|
|
|
{
|
2017-04-10 11:16:15 +00:00
|
|
|
int res = 0;
|
|
|
|
pthread_mutex_lock(&pthread_table_lock);
|
|
|
|
void *nalloc =
|
|
|
|
realloc(pthread_table, (pthread_table_num + 1) * sizeof(pthread_table[0]));
|
|
|
|
if (!nalloc) {
|
|
|
|
res = EAGAIN;
|
|
|
|
goto done;
|
2015-01-01 14:10:42 +00:00
|
|
|
}
|
2017-04-10 11:16:15 +00:00
|
|
|
pthread_table = nalloc;
|
|
|
|
pthread_table_num += 1;
|
|
|
|
struct m_thread_info *info = &pthread_table[pthread_table_num - 1];
|
|
|
|
*info = (struct m_thread_info) {
|
|
|
|
.user_fn = start_routine,
|
|
|
|
.user_arg = arg,
|
|
|
|
};
|
2017-08-21 15:34:27 +00:00
|
|
|
info->handle = CreateThread(NULL, 0, run_thread, NULL, CREATE_SUSPENDED,
|
2017-04-10 11:16:15 +00:00
|
|
|
&info->id);
|
|
|
|
if (!info->handle) {
|
|
|
|
remove_thread_info(info);
|
|
|
|
res = EAGAIN;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
*thread = info->id;
|
|
|
|
ResumeThread(info->handle);
|
|
|
|
done:
|
|
|
|
pthread_mutex_unlock(&pthread_table_lock);
|
|
|
|
return res;
|
2015-01-01 14:10:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 13:33:35 +00:00
|
|
|
void pthread_set_name_np(pthread_t thread, const char *name)
|
|
|
|
{
|
2017-06-27 11:50:58 +00:00
|
|
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && defined(_PROCESSTHREADSAPI_H_)
|
2017-05-17 13:33:35 +00:00
|
|
|
HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
|
|
|
|
if (!kernel32)
|
|
|
|
return;
|
|
|
|
HRESULT (WINAPI *pSetThreadDescription)(HANDLE, PCWSTR) =
|
|
|
|
(void*)GetProcAddress(kernel32, "SetThreadDescription");
|
|
|
|
if (!pSetThreadDescription)
|
|
|
|
return;
|
|
|
|
|
2017-04-10 11:16:15 +00:00
|
|
|
HANDLE th = OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE, thread);
|
2017-05-17 13:33:35 +00:00
|
|
|
if (!th)
|
|
|
|
return;
|
|
|
|
wchar_t wname[80];
|
|
|
|
int wc = MultiByteToWideChar(CP_UTF8, 0, name, -1, wname,
|
|
|
|
sizeof(wname) / sizeof(wchar_t) - 1);
|
|
|
|
if (wc > 0) {
|
|
|
|
wname[wc] = L'\0';
|
|
|
|
pSetThreadDescription(th, wname);
|
|
|
|
}
|
|
|
|
CloseHandle(th);
|
2017-06-27 11:50:58 +00:00
|
|
|
#endif
|
2017-05-17 13:33:35 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 14:10:42 +00:00
|
|
|
int sem_init(sem_t *sem, int pshared, unsigned int value)
|
|
|
|
{
|
|
|
|
if (pshared)
|
|
|
|
abort(); // unsupported
|
|
|
|
pthread_mutex_init(&sem->lock, NULL);
|
|
|
|
pthread_cond_init(&sem->wakeup, NULL);
|
|
|
|
sem->value = value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_destroy(sem_t *sem)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&sem->lock);
|
|
|
|
pthread_cond_destroy(&sem->wakeup);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_wait(sem_t *sem)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&sem->lock);
|
|
|
|
while (!sem->value)
|
|
|
|
pthread_cond_wait(&sem->wakeup, &sem->lock);
|
|
|
|
sem->value -= 1;
|
|
|
|
pthread_mutex_unlock(&sem->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_trywait(sem_t *sem)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&sem->lock);
|
|
|
|
int r;
|
|
|
|
if (sem->value > 0) {
|
|
|
|
sem->value -= 1;
|
|
|
|
r = 0;
|
|
|
|
} else {
|
|
|
|
errno = EAGAIN;
|
|
|
|
r = -1;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&sem->lock);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&sem->lock);
|
|
|
|
while (!sem->value) {
|
|
|
|
int err = pthread_cond_timedwait(&sem->wakeup, &sem->lock, abs_timeout);
|
|
|
|
if (err) {
|
|
|
|
pthread_mutex_unlock(&sem->lock);
|
|
|
|
errno = err;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sem->value -= 1;
|
|
|
|
pthread_mutex_unlock(&sem->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_post(sem_t *sem)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&sem->lock);
|
|
|
|
sem->value += 1;
|
|
|
|
pthread_cond_broadcast(&sem->wakeup);
|
|
|
|
pthread_mutex_unlock(&sem->lock);
|
|
|
|
return 0;
|
|
|
|
}
|