wasCSharpSQLite – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Diagnostics; |
||
3 | using System.Threading; |
||
4 | |||
5 | namespace Community.CsharpSqlite |
||
6 | { |
||
7 | public partial class Sqlite3 |
||
8 | { |
||
9 | /* |
||
10 | ** 2007 August 14 |
||
11 | ** |
||
12 | ** The author disclaims copyright to this source code. In place of |
||
13 | ** a legal notice, here is a blessing: |
||
14 | ** |
||
15 | ** May you do good and not evil. |
||
16 | ** May you find forgiveness for yourself and forgive others. |
||
17 | ** May you share freely, never taking more than you give. |
||
18 | ** |
||
19 | ************************************************************************* |
||
20 | ** This file contains the C functions that implement mutexes. |
||
21 | ** |
||
22 | ** This file contains code that is common across all mutex implementations. |
||
23 | ************************************************************************* |
||
24 | ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart |
||
25 | ** C#-SQLite is an independent reimplementation of the SQLite software library |
||
26 | ** |
||
27 | ** SQLITE_SOURCE_ID: 2011-05-19 13:26:54 ed1da510a239ea767a01dc332b667119fa3c908e |
||
28 | ** |
||
29 | ************************************************************************* |
||
30 | */ |
||
31 | //#include "sqliteInt.h" |
||
32 | |||
33 | #if (SQLITE_DEBUG) && !(SQLITE_MUTEX_OMIT) |
||
34 | /* |
||
35 | ** For debugging purposes, record when the mutex subsystem is initialized |
||
36 | ** and uninitialized so that we can assert() if there is an attempt to |
||
37 | ** allocate a mutex while the system is uninitialized. |
||
38 | */ |
||
39 | static int mutexIsInit = 0; |
||
40 | #endif //* SQLITE_DEBUG */ |
||
41 | |||
42 | |||
43 | #if !SQLITE_MUTEX_OMIT |
||
44 | /* |
||
45 | ** Initialize the mutex system. |
||
46 | */ |
||
47 | static int sqlite3MutexInit(){ |
||
48 | int rc = SQLITE_OK; |
||
49 | if( null==sqlite3GlobalConfig.mutex.xMutexAlloc ){ |
||
50 | /* If the xMutexAlloc method has not been set, then the user did not |
||
51 | ** install a mutex implementation via sqlite3_config() prior to |
||
52 | ** sqlite3_initialize() being called. This block copies pointers to |
||
53 | ** the default implementation into the sqlite3GlobalConfig structure. |
||
54 | */ |
||
55 | sqlite3_mutex_methods pFrom; |
||
56 | sqlite3_mutex_methods pTo = sqlite3GlobalConfig.mutex; |
||
57 | |||
58 | if( sqlite3GlobalConfig.bCoreMutex ){ |
||
59 | pFrom = sqlite3DefaultMutex(); |
||
60 | }else{ |
||
61 | pFrom = sqlite3NoopMutex(); |
||
62 | } |
||
63 | //memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc)); |
||
64 | //memcpy(pTo.xMutexFree, pFrom.xMutexFree, |
||
65 | // sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree)); |
||
66 | pTo.Copy(pFrom); |
||
67 | } |
||
68 | rc = sqlite3GlobalConfig.mutex.xMutexInit(); |
||
69 | |||
70 | #if SQLITE_DEBUG |
||
71 | mutexIsInit = 1; //GLOBAL(int, mutexIsInit) = 1; |
||
72 | #endif |
||
73 | |||
74 | return rc; |
||
75 | } |
||
76 | |||
77 | /* |
||
78 | ** Shutdown the mutex system. This call frees resources allocated by |
||
79 | ** sqlite3MutexInit(). |
||
80 | */ |
||
81 | static int sqlite3MutexEnd(){ |
||
82 | int rc = SQLITE_OK; |
||
83 | if( sqlite3GlobalConfig.mutex.xMutexEnd !=null){ |
||
84 | rc = sqlite3GlobalConfig.mutex.xMutexEnd(); |
||
85 | } |
||
86 | |||
87 | #if SQLITE_DEBUG |
||
88 | mutexIsInit = 0;//GLOBAL(int, mutexIsInit) = 0; |
||
89 | #endif |
||
90 | |||
91 | return rc; |
||
92 | } |
||
93 | |||
94 | /* |
||
95 | ** Retrieve a pointer to a static mutex or allocate a new dynamic one. |
||
96 | */ |
||
97 | static sqlite3_mutex sqlite3_mutex_alloc(int id){ |
||
98 | #if !SQLITE_OMIT_AUTOINIT |
||
99 | if( sqlite3_initialize()!=0 ) return null; |
||
100 | #endif |
||
101 | return sqlite3GlobalConfig.mutex.xMutexAlloc(id); |
||
102 | } |
||
103 | |||
104 | static sqlite3_mutex sqlite3MutexAlloc(int id){ |
||
105 | if( !sqlite3GlobalConfig.bCoreMutex ){ |
||
106 | return null; |
||
107 | } |
||
108 | Debug.Assert( mutexIsInit !=0 );//assert( GLOBAL(int, mutexIsInit) ); |
||
109 | return sqlite3GlobalConfig.mutex.xMutexAlloc(id); |
||
110 | } |
||
111 | |||
112 | /* |
||
113 | ** Free a dynamic mutex. |
||
114 | */ |
||
115 | static void sqlite3_mutex_free( sqlite3_mutex p){ |
||
116 | if( p!=null ){ |
||
117 | sqlite3GlobalConfig.mutex.xMutexFree( p); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | /* |
||
122 | ** Obtain the mutex p. If some other thread already has the mutex, block |
||
123 | ** until it can be obtained. |
||
124 | */ |
||
125 | static void sqlite3_mutex_enter(sqlite3_mutex p){ |
||
126 | if( p !=null){ |
||
127 | sqlite3GlobalConfig.mutex.xMutexEnter(p); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /* |
||
132 | ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another |
||
133 | ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY. |
||
134 | */ |
||
135 | static int sqlite3_mutex_try(sqlite3_mutex p){ |
||
136 | int rc = SQLITE_OK; |
||
137 | if( p!=null ){ |
||
138 | return sqlite3GlobalConfig.mutex.xMutexTry(p); |
||
139 | } |
||
140 | return rc; |
||
141 | } |
||
142 | |||
143 | /* |
||
144 | ** The sqlite3_mutex_leave() routine exits a mutex that was previously |
||
145 | ** entered by the same thread. The behavior is undefined if the mutex |
||
146 | ** is not currently entered. If a NULL pointer is passed as an argument |
||
147 | ** this function is a no-op. |
||
148 | */ |
||
149 | static void sqlite3_mutex_leave(sqlite3_mutex p){ |
||
150 | if( p !=null){ |
||
151 | sqlite3GlobalConfig.mutex.xMutexLeave(p); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | #if !NDEBUG |
||
156 | /* |
||
157 | ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are |
||
158 | ** intended for use inside assert() statements. |
||
159 | */ |
||
160 | static bool sqlite3_mutex_held(sqlite3_mutex p){ |
||
161 | return p==null || sqlite3GlobalConfig.mutex.xMutexHeld(p); |
||
162 | } |
||
163 | |||
164 | static bool sqlite3_mutex_notheld(sqlite3_mutex p){ |
||
165 | return p == null || sqlite3GlobalConfig.mutex.xMutexNotheld( p ); |
||
166 | } |
||
167 | #endif |
||
168 | |||
169 | #endif //* SQLITE_MUTEX_OMIT */ |
||
170 | } |
||
171 | } |