From ec70b6b02f2493e4e1fe0eaa66de94c538798b5c Mon Sep 17 00:00:00 2001 From: pweilbacher Date: Thu, 20 Sep 2007 21:40:23 +0000 Subject: [PATCH] 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 --- manifest | 14 ++-- manifest.uuid | 2 +- src/mutex_os2.c | 176 +++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 152 insertions(+), 40 deletions(-) diff --git a/manifest b/manifest index 877af76d82..26043a20d3 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Replace\s"i64"\swith\s"sqlite3_int64"\sin\sthe\sw32\sVFS.\s(CVS\s4441) -D 2007-09-20T14:39:24 +C Initial\sattempt\sof\sthe\snew\sOS/2\smutex\simplementation.\s(Compiles\sand\san\sattempt\sto\screate\sa\snew\stable\sdoes\snot\scrash\sin\smutex_os2\sany\smore.)\s(CVS\s4442) +D 2007-09-20T21:40:23 F Makefile.in cbfb898945536a8f9ea8b897e1586dd1fdbcc5db F Makefile.linux-gcc 65241babba6faf1152bf86574477baab19190499 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 @@ -106,7 +106,7 @@ F src/mem1.c 1f85902b98b38bd8a8b0c24012933957256db13e F src/mem2.c b97b4662bf5902cbde0a849c4739e64ce7b07177 F src/mutex.c 3259f62c2429967aee6dc112117a6d2f499ef061 F src/mutex.h 079fa6fe9da18ceb89e79012c010594c6672addb -F src/mutex_os2.c d47e9bd495583dd31263d8fe55160a31eb600a3c +F src/mutex_os2.c 07a2d5db7a6b1f6716dbb44dfaa79e9f24bcf20c F src/mutex_unix.c ff77650261a245035b79c5c8a174f4e05d3cae8a F src/mutex_w32.c d2c56fb81aca10af1577bdae2a4083eb2505f8ee F src/os.c 3b66834a5853ddaa83dfd6c146be9e4fc1864b98 @@ -580,7 +580,7 @@ F www/tclsqlite.tcl 8be95ee6dba05eabcd27a9d91331c803f2ce2130 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5 -P ac645c8f30aac0d98fc481260084c9bd3975a845 -R 35dd856dee6b05cd6c330ea61be3221e -U drh -Z d53eea7e4130e4187dcb9f3c7d105bcc +P 138d3fcc5a74eb570107ae1299184a318b5417df +R e9894f36c93ccc5a5ee19342f2f8d077 +U pweilbacher +Z de52f251ffc6cc3de4422ed1c0ef5cd9 diff --git a/manifest.uuid b/manifest.uuid index 62467c3247..7a1b90c0fb 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -138d3fcc5a74eb570107ae1299184a318b5417df \ No newline at end of file +aa61b244252399cce3b9c1ece9c6816ae9cb6717 \ No newline at end of file diff --git a/src/mutex_os2.c b/src/mutex_os2.c index b598f5d905..f175e0f815 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: +** +** +** +** 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 */