diff options
author | drh <drh@noemail.net> | 2007-08-28 16:34:42 +0000 |
---|---|---|
committer | drh <drh@noemail.net> | 2007-08-28 16:34:42 +0000 |
commit | 437b9013713bf27ad21776b100c6c4e95d8bdbe5 (patch) | |
tree | 50d90f11b69c7ac695d022d065fd9c17d94aeea0 /src/mutex_os2.c | |
parent | 9da9d96242b7d43383a1f193eb2add750cfd4e29 (diff) | |
download | sqlite-437b9013713bf27ad21776b100c6c4e95d8bdbe5.tar.gz sqlite-437b9013713bf27ad21776b100c6c4e95d8bdbe5.zip |
Break up the mutex implementation into separate source files, one
each for unix, w32, and os2. (CVS 4312)
FossilOrigin-Name: fc5cd71aef5ac194f51d73350d773d532020967e
Diffstat (limited to 'src/mutex_os2.c')
-rw-r--r-- | src/mutex_os2.c | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/mutex_os2.c b/src/mutex_os2.c new file mode 100644 index 000000000..b598f5d90 --- /dev/null +++ b/src/mutex_os2.c @@ -0,0 +1,124 @@ +/* +** 2007 August 28 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** 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 $ +*/ +#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. +****/ + +/* +** The mutex object +*/ +struct sqlite3_mutex { + int id; /* The mutex type */ + int cnt; /* Number of entries without a matching leave */ +}; + +/* +** 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. +*/ +sqlite3_mutex *sqlite3_mutex_alloc(int id){ + static sqlite3_mutex aStatic[4]; + sqlite3_mutex *pNew = 0; + switch( id ){ + case SQLITE_MUTEX_FAST: + case SQLITE_MUTEX_RECURSIVE: { + pNew = sqlite3_malloc(sizeof(*pNew)); + if( pNew ){ + pNew->id = id; + pNew->cnt = 0; + } + break; + } + default: { + assert( id-2 >= 0 ); + assert( id-2 < sizeof(aStatic)/sizeof(aStatic[0]) ); + pNew = &aStatic[id-2]; + pNew->id = id; + break; + } + } + return pNew; +} + +/* +** This routine deallocates a previously allocated mutex. +*/ +void sqlite3_mutex_free(sqlite3_mutex *p){ + assert( p ); + assert( p->cnt==0 ); + assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ); + sqlite3_free(p); +} + +/* +** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt +** to enter a mutex. If another thread is already within the mutex, +** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return +** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK +** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can +** be entered multiple times by the same thread. In such cases the, +** mutex must be exited an equal number of times before another thread +** can enter. If the same thread tries to enter any other kind of mutex +** more than once, the behavior is undefined. +*/ +void sqlite3_mutex_enter(sqlite3_mutex *p){ + assert( p ); + assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) ); + p->cnt++; +} +int sqlite3_mutex_try(sqlite3_mutex *p){ + assert( p ); + assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) ); + p->cnt++; + return SQLITE_OK; +} + +/* +** The sqlite3_mutex_leave() routine exits a mutex that was +** previously entered by the same thread. The behavior +** is undefined if the mutex is not currently entered or +** 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) ); +} + +/* +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are +** intended for use inside assert() statements. +*/ +int sqlite3_mutex_held(sqlite3_mutex *p){ + return p==0 || p->cnt>0; +} +int sqlite3_mutex_notheld(sqlite3_mutex *p){ + return p==0 || p->cnt==0; +} +#endif /* SQLITE_MUTEX_OS2 */ |