TUT HEVC Encoder
Loading...
Searching...
No Matches
threads.h
Go to the documentation of this file.
1#ifndef THREADS_H_
2#define THREADS_H_
3/*****************************************************************************
4 * This file is part of Kvazaar HEVC encoder.
5 *
6 * Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without modification,
10 * are permitted provided that the following conditions are met:
11 *
12 * * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright notice, this
16 * list of conditions and the following disclaimer in the documentation and/or
17 * other materials provided with the distribution.
18 *
19 * * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
20 * contributors may be used to endorse or promote products derived from
21 * this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
33 ****************************************************************************/
34
41#include "global.h" // IWYU pragma: keep
42
43#include <pthread.h>
44
45#ifdef __APPLE__
46#include <AvailabilityMacros.h>
47#endif
48
49#if defined(__GNUC__) && !defined(__MINGW32__)
50#include <unistd.h> // IWYU pragma: export
51#include <time.h> // IWYU pragma: export
52
53#define KVZ_CLOCK_T struct timespec
54
55#ifdef __MACH__
56// Workaround Mac OS not having clock_gettime.
57// This needs to work with pthread_cond_timedwait.
58# include <sys/time.h>
59# define KVZ_GET_TIME(clock_t) { \
60 struct timeval tv; \
61 gettimeofday(&tv, NULL); \
62 (clock_t)->tv_sec = tv.tv_sec; \
63 (clock_t)->tv_nsec = tv.tv_usec * 1000; \
64 }
65#else
66# define KVZ_GET_TIME(clock_t) { clock_gettime(CLOCK_MONOTONIC, (clock_t)); }
67#endif
68
69#define KVZ_CLOCK_T_AS_DOUBLE(ts) ((double)((ts).tv_sec) + (double)((ts).tv_nsec) / 1e9)
70#define KVZ_CLOCK_T_DIFF(start, stop) ((double)((stop).tv_sec - (start).tv_sec) + (double)((stop).tv_nsec - (start).tv_nsec) / 1e9)
71
72#define KVZ_ATOMIC_INC(ptr) __sync_add_and_fetch((volatile int32_t*)ptr, 1)
73#define KVZ_ATOMIC_DEC(ptr) __sync_add_and_fetch((volatile int32_t*)ptr, -1)
74
75#else //__GNUC__
76//TODO: we assume !GCC => Windows... this may be bad
77#include <windows.h> // IWYU pragma: export
78
79#define KVZ_CLOCK_T struct _FILETIME
80#define KVZ_GET_TIME(clock_t) { GetSystemTimeAsFileTime(clock_t); }
81// _FILETIME has 32bit low and high part of 64bit 100ns resolution timestamp (since 12:00 AM January 1, 1601)
82#define KVZ_CLOCK_T_AS_DOUBLE(ts) ((double)(((uint64_t)(ts).dwHighDateTime)<<32 | (uint64_t)(ts).dwLowDateTime) / 1e7)
83#define KVZ_CLOCK_T_DIFF(start, stop) ((double)((((uint64_t)(stop).dwHighDateTime)<<32 | (uint64_t)(stop).dwLowDateTime) - \
84 (((uint64_t)(start).dwHighDateTime)<<32 | (uint64_t)(start).dwLowDateTime)) / 1e7)
85
86#define KVZ_ATOMIC_INC(ptr) InterlockedIncrement((volatile LONG*)ptr)
87#define KVZ_ATOMIC_DEC(ptr) InterlockedDecrement((volatile LONG*)ptr)
88
89#endif //__GNUC__
90
91#if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED > 1050 && !defined(__ppc__)
92// POSIX semaphores are deprecated on Mac so we use Grand Central Dispatch semaphores instead.
93// However GCD is supported only on 10.6+, and is not supported on any ppc, including 10.6 Rosetta.
94#include <dispatch/dispatch.h>
95typedef dispatch_semaphore_t kvz_sem_t;
96
97static INLINE void kvz_sem_init(kvz_sem_t *sem, int value)
98{
99 assert(value >= 0);
100 *sem = dispatch_semaphore_create(value);
101}
102
103static INLINE void kvz_sem_wait(kvz_sem_t *sem)
104{
105 dispatch_semaphore_wait(*sem, DISPATCH_TIME_FOREVER);
106}
107
108static INLINE void kvz_sem_post(kvz_sem_t *sem)
109{
110 dispatch_semaphore_signal(*sem);
111}
112
113
114static INLINE void kvz_sem_destroy(kvz_sem_t *sem)
115{
116 // Do nothing for GCD semaphores.
117}
118
119#else
120// Use POSIX semaphores. This is also a fallback for old Darwin.
121#include <semaphore.h>
122
123typedef sem_t kvz_sem_t;
124
125static INLINE void kvz_sem_init(kvz_sem_t *sem, int value)
126{
127 assert(value >= 0);
128 // Pthreads-w32 does not support process-shared semaphores, so pshared
129 // must always be zero.
130 int pshared = 0;
131 sem_init(sem, pshared, value);
132}
133
135{
136 sem_wait(sem);
137}
138
140{
141 sem_post(sem);
142}
143
145{
146 sem_destroy(sem);
147}
148
149#endif
150
151#endif //THREADS_H_
Header that is included in every other header.
#define INLINE
Definition global.h:240
sem_t kvz_sem_t
Definition threads.h:123
static void kvz_sem_wait(kvz_sem_t *sem)
Definition threads.h:134
static void kvz_sem_post(kvz_sem_t *sem)
Definition threads.h:139
static void kvz_sem_destroy(kvz_sem_t *sem)
Definition threads.h:144
static void kvz_sem_init(kvz_sem_t *sem, int value)
Definition threads.h:125