xref: /reactos/sdk/lib/ucrt/internal/locks.cpp (revision 04e0dc4a)
1 //
2 // locks.cpp
3 //
4 //      Copyright (c) Microsoft Corporation. All rights reserved.
5 //
6 // Critical sections used for synchronization in the CoreCRT.
7 //
8 #include <corecrt_internal.h>
9 
10 
11 
12 // This table holds the locks used by the CoreCRT.  It is indexed using the
13 // enumerators of the __acrt_lock_id enumeration.
14 static CRITICAL_SECTION __acrt_lock_table[__acrt_lock_count];
15 
16 // This variable stores the number of locks that have been successfully
17 // initialized.  Locks are initialized in order and are destroyed in reverse
18 // order.  The startup and exit code must ensure that initialization and
19 // destruction is synchronized:  these functions should never be executed
20 // concurrently.
21 static unsigned __acrt_locks_initialized;
22 
23 
24 
__acrt_initialize_locks()25 extern "C" bool __cdecl __acrt_initialize_locks()
26 {
27     for (unsigned i = 0; i < __acrt_lock_count; ++i)
28     {
29         if (!__acrt_InitializeCriticalSectionEx(&__acrt_lock_table[i], _CORECRT_SPINCOUNT, 0))
30         {
31             __acrt_uninitialize_locks(false);
32             return false;
33         }
34 
35         ++__acrt_locks_initialized;
36     }
37 
38     return true;
39 }
40 
__acrt_uninitialize_locks(bool const)41 extern "C" bool __cdecl __acrt_uninitialize_locks(bool const /* terminating */)
42 {
43     for (unsigned i = __acrt_locks_initialized; i > 0; --i)
44     {
45         DeleteCriticalSection(&__acrt_lock_table[i - 1]);
46         --__acrt_locks_initialized;
47     }
48 
49     return true;
50 }
51 
__acrt_lock(_In_ __acrt_lock_id _Lock)52 extern "C" void __cdecl __acrt_lock(_In_ __acrt_lock_id _Lock)
53 {
54     EnterCriticalSection(&__acrt_lock_table[_Lock]);
55 }
56 
__acrt_unlock(_In_ __acrt_lock_id _Lock)57 extern "C" void __cdecl __acrt_unlock(_In_ __acrt_lock_id _Lock)
58 {
59     LeaveCriticalSection(&__acrt_lock_table[_Lock]);
60 }
61 
_lock_locales()62 extern "C" void __cdecl _lock_locales()
63 {
64 	__acrt_eagerly_load_locale_apis();
65     __acrt_lock(__acrt_locale_lock);
66 }
67 
_unlock_locales()68 extern "C" void __cdecl _unlock_locales()
69 {
70     __acrt_unlock(__acrt_locale_lock);
71 }
72