1.1 --- a/extensions/sqlite/sqlite-source/mutex_w32.c Fri Mar 28 06:21:34 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,208 +0,0 @@
1.4 -/*
1.5 -** 2007 August 14
1.6 -**
1.7 -** The author disclaims copyright to this source code. In place of
1.8 -** a legal notice, here is a blessing:
1.9 -**
1.10 -** May you do good and not evil.
1.11 -** May you find forgiveness for yourself and forgive others.
1.12 -** May you share freely, never taking more than you give.
1.13 -**
1.14 -*************************************************************************
1.15 -** This file contains the C functions that implement mutexes for win32
1.16 -**
1.17 -** $Id$
1.18 -*/
1.19 -#include "sqliteInt.h"
1.20 -
1.21 -/*
1.22 -** The code in this file is only used if we are compiling multithreaded
1.23 -** on a win32 system.
1.24 -*/
1.25 -#ifdef SQLITE_MUTEX_W32
1.26 -
1.27 -/*
1.28 -** Each recursive mutex is an instance of the following structure.
1.29 -*/
1.30 -struct sqlite3_mutex {
1.31 - CRITICAL_SECTION mutex; /* Mutex controlling the lock */
1.32 - int id; /* Mutex type */
1.33 - int nRef; /* Number of enterances */
1.34 - DWORD owner; /* Thread holding this mutex */
1.35 -};
1.36 -
1.37 -/*
1.38 -** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
1.39 -** or WinCE. Return false (zero) for Win95, Win98, or WinME.
1.40 -**
1.41 -** Here is an interesting observation: Win95, Win98, and WinME lack
1.42 -** the LockFileEx() API. But we can still statically link against that
1.43 -** API as long as we don't call it win running Win95/98/ME. A call to
1.44 -** this routine is used to determine if the host is Win95/98/ME or
1.45 -** WinNT/2K/XP so that we will know whether or not we can safely call
1.46 -** the LockFileEx() API.
1.47 -*/
1.48 -#if OS_WINCE
1.49 -# define mutexIsNT() (1)
1.50 -#else
1.51 - static int mutexIsNT(void){
1.52 - static int osType = 0;
1.53 - if( osType==0 ){
1.54 - OSVERSIONINFO sInfo;
1.55 - sInfo.dwOSVersionInfoSize = sizeof(sInfo);
1.56 - GetVersionEx(&sInfo);
1.57 - osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
1.58 - }
1.59 - return osType==2;
1.60 - }
1.61 -#endif /* OS_WINCE */
1.62 -
1.63 -
1.64 -/*
1.65 -** The sqlite3_mutex_alloc() routine allocates a new
1.66 -** mutex and returns a pointer to it. If it returns NULL
1.67 -** that means that a mutex could not be allocated. SQLite
1.68 -** will unwind its stack and return an error. The argument
1.69 -** to sqlite3_mutex_alloc() is one of these integer constants:
1.70 -**
1.71 -** <ul>
1.72 -** <li> SQLITE_MUTEX_FAST 0
1.73 -** <li> SQLITE_MUTEX_RECURSIVE 1
1.74 -** <li> SQLITE_MUTEX_STATIC_MASTER 2
1.75 -** <li> SQLITE_MUTEX_STATIC_MEM 3
1.76 -** <li> SQLITE_MUTEX_STATIC_PRNG 4
1.77 -** </ul>
1.78 -**
1.79 -** The first two constants cause sqlite3_mutex_alloc() to create
1.80 -** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
1.81 -** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
1.82 -** The mutex implementation does not need to make a distinction
1.83 -** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
1.84 -** not want to. But SQLite will only request a recursive mutex in
1.85 -** cases where it really needs one. If a faster non-recursive mutex
1.86 -** implementation is available on the host platform, the mutex subsystem
1.87 -** might return such a mutex in response to SQLITE_MUTEX_FAST.
1.88 -**
1.89 -** The other allowed parameters to sqlite3_mutex_alloc() each return
1.90 -** a pointer to a static preexisting mutex. Three static mutexes are
1.91 -** used by the current version of SQLite. Future versions of SQLite
1.92 -** may add additional static mutexes. Static mutexes are for internal
1.93 -** use by SQLite only. Applications that use SQLite mutexes should
1.94 -** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
1.95 -** SQLITE_MUTEX_RECURSIVE.
1.96 -**
1.97 -** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
1.98 -** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
1.99 -** returns a different mutex on every call. But for the static
1.100 -** mutex types, the same mutex is returned on every call that has
1.101 -** the same type number.
1.102 -*/
1.103 -sqlite3_mutex *sqlite3_mutex_alloc(int iType){
1.104 - sqlite3_mutex *p;
1.105 -
1.106 - switch( iType ){
1.107 - case SQLITE_MUTEX_FAST:
1.108 - case SQLITE_MUTEX_RECURSIVE: {
1.109 - p = sqlite3MallocZero( sizeof(*p) );
1.110 - if( p ){
1.111 - p->id = iType;
1.112 - InitializeCriticalSection(&p->mutex);
1.113 - }
1.114 - break;
1.115 - }
1.116 - default: {
1.117 - static sqlite3_mutex staticMutexes[5];
1.118 - static int isInit = 0;
1.119 - while( !isInit ){
1.120 - static long lock = 0;
1.121 - if( InterlockedIncrement(&lock)==1 ){
1.122 - int i;
1.123 - for(i=0; i<sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++){
1.124 - InitializeCriticalSection(&staticMutexes[i].mutex);
1.125 - }
1.126 - isInit = 1;
1.127 - }else{
1.128 - Sleep(1);
1.129 - }
1.130 - }
1.131 - assert( iType-2 >= 0 );
1.132 - assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
1.133 - p = &staticMutexes[iType-2];
1.134 - p->id = iType;
1.135 - break;
1.136 - }
1.137 - }
1.138 - return p;
1.139 -}
1.140 -
1.141 -
1.142 -/*
1.143 -** This routine deallocates a previously
1.144 -** allocated mutex. SQLite is careful to deallocate every
1.145 -** mutex that it allocates.
1.146 -*/
1.147 -void sqlite3_mutex_free(sqlite3_mutex *p){
1.148 - assert( p );
1.149 - assert( p->nRef==0 );
1.150 - assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
1.151 - DeleteCriticalSection(&p->mutex);
1.152 - sqlite3_free(p);
1.153 -}
1.154 -
1.155 -/*
1.156 -** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
1.157 -** to enter a mutex. If another thread is already within the mutex,
1.158 -** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
1.159 -** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
1.160 -** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
1.161 -** be entered multiple times by the same thread. In such cases the,
1.162 -** mutex must be exited an equal number of times before another thread
1.163 -** can enter. If the same thread tries to enter any other kind of mutex
1.164 -** more than once, the behavior is undefined.
1.165 -*/
1.166 -void sqlite3_mutex_enter(sqlite3_mutex *p){
1.167 - assert( p );
1.168 - assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
1.169 - EnterCriticalSection(&p->mutex);
1.170 - p->owner = GetCurrentThreadId();
1.171 - p->nRef++;
1.172 -}
1.173 -int sqlite3_mutex_try(sqlite3_mutex *p){
1.174 - int rc;
1.175 - assert( p );
1.176 - assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
1.177 - if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
1.178 - p->owner = GetCurrentThreadId();
1.179 - p->nRef++;
1.180 - rc = SQLITE_OK;
1.181 - }else{
1.182 - rc = SQLITE_BUSY;
1.183 - }
1.184 - return rc;
1.185 -}
1.186 -
1.187 -/*
1.188 -** The sqlite3_mutex_leave() routine exits a mutex that was
1.189 -** previously entered by the same thread. The behavior
1.190 -** is undefined if the mutex is not currently entered or
1.191 -** is not currently allocated. SQLite will never do either.
1.192 -*/
1.193 -void sqlite3_mutex_leave(sqlite3_mutex *p){
1.194 - assert( p->nRef>0 );
1.195 - assert( p->owner==GetCurrentThreadId() );
1.196 - p->nRef--;
1.197 - assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
1.198 - LeaveCriticalSection(&p->mutex);
1.199 -}
1.200 -
1.201 -/*
1.202 -** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
1.203 -** intended for use only inside assert() statements.
1.204 -*/
1.205 -int sqlite3_mutex_held(sqlite3_mutex *p){
1.206 - return p==0 || (p->nRef!=0 && p->owner==GetCurrentThreadId());
1.207 -}
1.208 -int sqlite3_mutex_notheld(sqlite3_mutex *p){
1.209 - return p==0 || p->nRef==0 || p->owner!=GetCurrentThreadId();
1.210 -}
1.211 -#endif /* SQLITE_MUTEX_W32 */