2014-11-23 20:25:05 +00:00
|
|
|
/*
|
2014-11-27 21:07:53 +00:00
|
|
|
* This file is part of FFmpeg.
|
2014-11-23 20:25:05 +00:00
|
|
|
*
|
2014-11-27 21:07:53 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2014-11-23 20:25:05 +00: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.
|
|
|
|
*
|
2014-11-27 21:07:53 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2014-11-23 20:25:05 +00: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
|
2014-11-27 21:07:53 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2014-11-23 20:25:05 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This header should only be used to simplify code where
|
|
|
|
// threading is optional, not as a generic threading abstraction.
|
|
|
|
|
|
|
|
#ifndef AVUTIL_THREAD_H
|
|
|
|
#define AVUTIL_THREAD_H
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2014-11-29 07:34:20 +00:00
|
|
|
#if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
|
2014-11-23 20:25:05 +00:00
|
|
|
|
|
|
|
#if HAVE_PTHREADS
|
|
|
|
#include <pthread.h>
|
2015-12-22 19:41:46 +00:00
|
|
|
|
|
|
|
#if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
|
2020-01-22 20:03:13 +00:00
|
|
|
#define ASSERT_PTHREAD_ABORT(func, ret) do { \
|
|
|
|
char errbuf[AV_ERROR_MAX_STRING_SIZE] = ""; \
|
|
|
|
av_log(NULL, AV_LOG_FATAL, AV_STRINGIFY(func) \
|
|
|
|
" failed with error: %s\n", \
|
|
|
|
av_make_error_string(errbuf, AV_ERROR_MAX_STRING_SIZE, \
|
|
|
|
AVERROR(ret))); \
|
|
|
|
abort(); \
|
|
|
|
} while (0)
|
|
|
|
|
2015-12-22 19:41:46 +00:00
|
|
|
#define ASSERT_PTHREAD_NORET(func, ...) do { \
|
|
|
|
int ret = func(__VA_ARGS__); \
|
2020-01-22 20:03:13 +00:00
|
|
|
if (ret) \
|
|
|
|
ASSERT_PTHREAD_ABORT(func, ret); \
|
2015-12-22 19:41:46 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ASSERT_PTHREAD(func, ...) do { \
|
|
|
|
ASSERT_PTHREAD_NORET(func, __VA_ARGS__); \
|
|
|
|
return 0; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static inline int strict_pthread_join(pthread_t thread, void **value_ptr)
|
|
|
|
{
|
|
|
|
ASSERT_PTHREAD(pthread_join, thread, value_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int strict_pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
|
|
|
|
{
|
|
|
|
if (attr) {
|
|
|
|
ASSERT_PTHREAD_NORET(pthread_mutex_init, mutex, attr);
|
|
|
|
} else {
|
|
|
|
pthread_mutexattr_t local_attr;
|
|
|
|
ASSERT_PTHREAD_NORET(pthread_mutexattr_init, &local_attr);
|
|
|
|
ASSERT_PTHREAD_NORET(pthread_mutexattr_settype, &local_attr, PTHREAD_MUTEX_ERRORCHECK);
|
|
|
|
ASSERT_PTHREAD_NORET(pthread_mutex_init, mutex, &local_attr);
|
|
|
|
ASSERT_PTHREAD_NORET(pthread_mutexattr_destroy, &local_attr);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int strict_pthread_mutex_destroy(pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
ASSERT_PTHREAD(pthread_mutex_destroy, mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int strict_pthread_mutex_lock(pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
ASSERT_PTHREAD(pthread_mutex_lock, mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int strict_pthread_mutex_unlock(pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
ASSERT_PTHREAD(pthread_mutex_unlock, mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int strict_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
|
|
|
|
{
|
|
|
|
ASSERT_PTHREAD(pthread_cond_init, cond, attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int strict_pthread_cond_destroy(pthread_cond_t *cond)
|
|
|
|
{
|
|
|
|
ASSERT_PTHREAD(pthread_cond_destroy, cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int strict_pthread_cond_signal(pthread_cond_t *cond)
|
|
|
|
{
|
|
|
|
ASSERT_PTHREAD(pthread_cond_signal, cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int strict_pthread_cond_broadcast(pthread_cond_t *cond)
|
|
|
|
{
|
|
|
|
ASSERT_PTHREAD(pthread_cond_broadcast, cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int strict_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
ASSERT_PTHREAD(pthread_cond_wait, cond, mutex);
|
|
|
|
}
|
|
|
|
|
2016-12-07 05:55:35 +00:00
|
|
|
static inline int strict_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
|
|
|
const struct timespec *abstime)
|
|
|
|
{
|
2020-01-22 20:03:13 +00:00
|
|
|
int ret = pthread_cond_timedwait(cond, mutex, abstime);
|
|
|
|
if (ret && ret != ETIMEDOUT)
|
|
|
|
ASSERT_PTHREAD_ABORT(pthread_cond_timedwait, ret);
|
|
|
|
return ret;
|
2016-12-07 05:55:35 +00:00
|
|
|
}
|
|
|
|
|
2015-12-22 19:41:46 +00:00
|
|
|
static inline int strict_pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
|
|
|
|
{
|
|
|
|
ASSERT_PTHREAD(pthread_once, once_control, init_routine);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define pthread_join strict_pthread_join
|
|
|
|
#define pthread_mutex_init strict_pthread_mutex_init
|
|
|
|
#define pthread_mutex_destroy strict_pthread_mutex_destroy
|
|
|
|
#define pthread_mutex_lock strict_pthread_mutex_lock
|
|
|
|
#define pthread_mutex_unlock strict_pthread_mutex_unlock
|
|
|
|
#define pthread_cond_init strict_pthread_cond_init
|
|
|
|
#define pthread_cond_destroy strict_pthread_cond_destroy
|
|
|
|
#define pthread_cond_signal strict_pthread_cond_signal
|
|
|
|
#define pthread_cond_broadcast strict_pthread_cond_broadcast
|
|
|
|
#define pthread_cond_wait strict_pthread_cond_wait
|
2016-12-07 05:55:35 +00:00
|
|
|
#define pthread_cond_timedwait strict_pthread_cond_timedwait
|
2015-12-22 19:41:46 +00:00
|
|
|
#define pthread_once strict_pthread_once
|
|
|
|
#endif
|
|
|
|
|
2014-11-29 07:34:20 +00:00
|
|
|
#elif HAVE_OS2THREADS
|
|
|
|
#include "compat/os2threads.h"
|
2014-11-23 20:25:05 +00:00
|
|
|
#else
|
2014-11-29 07:36:06 +00:00
|
|
|
#include "compat/w32pthreads.h"
|
2014-11-23 20:25:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define AVMutex pthread_mutex_t
|
2017-12-21 21:39:24 +00:00
|
|
|
#define AV_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
2014-11-23 20:25:05 +00:00
|
|
|
|
|
|
|
#define ff_mutex_init pthread_mutex_init
|
|
|
|
#define ff_mutex_lock pthread_mutex_lock
|
|
|
|
#define ff_mutex_unlock pthread_mutex_unlock
|
|
|
|
#define ff_mutex_destroy pthread_mutex_destroy
|
|
|
|
|
2015-10-07 20:43:38 +00:00
|
|
|
#define AVOnce pthread_once_t
|
|
|
|
#define AV_ONCE_INIT PTHREAD_ONCE_INIT
|
|
|
|
|
|
|
|
#define ff_thread_once(control, routine) pthread_once(control, routine)
|
|
|
|
|
2014-11-23 20:25:05 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define AVMutex char
|
2017-12-21 21:39:24 +00:00
|
|
|
#define AV_MUTEX_INITIALIZER 0
|
2014-11-23 20:25:05 +00:00
|
|
|
|
2017-03-31 17:04:56 +00:00
|
|
|
static inline int ff_mutex_init(AVMutex *mutex, const void *attr){ return 0; }
|
|
|
|
static inline int ff_mutex_lock(AVMutex *mutex){ return 0; }
|
|
|
|
static inline int ff_mutex_unlock(AVMutex *mutex){ return 0; }
|
|
|
|
static inline int ff_mutex_destroy(AVMutex *mutex){ return 0; }
|
2014-11-23 20:25:05 +00:00
|
|
|
|
2015-10-07 20:43:38 +00:00
|
|
|
#define AVOnce char
|
|
|
|
#define AV_ONCE_INIT 0
|
|
|
|
|
|
|
|
static inline int ff_thread_once(char *control, void (*routine)(void))
|
|
|
|
{
|
|
|
|
if (!*control) {
|
|
|
|
routine();
|
|
|
|
*control = 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-23 20:25:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* AVUTIL_THREAD_H */
|