1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sqlite/sqlite-source/mutex.c Sun Oct 28 05:48:06 2007 +0000
1.3 @@ -0,0 +1,126 @@
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.
1.16 +**
1.17 +** The implementation in this file does not provide any mutual
1.18 +** exclusion and is thus suitable for use only in applications
1.19 +** that use SQLite in a single thread. But this implementation
1.20 +** does do a lot of error checking on mutexes to make sure they
1.21 +** are called correctly and at appropriate times. Hence, this
1.22 +** implementation is suitable for testing.
1.23 +** debugging purposes
1.24 +**
1.25 +** $Id$
1.26 +*/
1.27 +#include "sqliteInt.h"
1.28 +
1.29 +#ifdef SQLITE_MUTEX_NOOP_DEBUG
1.30 +/*
1.31 +** In this implementation, mutexes do not provide any mutual exclusion.
1.32 +** But the error checking is provided. This implementation is useful
1.33 +** for test purposes.
1.34 +*/
1.35 +
1.36 +/*
1.37 +** The mutex object
1.38 +*/
1.39 +struct sqlite3_mutex {
1.40 + int id; /* The mutex type */
1.41 + int cnt; /* Number of entries without a matching leave */
1.42 +};
1.43 +
1.44 +/*
1.45 +** The sqlite3_mutex_alloc() routine allocates a new
1.46 +** mutex and returns a pointer to it. If it returns NULL
1.47 +** that means that a mutex could not be allocated.
1.48 +*/
1.49 +sqlite3_mutex *sqlite3_mutex_alloc(int id){
1.50 + static sqlite3_mutex aStatic[5];
1.51 + sqlite3_mutex *pNew = 0;
1.52 + switch( id ){
1.53 + case SQLITE_MUTEX_FAST:
1.54 + case SQLITE_MUTEX_RECURSIVE: {
1.55 + pNew = sqlite3_malloc(sizeof(*pNew));
1.56 + if( pNew ){
1.57 + pNew->id = id;
1.58 + pNew->cnt = 0;
1.59 + }
1.60 + break;
1.61 + }
1.62 + default: {
1.63 + assert( id-2 >= 0 );
1.64 + assert( id-2 < sizeof(aStatic)/sizeof(aStatic[0]) );
1.65 + pNew = &aStatic[id-2];
1.66 + pNew->id = id;
1.67 + break;
1.68 + }
1.69 + }
1.70 + return pNew;
1.71 +}
1.72 +
1.73 +/*
1.74 +** This routine deallocates a previously allocated mutex.
1.75 +*/
1.76 +void sqlite3_mutex_free(sqlite3_mutex *p){
1.77 + assert( p );
1.78 + assert( p->cnt==0 );
1.79 + assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
1.80 + sqlite3_free(p);
1.81 +}
1.82 +
1.83 +/*
1.84 +** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
1.85 +** to enter a mutex. If another thread is already within the mutex,
1.86 +** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
1.87 +** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
1.88 +** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
1.89 +** be entered multiple times by the same thread. In such cases the,
1.90 +** mutex must be exited an equal number of times before another thread
1.91 +** can enter. If the same thread tries to enter any other kind of mutex
1.92 +** more than once, the behavior is undefined.
1.93 +*/
1.94 +void sqlite3_mutex_enter(sqlite3_mutex *p){
1.95 + assert( p );
1.96 + assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
1.97 + p->cnt++;
1.98 +}
1.99 +int sqlite3_mutex_try(sqlite3_mutex *p){
1.100 + assert( p );
1.101 + assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
1.102 + p->cnt++;
1.103 + return SQLITE_OK;
1.104 +}
1.105 +
1.106 +/*
1.107 +** The sqlite3_mutex_leave() routine exits a mutex that was
1.108 +** previously entered by the same thread. The behavior
1.109 +** is undefined if the mutex is not currently entered or
1.110 +** is not currently allocated. SQLite will never do either.
1.111 +*/
1.112 +void sqlite3_mutex_leave(sqlite3_mutex *p){
1.113 + assert( p );
1.114 + assert( sqlite3_mutex_held(p) );
1.115 + p->cnt--;
1.116 + assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
1.117 +}
1.118 +
1.119 +/*
1.120 +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
1.121 +** intended for use inside assert() statements.
1.122 +*/
1.123 +int sqlite3_mutex_held(sqlite3_mutex *p){
1.124 + return p==0 || p->cnt>0;
1.125 +}
1.126 +int sqlite3_mutex_notheld(sqlite3_mutex *p){
1.127 + return p==0 || p->cnt==0;
1.128 +}
1.129 +#endif /* SQLITE_MUTEX_NOOP_DEBUG */