xref: /aosp_15_r20/external/libdav1d/src/win32/thread.c (revision c09093415860a1c2373dacd84c4fde00c507cdfd)
1*c0909341SAndroid Build Coastguard Worker /*
2*c0909341SAndroid Build Coastguard Worker  * Copyright © 2018-2021, VideoLAN and dav1d authors
3*c0909341SAndroid Build Coastguard Worker  * Copyright © 2018, Two Orioles, LLC
4*c0909341SAndroid Build Coastguard Worker  * All rights reserved.
5*c0909341SAndroid Build Coastguard Worker  *
6*c0909341SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
7*c0909341SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions are met:
8*c0909341SAndroid Build Coastguard Worker  *
9*c0909341SAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright notice, this
10*c0909341SAndroid Build Coastguard Worker  *    list of conditions and the following disclaimer.
11*c0909341SAndroid Build Coastguard Worker  *
12*c0909341SAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright notice,
13*c0909341SAndroid Build Coastguard Worker  *    this list of conditions and the following disclaimer in the documentation
14*c0909341SAndroid Build Coastguard Worker  *    and/or other materials provided with the distribution.
15*c0909341SAndroid Build Coastguard Worker  *
16*c0909341SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17*c0909341SAndroid Build Coastguard Worker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18*c0909341SAndroid Build Coastguard Worker  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19*c0909341SAndroid Build Coastguard Worker  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20*c0909341SAndroid Build Coastguard Worker  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21*c0909341SAndroid Build Coastguard Worker  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*c0909341SAndroid Build Coastguard Worker  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23*c0909341SAndroid Build Coastguard Worker  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*c0909341SAndroid Build Coastguard Worker  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25*c0909341SAndroid Build Coastguard Worker  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*c0909341SAndroid Build Coastguard Worker  */
27*c0909341SAndroid Build Coastguard Worker 
28*c0909341SAndroid Build Coastguard Worker #include "config.h"
29*c0909341SAndroid Build Coastguard Worker 
30*c0909341SAndroid Build Coastguard Worker #if defined(_WIN32)
31*c0909341SAndroid Build Coastguard Worker 
32*c0909341SAndroid Build Coastguard Worker #include <process.h>
33*c0909341SAndroid Build Coastguard Worker #include <stdlib.h>
34*c0909341SAndroid Build Coastguard Worker #include <windows.h>
35*c0909341SAndroid Build Coastguard Worker 
36*c0909341SAndroid Build Coastguard Worker #include "common/attributes.h"
37*c0909341SAndroid Build Coastguard Worker 
38*c0909341SAndroid Build Coastguard Worker #include "src/thread.h"
39*c0909341SAndroid Build Coastguard Worker 
40*c0909341SAndroid Build Coastguard Worker static HRESULT (WINAPI *set_thread_description)(HANDLE, PCWSTR);
41*c0909341SAndroid Build Coastguard Worker 
dav1d_init_thread(void)42*c0909341SAndroid Build Coastguard Worker COLD void dav1d_init_thread(void) {
43*c0909341SAndroid Build Coastguard Worker #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
44*c0909341SAndroid Build Coastguard Worker     HANDLE kernel32 = GetModuleHandleW(L"kernel32.dll");
45*c0909341SAndroid Build Coastguard Worker     if (kernel32)
46*c0909341SAndroid Build Coastguard Worker         set_thread_description =
47*c0909341SAndroid Build Coastguard Worker             (void*)GetProcAddress(kernel32, "SetThreadDescription");
48*c0909341SAndroid Build Coastguard Worker #endif
49*c0909341SAndroid Build Coastguard Worker }
50*c0909341SAndroid Build Coastguard Worker 
51*c0909341SAndroid Build Coastguard Worker #undef dav1d_set_thread_name
dav1d_set_thread_name(const wchar_t * const name)52*c0909341SAndroid Build Coastguard Worker COLD void dav1d_set_thread_name(const wchar_t *const name) {
53*c0909341SAndroid Build Coastguard Worker     if (set_thread_description) /* Only available since Windows 10 1607 */
54*c0909341SAndroid Build Coastguard Worker         set_thread_description(GetCurrentThread(), name);
55*c0909341SAndroid Build Coastguard Worker }
56*c0909341SAndroid Build Coastguard Worker 
thread_entrypoint(void * const data)57*c0909341SAndroid Build Coastguard Worker static COLD unsigned __stdcall thread_entrypoint(void *const data) {
58*c0909341SAndroid Build Coastguard Worker     pthread_t *const t = data;
59*c0909341SAndroid Build Coastguard Worker     t->arg = t->func(t->arg);
60*c0909341SAndroid Build Coastguard Worker     return 0;
61*c0909341SAndroid Build Coastguard Worker }
62*c0909341SAndroid Build Coastguard Worker 
dav1d_pthread_create(pthread_t * const thread,const pthread_attr_t * const attr,void * (* const func)(void *),void * const arg)63*c0909341SAndroid Build Coastguard Worker COLD int dav1d_pthread_create(pthread_t *const thread,
64*c0909341SAndroid Build Coastguard Worker                               const pthread_attr_t *const attr,
65*c0909341SAndroid Build Coastguard Worker                               void *(*const func)(void*), void *const arg)
66*c0909341SAndroid Build Coastguard Worker {
67*c0909341SAndroid Build Coastguard Worker     const unsigned stack_size = attr ? attr->stack_size : 0;
68*c0909341SAndroid Build Coastguard Worker     thread->func = func;
69*c0909341SAndroid Build Coastguard Worker     thread->arg = arg;
70*c0909341SAndroid Build Coastguard Worker     thread->h = (HANDLE)_beginthreadex(NULL, stack_size, thread_entrypoint, thread,
71*c0909341SAndroid Build Coastguard Worker                                        STACK_SIZE_PARAM_IS_A_RESERVATION, NULL);
72*c0909341SAndroid Build Coastguard Worker     return !thread->h;
73*c0909341SAndroid Build Coastguard Worker }
74*c0909341SAndroid Build Coastguard Worker 
dav1d_pthread_join(pthread_t * const thread,void ** const res)75*c0909341SAndroid Build Coastguard Worker COLD int dav1d_pthread_join(pthread_t *const thread, void **const res) {
76*c0909341SAndroid Build Coastguard Worker     if (WaitForSingleObject(thread->h, INFINITE))
77*c0909341SAndroid Build Coastguard Worker         return 1;
78*c0909341SAndroid Build Coastguard Worker 
79*c0909341SAndroid Build Coastguard Worker     if (res)
80*c0909341SAndroid Build Coastguard Worker         *res = thread->arg;
81*c0909341SAndroid Build Coastguard Worker 
82*c0909341SAndroid Build Coastguard Worker     return !CloseHandle(thread->h);
83*c0909341SAndroid Build Coastguard Worker }
84*c0909341SAndroid Build Coastguard Worker 
dav1d_pthread_once(pthread_once_t * const once_control,void (* const init_routine)(void))85*c0909341SAndroid Build Coastguard Worker COLD int dav1d_pthread_once(pthread_once_t *const once_control,
86*c0909341SAndroid Build Coastguard Worker                             void (*const init_routine)(void))
87*c0909341SAndroid Build Coastguard Worker {
88*c0909341SAndroid Build Coastguard Worker     BOOL pending = FALSE;
89*c0909341SAndroid Build Coastguard Worker 
90*c0909341SAndroid Build Coastguard Worker     if (InitOnceBeginInitialize(once_control, 0, &pending, NULL) != TRUE)
91*c0909341SAndroid Build Coastguard Worker         return 1;
92*c0909341SAndroid Build Coastguard Worker 
93*c0909341SAndroid Build Coastguard Worker     if (pending == TRUE)
94*c0909341SAndroid Build Coastguard Worker         init_routine();
95*c0909341SAndroid Build Coastguard Worker 
96*c0909341SAndroid Build Coastguard Worker     return !InitOnceComplete(once_control, 0, NULL);
97*c0909341SAndroid Build Coastguard Worker }
98*c0909341SAndroid Build Coastguard Worker 
99*c0909341SAndroid Build Coastguard Worker #endif
100