| author | Scott Ehlert <ds@alliedmods.net> |
| Sun Mar 30 07:00:22 2008 +0000 (2008-03-30) | |
| changeset 1952 | b7aa2b626807 |
| permissions | -rw-r--r-- |
| ds@1952 | 1 |
/* |
| ds@1952 | 2 |
** 2007 August 14 |
| ds@1952 | 3 |
** |
| ds@1952 | 4 |
** The author disclaims copyright to this source code. In place of |
| ds@1952 | 5 |
** a legal notice, here is a blessing: |
| ds@1952 | 6 |
** |
| ds@1952 | 7 |
** May you do good and not evil. |
| ds@1952 | 8 |
** May you find forgiveness for yourself and forgive others. |
| ds@1952 | 9 |
** May you share freely, never taking more than you give. |
| ds@1952 | 10 |
** |
| ds@1952 | 11 |
************************************************************************* |
| ds@1952 | 12 |
** This file contains the C functions that implement mutexes. |
| ds@1952 | 13 |
** |
| ds@1952 | 14 |
** The implementation in this file does not provide any mutual |
| ds@1952 | 15 |
** exclusion and is thus suitable for use only in applications |
| ds@1952 | 16 |
** that use SQLite in a single thread. But this implementation |
| ds@1952 | 17 |
** does do a lot of error checking on mutexes to make sure they |
| ds@1952 | 18 |
** are called correctly and at appropriate times. Hence, this |
| ds@1952 | 19 |
** implementation is suitable for testing. |
| ds@1952 | 20 |
** debugging purposes |
| ds@1952 | 21 |
** |
| ds@1952 | 22 |
** $Id$ |
| ds@1952 | 23 |
*/ |
| ds@1952 | 24 |
#include "sqliteInt.h" |
| ds@1952 | 25 |
|
| ds@1952 | 26 |
#ifdef SQLITE_MUTEX_NOOP_DEBUG |
| ds@1952 | 27 |
/* |
| ds@1952 | 28 |
** In this implementation, mutexes do not provide any mutual exclusion. |
| ds@1952 | 29 |
** But the error checking is provided. This implementation is useful |
| ds@1952 | 30 |
** for test purposes. |
| ds@1952 | 31 |
*/ |
| ds@1952 | 32 |
|
| ds@1952 | 33 |
/* |
| ds@1952 | 34 |
** The mutex object |
| ds@1952 | 35 |
*/ |
| ds@1952 | 36 |
struct sqlite3_mutex { |
| ds@1952 | 37 |
int id; /* The mutex type */ |
| ds@1952 | 38 |
int cnt; /* Number of entries without a matching leave */ |
| ds@1952 | 39 |
}; |
| ds@1952 | 40 |
|
| ds@1952 | 41 |
/* |
| ds@1952 | 42 |
** The sqlite3_mutex_alloc() routine allocates a new |
| ds@1952 | 43 |
** mutex and returns a pointer to it. If it returns NULL |
| ds@1952 | 44 |
** that means that a mutex could not be allocated. |
| ds@1952 | 45 |
*/ |
| ds@1952 | 46 |
sqlite3_mutex *sqlite3_mutex_alloc(int id){ |
| ds@1952 | 47 |
static sqlite3_mutex aStatic[5]; |
| ds@1952 | 48 |
sqlite3_mutex *pNew = 0; |
| ds@1952 | 49 |
switch( id ){ |
| ds@1952 | 50 |
case SQLITE_MUTEX_FAST: |
| ds@1952 | 51 |
case SQLITE_MUTEX_RECURSIVE: { |
| ds@1952 | 52 |
pNew = sqlite3_malloc(sizeof(*pNew)); |
| ds@1952 | 53 |
if( pNew ){ |
| ds@1952 | 54 |
pNew->id = id; |
| ds@1952 | 55 |
pNew->cnt = 0; |
| ds@1952 | 56 |
} |
| ds@1952 | 57 |
break; |
| ds@1952 | 58 |
} |
| ds@1952 | 59 |
default: { |
| ds@1952 | 60 |
assert( id-2 >= 0 ); |
| ds@1952 | 61 |
assert( id-2 < sizeof(aStatic)/sizeof(aStatic[0]) ); |
| ds@1952 | 62 |
pNew = &aStatic[id-2]; |
| ds@1952 | 63 |
pNew->id = id; |
| ds@1952 | 64 |
break; |
| ds@1952 | 65 |
} |
| ds@1952 | 66 |
} |
| ds@1952 | 67 |
return pNew; |
| ds@1952 | 68 |
} |
| ds@1952 | 69 |
|
| ds@1952 | 70 |
/* |
| ds@1952 | 71 |
** This routine deallocates a previously allocated mutex. |
| ds@1952 | 72 |
*/ |
| ds@1952 | 73 |
void sqlite3_mutex_free(sqlite3_mutex *p){ |
| ds@1952 | 74 |
assert( p ); |
| ds@1952 | 75 |
assert( p->cnt==0 ); |
| ds@1952 | 76 |
assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ); |
| ds@1952 | 77 |
sqlite3_free(p); |
| ds@1952 | 78 |
} |
| ds@1952 | 79 |
|
| ds@1952 | 80 |
/* |
| ds@1952 | 81 |
** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt |
| ds@1952 | 82 |
** to enter a mutex. If another thread is already within the mutex, |
| ds@1952 | 83 |
** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return |
| ds@1952 | 84 |
** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK |
| ds@1952 | 85 |
** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can |
| ds@1952 | 86 |
** be entered multiple times by the same thread. In such cases the, |
| ds@1952 | 87 |
** mutex must be exited an equal number of times before another thread |
| ds@1952 | 88 |
** can enter. If the same thread tries to enter any other kind of mutex |
| ds@1952 | 89 |
** more than once, the behavior is undefined. |
| ds@1952 | 90 |
*/ |
| ds@1952 | 91 |
void sqlite3_mutex_enter(sqlite3_mutex *p){ |
| ds@1952 | 92 |
assert( p ); |
| ds@1952 | 93 |
assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) ); |
| ds@1952 | 94 |
p->cnt++; |
| ds@1952 | 95 |
} |
| ds@1952 | 96 |
int sqlite3_mutex_try(sqlite3_mutex *p){ |
| ds@1952 | 97 |
assert( p ); |
| ds@1952 | 98 |
assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) ); |
| ds@1952 | 99 |
p->cnt++; |
| ds@1952 | 100 |
return SQLITE_OK; |
| ds@1952 | 101 |
} |
| ds@1952 | 102 |
|
| ds@1952 | 103 |
/* |
| ds@1952 | 104 |
** The sqlite3_mutex_leave() routine exits a mutex that was |
| ds@1952 | 105 |
** previously entered by the same thread. The behavior |
| ds@1952 | 106 |
** is undefined if the mutex is not currently entered or |
| ds@1952 | 107 |
** is not currently allocated. SQLite will never do either. |
| ds@1952 | 108 |
*/ |
| ds@1952 | 109 |
void sqlite3_mutex_leave(sqlite3_mutex *p){ |
| ds@1952 | 110 |
assert( p ); |
| ds@1952 | 111 |
assert( sqlite3_mutex_held(p) ); |
| ds@1952 | 112 |
p->cnt--; |
| ds@1952 | 113 |
assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) ); |
| ds@1952 | 114 |
} |
| ds@1952 | 115 |
|
| ds@1952 | 116 |
/* |
| ds@1952 | 117 |
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are |
| ds@1952 | 118 |
** intended for use inside assert() statements. |
| ds@1952 | 119 |
*/ |
| ds@1952 | 120 |
int sqlite3_mutex_held(sqlite3_mutex *p){ |
| ds@1952 | 121 |
return p==0 || p->cnt>0; |
| ds@1952 | 122 |
} |
| ds@1952 | 123 |
int sqlite3_mutex_notheld(sqlite3_mutex *p){ |
| ds@1952 | 124 |
return p==0 || p->cnt==0; |
| ds@1952 | 125 |
} |
| ds@1952 | 126 |
#endif /* SQLITE_MUTEX_NOOP_DEBUG */ |