]> git.sesse.net Git - x264/blob - common/win32thread.c
Add mb_info API for signalling constant macroblocks
[x264] / common / win32thread.c
1 /*****************************************************************************
2  * win32thread.c: windows threading
3  *****************************************************************************
4  * Copyright (C) 2010-2012 x264 project
5  *
6  * Authors: Steven Walters <kemuri9@gmail.com>
7  *          Pegasys Inc. <http://www.pegasys-inc.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22  *
23  * This program is also available under a commercial proprietary license.
24  * For more information, contact us at licensing@x264.com.
25  *****************************************************************************/
26
27 /* Microsoft's way of supporting systems with >64 logical cpus can be found at
28  * http://www.microsoft.com/whdc/system/Sysinternals/MoreThan64proc.mspx */
29
30 /* Based on the agreed standing that x264 does not need to utilize >64 logical cpus,
31  * this API does not detect nor utilize more than 64 cpus for systems that have them. */
32
33 #include "common.h"
34 #include <process.h>
35
36 /* number of times to spin a thread about to block on a locked mutex before retrying and sleeping if still locked */
37 #define X264_SPIN_COUNT 0
38
39 /* GROUP_AFFINITY struct */
40 typedef struct
41 {
42     ULONG_PTR mask; // KAFFINITY = ULONG_PTR
43     USHORT group;
44     USHORT reserved[3];
45 } x264_group_affinity_t;
46
47 typedef struct
48 {
49     /* global mutex for replacing MUTEX_INITIALIZER instances */
50     x264_pthread_mutex_t static_mutex;
51
52     /* function pointers to conditional variable API on windows 6.0+ kernels */
53     void (WINAPI *cond_broadcast)( x264_pthread_cond_t *cond );
54     void (WINAPI *cond_init)( x264_pthread_cond_t *cond );
55     void (WINAPI *cond_signal)( x264_pthread_cond_t *cond );
56     BOOL (WINAPI *cond_wait)( x264_pthread_cond_t *cond, x264_pthread_mutex_t *mutex, DWORD milliseconds );
57 } x264_win32thread_control_t;
58
59 static x264_win32thread_control_t thread_control;
60
61 /* _beginthreadex requires that the start routine is __stdcall */
62 static unsigned __stdcall x264_win32thread_worker( void *arg )
63 {
64     x264_pthread_t *h = arg;
65     h->ret = h->func( h->arg );
66     return 0;
67 }
68
69 int x264_pthread_create( x264_pthread_t *thread, const x264_pthread_attr_t *attr,
70                          void *(*start_routine)( void* ), void *arg )
71 {
72     thread->func   = start_routine;
73     thread->arg    = arg;
74     thread->handle = (void*)_beginthreadex( NULL, 0, x264_win32thread_worker, thread, 0, NULL );
75     return !thread->handle;
76 }
77
78 int x264_pthread_join( x264_pthread_t thread, void **value_ptr )
79 {
80     DWORD ret = WaitForSingleObject( thread.handle, INFINITE );
81     if( ret != WAIT_OBJECT_0 )
82         return -1;
83     if( value_ptr )
84         *value_ptr = thread.ret;
85     CloseHandle( thread.handle );
86     return 0;
87 }
88
89 int x264_pthread_mutex_init( x264_pthread_mutex_t *mutex, const x264_pthread_mutexattr_t *attr )
90 {
91     return !InitializeCriticalSectionAndSpinCount( mutex, X264_SPIN_COUNT );
92 }
93
94 int x264_pthread_mutex_destroy( x264_pthread_mutex_t *mutex )
95 {
96     DeleteCriticalSection( mutex );
97     return 0;
98 }
99
100 int x264_pthread_mutex_lock( x264_pthread_mutex_t *mutex )
101 {
102     static x264_pthread_mutex_t init = X264_PTHREAD_MUTEX_INITIALIZER;
103     if( !memcmp( mutex, &init, sizeof(x264_pthread_mutex_t) ) )
104         *mutex = thread_control.static_mutex;
105     EnterCriticalSection( mutex );
106     return 0;
107 }
108
109 int x264_pthread_mutex_unlock( x264_pthread_mutex_t *mutex )
110 {
111     LeaveCriticalSection( mutex );
112     return 0;
113 }
114
115 /* for pre-Windows 6.0 platforms we need to define and use our own condition variable and api */
116 typedef struct
117 {
118     x264_pthread_mutex_t mtx_broadcast;
119     x264_pthread_mutex_t mtx_waiter_count;
120     volatile int waiter_count;
121     HANDLE semaphore;
122     HANDLE waiters_done;
123     volatile int is_broadcast;
124 } x264_win32_cond_t;
125
126 int x264_pthread_cond_init( x264_pthread_cond_t *cond, const x264_pthread_condattr_t *attr )
127 {
128     if( thread_control.cond_init )
129     {
130         thread_control.cond_init( cond );
131         return 0;
132     }
133
134     /* non native condition variables */
135     x264_win32_cond_t *win32_cond = calloc( 1, sizeof(x264_win32_cond_t) );
136     if( !win32_cond )
137         return -1;
138     cond->ptr = win32_cond;
139     win32_cond->semaphore = CreateSemaphore( NULL, 0, 0x7fffffff, NULL );
140     if( !win32_cond->semaphore )
141         return -1;
142
143     if( x264_pthread_mutex_init( &win32_cond->mtx_waiter_count, NULL ) )
144         return -1;
145     if( x264_pthread_mutex_init( &win32_cond->mtx_broadcast, NULL ) )
146         return -1;
147
148     win32_cond->waiters_done = CreateEvent( NULL, FALSE, FALSE, NULL );
149     if( !win32_cond->waiters_done )
150         return -1;
151
152     return 0;
153 }
154
155 int x264_pthread_cond_destroy( x264_pthread_cond_t *cond )
156 {
157     /* native condition variables do not destroy */
158     if( thread_control.cond_init )
159         return 0;
160
161     /* non native condition variables */
162     x264_win32_cond_t *win32_cond = cond->ptr;
163     CloseHandle( win32_cond->semaphore );
164     CloseHandle( win32_cond->waiters_done );
165     x264_pthread_mutex_destroy( &win32_cond->mtx_broadcast );
166     x264_pthread_mutex_destroy( &win32_cond->mtx_waiter_count );
167     free( win32_cond );
168
169     return 0;
170 }
171
172 int x264_pthread_cond_broadcast( x264_pthread_cond_t *cond )
173 {
174     if( thread_control.cond_broadcast )
175     {
176         thread_control.cond_broadcast( cond );
177         return 0;
178     }
179
180     /* non native condition variables */
181     x264_win32_cond_t *win32_cond = cond->ptr;
182     x264_pthread_mutex_lock( &win32_cond->mtx_broadcast );
183     x264_pthread_mutex_lock( &win32_cond->mtx_waiter_count );
184     int have_waiter = 0;
185
186     if( win32_cond->waiter_count )
187     {
188         win32_cond->is_broadcast = 1;
189         have_waiter = 1;
190     }
191
192     if( have_waiter )
193     {
194         ReleaseSemaphore( win32_cond->semaphore, win32_cond->waiter_count, NULL );
195         x264_pthread_mutex_unlock( &win32_cond->mtx_waiter_count );
196         WaitForSingleObject( win32_cond->waiters_done, INFINITE );
197         win32_cond->is_broadcast = 0;
198     }
199     else
200         x264_pthread_mutex_unlock( &win32_cond->mtx_waiter_count );
201     return x264_pthread_mutex_unlock( &win32_cond->mtx_broadcast );
202 }
203
204 int x264_pthread_cond_signal( x264_pthread_cond_t *cond )
205 {
206     if( thread_control.cond_signal )
207     {
208         thread_control.cond_signal( cond );
209         return 0;
210     }
211
212     /* non-native condition variables */
213     x264_win32_cond_t *win32_cond = cond->ptr;
214
215     x264_pthread_mutex_lock( &win32_cond->mtx_broadcast );
216     x264_pthread_mutex_lock( &win32_cond->mtx_waiter_count );
217     int have_waiter = win32_cond->waiter_count;
218     x264_pthread_mutex_unlock( &win32_cond->mtx_waiter_count );
219
220     if( have_waiter )
221     {
222         ReleaseSemaphore( win32_cond->semaphore, 1, NULL );
223         WaitForSingleObject( win32_cond->waiters_done, INFINITE );
224     }
225
226     return x264_pthread_mutex_unlock( &win32_cond->mtx_broadcast );
227 }
228
229 int x264_pthread_cond_wait( x264_pthread_cond_t *cond, x264_pthread_mutex_t *mutex )
230 {
231     if( thread_control.cond_wait )
232         return !thread_control.cond_wait( cond, mutex, INFINITE );
233
234     /* non native condition variables */
235     x264_win32_cond_t *win32_cond = cond->ptr;
236
237     x264_pthread_mutex_lock( &win32_cond->mtx_broadcast );
238     x264_pthread_mutex_lock( &win32_cond->mtx_waiter_count );
239     win32_cond->waiter_count++;
240     x264_pthread_mutex_unlock( &win32_cond->mtx_waiter_count );
241     x264_pthread_mutex_unlock( &win32_cond->mtx_broadcast );
242
243     // unlock the external mutex
244     x264_pthread_mutex_unlock( mutex );
245     WaitForSingleObject( win32_cond->semaphore, INFINITE );
246
247     x264_pthread_mutex_lock( &win32_cond->mtx_waiter_count );
248     win32_cond->waiter_count--;
249     int last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
250     x264_pthread_mutex_unlock( &win32_cond->mtx_waiter_count );
251
252     if( last_waiter )
253         SetEvent( win32_cond->waiters_done );
254
255     // lock the external mutex
256     return x264_pthread_mutex_lock( mutex );
257 }
258
259 int x264_win32_threading_init( void )
260 {
261     /* find function pointers to API functions, if they exist */
262     HANDLE kernel_dll = GetModuleHandle( TEXT( "kernel32.dll" ) );
263     thread_control.cond_init = (void*)GetProcAddress( kernel_dll, "InitializeConditionVariable" );
264     if( thread_control.cond_init )
265     {
266         /* we're on a windows 6.0+ kernel, acquire the rest of the functions */
267         thread_control.cond_broadcast = (void*)GetProcAddress( kernel_dll, "WakeAllConditionVariable" );
268         thread_control.cond_signal = (void*)GetProcAddress( kernel_dll, "WakeConditionVariable" );
269         thread_control.cond_wait = (void*)GetProcAddress( kernel_dll, "SleepConditionVariableCS" );
270     }
271     return x264_pthread_mutex_init( &thread_control.static_mutex, NULL );
272 }
273
274 void x264_win32_threading_destroy( void )
275 {
276     x264_pthread_mutex_destroy( &thread_control.static_mutex );
277     memset( &thread_control, 0, sizeof(x264_win32thread_control_t) );
278 }
279
280 int x264_pthread_num_processors_np()
281 {
282     DWORD_PTR system_cpus, process_cpus = 0;
283     int cpus = 0;
284
285     /* GetProcessAffinityMask returns affinities of 0 when the process has threads in multiple processor groups.
286      * On platforms that support processor grouping, use GetThreadGroupAffinity to get the current thread's affinity instead. */
287 #if ARCH_X86_64
288     /* find function pointers to API functions specific to x86_64 platforms, if they exist */
289     HANDLE kernel_dll = GetModuleHandle( TEXT( "kernel32.dll" ) );
290     BOOL (*get_thread_affinity)( HANDLE thread, x264_group_affinity_t *group_affinity ) = (void*)GetProcAddress( kernel_dll, "GetThreadGroupAffinity" );
291     if( get_thread_affinity )
292     {
293         /* running on a platform that supports >64 logical cpus */
294         x264_group_affinity_t thread_affinity;
295         if( get_thread_affinity( GetCurrentThread(), &thread_affinity ) )
296             process_cpus = thread_affinity.mask;
297     }
298 #endif
299     if( !process_cpus )
300         GetProcessAffinityMask( GetCurrentProcess(), &process_cpus, &system_cpus );
301     for( DWORD_PTR bit = 1; bit; bit <<= 1 )
302         cpus += !!(process_cpus & bit);
303
304     return cpus ? cpus : 1;
305 }