-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathmutex_c.cs
More file actions
executable file
·171 lines (152 loc) · 4.59 KB
/
mutex_c.cs
File metadata and controls
executable file
·171 lines (152 loc) · 4.59 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
using System;
using System.Diagnostics;
using System.Threading;
namespace System.Data.SQLite
{
public partial class Sqlite3
{
/*
** 2007 August 14
**
** 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 file contains code that is common across all mutex implementations.
*************************************************************************
** 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: 2011-05-19 13:26:54 ed1da510a239ea767a01dc332b667119fa3c908e
**
*************************************************************************
*/
//#include "sqliteInt.h"
#if (SQLITE_DEBUG) && !(SQLITE_MUTEX_OMIT)
/*
** For debugging purposes, record when the mutex subsystem is initialized
** and uninitialized so that we can assert() if there is an attempt to
** allocate a mutex while the system is uninitialized.
*/
static int mutexIsInit = 0;
#endif //* SQLITE_DEBUG */
#if !SQLITE_MUTEX_OMIT
/*
** Initialize the mutex system.
*/
static int sqlite3MutexInit(){
int rc = SQLITE_OK;
if( null==sqlite3GlobalConfig.mutex.xMutexAlloc ){
/* If the xMutexAlloc method has not been set, then the user did not
** install a mutex implementation via sqlite3_config() prior to
** sqlite3_initialize() being called. This block copies pointers to
** the default implementation into the sqlite3GlobalConfig structure.
*/
sqlite3_mutex_methods pFrom;
sqlite3_mutex_methods pTo = sqlite3GlobalConfig.mutex;
if( sqlite3GlobalConfig.bCoreMutex ){
pFrom = sqlite3DefaultMutex();
}else{
pFrom = sqlite3NoopMutex();
}
//memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
//memcpy(pTo.xMutexFree, pFrom.xMutexFree,
// sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
pTo.Copy(pFrom);
}
rc = sqlite3GlobalConfig.mutex.xMutexInit();
#if SQLITE_DEBUG
mutexIsInit = 1; //GLOBAL(int, mutexIsInit) = 1;
#endif
return rc;
}
/*
** Shutdown the mutex system. This call frees resources allocated by
** sqlite3MutexInit().
*/
static int sqlite3MutexEnd(){
int rc = SQLITE_OK;
if( sqlite3GlobalConfig.mutex.xMutexEnd !=null){
rc = sqlite3GlobalConfig.mutex.xMutexEnd();
}
#if SQLITE_DEBUG
mutexIsInit = 0;//GLOBAL(int, mutexIsInit) = 0;
#endif
return rc;
}
/*
** Retrieve a pointer to a static mutex or allocate a new dynamic one.
*/
static sqlite3_mutex sqlite3_mutex_alloc(int id){
#if !SQLITE_OMIT_AUTOINIT
if( sqlite3_initialize()!=0 ) return null;
#endif
return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
}
static sqlite3_mutex sqlite3MutexAlloc(int id){
if( !sqlite3GlobalConfig.bCoreMutex ){
return null;
}
Debug.Assert( mutexIsInit !=0 );//assert( GLOBAL(int, mutexIsInit) );
return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
}
/*
** Free a dynamic mutex.
*/
static void sqlite3_mutex_free( sqlite3_mutex p){
if( p!=null ){
sqlite3GlobalConfig.mutex.xMutexFree( p);
}
}
/*
** Obtain the mutex p. If some other thread already has the mutex, block
** until it can be obtained.
*/
static void sqlite3_mutex_enter(sqlite3_mutex p){
if( p !=null){
sqlite3GlobalConfig.mutex.xMutexEnter(p);
}
}
/*
** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
*/
static int sqlite3_mutex_try(sqlite3_mutex p){
int rc = SQLITE_OK;
if( p!=null ){
return sqlite3GlobalConfig.mutex.xMutexTry(p);
}
return rc;
}
/*
** 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. If a NULL pointer is passed as an argument
** this function is a no-op.
*/
static void sqlite3_mutex_leave(sqlite3_mutex p){
if( p !=null){
sqlite3GlobalConfig.mutex.xMutexLeave(p);
}
}
#if !NDEBUG
/*
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
** intended for use inside assert() statements.
*/
static bool sqlite3_mutex_held(sqlite3_mutex p){
return p==null || sqlite3GlobalConfig.mutex.xMutexHeld(p);
}
static bool sqlite3_mutex_notheld(sqlite3_mutex p){
return p == null || sqlite3GlobalConfig.mutex.xMutexNotheld( p );
}
#endif
#endif //* SQLITE_MUTEX_OMIT */
}
}