ReactOS 0.4.16-dev-2104-gb84fa49
lock.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2002, TransGaming Technologies Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19#include <stdarg.h>
20#include <stdbool.h>
21
22#include "wine/debug.h"
23#include "windef.h"
24#include "winbase.h"
25#include "winternl.h"
26#include "msvcrt.h"
27#include "mtdll.h"
28#include "cxx.h"
29
31
32typedef struct
33{
37
39
40static inline void msvcrt_mlock_set_entry_initialized( int locknum, BOOL initialized )
41{
42 lock_table[ locknum ].bInit = initialized;
43}
44
45static inline void msvcrt_initialize_mlock( int locknum )
46{
48 lock_table[ locknum ].crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": LOCKTABLEENTRY.crit");
50}
51
52static inline void msvcrt_uninitialize_mlock( int locknum )
53{
54 lock_table[ locknum ].crit.DebugInfo->Spare[0] = 0;
55 DeleteCriticalSection( &(lock_table[ locknum ].crit) );
57}
58
59/**********************************************************************
60 * msvcrt_init_mt_locks (internal)
61 *
62 * Initialize the table lock. All other locks will be initialized
63 * upon first use.
64 *
65 */
67{
68 int i;
69
70 TRACE( "initializing mtlocks\n" );
71
72 /* Initialize the table */
73 for( i=0; i < _TOTAL_LOCKS; i++ )
74 {
76 }
77
78 /* Initialize our lock table lock */
80}
81
82/**********************************************************************
83 * _lock (MSVCRT.@)
84 */
85void CDECL _lock( int locknum )
86{
87 TRACE( "(%d)\n", locknum );
88
89 /* If the lock doesn't exist yet, create it */
90 if( lock_table[ locknum ].bInit == FALSE )
91 {
92 /* Lock while we're changing the lock table */
94
95 /* Check again if we've got a bit of a race on lock creation */
96 if( lock_table[ locknum ].bInit == FALSE )
97 {
98 TRACE( ": creating lock #%d\n", locknum );
99 msvcrt_initialize_mlock( locknum );
100 }
101
102 /* Unlock ourselves */
104 }
105
106 EnterCriticalSection( &(lock_table[ locknum ].crit) );
107}
108
109/**********************************************************************
110 * _unlock (MSVCRT.@)
111 *
112 * NOTE: There is no error detection to make sure the lock exists and is acquired.
113 */
114void CDECL _unlock( int locknum )
115{
116 TRACE( "(%d)\n", locknum );
117
118 LeaveCriticalSection( &(lock_table[ locknum ].crit) );
119}
120
121#if _MSVCR_VER == 110
122static LONG shared_ptr_lock;
123
124void __cdecl _Lock_shared_ptr_spin_lock(void)
125{
126 LONG l = 0;
127
128 while(InterlockedCompareExchange(&shared_ptr_lock, 1, 0) != 0) {
129 if(l++ == 1000) {
130 Sleep(0);
131 l = 0;
132 }
133 }
134}
135
136void __cdecl _Unlock_shared_ptr_spin_lock(void)
137{
138 shared_ptr_lock = 0;
139}
140#endif
141
142/**********************************************************************
143 * msvcrt_free_locks (internal)
144 *
145 * Uninitialize all mt locks. Assume that neither _lock or _unlock will
146 * be called once we're calling this routine (ie _LOCKTAB_LOCK can be deleted)
147 *
148 */
150{
151 int i;
152
153 TRACE( ": uninitializing all mtlocks\n" );
154
155 /* Uninitialize the table */
156 for( i=0; i < _TOTAL_LOCKS; i++ )
157 {
158 if( lock_table[ i ].bInit )
159 {
161 }
162 }
163}
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
r l[0]
Definition: byte_order.h:168
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CDECL
Definition: compat.h:29
BOOL WINAPI InitializeCriticalSectionEx(OUT LPCRITICAL_SECTION lpCriticalSection, IN DWORD dwSpinCount, IN DWORD flags)
Definition: sync.c:107
#define __cdecl
Definition: corecrt.h:121
void CDECL _lock(int locknum)
Definition: lock.c:85
static LOCKTABLEENTRY lock_table[_TOTAL_LOCKS]
Definition: lock.c:38
void msvcrt_init_mt_locks(void)
Definition: lock.c:66
void CDECL _unlock(int locknum)
Definition: lock.c:114
static void msvcrt_mlock_set_entry_initialized(int locknum, BOOL initialized)
Definition: lock.c:40
static void msvcrt_initialize_mlock(int locknum)
Definition: lock.c:45
void msvcrt_free_locks(void)
Definition: lock.c:149
static void msvcrt_uninitialize_mlock(int locknum)
Definition: lock.c:52
#define _LOCKTAB_LOCK
Definition: mtdll.h:41
#define _TOTAL_LOCKS
Definition: mtdll.h:59
unsigned int BOOL
Definition: ntddk_ex.h:94
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define InterlockedCompareExchange
Definition: interlocked.h:119
long LONG
Definition: pedump.c:60
#define TRACE(s)
Definition: solgame.cpp:4
CRITICAL_SECTION crit
Definition: lock.c:35
BOOL bInit
Definition: lock.c:34
PRTL_CRITICAL_SECTION_DEBUG DebugInfo
Definition: rtltypes.h:1450
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
static BOOL initialized
Definition: syslog.c:39
#define DWORD_PTR
Definition: treelist.c:76
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)
#define RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO
Definition: winnt_old.h:1146