2011-10-09 22:38:35 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010-2011 x264 project
|
|
|
|
*
|
|
|
|
* Authors: Steven Walters <kemuri9@gmail.com>
|
|
|
|
* Pegasys Inc. <http://www.pegasys-inc.com>
|
|
|
|
*
|
2013-05-24 13:32:18 +03:00
|
|
|
* This file is part of FFmpeg.
|
2011-10-09 22:38:35 +03:00
|
|
|
*
|
2013-05-24 13:32:18 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2011-10-09 22:38:35 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2013-05-24 13:32:18 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2011-10-09 22:38:35 +03:00
|
|
|
* 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
|
2013-05-24 13:32:18 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2011-10-09 22:38:35 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* w32threads to pthreads wrapper
|
|
|
|
*/
|
|
|
|
|
2016-01-29 04:25:13 +02:00
|
|
|
#ifndef COMPAT_W32PTHREADS_H
|
|
|
|
#define COMPAT_W32PTHREADS_H
|
2011-10-09 22:38:35 +03:00
|
|
|
|
|
|
|
/* Build up a pthread-like API using underlying Windows API. Have only static
|
|
|
|
* methods so as to not conflict with a potentially linked in pthread-win32
|
|
|
|
* library.
|
|
|
|
* As most functions here are used without checking return values,
|
|
|
|
* only implement return values as necessary. */
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <process.h>
|
2016-12-07 07:55:35 +02:00
|
|
|
#include <time.h>
|
2011-10-09 22:38:35 +03:00
|
|
|
|
2014-08-14 15:40:19 +03:00
|
|
|
#include "libavutil/attributes.h"
|
2012-07-14 02:05:06 +03:00
|
|
|
#include "libavutil/common.h"
|
2012-08-15 00:17:20 +03:00
|
|
|
#include "libavutil/internal.h"
|
|
|
|
#include "libavutil/mem.h"
|
2016-12-07 07:55:35 +02:00
|
|
|
#include "libavutil/time.h"
|
2012-07-14 02:05:06 +03:00
|
|
|
|
2012-09-27 11:19:53 +03:00
|
|
|
typedef struct pthread_t {
|
2011-10-09 22:38:35 +03:00
|
|
|
void *handle;
|
|
|
|
void *(*func)(void* arg);
|
|
|
|
void *arg;
|
|
|
|
void *ret;
|
|
|
|
} pthread_t;
|
|
|
|
|
2017-12-21 21:23:14 +02:00
|
|
|
/* use light weight mutex/condition variable API for Windows Vista and later */
|
|
|
|
typedef SRWLOCK pthread_mutex_t;
|
2014-10-09 19:20:00 +03:00
|
|
|
typedef CONDITION_VARIABLE pthread_cond_t;
|
2017-12-21 21:23:14 +02:00
|
|
|
|
|
|
|
#define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
|
|
|
|
#define PTHREAD_COND_INITIALIZER CONDITION_VARIABLE_INIT
|
2011-10-09 22:38:35 +03:00
|
|
|
|
2014-08-06 09:52:38 +03:00
|
|
|
#define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
|
|
|
|
#define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
|
2011-10-09 22:38:35 +03:00
|
|
|
|
2020-03-02 22:48:41 +02:00
|
|
|
#define PTHREAD_CANCEL_ENABLE 1
|
|
|
|
#define PTHREAD_CANCEL_DISABLE 0
|
|
|
|
|
2023-08-01 23:48:50 +02:00
|
|
|
#if HAVE_WINRT
|
|
|
|
#define THREADFUNC_RETTYPE DWORD
|
|
|
|
#else
|
|
|
|
#define THREADFUNC_RETTYPE unsigned
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static av_unused THREADFUNC_RETTYPE
|
|
|
|
__stdcall attribute_align_arg win32thread_worker(void *arg)
|
2011-10-09 22:38:35 +03:00
|
|
|
{
|
2017-04-13 11:38:02 +02:00
|
|
|
pthread_t *h = (pthread_t*)arg;
|
2011-10-09 22:38:35 +03:00
|
|
|
h->ret = h->func(h->arg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-14 15:40:19 +03:00
|
|
|
static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
|
|
|
|
void *(*start_routine)(void*), void *arg)
|
2011-10-09 22:38:35 +03:00
|
|
|
{
|
|
|
|
thread->func = start_routine;
|
|
|
|
thread->arg = arg;
|
2015-09-29 12:11:03 +02:00
|
|
|
#if HAVE_WINRT
|
|
|
|
thread->handle = (void*)CreateThread(NULL, 0, win32thread_worker, thread,
|
|
|
|
0, NULL);
|
|
|
|
#else
|
2011-10-09 22:38:35 +03:00
|
|
|
thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
|
|
|
|
0, NULL);
|
2015-09-29 12:11:03 +02:00
|
|
|
#endif
|
2011-10-09 22:38:35 +03:00
|
|
|
return !thread->handle;
|
|
|
|
}
|
|
|
|
|
2015-09-30 05:41:33 +02:00
|
|
|
static av_unused int pthread_join(pthread_t thread, void **value_ptr)
|
2011-10-09 22:38:35 +03:00
|
|
|
{
|
|
|
|
DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
|
2015-09-30 05:41:33 +02:00
|
|
|
if (ret != WAIT_OBJECT_0) {
|
|
|
|
if (ret == WAIT_ABANDONED)
|
|
|
|
return EINVAL;
|
|
|
|
else
|
|
|
|
return EDEADLK;
|
|
|
|
}
|
2011-10-09 22:38:35 +03:00
|
|
|
if (value_ptr)
|
|
|
|
*value_ptr = thread.ret;
|
|
|
|
CloseHandle(thread.handle);
|
2015-09-30 05:41:33 +02:00
|
|
|
return 0;
|
2011-10-09 22:38:35 +03:00
|
|
|
}
|
|
|
|
|
2011-11-01 16:23:03 +03:00
|
|
|
static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
|
|
|
|
{
|
2017-12-21 21:23:14 +02:00
|
|
|
InitializeSRWLock(m);
|
2011-11-01 16:23:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static inline int pthread_mutex_destroy(pthread_mutex_t *m)
|
|
|
|
{
|
2017-12-21 21:23:14 +02:00
|
|
|
/* Unlocked SWR locks use no resources */
|
2011-11-01 16:23:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static inline int pthread_mutex_lock(pthread_mutex_t *m)
|
|
|
|
{
|
2017-12-21 21:23:14 +02:00
|
|
|
AcquireSRWLockExclusive(m);
|
2011-11-01 16:23:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static inline int pthread_mutex_unlock(pthread_mutex_t *m)
|
|
|
|
{
|
2017-12-21 21:23:14 +02:00
|
|
|
ReleaseSRWLockExclusive(m);
|
2011-11-01 16:23:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2011-10-09 22:38:35 +03:00
|
|
|
|
2015-10-07 15:10:59 +02:00
|
|
|
typedef INIT_ONCE pthread_once_t;
|
|
|
|
#define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
|
|
|
|
|
|
|
|
static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
|
|
|
|
{
|
|
|
|
BOOL pending = FALSE;
|
|
|
|
InitOnceBeginInitialize(once_control, 0, &pending, NULL);
|
|
|
|
if (pending)
|
|
|
|
init_routine();
|
|
|
|
InitOnceComplete(once_control, 0, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-09 23:49:22 +03:00
|
|
|
static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
|
|
|
|
{
|
|
|
|
InitializeConditionVariable(cond);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* native condition variables do not destroy */
|
2015-09-30 05:41:33 +02:00
|
|
|
static inline int pthread_cond_destroy(pthread_cond_t *cond)
|
2014-10-09 23:49:22 +03:00
|
|
|
{
|
2015-09-30 05:41:33 +02:00
|
|
|
return 0;
|
2014-10-09 23:49:22 +03:00
|
|
|
}
|
|
|
|
|
2015-09-30 05:41:33 +02:00
|
|
|
static inline int pthread_cond_broadcast(pthread_cond_t *cond)
|
2014-10-09 23:49:22 +03:00
|
|
|
{
|
|
|
|
WakeAllConditionVariable(cond);
|
2015-09-30 05:41:33 +02:00
|
|
|
return 0;
|
2014-10-09 23:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
|
|
|
|
{
|
2017-12-21 21:23:14 +02:00
|
|
|
SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
|
2014-10-09 23:49:22 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-07 07:55:35 +02:00
|
|
|
static inline int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
|
|
|
const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000;
|
|
|
|
DWORD t = av_clip64(abs_milli - av_gettime() / 1000, 0, UINT32_MAX);
|
|
|
|
|
|
|
|
if (!SleepConditionVariableSRW(cond, mutex, t, 0)) {
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
if (err == ERROR_TIMEOUT)
|
|
|
|
return ETIMEDOUT;
|
|
|
|
else
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-30 05:41:33 +02:00
|
|
|
static inline int pthread_cond_signal(pthread_cond_t *cond)
|
2014-10-09 23:49:22 +03:00
|
|
|
{
|
|
|
|
WakeConditionVariable(cond);
|
2015-09-30 05:41:33 +02:00
|
|
|
return 0;
|
2014-10-09 23:49:22 +03:00
|
|
|
}
|
|
|
|
|
2020-03-02 22:48:41 +02:00
|
|
|
static inline int pthread_setcancelstate(int state, int *oldstate)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-29 04:25:13 +02:00
|
|
|
#endif /* COMPAT_W32PTHREADS_H */
|