1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-08 14:02:16 +03:00

Enhancements and updates to the Win32 mutex subsystem.

FossilOrigin-Name: 18984c321049a759f6619cfa17fb3f4e7b3e08ea
This commit is contained in:
mistachkin
2014-07-29 05:49:02 +00:00
parent e6bc1ef68f
commit d6918657d8
5 changed files with 105 additions and 89 deletions

View File

@@ -1,5 +1,5 @@
C Remove\san\sunreachable\sbranch\sfrom\sthe\ssqlite3_value_numeric_type()\sinterface. C Enhancements\sand\supdates\sto\sthe\sWin32\smutex\ssubsystem.
D 2014-07-26T20:12:56.006 D 2014-07-29T05:49:02.604
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 5eb79e334a5de69c87740edd56af6527dd219308 F Makefile.in 5eb79e334a5de69c87740edd56af6527dd219308
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -189,7 +189,7 @@ F src/journal.c b4124532212b6952f42eb2c12fa3c25701d8ba8d
F src/legacy.c 0df0b1550b9cc1f58229644735e317ac89131f12 F src/legacy.c 0df0b1550b9cc1f58229644735e317ac89131f12
F src/lempar.c cdf0a000315332fc9b50b62f3b5e22e080a0952b F src/lempar.c cdf0a000315332fc9b50b62f3b5e22e080a0952b
F src/loadext.c 867c7b330b740c6c917af9956b13b81d0a048303 F src/loadext.c 867c7b330b740c6c917af9956b13b81d0a048303
F src/main.c cfdb2aa5d248ff1af60227cc3f6d485ba86f92dc F src/main.c 2c7647fd706cbb048e2b70429e552a7bf732d298
F src/malloc.c 0203ebce9152c6a0e5de520140b8ba65187350be F src/malloc.c 0203ebce9152c6a0e5de520140b8ba65187350be
F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645 F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645
F src/mem1.c c0c990fcaddff810ea277b4fb5d9138603dd5d4b F src/mem1.c c0c990fcaddff810ea277b4fb5d9138603dd5d4b
@@ -201,14 +201,14 @@ F src/mutex.c d3b66a569368015e0fcb1ac15f81c119f504d3bc
F src/mutex.h 5bc526e19dccc412b7ff04642f6fdad3fdfdabea F src/mutex.h 5bc526e19dccc412b7ff04642f6fdad3fdfdabea
F src/mutex_noop.c 7682796b7d8d39bf1c138248858efcd10c9e1553 F src/mutex_noop.c 7682796b7d8d39bf1c138248858efcd10c9e1553
F src/mutex_unix.c c3a4e00f96ba068a8dbef34084465979aaf369cc F src/mutex_unix.c c3a4e00f96ba068a8dbef34084465979aaf369cc
F src/mutex_w32.c 9bcab6e699fdb20eae784237c36b299830fb3316 F src/mutex_w32.c 45020ed78735a202ff14efcf19415d29f556f16d
F src/notify.c 976dd0f6171d4588e89e874fcc765e92914b6d30 F src/notify.c 976dd0f6171d4588e89e874fcc765e92914b6d30
F src/os.c 1b147e4cf7cc39e618115c14a086aed44bc91ace F src/os.c 1b147e4cf7cc39e618115c14a086aed44bc91ace
F src/os.h 60d419395e32a8029fa380a80a3da2e9030f635e F src/os.h 60d419395e32a8029fa380a80a3da2e9030f635e
F src/os_common.h 92815ed65f805560b66166e3583470ff94478f04 F src/os_common.h 92815ed65f805560b66166e3583470ff94478f04
F src/os_setup.h c9d4553b5aaa6f73391448b265b89bed0b890faa F src/os_setup.h c9d4553b5aaa6f73391448b265b89bed0b890faa
F src/os_unix.c a7baf1b30f3c58ba20b813e01aab23b18ae44f85 F src/os_unix.c a7baf1b30f3c58ba20b813e01aab23b18ae44f85
F src/os_win.c 8dbf6c11780fe2eb96c1f289e664d0c7b2911d37 F src/os_win.c c29e3a80b47ebdbabd61fc3d4015e52d2654d8c5
F src/os_win.h 057344a6720b4c8405d9bd98f58cb37a6ee46c25 F src/os_win.h 057344a6720b4c8405d9bd98f58cb37a6ee46c25
F src/pager.c f6bb1fa6cdf2062f2d8aec3e64db302bca519ab8 F src/pager.c f6bb1fa6cdf2062f2d8aec3e64db302bca519ab8
F src/pager.h ffd5607f7b3e4590b415b007a4382f693334d428 F src/pager.h ffd5607f7b3e4590b415b007a4382f693334d428
@@ -1184,7 +1184,10 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 413d7287977702fa651c0140bd5cf29021fe3e79 P 5350229b52b18a4961858a30538c5c75e5bd3048
R 882befeb8b61995267c9bc4311daa690 R de509ee2752907682d663ce7b389c5a6
U drh T *branch * winMutex
Z 28fa8e103cbfa69958d1f03600e24fde T *sym-winMutex *
T -sym-trunk *
U mistachkin
Z 9395c7aaf9a6a0a0988325332158758a

View File

@@ -1 +1 @@
5350229b52b18a4961858a30538c5c75e5bd3048 18984c321049a759f6619cfa17fb3f4e7b3e08ea

View File

@@ -1056,7 +1056,7 @@ void sqlite3RollbackAll(sqlite3 *db, int tripCode){
** Return a static string containing the name corresponding to the error code ** Return a static string containing the name corresponding to the error code
** specified in the argument. ** specified in the argument.
*/ */
#if defined(SQLITE_TEST) #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
const char *sqlite3ErrName(int rc){ const char *sqlite3ErrName(int rc){
const char *zName = 0; const char *zName = 0;
int i, origRc = rc; int i, origRc = rc;

View File

@@ -9,11 +9,16 @@
** May you share freely, never taking more than you give. ** May you share freely, never taking more than you give.
** **
************************************************************************* *************************************************************************
** This file contains the C functions that implement mutexes for win32 ** This file contains the C functions that implement mutexes for Win32.
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#if SQLITE_OS_WIN #if SQLITE_OS_WIN
/*
** Include code that is common to all os_*.c files
*/
#include "os_common.h"
/* /*
** Include the header file for the Windows VFS. ** Include the header file for the Windows VFS.
*/ */
@@ -22,7 +27,7 @@
/* /*
** The code in this file is only used if we are compiling multithreaded ** The code in this file is only used if we are compiling multithreaded
** on a win32 system. ** on a Win32 system.
*/ */
#ifdef SQLITE_MUTEX_W32 #ifdef SQLITE_MUTEX_W32
@@ -35,48 +40,22 @@ struct sqlite3_mutex {
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
volatile int nRef; /* Number of enterances */ volatile int nRef; /* Number of enterances */
volatile DWORD owner; /* Thread holding this mutex */ volatile DWORD owner; /* Thread holding this mutex */
int trace; /* True to trace changes */ volatile int trace; /* True to trace changes */
#endif #endif
}; };
#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
#ifdef SQLITE_DEBUG
#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
#else
#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
#endif
/* /*
** Return true (non-zero) if we are running under WinNT, Win2K, WinXP, ** These are the initializer values used when declaring a "static" mutex
** or WinCE. Return false (zero) for Win95, Win98, or WinME. ** on Win32. It should be noted that all mutexes require initialization
** ** on the Win32 platform.
** Here is an interesting observation: Win95, Win98, and WinME lack
** the LockFileEx() API. But we can still statically link against that
** API as long as we don't call it win running Win95/98/ME. A call to
** this routine is used to determine if the host is Win95/98/ME or
** WinNT/2K/XP so that we will know whether or not we can safely call
** the LockFileEx() API.
**
** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
** which is only available if your application was compiled with
** _WIN32_WINNT defined to a value >= 0x0400. Currently, the only
** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
** this out as well.
*/ */
#if 0 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
# define mutexIsNT() (1) #ifdef SQLITE_DEBUG
#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
0L, (DWORD)0, 0 }
#else #else
static int mutexIsNT(void){ #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
static int osType = 0;
if( osType==0 ){
OSVERSIONINFO sInfo;
sInfo.dwOSVersionInfoSize = sizeof(sInfo);
GetVersionEx(&sInfo);
osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
}
return osType==2;
}
#endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
#endif #endif
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
@@ -87,16 +66,17 @@ struct sqlite3_mutex {
static int winMutexHeld(sqlite3_mutex *p){ static int winMutexHeld(sqlite3_mutex *p){
return p->nRef!=0 && p->owner==GetCurrentThreadId(); return p->nRef!=0 && p->owner==GetCurrentThreadId();
} }
static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){ static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
return p->nRef==0 || p->owner!=tid; return p->nRef==0 || p->owner!=tid;
} }
static int winMutexNotheld(sqlite3_mutex *p){ static int winMutexNotheld(sqlite3_mutex *p){
DWORD tid = GetCurrentThreadId(); DWORD tid = GetCurrentThreadId();
return winMutexNotheld2(p, tid); return winMutexNotheld2(p, tid);
} }
#endif #endif
/* /*
** Initialize and deinitialize the mutex subsystem. ** Initialize and deinitialize the mutex subsystem.
*/ */
@@ -108,14 +88,17 @@ static sqlite3_mutex winMutex_staticMutexes[6] = {
SQLITE3_MUTEX_INITIALIZER, SQLITE3_MUTEX_INITIALIZER,
SQLITE3_MUTEX_INITIALIZER SQLITE3_MUTEX_INITIALIZER
}; };
static int winMutex_isInit = 0; static int winMutex_isInit = 0;
/* As winMutexInit() and winMutexEnd() are called as part
** of the sqlite3_initialize and sqlite3_shutdown() /* As winMutexInit() and winMutexEnd() are called as part of the
** processing, the "interlocked" magic is probably not ** sqlite3_initialize() and sqlite3_shutdown() processing, the
** strictly necessary. ** "interlocked" magic used in this section may not strictly
** necessary.
*/ */
static LONG winMutex_lock = 0; static LONG winMutex_lock = 0;
int sqlite3_win32_is_nt(void); /* os_win.c */
void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */ void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
static int winMutexInit(void){ static int winMutexInit(void){
@@ -131,7 +114,8 @@ static int winMutexInit(void){
} }
winMutex_isInit = 1; winMutex_isInit = 1;
}else{ }else{
/* Someone else is in the process of initing the static mutexes */ /* Another thread is (in the process of) initializing the static
** mutexes */
while( !winMutex_isInit ){ while( !winMutex_isInit ){
sqlite3_win32_sleep(1); sqlite3_win32_sleep(1);
} }
@@ -206,6 +190,9 @@ static sqlite3_mutex *winMutexAlloc(int iType){
if( p ){ if( p ){
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
p->id = iType; p->id = iType;
#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
p->trace = 1;
#endif
#endif #endif
#if SQLITE_OS_WINRT #if SQLITE_OS_WINRT
InitializeCriticalSectionEx(&p->mutex, 0, 0); InitializeCriticalSectionEx(&p->mutex, 0, 0);
@@ -216,12 +203,15 @@ static sqlite3_mutex *winMutexAlloc(int iType){
break; break;
} }
default: { default: {
assert( winMutex_isInit==1 );
assert( iType-2 >= 0 ); assert( iType-2 >= 0 );
assert( iType-2 < ArraySize(winMutex_staticMutexes) ); assert( iType-2 < ArraySize(winMutex_staticMutexes) );
assert( winMutex_isInit==1 );
p = &winMutex_staticMutexes[iType-2]; p = &winMutex_staticMutexes[iType-2];
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
p->id = iType; p->id = iType;
#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
p->trace = 1;
#endif
#endif #endif
break; break;
} }
@@ -237,8 +227,10 @@ static sqlite3_mutex *winMutexAlloc(int iType){
*/ */
static void winMutexFree(sqlite3_mutex *p){ static void winMutexFree(sqlite3_mutex *p){
assert( p ); assert( p );
#ifdef SQLITE_DEBUG
assert( p->nRef==0 && p->owner==0 ); assert( p->nRef==0 && p->owner==0 );
assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ); assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
#endif
DeleteCriticalSection(&p->mutex); DeleteCriticalSection(&p->mutex);
sqlite3_free(p); sqlite3_free(p);
} }
@@ -255,9 +247,14 @@ static void winMutexFree(sqlite3_mutex *p){
** more than once, the behavior is undefined. ** more than once, the behavior is undefined.
*/ */
static void winMutexEnter(sqlite3_mutex *p){ static void winMutexEnter(sqlite3_mutex *p){
#ifdef SQLITE_DEBUG #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
DWORD tid = GetCurrentThreadId(); DWORD tid = GetCurrentThreadId();
#endif
#ifdef SQLITE_DEBUG
assert( p );
assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
#else
assert( p );
#endif #endif
EnterCriticalSection(&p->mutex); EnterCriticalSection(&p->mutex);
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
@@ -265,15 +262,18 @@ static void winMutexEnter(sqlite3_mutex *p){
p->owner = tid; p->owner = tid;
p->nRef++; p->nRef++;
if( p->trace ){ if( p->trace ){
printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
tid, p, p->trace, p->nRef));
} }
#endif #endif
} }
static int winMutexTry(sqlite3_mutex *p){ static int winMutexTry(sqlite3_mutex *p){
#ifndef NDEBUG #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
DWORD tid = GetCurrentThreadId(); DWORD tid = GetCurrentThreadId();
#endif #endif
int rc = SQLITE_BUSY; int rc = SQLITE_BUSY;
assert( p );
assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
/* /*
** The sqlite3_mutex_try() routine is very rarely used, and when it ** The sqlite3_mutex_try() routine is very rarely used, and when it
@@ -286,18 +286,21 @@ static int winMutexTry(sqlite3_mutex *p){
** For that reason, we will omit this optimization for now. See ** For that reason, we will omit this optimization for now. See
** ticket #2685. ** ticket #2685.
*/ */
#if 0 #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){ if( sqlite3_win32_is_nt() && TryEnterCriticalSection(&p->mutex) ){
#ifdef SQLITE_DEBUG
p->owner = tid; p->owner = tid;
p->nRef++; p->nRef++;
#endif
rc = SQLITE_OK; rc = SQLITE_OK;
} }
#else #else
UNUSED_PARAMETER(p); UNUSED_PARAMETER(p);
#endif #endif
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
if( rc==SQLITE_OK && p->trace ){ if( p->trace ){
printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
} }
#endif #endif
return rc; return rc;
@@ -310,8 +313,11 @@ static int winMutexTry(sqlite3_mutex *p){
** is not currently allocated. SQLite will never do either. ** is not currently allocated. SQLite will never do either.
*/ */
static void winMutexLeave(sqlite3_mutex *p){ static void winMutexLeave(sqlite3_mutex *p){
#ifndef NDEBUG #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
DWORD tid = GetCurrentThreadId(); DWORD tid = GetCurrentThreadId();
#endif
assert( p );
#ifdef SQLITE_DEBUG
assert( p->nRef>0 ); assert( p->nRef>0 );
assert( p->owner==tid ); assert( p->owner==tid );
p->nRef--; p->nRef--;
@@ -321,7 +327,8 @@ static void winMutexLeave(sqlite3_mutex *p){
LeaveCriticalSection(&p->mutex); LeaveCriticalSection(&p->mutex);
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
if( p->trace ){ if( p->trace ){
printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
tid, p, p->trace, p->nRef));
} }
#endif #endif
} }
@@ -343,7 +350,7 @@ sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
0 0
#endif #endif
}; };
return &sMutex; return &sMutex;
} }
#endif /* SQLITE_MUTEX_W32 */ #endif /* SQLITE_MUTEX_W32 */

View File

@@ -1298,7 +1298,14 @@ void sqlite3_win32_sleep(DWORD milliseconds){
#elif !defined(SQLITE_WIN32_HAS_WIDE) #elif !defined(SQLITE_WIN32_HAS_WIDE)
# define osIsNT() (0) # define osIsNT() (0)
#else #else
static int osIsNT(void){ # define osIsNT() (sqlite3_win32_is_nt())
#endif
/*
** This function determines if the machine is running a version of Windows
** based on the NT kernel.
*/
int sqlite3_win32_is_nt(void){
if( sqlite3_os_type==0 ){ if( sqlite3_os_type==0 ){
#if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8 #if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
OSVERSIONINFOW sInfo; OSVERSIONINFOW sInfo;
@@ -1309,11 +1316,10 @@ void sqlite3_win32_sleep(DWORD milliseconds){
sInfo.dwOSVersionInfoSize = sizeof(sInfo); sInfo.dwOSVersionInfoSize = sizeof(sInfo);
osGetVersionExA(&sInfo); osGetVersionExA(&sInfo);
#endif #endif
sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1; sqlite3_os_type = (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1;
} }
return sqlite3_os_type==2; return (sqlite3_os_type == 2);
} }
#endif
#ifdef SQLITE_WIN32_MALLOC #ifdef SQLITE_WIN32_MALLOC
/* /*