-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathmutex_noop_c.cs
More file actions
executable file
·243 lines (224 loc) · 6.91 KB
/
mutex_noop_c.cs
File metadata and controls
executable file
·243 lines (224 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Diagnostics;
using System.Threading;
namespace System.Data.SQLite
{
public partial class Sqlite3
{
/*
** 2008 October 07
**
** 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.
**
** This implementation in this file does not provide any mutual
** exclusion and is thus suitable for use only in applications
** that use SQLite in a single thread. The routines defined
** here are place-holders. Applications can substitute working
** mutex routines at start-time using the
**
** sqlite3_config(SQLITE_CONFIG_MUTEX,...)
**
** interface.
**
** If compiled with SQLITE_DEBUG, then additional logic is inserted
** that does error checking on mutexes to make sure they are being
** called correctly.
*************************************************************************
** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart
** C#-SQLite is an independent reimplementation of the SQLite software library
**
** SQLITE_SOURCE_ID: 2009-12-07 16:39:13 1ed88e9d01e9eda5cbc622e7614277f29bcc551c
**
*************************************************************************
*/
//#include "sqliteInt.h"
#if !SQLITE_DEBUG
/*
** Stub routines for all mutex methods.
**
** This routines provide no mutual exclusion or error checking.
*/
static int noopMutexHeld(sqlite3_mutex p) { return 1; }
static int noopMutexNotheld(sqlite3_mutex p) { return 1; }
static int noopMutexInit() { return SQLITE_OK; }
static int noopMutexEnd() { return SQLITE_OK; }
static sqlite3_mutex noopMutexAlloc(int id) { return new sqlite3_mutex(); }
static void noopMutexFree(sqlite3_mutex p) { }
static void noopMutexEnter(sqlite3_mutex p) { }
static int noopMutexTry(sqlite3_mutex p) { return SQLITE_OK; }
static void noopMutexLeave(sqlite3_mutex p) { }
sqlite3_mutex_methods sqlite3DefaultMutex()
{
sqlite3_mutex_methods sMutex = new sqlite3_mutex_methods(
(dxMutexInit)noopMutexInit,
(dxMutexEnd)noopMutexEnd,
(dxMutexAlloc)noopMutexAlloc,
(dxMutexFree)noopMutexFree,
(dxMutexEnter)noopMutexEnter,
(dxMutexTry)noopMutexTry,
(dxMutexLeave)noopMutexLeave,
#if SQLITE_DEBUG
(dxMutexHeld)noopMutexHeld,
(dxMutexNotheld)noopMutexNotheld
#else
null,
null
#endif
);
return sMutex;
}
#endif //* !SQLITE_DEBUG */
#if SQLITE_DEBUG && !SQLITE_MUTEX_OMIT
/*
** In this implementation, error checking is provided for testing
** and debugging purposes. The mutexes still do not provide any
** mutual exclusion.
*/
/*
** The mutex object
*/
public class sqlite3_debug_mutex : sqlite3_mutex
{
//public int id; /* The mutex type */
public int cnt; /* Number of entries without a matching leave */
};
/*
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
** intended for use inside Debug.Assert() statements.
*/
static bool debugMutexHeld( sqlite3_mutex pX )
{
sqlite3_debug_mutex p = (sqlite3_debug_mutex)pX;
return p == null || p.cnt > 0;
}
static bool debugMutexNotheld( sqlite3_mutex pX )
{
sqlite3_debug_mutex p = (sqlite3_debug_mutex)pX;
return p == null || p.cnt == 0;
}
/*
** Initialize and deinitialize the mutex subsystem.
*/
static int debugMutexInit()
{
return SQLITE_OK;
}
static int debugMutexEnd()
{
return SQLITE_OK;
}
/*
** 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.
*/
static sqlite3_mutex debugMutexAlloc( int id )
{
sqlite3_debug_mutex[] aStatic = new sqlite3_debug_mutex[6];
sqlite3_debug_mutex pNew = null;
switch ( id )
{
case SQLITE_MUTEX_FAST:
case SQLITE_MUTEX_RECURSIVE:
{
pNew = new sqlite3_debug_mutex();//sqlite3Malloc(sizeof(*pNew));
if ( pNew != null )
{
pNew.id = id;
pNew.cnt = 0;
}
break;
}
default:
{
Debug.Assert( id - 2 >= 0 );
Debug.Assert( id - 2 < aStatic.Length );//(int)(sizeof(aStatic)/sizeof(aStatic[0])) );
pNew = aStatic[id - 2];
pNew.id = id;
break;
}
}
return pNew;
}
/*
** This routine deallocates a previously allocated mutex.
*/
static void debugMutexFree( sqlite3_mutex pX )
{
sqlite3_debug_mutex p = (sqlite3_debug_mutex)pX;
Debug.Assert( p.cnt == 0 );
Debug.Assert( p.id == SQLITE_MUTEX_FAST || p.id == SQLITE_MUTEX_RECURSIVE );
//sqlite3_free(ref 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.
*/
static void debugMutexEnter( sqlite3_mutex pX )
{
sqlite3_debug_mutex p = (sqlite3_debug_mutex)pX;
Debug.Assert( p.id == SQLITE_MUTEX_RECURSIVE || debugMutexNotheld( p ) );
p.cnt++;
}
static int debugMutexTry( sqlite3_mutex pX )
{
sqlite3_debug_mutex p = (sqlite3_debug_mutex)pX;
Debug.Assert( p.id == SQLITE_MUTEX_RECURSIVE || debugMutexNotheld( 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.
*/
static void debugMutexLeave( sqlite3_mutex pX )
{
sqlite3_debug_mutex p = (sqlite3_debug_mutex)pX;
Debug.Assert( debugMutexHeld( p ) );
p.cnt--;
Debug.Assert( p.id == SQLITE_MUTEX_RECURSIVE || debugMutexNotheld( p ) );
}
static sqlite3_mutex_methods sqlite3NoopMutex()
{
sqlite3_mutex_methods sMutex = new sqlite3_mutex_methods(
(dxMutexInit)debugMutexInit,
(dxMutexEnd)debugMutexEnd,
(dxMutexAlloc)debugMutexAlloc,
(dxMutexFree)debugMutexFree,
(dxMutexEnter)debugMutexEnter,
(dxMutexTry)debugMutexTry,
(dxMutexLeave)debugMutexLeave,
(dxMutexHeld)debugMutexHeld,
(dxMutexNotheld)debugMutexNotheld
);
return sMutex;
}
#endif //* SQLITE_DEBUG */
/*
** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
** is used regardless of the run-time threadsafety setting.
*/
#if SQLITE_MUTEX_NOOP
sqlite3_mutex_methods const sqlite3DefaultMutex(void){
return sqlite3NoopMutex();
}
#endif //* SQLITE_MUTEX_NOOP */
}
}