1 /*
2 ** 2012-01-30
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 **
13 ** Mutex functions for LSM.
14 */
15 #include "lsmInt.h"
16 
17 /*
18 ** Allocate a new mutex.
19 */
lsmMutexNew(lsm_env * pEnv,lsm_mutex ** ppNew)20 int lsmMutexNew(lsm_env *pEnv, lsm_mutex **ppNew){
21   return pEnv->xMutexNew(pEnv, ppNew);
22 }
23 
24 /*
25 ** Return a handle for one of the static mutexes.
26 */
lsmMutexStatic(lsm_env * pEnv,int iMutex,lsm_mutex ** ppStatic)27 int lsmMutexStatic(lsm_env *pEnv, int iMutex, lsm_mutex **ppStatic){
28   return pEnv->xMutexStatic(pEnv, iMutex, ppStatic);
29 }
30 
31 /*
32 ** Free a mutex allocated by lsmMutexNew().
33 */
lsmMutexDel(lsm_env * pEnv,lsm_mutex * pMutex)34 void lsmMutexDel(lsm_env *pEnv, lsm_mutex *pMutex){
35   if( pMutex ) pEnv->xMutexDel(pMutex);
36 }
37 
38 /*
39 ** Enter a mutex.
40 */
lsmMutexEnter(lsm_env * pEnv,lsm_mutex * pMutex)41 void lsmMutexEnter(lsm_env *pEnv, lsm_mutex *pMutex){
42   pEnv->xMutexEnter(pMutex);
43 }
44 
45 /*
46 ** Attempt to enter a mutex, but do not block. If successful, return zero.
47 ** Otherwise, if the mutex is already held by some other thread and is not
48 ** entered, return non zero.
49 **
50 ** Each successful call to this function must be matched by a call to
51 ** lsmMutexLeave().
52 */
lsmMutexTry(lsm_env * pEnv,lsm_mutex * pMutex)53 int lsmMutexTry(lsm_env *pEnv, lsm_mutex *pMutex){
54   return pEnv->xMutexTry(pMutex);
55 }
56 
57 /*
58 ** Leave a mutex.
59 */
lsmMutexLeave(lsm_env * pEnv,lsm_mutex * pMutex)60 void lsmMutexLeave(lsm_env *pEnv, lsm_mutex *pMutex){
61   pEnv->xMutexLeave(pMutex);
62 }
63 
64 #ifndef NDEBUG
65 /*
66 ** Return non-zero if the mutex passed as the second argument is held
67 ** by the calling thread, or zero otherwise. If the implementation is not
68 ** able to tell if the mutex is held by the caller, it should return
69 ** non-zero.
70 **
71 ** This function is only used as part of assert() statements.
72 */
lsmMutexHeld(lsm_env * pEnv,lsm_mutex * pMutex)73 int lsmMutexHeld(lsm_env *pEnv, lsm_mutex *pMutex){
74   return pEnv->xMutexHeld ? pEnv->xMutexHeld(pMutex) : 1;
75 }
76 
77 /*
78 ** Return non-zero if the mutex passed as the second argument is not
79 ** held by the calling thread, or zero otherwise. If the implementation
80 ** is not able to tell if the mutex is held by the caller, it should
81 ** return non-zero.
82 **
83 ** This function is only used as part of assert() statements.
84 */
lsmMutexNotHeld(lsm_env * pEnv,lsm_mutex * pMutex)85 int lsmMutexNotHeld(lsm_env *pEnv, lsm_mutex *pMutex){
86   return pEnv->xMutexNotHeld ? pEnv->xMutexNotHeld(pMutex) : 1;
87 }
88 #endif
89