2002-02-21 21:22:00 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002, TransGaming Technologies Inc.
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-02-21 21:22:00 +01:00
|
|
|
*/
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2020-11-27 14:17:05 +01:00
|
|
|
#include <stdbool.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2002-02-21 21:22:00 +01:00
|
|
|
#include "winbase.h"
|
2014-06-21 14:22:28 +02:00
|
|
|
#include "winternl.h"
|
|
|
|
#include "msvcrt.h"
|
2008-12-17 14:45:18 +01:00
|
|
|
#include "mtdll.h"
|
2014-06-21 14:22:28 +02:00
|
|
|
#include "cxx.h"
|
2002-02-21 21:22:00 +01:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2002-02-21 21:22:00 +01:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
BOOL bInit;
|
2002-02-21 21:22:00 +01:00
|
|
|
CRITICAL_SECTION crit;
|
|
|
|
} LOCKTABLEENTRY;
|
|
|
|
|
|
|
|
static LOCKTABLEENTRY lock_table[ _TOTAL_LOCKS ];
|
|
|
|
|
|
|
|
static inline void msvcrt_mlock_set_entry_initialized( int locknum, BOOL initialized )
|
|
|
|
{
|
|
|
|
lock_table[ locknum ].bInit = initialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void msvcrt_initialize_mlock( int locknum )
|
|
|
|
{
|
|
|
|
InitializeCriticalSection( &(lock_table[ locknum ].crit) );
|
2007-03-10 22:09:17 +01:00
|
|
|
lock_table[ locknum ].crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": LOCKTABLEENTRY.crit");
|
2002-06-01 01:06:46 +02:00
|
|
|
msvcrt_mlock_set_entry_initialized( locknum, TRUE );
|
2002-02-21 21:22:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void msvcrt_uninitialize_mlock( int locknum )
|
|
|
|
{
|
2007-03-10 22:09:17 +01:00
|
|
|
lock_table[ locknum ].crit.DebugInfo->Spare[0] = 0;
|
2002-02-21 21:22:00 +01:00
|
|
|
DeleteCriticalSection( &(lock_table[ locknum ].crit) );
|
|
|
|
msvcrt_mlock_set_entry_initialized( locknum, FALSE );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* msvcrt_init_mt_locks (internal)
|
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Initialize the table lock. All other locks will be initialized
|
2002-02-21 21:22:00 +01:00
|
|
|
* upon first use.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void msvcrt_init_mt_locks(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
TRACE( "initializing mtlocks\n" );
|
|
|
|
|
|
|
|
/* Initialize the table */
|
|
|
|
for( i=0; i < _TOTAL_LOCKS; i++ )
|
|
|
|
{
|
|
|
|
msvcrt_mlock_set_entry_initialized( i, FALSE );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize our lock table lock */
|
2002-06-01 01:06:46 +02:00
|
|
|
msvcrt_initialize_mlock( _LOCKTAB_LOCK );
|
2002-02-21 21:22:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* _lock (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
void CDECL _lock( int locknum )
|
2002-02-21 21:22:00 +01:00
|
|
|
{
|
|
|
|
TRACE( "(%d)\n", locknum );
|
|
|
|
|
|
|
|
/* If the lock doesn't exist yet, create it */
|
|
|
|
if( lock_table[ locknum ].bInit == FALSE )
|
|
|
|
{
|
|
|
|
/* Lock while we're changing the lock table */
|
|
|
|
_lock( _LOCKTAB_LOCK );
|
|
|
|
|
|
|
|
/* Check again if we've got a bit of a race on lock creation */
|
|
|
|
if( lock_table[ locknum ].bInit == FALSE )
|
|
|
|
{
|
|
|
|
TRACE( ": creating lock #%d\n", locknum );
|
|
|
|
msvcrt_initialize_mlock( locknum );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock ourselves */
|
|
|
|
_unlock( _LOCKTAB_LOCK );
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
EnterCriticalSection( &(lock_table[ locknum ].crit) );
|
2002-02-21 21:22:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* _unlock (MSVCRT.@)
|
|
|
|
*
|
|
|
|
* NOTE: There is no error detection to make sure the lock exists and is acquired.
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
void CDECL _unlock( int locknum )
|
2002-02-21 21:22:00 +01:00
|
|
|
{
|
|
|
|
TRACE( "(%d)\n", locknum );
|
|
|
|
|
|
|
|
LeaveCriticalSection( &(lock_table[ locknum ].crit) );
|
|
|
|
}
|
2014-06-21 14:22:28 +02:00
|
|
|
|
2017-02-28 11:21:12 +01:00
|
|
|
#if _MSVCR_VER == 110
|
|
|
|
static LONG shared_ptr_lock;
|
|
|
|
|
|
|
|
void __cdecl _Lock_shared_ptr_spin_lock(void)
|
|
|
|
{
|
|
|
|
LONG l = 0;
|
|
|
|
|
|
|
|
while(InterlockedCompareExchange(&shared_ptr_lock, 1, 0) != 0) {
|
|
|
|
if(l++ == 1000) {
|
|
|
|
Sleep(0);
|
|
|
|
l = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void __cdecl _Unlock_shared_ptr_spin_lock(void)
|
|
|
|
{
|
|
|
|
shared_ptr_lock = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-24 11:57:47 +02:00
|
|
|
/**********************************************************************
|
|
|
|
* msvcrt_free_locks (internal)
|
|
|
|
*
|
|
|
|
* Uninitialize all mt locks. Assume that neither _lock or _unlock will
|
|
|
|
* be called once we're calling this routine (ie _LOCKTAB_LOCK can be deleted)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void msvcrt_free_locks(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
TRACE( ": uninitializing all mtlocks\n" );
|
|
|
|
|
|
|
|
/* Uninitialize the table */
|
|
|
|
for( i=0; i < _TOTAL_LOCKS; i++ )
|
|
|
|
{
|
|
|
|
if( lock_table[ i ].bInit )
|
|
|
|
{
|
|
|
|
msvcrt_uninitialize_mlock( i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|