diff options
author | pweilbacher <pweilbacher@noemail.net> | 2007-09-20 21:40:23 +0000 |
---|---|---|
committer | pweilbacher <pweilbacher@noemail.net> | 2007-09-20 21:40:23 +0000 |
commit | ec70b6b02f2493e4e1fe0eaa66de94c538798b5c (patch) | |
tree | 500d10eb175c9cdb95dee47b89676c01e7bd39ff /src/mutex_os2.c | |
parent | c51250a56b0023bbaf83bae46a930bd041ac687f (diff) | |
download | sqlite-ec70b6b02f2493e4e1fe0eaa66de94c538798b5c.tar.gz sqlite-ec70b6b02f2493e4e1fe0eaa66de94c538798b5c.zip |
Initial attempt of the new OS/2 mutex implementation. (Compiles and an attempt to create a new table does not crash in mutex_os2 any more.) (CVS 4442)
FossilOrigin-Name: aa61b244252399cce3b9c1ece9c6816ae9cb6717
Diffstat (limited to 'src/mutex_os2.c')
-rw-r--r-- | src/mutex_os2.c | 176 |
1 files changed, 144 insertions, 32 deletions
diff --git a/src/mutex_os2.c b/src/mutex_os2.c index b598f5d90..f175e0f81 100644 --- a/src/mutex_os2.c +++ b/src/mutex_os2.c @@ -11,67 +11,136 @@ ************************************************************************* ** This file contains the C functions that implement mutexes for OS/2 ** -** $Id: mutex_os2.c,v 1.1 2007/08/28 16:34:43 drh Exp $ +** $Id: mutex_os2.c,v 1.2 2007/09/20 21:40:23 pweilbacher Exp $ */ #include "sqliteInt.h" - /* ** The code in this file is only used if SQLITE_MUTEX_OS2 is defined. ** See the mutex.h file for details. */ #ifdef SQLITE_MUTEX_OS2 -/**** FIX ME: -***** This is currently a no-op implementation suitable for use -***** in single-threaded applications only. Somebody please replace -***** this with a real mutex implementation for OS/2. -****/ +/********************** OS/2 Mutex Implementation ********************** +** +** This implementation of mutexes is built using the OS/2 API. +*/ /* ** The mutex object +** Each recursive mutex is an instance of the following structure. */ struct sqlite3_mutex { - int id; /* The mutex type */ - int cnt; /* Number of entries without a matching leave */ + PSZ mutexName; /* Mutex name controlling the lock */ + HMTX mutex; /* Mutex controlling the lock */ + int id; /* Mutex type */ + int nRef; /* Number of references */ + TID owner; /* Thread holding this mutex */ }; /* ** The sqlite3_mutex_alloc() routine allocates a new ** mutex and returns a pointer to it. If it returns NULL ** that means that a mutex could not be allocated. +** SQLite will unwind its stack and return an error. The argument +** to sqlite3_mutex_alloc() is one of these integer constants: +** +** <ul> +** <li> SQLITE_MUTEX_FAST 0 +** <li> SQLITE_MUTEX_RECURSIVE 1 +** <li> SQLITE_MUTEX_STATIC_MASTER 2 +** <li> SQLITE_MUTEX_STATIC_MEM 3 +** <li> SQLITE_MUTEX_STATIC_PRNG 4 +** </ul> +** +** The first two constants cause sqlite3_mutex_alloc() to create +** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE +** is used but not necessarily so when SQLITE_MUTEX_FAST is used. +** The mutex implementation does not need to make a distinction +** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does +** not want to. But SQLite will only request a recursive mutex in +** cases where it really needs one. If a faster non-recursive mutex +** implementation is available on the host platform, the mutex subsystem +** might return such a mutex in response to SQLITE_MUTEX_FAST. +** +** The other allowed parameters to sqlite3_mutex_alloc() each return +** a pointer to a static preexisting mutex. Three static mutexes are +** used by the current version of SQLite. Future versions of SQLite +** may add additional static mutexes. Static mutexes are for internal +** use by SQLite only. Applications that use SQLite mutexes should +** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or +** SQLITE_MUTEX_RECURSIVE. +** +** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST +** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() +** returns a different mutex on every call. But for the static +** mutex types, the same mutex is returned on every call that has +** the same type number. */ -sqlite3_mutex *sqlite3_mutex_alloc(int id){ - static sqlite3_mutex aStatic[4]; - sqlite3_mutex *pNew = 0; - switch( id ){ +sqlite3_mutex *sqlite3_mutex_alloc(int iType){ +#define MUTEX_NAME "\\SEM32\\SQLITE\\MUTEX" +#define MUTEX_NAME_LEN 20 /* name length + null byte */ + sqlite3_mutex *p; + + switch( iType ){ case SQLITE_MUTEX_FAST: case SQLITE_MUTEX_RECURSIVE: { - pNew = sqlite3_malloc(sizeof(*pNew)); - if( pNew ){ - pNew->id = id; - pNew->cnt = 0; + p = sqlite3MallocZero( sizeof(*p) ); + if( p ){ + p->mutexName = (PSZ)malloc(MUTEX_NAME_LEN); + sqlite3_snprintf(MUTEX_NAME_LEN, p->mutexName, "%s", MUTEX_NAME); + p->id = iType; + DosCreateMutexSem(p->mutexName, &p->mutex, 0, FALSE); + DosOpenMutexSem(p->mutexName, &p->mutex); } break; } default: { - assert( id-2 >= 0 ); - assert( id-2 < sizeof(aStatic)/sizeof(aStatic[0]) ); - pNew = &aStatic[id-2]; - pNew->id = id; + static sqlite3_mutex staticMutexes[5]; + static int isInit = 0; + while( !isInit ) { + static long lock = 0; + DosEnterCritSec(); + lock++; + if( lock == 1 ) { + DosExitCritSec(); + int i; + for(i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++) { + staticMutexes[i].mutexName = (PSZ)malloc(MUTEX_NAME_LEN + 1); + sqlite3_snprintf(MUTEX_NAME_LEN + 1, + staticMutexes[i].mutexName, "%s%1d", MUTEX_NAME, i); + DosCreateMutexSem(staticMutexes[i].mutexName, + &staticMutexes[i].mutex, 0, FALSE); + DosOpenMutexSem(staticMutexes[i].mutexName, + &staticMutexes[i].mutex); + } + isInit = 1; + } else { + DosExitCritSec(); + DosSleep(1); + } + } + assert( iType-2 >= 0 ); + assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) ); + p = &staticMutexes[iType-2]; + p->id = iType; break; } } - return pNew; + return p; } + /* ** This routine deallocates a previously allocated mutex. +** SQLite is careful to deallocate every mutex that it allocates. */ void sqlite3_mutex_free(sqlite3_mutex *p){ assert( p ); - assert( p->cnt==0 ); + assert( p->nRef==0 ); assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ); + DosCloseMutexSem(p->mutex); + free(p->mutexName); sqlite3_free(p); } @@ -87,15 +156,33 @@ void sqlite3_mutex_free(sqlite3_mutex *p){ ** more than once, the behavior is undefined. */ void sqlite3_mutex_enter(sqlite3_mutex *p){ + TID tid; + PID holder1; + ULONG holder2; assert( p ); assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) ); - p->cnt++; + DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT); + DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2); + p->owner = tid; + p->nRef++; } int sqlite3_mutex_try(sqlite3_mutex *p){ + int rc; + TID tid; + PID holder1; + ULONG holder2; assert( p ); assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) ); - p->cnt++; - return SQLITE_OK; + if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR) { + DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2); + p->owner = tid; + p->nRef++; + rc = SQLITE_OK; + } else { + rc = SQLITE_BUSY; + } + + return rc; } /* @@ -105,10 +192,15 @@ int sqlite3_mutex_try(sqlite3_mutex *p){ ** is not currently allocated. SQLite will never do either. */ void sqlite3_mutex_leave(sqlite3_mutex *p){ - assert( p ); - assert( sqlite3_mutex_held(p) ); - p->cnt--; - assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) ); + TID tid; + PID holder1; + ULONG holder2; + assert( p->nRef>0 ); + DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2); + assert( p->owner==tid ); + p->nRef--; + assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); + DosReleaseMutexSem(p->mutex); } /* @@ -116,9 +208,29 @@ void sqlite3_mutex_leave(sqlite3_mutex *p){ ** intended for use inside assert() statements. */ int sqlite3_mutex_held(sqlite3_mutex *p){ - return p==0 || p->cnt>0; + TID tid; + PID pid; + ULONG ulCount; + PTIB ptib; + if( p!=0 ) { + DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount); + } else { + DosGetInfoBlocks(&ptib, NULL); + tid = ptib->tib_ptib2->tib2_ultid; + } + return p==0 || (p->nRef!=0 && p->owner==tid); } int sqlite3_mutex_notheld(sqlite3_mutex *p){ - return p==0 || p->cnt==0; + TID tid; + PID pid; + ULONG ulCount; + PTIB ptib; + if( p!= 0 ) { + DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount); + } else { + DosGetInfoBlocks(&ptib, NULL); + tid = ptib->tib_ptib2->tib2_ultid; + } + return p==0 || p->nRef==0 || p->owner!=tid; } #endif /* SQLITE_MUTEX_OS2 */ |