1 /*****************************************************************************
2 * thread.c : Win32 back-end for LibVLC
3 *****************************************************************************
4 * Copyright (C) 1999-2016 VLC authors and VideoLAN
6 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
7 * Samuel Hocevar <sam@zoy.org>
8 * Gildas Bazin <gbazin@netcourrier.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_atomic.h>
45 /*** Static mutex and condition variable ***/
46 static CRITICAL_SECTION super_mutex
;
47 static CONDITION_VARIABLE super_variable
;
49 #define IS_INTERRUPTIBLE (!VLC_WINSTORE_APP || _WIN32_WINNT >= 0x0A00)
52 static DWORD thread_key
;
60 vlc_cleanup_t
*cleaners
;
62 void *(*entry
) (void *);
68 CRITICAL_SECTION lock
;
72 /*** Condition variables (low-level) ***/
73 #if (_WIN32_WINNT < _WIN32_WINNT_VISTA)
74 static VOID (WINAPI
*InitializeConditionVariable_
)(PCONDITION_VARIABLE
);
75 #define InitializeConditionVariable InitializeConditionVariable_
76 static BOOL (WINAPI
*SleepConditionVariableCS_
)(PCONDITION_VARIABLE
,
77 PCRITICAL_SECTION
, DWORD
);
78 #define SleepConditionVariableCS SleepConditionVariableCS_
79 static VOID (WINAPI
*WakeAllConditionVariable_
)(PCONDITION_VARIABLE
);
80 #define WakeAllConditionVariable WakeAllConditionVariable_
82 static void WINAPI
DummyConditionVariable(CONDITION_VARIABLE
*cv
)
87 static BOOL WINAPI
SleepConditionVariableFallback(CONDITION_VARIABLE
*cv
,
92 LeaveCriticalSection(cs
);
93 SleepEx(ms
> 5 ? 5 : ms
, TRUE
);
94 EnterCriticalSection(cs
);
100 void vlc_mutex_init( vlc_mutex_t
*p_mutex
)
102 /* This creates a recursive mutex. This is OK as fast mutexes have
103 * no defined behavior in case of recursive locking. */
104 InitializeCriticalSection (&p_mutex
->mutex
);
105 p_mutex
->dynamic
= true;
108 void vlc_mutex_init_recursive( vlc_mutex_t
*p_mutex
)
110 InitializeCriticalSection( &p_mutex
->mutex
);
111 p_mutex
->dynamic
= true;
115 void vlc_mutex_destroy (vlc_mutex_t
*p_mutex
)
117 assert (p_mutex
->dynamic
);
118 DeleteCriticalSection (&p_mutex
->mutex
);
121 void vlc_mutex_lock (vlc_mutex_t
*p_mutex
)
123 if (!p_mutex
->dynamic
)
124 { /* static mutexes */
125 EnterCriticalSection(&super_mutex
);
126 while (p_mutex
->locked
)
128 p_mutex
->contention
++;
129 SleepConditionVariableCS(&super_variable
, &super_mutex
, INFINITE
);
130 p_mutex
->contention
--;
132 p_mutex
->locked
= true;
133 LeaveCriticalSection(&super_mutex
);
137 EnterCriticalSection (&p_mutex
->mutex
);
140 int vlc_mutex_trylock (vlc_mutex_t
*p_mutex
)
142 if (!p_mutex
->dynamic
)
143 { /* static mutexes */
146 EnterCriticalSection(&super_mutex
);
147 if (!p_mutex
->locked
)
149 p_mutex
->locked
= true;
152 LeaveCriticalSection(&super_mutex
);
156 return TryEnterCriticalSection (&p_mutex
->mutex
) ? 0 : EBUSY
;
159 void vlc_mutex_unlock (vlc_mutex_t
*p_mutex
)
161 if (!p_mutex
->dynamic
)
162 { /* static mutexes */
163 EnterCriticalSection(&super_mutex
);
164 assert (p_mutex
->locked
);
165 p_mutex
->locked
= false;
166 if (p_mutex
->contention
)
167 WakeAllConditionVariable(&super_variable
);
168 LeaveCriticalSection(&super_mutex
);
172 LeaveCriticalSection (&p_mutex
->mutex
);
176 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
177 # include <stdalign.h>
179 static inline HANDLE
*vlc_sem_handle_p(vlc_sem_t
*sem
)
181 /* NOTE: vlc_sem_t layout cannot easily depend on Windows version */
182 static_assert (sizeof (HANDLE
) <= sizeof (vlc_sem_t
), "Size mismatch!");
183 static_assert ((alignof (HANDLE
) % alignof (vlc_sem_t
)) == 0,
184 "Alignment mismatch");
185 return (HANDLE
*)sem
;
187 #define vlc_sem_handle(sem) (*vlc_sem_handle_p(sem))
189 void vlc_sem_init (vlc_sem_t
*sem
, unsigned value
)
191 HANDLE handle
= CreateSemaphore(NULL
, value
, 0x7fffffff, NULL
);
195 vlc_sem_handle(sem
) = handle
;
198 void vlc_sem_destroy (vlc_sem_t
*sem
)
200 CloseHandle(vlc_sem_handle(sem
));
203 int vlc_sem_post (vlc_sem_t
*sem
)
205 ReleaseSemaphore(vlc_sem_handle(sem
), 1, NULL
);
206 return 0; /* FIXME */
209 void vlc_sem_wait (vlc_sem_t
*sem
)
211 HANDLE handle
= vlc_sem_handle(sem
);
217 result
= WaitForSingleObjectEx(handle
, INFINITE
, TRUE
);
219 /* Semaphore abandoned would be a bug. */
220 assert(result
!= WAIT_ABANDONED_0
);
222 while (result
== WAIT_IO_COMPLETION
|| result
== WAIT_FAILED
);
226 /*** Thread-specific variables (TLS) ***/
230 void (*destroy
) (void *);
231 struct vlc_threadvar
*prev
;
232 struct vlc_threadvar
*next
;
233 } *vlc_threadvar_last
= NULL
;
235 int vlc_threadvar_create (vlc_threadvar_t
*p_tls
, void (*destr
) (void *))
237 struct vlc_threadvar
*var
= malloc (sizeof (*var
));
238 if (unlikely(var
== NULL
))
241 var
->id
= TlsAlloc();
242 if (var
->id
== TLS_OUT_OF_INDEXES
)
247 var
->destroy
= destr
;
251 EnterCriticalSection(&super_mutex
);
252 var
->prev
= vlc_threadvar_last
;
254 var
->prev
->next
= var
;
256 vlc_threadvar_last
= var
;
257 LeaveCriticalSection(&super_mutex
);
261 void vlc_threadvar_delete (vlc_threadvar_t
*p_tls
)
263 struct vlc_threadvar
*var
= *p_tls
;
265 EnterCriticalSection(&super_mutex
);
266 if (var
->prev
!= NULL
)
267 var
->prev
->next
= var
->next
;
269 if (var
->next
!= NULL
)
270 var
->next
->prev
= var
->prev
;
272 vlc_threadvar_last
= var
->prev
;
274 LeaveCriticalSection(&super_mutex
);
280 int vlc_threadvar_set (vlc_threadvar_t key
, void *value
)
282 int saved
= GetLastError ();
284 if (!TlsSetValue(key
->id
, value
))
291 void *vlc_threadvar_get (vlc_threadvar_t key
)
293 int saved
= GetLastError ();
294 void *value
= TlsGetValue (key
->id
);
300 static void vlc_threadvars_cleanup(void)
304 /* TODO: use RW lock or something similar */
305 EnterCriticalSection(&super_mutex
);
306 for (key
= vlc_threadvar_last
; key
!= NULL
; key
= key
->prev
)
308 void *value
= vlc_threadvar_get(key
);
309 if (value
!= NULL
&& key
->destroy
!= NULL
)
311 LeaveCriticalSection(&super_mutex
);
312 vlc_threadvar_set(key
, NULL
);
317 LeaveCriticalSection(&super_mutex
);
320 /*** Futeces^WAddress waits ***/
321 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
322 static BOOL (WINAPI
*WaitOnAddress_
)(VOID
volatile *, PVOID
, SIZE_T
, DWORD
);
323 #define WaitOnAddress (*WaitOnAddress_)
324 static VOID (WINAPI
*WakeByAddressAll_
)(PVOID
);
325 #define WakeByAddressAll (*WakeByAddressAll_)
326 static VOID (WINAPI
*WakeByAddressSingle_
)(PVOID
);
327 #define WakeByAddressSingle (*WakeByAddressSingle_)
329 static struct wait_addr_bucket
331 CRITICAL_SECTION lock
;
332 CONDITION_VARIABLE wait
;
333 } wait_addr_buckets
[32];
335 static struct wait_addr_bucket
*wait_addr_get_bucket(void volatile *addr
)
337 uintptr_t u
= (uintptr_t)addr
;
339 return wait_addr_buckets
+ ((u
>> 3) % ARRAY_SIZE(wait_addr_buckets
));
342 static void vlc_wait_addr_init(void)
344 for (size_t i
= 0; i
< ARRAY_SIZE(wait_addr_buckets
); i
++)
346 struct wait_addr_bucket
*bucket
= wait_addr_buckets
+ i
;
348 InitializeCriticalSection(&bucket
->lock
);
349 InitializeConditionVariable(&bucket
->wait
);
353 static void vlc_wait_addr_deinit(void)
355 for (size_t i
= 0; i
< ARRAY_SIZE(wait_addr_buckets
); i
++)
357 struct wait_addr_bucket
*bucket
= wait_addr_buckets
+ i
;
359 DeleteCriticalSection(&bucket
->lock
);
363 static BOOL WINAPI
WaitOnAddressFallback(void volatile *addr
, void *value
,
364 SIZE_T size
, DWORD ms
)
366 struct wait_addr_bucket
*bucket
= wait_addr_get_bucket(addr
);
367 uint64_t futex
, val
= 0;
370 EnterCriticalSection(&bucket
->lock
);
375 futex
= atomic_load_explicit((atomic_char
*)addr
,
376 memory_order_relaxed
);
377 val
= *(const char *)value
;
380 futex
= atomic_load_explicit((atomic_short
*)addr
,
381 memory_order_relaxed
);
382 val
= *(const short *)value
;
385 futex
= atomic_load_explicit((atomic_int
*)addr
,
386 memory_order_relaxed
);
387 val
= *(const int *)value
;
390 futex
= atomic_load_explicit((atomic_llong
*)addr
,
391 memory_order_relaxed
);
392 val
= *(const long long *)value
;
395 vlc_assert_unreachable();
399 ret
= SleepConditionVariableCS(&bucket
->wait
, &bucket
->lock
, ms
);
401 LeaveCriticalSection(&bucket
->lock
);
405 static void WINAPI
WakeByAddressFallback(void *addr
)
407 struct wait_addr_bucket
*bucket
= wait_addr_get_bucket(addr
);
409 /* Acquire the bucket critical section (only) to enforce proper sequencing.
410 * The critical section does not protect any actual memory object. */
411 EnterCriticalSection(&bucket
->lock
);
412 /* No other threads can hold the lock for this bucket while it is held
413 * here. Thus any other thread either:
414 * - is already sleeping in SleepConditionVariableCS(), and to be woken up
415 * by the following WakeAllConditionVariable(), or
416 * - has yet to retrieve the value at the wait address (with the
417 * 'switch (size)' block). */
418 LeaveCriticalSection(&bucket
->lock
);
419 /* At this point, other threads can retrieve the value at the wait address.
420 * But the value will have already been changed by our call site, thus
421 * (futex == val) will be false, and the threads will not go to sleep. */
423 /* Wake up any thread that was already sleeping. Since there are more than
424 * one wait address per bucket, all threads must be woken up :-/ */
425 WakeAllConditionVariable(&bucket
->wait
);
429 void vlc_addr_wait(void *addr
, unsigned val
)
431 WaitOnAddress(addr
, &val
, sizeof (val
), -1);
434 bool vlc_addr_timedwait(void *addr
, unsigned val
, mtime_t delay
)
436 delay
= (delay
+ 999) / 1000;
438 if (delay
> 0x7fffffff)
440 WaitOnAddress(addr
, &val
, sizeof (val
), 0x7fffffff);
441 return true; /* woke up early, claim spurious wake-up */
444 return WaitOnAddress(addr
, &val
, sizeof (val
), delay
);
447 void vlc_addr_signal(void *addr
)
449 WakeByAddressSingle(addr
);
452 void vlc_addr_broadcast(void *addr
)
454 WakeByAddressAll(addr
);
458 static void vlc_thread_destroy(vlc_thread_t th
)
460 DeleteCriticalSection(&th
->wait
.lock
);
464 static unsigned __stdcall
vlc_entry (void *p
)
466 struct vlc_thread
*th
= p
;
468 TlsSetValue(thread_key
, th
);
470 th
->data
= th
->entry (th
->data
);
471 TlsSetValue(thread_key
, NULL
);
473 if (th
->id
== NULL
) /* Detached thread */
474 vlc_thread_destroy(th
);
478 static int vlc_clone_attr (vlc_thread_t
*p_handle
, bool detached
,
479 void *(*entry
) (void *), void *data
, int priority
)
481 struct vlc_thread
*th
= malloc (sizeof (*th
));
482 if (unlikely(th
== NULL
))
486 th
->killable
= false; /* not until vlc_entry() ! */
487 atomic_init(&th
->killed
, false);
489 th
->wait
.addr
= NULL
;
490 InitializeCriticalSection(&th
->wait
.lock
);
492 /* When using the MSVCRT C library you have to use the _beginthreadex
493 * function instead of CreateThread, otherwise you'll end up with
494 * memory leaks and the signal functions not working (see Microsoft
495 * Knowledge Base, article 104641) */
496 uintptr_t h
= _beginthreadex (NULL
, 0, vlc_entry
, th
, 0, NULL
);
506 CloseHandle((HANDLE
)h
);
512 if (p_handle
!= NULL
)
516 SetThreadPriority (th
->id
, priority
);
521 int vlc_clone (vlc_thread_t
*p_handle
, void *(*entry
) (void *),
522 void *data
, int priority
)
524 return vlc_clone_attr (p_handle
, false, entry
, data
, priority
);
527 void vlc_join (vlc_thread_t th
, void **result
)
534 ret
= WaitForSingleObjectEx(th
->id
, INFINITE
, TRUE
);
535 assert(ret
!= WAIT_ABANDONED_0
);
537 while (ret
== WAIT_IO_COMPLETION
|| ret
== WAIT_FAILED
);
541 CloseHandle (th
->id
);
542 vlc_thread_destroy(th
);
545 int vlc_clone_detach (vlc_thread_t
*p_handle
, void *(*entry
) (void *),
546 void *data
, int priority
)
549 if (p_handle
== NULL
)
552 return vlc_clone_attr (p_handle
, true, entry
, data
, priority
);
555 vlc_thread_t
vlc_thread_self (void)
557 return TlsGetValue(thread_key
);
560 unsigned long vlc_thread_id (void)
562 return GetCurrentThreadId ();
565 int vlc_set_priority (vlc_thread_t th
, int priority
)
567 if (!SetThreadPriority (th
->id
, priority
))
572 /*** Thread cancellation ***/
575 /* APC procedure for thread cancellation */
576 static void CALLBACK
vlc_cancel_self (ULONG_PTR self
)
582 void vlc_cancel (vlc_thread_t th
)
584 atomic_store_explicit(&th
->killed
, true, memory_order_relaxed
);
586 EnterCriticalSection(&th
->wait
.lock
);
587 if (th
->wait
.addr
!= NULL
)
589 atomic_fetch_or_explicit(th
->wait
.addr
, 1, memory_order_relaxed
);
590 vlc_addr_broadcast(th
->wait
.addr
);
592 LeaveCriticalSection(&th
->wait
.lock
);
595 QueueUserAPC (vlc_cancel_self
, th
->id
, (uintptr_t)th
);
599 int vlc_savecancel (void)
601 struct vlc_thread
*th
= vlc_thread_self();
603 return false; /* Main thread - cannot be cancelled anyway */
605 int state
= th
->killable
;
606 th
->killable
= false;
610 void vlc_restorecancel (int state
)
612 struct vlc_thread
*th
= vlc_thread_self();
613 assert (state
== false || state
== true);
616 return; /* Main thread - cannot be cancelled anyway */
618 assert (!th
->killable
);
619 th
->killable
= state
!= 0;
622 void vlc_testcancel (void)
624 struct vlc_thread
*th
= vlc_thread_self();
626 return; /* Main thread - cannot be cancelled anyway */
629 if (!atomic_load_explicit(&th
->killed
, memory_order_relaxed
))
632 th
->killable
= true; /* Do not re-enter cancellation cleanup */
634 for (vlc_cleanup_t
*p
= th
->cleaners
; p
!= NULL
; p
= p
->next
)
637 th
->data
= NULL
; /* TODO: special value? */
638 if (th
->id
== NULL
) /* Detached thread */
639 vlc_thread_destroy(th
);
643 void vlc_control_cancel (int cmd
, ...)
645 /* NOTE: This function only modifies thread-specific data, so there is no
646 * need to lock anything. */
649 struct vlc_thread
*th
= vlc_thread_self();
651 return; /* Main thread - cannot be cancelled anyway */
656 case VLC_CLEANUP_PUSH
:
658 /* cleaner is a pointer to the caller stack, no need to allocate
659 * and copy anything. As a nice side effect, this cannot fail. */
660 vlc_cleanup_t
*cleaner
= va_arg (ap
, vlc_cleanup_t
*);
661 cleaner
->next
= th
->cleaners
;
662 th
->cleaners
= cleaner
;
666 case VLC_CLEANUP_POP
:
668 th
->cleaners
= th
->cleaners
->next
;
672 case VLC_CANCEL_ADDR_SET
:
674 void *addr
= va_arg(ap
, void *);
676 EnterCriticalSection(&th
->wait
.lock
);
677 assert(th
->wait
.addr
== NULL
);
678 th
->wait
.addr
= addr
;
679 LeaveCriticalSection(&th
->wait
.lock
);
683 case VLC_CANCEL_ADDR_CLEAR
:
685 void *addr
= va_arg(ap
, void *);
687 EnterCriticalSection(&th
->wait
.lock
);
688 assert(th
->wait
.addr
== addr
);
689 th
->wait
.addr
= NULL
;
690 LeaveCriticalSection(&th
->wait
.lock
);
700 #if (_WIN32_WINNT < _WIN32_WINNT_WIN7)
703 BOOL (*query
) (PULONGLONG
);
706 #if (_WIN32_WINNT < _WIN32_WINNT_VISTA)
709 ULONGLONG (*get
) (void);
716 #if !VLC_WINSTORE_APP
719 MMRESULT (WINAPI
*timeGetDevCaps
)(LPTIMECAPS ptc
,UINT cbtc
);
720 DWORD (WINAPI
*timeGetTime
)(void);
725 static mtime_t
mdate_interrupt (void)
730 #if (_WIN32_WINNT >= _WIN32_WINNT_WIN7)
731 ret
= QueryUnbiasedInterruptTime (&ts
);
733 ret
= clk
.interrupt
.query (&ts
);
738 /* hundreds of nanoseconds */
739 static_assert ((10000000 % CLOCK_FREQ
) == 0, "Broken frequencies ratio");
740 return ts
/ (10000000 / CLOCK_FREQ
);
743 static mtime_t
mdate_tick (void)
745 #if (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
746 ULONGLONG ts
= GetTickCount64 ();
748 ULONGLONG ts
= clk
.tick
.get ();
752 static_assert ((CLOCK_FREQ
% 1000) == 0, "Broken frequencies ratio");
753 return ts
* (CLOCK_FREQ
/ 1000);
755 #if !VLC_WINSTORE_APP
756 static mtime_t
mdate_multimedia (void)
758 DWORD ts
= clk
.multimedia
.timeGetTime ();
761 static_assert ((CLOCK_FREQ
% 1000) == 0, "Broken frequencies ratio");
762 return ts
* (CLOCK_FREQ
/ 1000);
766 static mtime_t
mdate_perf (void)
768 /* We don't need the real date, just the value of a high precision timer */
769 LARGE_INTEGER counter
;
770 if (!QueryPerformanceCounter (&counter
))
773 /* Convert to from (1/freq) to microsecond resolution */
774 /* We need to split the division to avoid 63-bits overflow */
775 lldiv_t d
= lldiv (counter
.QuadPart
, clk
.perf
.freq
.QuadPart
);
777 return (d
.quot
* 1000000) + ((d
.rem
* 1000000) / clk
.perf
.freq
.QuadPart
);
780 static mtime_t
mdate_wall (void)
785 #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) && !VLC_WINSTORE_APP
786 GetSystemTimePreciseAsFileTime (&ts
);
788 GetSystemTimeAsFileTime (&ts
);
790 s
.LowPart
= ts
.dwLowDateTime
;
791 s
.HighPart
= ts
.dwHighDateTime
;
792 /* hundreds of nanoseconds */
793 static_assert ((10000000 % CLOCK_FREQ
) == 0, "Broken frequencies ratio");
794 return s
.QuadPart
/ (10000000 / CLOCK_FREQ
);
797 static mtime_t
mdate_default(void)
799 vlc_threads_setup(NULL
);
803 static mtime_t (*mdate_selected
) (void) = mdate_default
;
807 return mdate_selected ();
810 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
811 void (mwait
)(mtime_t deadline
)
816 while ((delay
= (deadline
- mdate())) > 0)
818 delay
= (delay
+ 999) / 1000;
819 if (unlikely(delay
> 0x7fffffff))
822 SleepEx(delay
, TRUE
);
827 void (msleep
)(mtime_t delay
)
829 mwait (mdate () + delay
);
833 static BOOL
SelectClockSource(void *data
)
835 vlc_object_t
*obj
= data
;
838 const char *name
= "perf";
840 const char *name
= "multimedia";
844 str
= var_InheritString(obj
, "clock-source");
847 if (!strcmp (name
, "interrupt"))
849 msg_Dbg (obj
, "using interrupt time as clock source");
850 #if (_WIN32_WINNT < _WIN32_WINNT_WIN7)
851 HANDLE h
= GetModuleHandle (_T("kernel32.dll"));
852 if (unlikely(h
== NULL
))
854 clk
.interrupt
.query
= (void *)GetProcAddress (h
,
855 "QueryUnbiasedInterruptTime");
856 if (unlikely(clk
.interrupt
.query
== NULL
))
859 mdate_selected
= mdate_interrupt
;
862 if (!strcmp (name
, "tick"))
864 msg_Dbg (obj
, "using Windows time as clock source");
865 #if (_WIN32_WINNT < _WIN32_WINNT_VISTA)
866 HANDLE h
= GetModuleHandle (_T("kernel32.dll"));
867 if (unlikely(h
== NULL
))
869 clk
.tick
.get
= (void *)GetProcAddress (h
, "GetTickCount64");
870 if (unlikely(clk
.tick
.get
== NULL
))
873 mdate_selected
= mdate_tick
;
875 #if !VLC_WINSTORE_APP
877 if (!strcmp (name
, "multimedia"))
880 MMRESULT (WINAPI
* timeBeginPeriod
)(UINT
);
882 HMODULE hWinmm
= LoadLibrary(TEXT("winmm.dll"));
886 clk
.multimedia
.timeGetDevCaps
= (void*)GetProcAddress(hWinmm
, "timeGetDevCaps");
887 clk
.multimedia
.timeGetTime
= (void*)GetProcAddress(hWinmm
, "timeGetTime");
888 if (!clk
.multimedia
.timeGetDevCaps
|| !clk
.multimedia
.timeGetTime
)
891 msg_Dbg (obj
, "using multimedia timers as clock source");
892 if (clk
.multimedia
.timeGetDevCaps (&caps
, sizeof (caps
)) != MMSYSERR_NOERROR
)
894 msg_Dbg (obj
, " min period: %u ms, max period: %u ms",
895 caps
.wPeriodMin
, caps
.wPeriodMax
);
896 mdate_selected
= mdate_multimedia
;
898 timeBeginPeriod
= (void*)GetProcAddress(hWinmm
, "timeBeginPeriod");
899 if (timeBeginPeriod
!= NULL
)
904 if (!strcmp (name
, "perf"))
907 msg_Dbg (obj
, "using performance counters as clock source");
908 if (!QueryPerformanceFrequency (&clk
.perf
.freq
))
910 msg_Dbg (obj
, " frequency: %llu Hz", clk
.perf
.freq
.QuadPart
);
911 mdate_selected
= mdate_perf
;
914 if (!strcmp (name
, "wall"))
916 msg_Dbg (obj
, "using system time as clock source");
917 mdate_selected
= mdate_wall
;
921 msg_Err (obj
, "invalid clock source \"%s\"", name
);
928 size_t EnumClockSource (vlc_object_t
*obj
, const char *var
,
929 char ***vp
, char ***np
)
931 const size_t max
= 6;
932 char **values
= xmalloc (sizeof (*values
) * max
);
933 char **names
= xmalloc (sizeof (*names
) * max
);
936 #if (_WIN32_WINNT < _WIN32_WINNT_WIN7)
937 DWORD version
= LOWORD(GetVersion());
938 version
= (LOBYTE(version
) << 8) | (HIBYTE(version
) << 0);
941 values
[n
] = xstrdup ("");
942 names
[n
] = xstrdup (_("Auto"));
944 #if (_WIN32_WINNT < _WIN32_WINNT_WIN7)
945 if (version
>= 0x0601)
948 values
[n
] = xstrdup ("interrupt");
949 names
[n
] = xstrdup ("Interrupt time");
952 #if (_WIN32_WINNT < _WIN32_WINNT_VISTA)
953 if (version
>= 0x0600)
956 values
[n
] = xstrdup ("tick");
957 names
[n
] = xstrdup ("Windows time");
960 #if !VLC_WINSTORE_APP
961 values
[n
] = xstrdup ("multimedia");
962 names
[n
] = xstrdup ("Multimedia timers");
965 values
[n
] = xstrdup ("perf");
966 names
[n
] = xstrdup ("Performance counters");
968 values
[n
] = xstrdup ("wall");
969 names
[n
] = xstrdup ("System time (DANGEROUS!)");
974 (void) obj
; (void) var
;
980 unsigned vlc_GetCPUCount (void)
982 SYSTEM_INFO systemInfo
;
984 GetNativeSystemInfo(&systemInfo
);
986 return systemInfo
.dwNumberOfProcessors
;
990 /*** Initialization ***/
991 static CRITICAL_SECTION setup_lock
; /* FIXME: use INIT_ONCE */
993 void vlc_threads_setup(libvlc_int_t
*vlc
)
995 EnterCriticalSection(&setup_lock
);
996 if (mdate_selected
!= mdate_default
)
998 LeaveCriticalSection(&setup_lock
);
1002 if (!SelectClockSource((vlc
!= NULL
) ? VLC_OBJECT(vlc
) : NULL
))
1004 assert(mdate_selected
!= mdate_default
);
1006 #if !VLC_WINSTORE_APP
1007 /* Raise default priority of the current process */
1008 #ifndef ABOVE_NORMAL_PRIORITY_CLASS
1009 # define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
1011 if (var_InheritBool(vlc
, "high-priority"))
1013 if (SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS
)
1014 || SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS
))
1015 msg_Dbg(vlc
, "raised process priority");
1017 msg_Dbg(vlc
, "could not raise process priority");
1020 LeaveCriticalSection(&setup_lock
);
1023 #define LOOKUP(s) (((s##_) = (void *)GetProcAddress(h, #s)) != NULL)
1025 extern vlc_rwlock_t config_lock
;
1026 BOOL WINAPI
DllMain (HINSTANCE
, DWORD
, LPVOID
);
1028 BOOL WINAPI
DllMain (HINSTANCE hinstDll
, DWORD fdwReason
, LPVOID lpvReserved
)
1035 case DLL_PROCESS_ATTACH
:
1037 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
1038 HANDLE h
= GetModuleHandle(TEXT("kernel32.dll"));
1039 if (unlikely(h
== NULL
))
1042 if (!LOOKUP(WaitOnAddress
)
1043 || !LOOKUP(WakeByAddressAll
) || !LOOKUP(WakeByAddressSingle
))
1045 # if (_WIN32_WINNT < _WIN32_WINNT_VISTA)
1046 if (!LOOKUP(InitializeConditionVariable
)
1047 || !LOOKUP(SleepConditionVariableCS
)
1048 || !LOOKUP(WakeAllConditionVariable
))
1050 InitializeConditionVariable_
= DummyConditionVariable
;
1051 SleepConditionVariableCS_
= SleepConditionVariableFallback
;
1052 WakeAllConditionVariable_
= DummyConditionVariable
;
1055 vlc_wait_addr_init();
1056 WaitOnAddress_
= WaitOnAddressFallback
;
1057 WakeByAddressAll_
= WakeByAddressFallback
;
1058 WakeByAddressSingle_
= WakeByAddressFallback
;
1061 thread_key
= TlsAlloc();
1062 if (unlikely(thread_key
== TLS_OUT_OF_INDEXES
))
1064 InitializeCriticalSection(&setup_lock
);
1065 InitializeCriticalSection(&super_mutex
);
1066 InitializeConditionVariable(&super_variable
);
1067 vlc_rwlock_init (&config_lock
);
1072 case DLL_PROCESS_DETACH
:
1073 vlc_rwlock_destroy (&config_lock
);
1074 DeleteCriticalSection(&super_mutex
);
1075 DeleteCriticalSection(&setup_lock
);
1076 TlsFree(thread_key
);
1077 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
1078 if (WaitOnAddress_
== WaitOnAddressFallback
)
1079 vlc_wait_addr_deinit();
1083 case DLL_THREAD_DETACH
:
1084 vlc_threadvars_cleanup();