uvg266/src/threads.h

148 lines
5 KiB
C
Raw Normal View History

2014-05-05 05:54:39 +00:00
#ifndef THREADS_H_
#define THREADS_H_
/*****************************************************************************
2021-11-23 06:46:06 +00:00
* This file is part of uvg266 VVC encoder.
2014-05-05 05:54:39 +00:00
*
* Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
2014-05-05 05:54:39 +00:00
****************************************************************************/
/**
* \ingroup Threading
* \file
* Abstractions for operating system specific stuff.
*/
2016-03-30 09:41:37 +00:00
#include "global.h" // IWYU pragma: keep
2014-05-19 10:56:27 +00:00
2014-05-05 05:54:39 +00:00
#include <pthread.h>
#if defined(__GNUC__) && !defined(__MINGW32__)
2016-03-30 09:41:37 +00:00
#include <unistd.h> // IWYU pragma: export
#include <time.h> // IWYU pragma: export
2014-06-16 08:18:11 +00:00
2022-04-28 11:18:09 +00:00
#define UVG_CLOCK_T struct timespec
#ifdef __MACH__
// Workaround Mac OS not having clock_gettime.
// This needs to work with pthread_cond_timedwait.
# include <sys/time.h>
2022-04-28 11:18:09 +00:00
# define UVG_GET_TIME(clock_t) { \
struct timeval tv; \
gettimeofday(&tv, NULL); \
(clock_t)->tv_sec = tv.tv_sec; \
(clock_t)->tv_nsec = tv.tv_usec * 1000; \
}
#else
2022-04-28 11:18:09 +00:00
# define UVG_GET_TIME(clock_t) { clock_gettime(CLOCK_MONOTONIC, (clock_t)); }
#endif
2022-04-28 11:18:09 +00:00
#define UVG_CLOCK_T_AS_DOUBLE(ts) ((double)((ts).tv_sec) + (double)((ts).tv_nsec) / 1e9)
#define UVG_CLOCK_T_DIFF(start, stop) ((double)((stop).tv_sec - (start).tv_sec) + (double)((stop).tv_nsec - (start).tv_nsec) / 1e9)
2022-04-28 11:18:09 +00:00
#define UVG_ATOMIC_INC(ptr) __sync_add_and_fetch((volatile int32_t*)ptr, 1)
#define UVG_ATOMIC_DEC(ptr) __sync_add_and_fetch((volatile int32_t*)ptr, -1)
2014-05-05 05:54:39 +00:00
#else //__GNUC__
//TODO: we assume !GCC => Windows... this may be bad
2016-03-30 09:41:37 +00:00
#include <windows.h> // IWYU pragma: export
2022-04-28 11:18:09 +00:00
#define UVG_CLOCK_T struct _FILETIME
#define UVG_GET_TIME(clock_t) { GetSystemTimeAsFileTime(clock_t); }
// _FILETIME has 32bit low and high part of 64bit 100ns resolution timestamp (since 12:00 AM January 1, 1601)
2022-04-28 11:18:09 +00:00
#define UVG_CLOCK_T_AS_DOUBLE(ts) ((double)(((uint64_t)(ts).dwHighDateTime)<<32 | (uint64_t)(ts).dwLowDateTime) / 1e7)
#define UVG_CLOCK_T_DIFF(start, stop) ((double)((((uint64_t)(stop).dwHighDateTime)<<32 | (uint64_t)(stop).dwLowDateTime) - \
(((uint64_t)(start).dwHighDateTime)<<32 | (uint64_t)(start).dwLowDateTime)) / 1e7)
2022-04-28 11:18:09 +00:00
#define UVG_ATOMIC_INC(ptr) InterlockedIncrement((volatile LONG*)ptr)
#define UVG_ATOMIC_DEC(ptr) InterlockedDecrement((volatile LONG*)ptr)
2014-05-13 09:26:20 +00:00
2014-05-05 05:54:39 +00:00
#endif //__GNUC__
#ifdef __APPLE__
// POSIX semaphores are deprecated on Mac so we use Grand Central Dispatch
// semaphores instead.
#include <dispatch/dispatch.h>
2022-04-28 11:18:09 +00:00
typedef dispatch_semaphore_t uvg_sem_t;
2022-04-28 11:18:09 +00:00
static INLINE void uvg_sem_init(uvg_sem_t *sem, int value)
{
assert(value >= 0);
*sem = dispatch_semaphore_create(value);
}
2022-04-28 11:18:09 +00:00
static INLINE void uvg_sem_wait(uvg_sem_t *sem)
{
dispatch_semaphore_wait(*sem, DISPATCH_TIME_FOREVER);
}
2022-04-28 11:18:09 +00:00
static INLINE void uvg_sem_post(uvg_sem_t *sem)
{
dispatch_semaphore_signal(*sem);
}
2022-04-28 11:18:09 +00:00
static INLINE void uvg_sem_destroy(uvg_sem_t *sem)
{
// Do nothing for GCD semaphores.
}
#else
// Use POSIX semaphores.
#include <semaphore.h>
2022-04-28 11:18:09 +00:00
typedef sem_t uvg_sem_t;
2022-04-28 11:18:09 +00:00
static INLINE void uvg_sem_init(uvg_sem_t *sem, int value)
{
assert(value >= 0);
// Pthreads-w32 does not support process-shared semaphores, so pshared
// must always be zero.
int pshared = 0;
sem_init(sem, pshared, value);
}
2022-04-28 11:18:09 +00:00
static INLINE void uvg_sem_wait(uvg_sem_t *sem)
{
sem_wait(sem);
}
2022-04-28 11:18:09 +00:00
static INLINE void uvg_sem_post(uvg_sem_t *sem)
{
sem_post(sem);
}
2022-04-28 11:18:09 +00:00
static INLINE void uvg_sem_destroy(uvg_sem_t *sem)
{
sem_destroy(sem);
}
#endif
2016-03-30 09:41:37 +00:00
#endif //THREADS_H_