/src/x265/source/common/threading.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /***************************************************************************** |
2 | | * Copyright (C) 2013-2020 MulticoreWare, Inc |
3 | | * |
4 | | * Authors: Steve Borho <steve@borho.org> |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation; either version 2 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. |
19 | | * |
20 | | * This program is also available under a commercial proprietary license. |
21 | | * For more information, contact us at license @ x265.com |
22 | | *****************************************************************************/ |
23 | | |
24 | | #include "common.h" |
25 | | #include "threading.h" |
26 | | #include "cpu.h" |
27 | | |
28 | | namespace X265_NS { |
29 | | // x265 private namespace |
30 | | |
31 | | #if X265_ARCH_X86 && !defined(X86_64) && ENABLE_ASSEMBLY && defined(__GNUC__) |
32 | | extern "C" intptr_t PFX(stack_align)(void (*func)(), ...); |
33 | | #define STACK_ALIGN(func, ...) PFX(stack_align)((void (*)())func, __VA_ARGS__) |
34 | | #else |
35 | 25.4k | #define STACK_ALIGN(func, ...) func(__VA_ARGS__) |
36 | | #endif |
37 | | |
38 | | #if NO_ATOMICS |
39 | | pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; |
40 | | |
41 | | int no_atomic_or(int* ptr, int mask) |
42 | | { |
43 | | pthread_mutex_lock(&g_mutex); |
44 | | int ret = *ptr; |
45 | | *ptr |= mask; |
46 | | pthread_mutex_unlock(&g_mutex); |
47 | | return ret; |
48 | | } |
49 | | |
50 | | int no_atomic_and(int* ptr, int mask) |
51 | | { |
52 | | pthread_mutex_lock(&g_mutex); |
53 | | int ret = *ptr; |
54 | | *ptr &= mask; |
55 | | pthread_mutex_unlock(&g_mutex); |
56 | | return ret; |
57 | | } |
58 | | |
59 | | int no_atomic_inc(int* ptr) |
60 | | { |
61 | | pthread_mutex_lock(&g_mutex); |
62 | | *ptr += 1; |
63 | | int ret = *ptr; |
64 | | pthread_mutex_unlock(&g_mutex); |
65 | | return ret; |
66 | | } |
67 | | |
68 | | int no_atomic_dec(int* ptr) |
69 | | { |
70 | | pthread_mutex_lock(&g_mutex); |
71 | | *ptr -= 1; |
72 | | int ret = *ptr; |
73 | | pthread_mutex_unlock(&g_mutex); |
74 | | return ret; |
75 | | } |
76 | | |
77 | | int no_atomic_add(int* ptr, int val) |
78 | | { |
79 | | pthread_mutex_lock(&g_mutex); |
80 | | *ptr += val; |
81 | | int ret = *ptr; |
82 | | pthread_mutex_unlock(&g_mutex); |
83 | | return ret; |
84 | | } |
85 | | #endif |
86 | | |
87 | | /* C shim for forced stack alignment */ |
88 | | static void stackAlignMain(Thread *instance) |
89 | 25.4k | { |
90 | | // defer processing to the virtual function implemented in the derived class |
91 | 25.4k | instance->threadMain(); |
92 | 25.4k | } |
93 | | |
94 | | #if _WIN32 |
95 | | |
96 | | static DWORD WINAPI ThreadShim(Thread *instance) |
97 | | { |
98 | | STACK_ALIGN(stackAlignMain, instance); |
99 | | |
100 | | return 0; |
101 | | } |
102 | | |
103 | | bool Thread::start() |
104 | | { |
105 | | DWORD threadId; |
106 | | |
107 | | thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadShim, this, 0, &threadId); |
108 | | |
109 | | return threadId > 0; |
110 | | } |
111 | | |
112 | | void Thread::stop() |
113 | | { |
114 | | if (thread) |
115 | | WaitForSingleObject(thread, INFINITE); |
116 | | } |
117 | | |
118 | | Thread::~Thread() |
119 | | { |
120 | | if (thread) |
121 | | CloseHandle(thread); |
122 | | } |
123 | | |
124 | | #else /* POSIX / pthreads */ |
125 | | |
126 | | static void *ThreadShim(void *opaque) |
127 | 25.4k | { |
128 | | // defer processing to the virtual function implemented in the derived class |
129 | 25.4k | Thread *instance = reinterpret_cast<Thread *>(opaque); |
130 | | |
131 | 25.4k | STACK_ALIGN(stackAlignMain, instance); |
132 | | |
133 | 25.4k | return NULL; |
134 | 25.4k | } |
135 | | |
136 | | bool Thread::start() |
137 | 25.4k | { |
138 | 25.4k | if (pthread_create(&thread, NULL, ThreadShim, this)) |
139 | 0 | { |
140 | 0 | thread = 0; |
141 | 0 | return false; |
142 | 0 | } |
143 | | |
144 | 25.4k | return true; |
145 | 25.4k | } |
146 | | |
147 | | void Thread::stop() |
148 | 25.4k | { |
149 | 25.4k | if (thread) |
150 | 25.4k | pthread_join(thread, NULL); |
151 | 25.4k | } |
152 | | |
153 | 25.4k | Thread::~Thread() {} |
154 | | |
155 | | #endif // if _WIN32 |
156 | | |
157 | | Thread::Thread() |
158 | 25.4k | { |
159 | 25.4k | thread = 0; |
160 | 25.4k | } |
161 | | |
162 | | } |