sl@0
|
1 |
/*
|
sl@0
|
2 |
** 2006 January 07
|
sl@0
|
3 |
**
|
sl@0
|
4 |
** The author disclaims copyright to this source code. In place of
|
sl@0
|
5 |
** a legal notice, here is a blessing:
|
sl@0
|
6 |
**
|
sl@0
|
7 |
** May you do good and not evil.
|
sl@0
|
8 |
** May you find forgiveness for yourself and forgive others.
|
sl@0
|
9 |
** May you share freely, never taking more than you give.
|
sl@0
|
10 |
**
|
sl@0
|
11 |
******************************************************************************
|
sl@0
|
12 |
**
|
sl@0
|
13 |
** $Id: test_server.c,v 1.8 2008/06/26 10:41:19 danielk1977 Exp $
|
sl@0
|
14 |
**
|
sl@0
|
15 |
** This file contains demonstration code. Nothing in this file gets compiled
|
sl@0
|
16 |
** or linked into the SQLite library unless you use a non-standard option:
|
sl@0
|
17 |
**
|
sl@0
|
18 |
** -DSQLITE_SERVER=1
|
sl@0
|
19 |
**
|
sl@0
|
20 |
** The configure script will never generate a Makefile with the option
|
sl@0
|
21 |
** above. You will need to manually modify the Makefile if you want to
|
sl@0
|
22 |
** include any of the code from this file in your project. Or, at your
|
sl@0
|
23 |
** option, you may copy and paste the code from this file and
|
sl@0
|
24 |
** thereby avoiding a recompile of SQLite.
|
sl@0
|
25 |
**
|
sl@0
|
26 |
**
|
sl@0
|
27 |
** This source file demonstrates how to use SQLite to create an SQL database
|
sl@0
|
28 |
** server thread in a multiple-threaded program. One or more client threads
|
sl@0
|
29 |
** send messages to the server thread and the server thread processes those
|
sl@0
|
30 |
** messages in the order received and returns the results to the client.
|
sl@0
|
31 |
**
|
sl@0
|
32 |
** One might ask: "Why bother? Why not just let each thread connect
|
sl@0
|
33 |
** to the database directly?" There are a several of reasons to
|
sl@0
|
34 |
** prefer the client/server approach.
|
sl@0
|
35 |
**
|
sl@0
|
36 |
** (1) Some systems (ex: Redhat9) have broken threading implementations
|
sl@0
|
37 |
** that prevent SQLite database connections from being used in
|
sl@0
|
38 |
** a thread different from the one where they were created. With
|
sl@0
|
39 |
** the client/server approach, all database connections are created
|
sl@0
|
40 |
** and used within the server thread. Client calls to the database
|
sl@0
|
41 |
** can be made from multiple threads (though not at the same time!)
|
sl@0
|
42 |
**
|
sl@0
|
43 |
** (2) Beginning with SQLite version 3.3.0, when two or more
|
sl@0
|
44 |
** connections to the same database occur within the same thread,
|
sl@0
|
45 |
** they can optionally share their database cache. This reduces
|
sl@0
|
46 |
** I/O and memory requirements. Cache shared is controlled using
|
sl@0
|
47 |
** the sqlite3_enable_shared_cache() API.
|
sl@0
|
48 |
**
|
sl@0
|
49 |
** (3) Database connections on a shared cache use table-level locking
|
sl@0
|
50 |
** instead of file-level locking for improved concurrency.
|
sl@0
|
51 |
**
|
sl@0
|
52 |
** (4) Database connections on a shared cache can by optionally
|
sl@0
|
53 |
** set to READ UNCOMMITTED isolation. (The default isolation for
|
sl@0
|
54 |
** SQLite is SERIALIZABLE.) When this occurs, readers will
|
sl@0
|
55 |
** never be blocked by a writer and writers will not be
|
sl@0
|
56 |
** blocked by readers. There can still only be a single writer
|
sl@0
|
57 |
** at a time, but multiple readers can simultaneously exist with
|
sl@0
|
58 |
** that writer. This is a huge increase in concurrency.
|
sl@0
|
59 |
**
|
sl@0
|
60 |
** To summarize the rational for using a client/server approach: prior
|
sl@0
|
61 |
** to SQLite version 3.3.0 it probably was not worth the trouble. But
|
sl@0
|
62 |
** with SQLite version 3.3.0 and beyond you can get significant performance
|
sl@0
|
63 |
** and concurrency improvements and memory usage reductions by going
|
sl@0
|
64 |
** client/server.
|
sl@0
|
65 |
**
|
sl@0
|
66 |
** Note: The extra features of version 3.3.0 described by points (2)
|
sl@0
|
67 |
** through (4) above are only available if you compile without the
|
sl@0
|
68 |
** option -DSQLITE_OMIT_SHARED_CACHE.
|
sl@0
|
69 |
**
|
sl@0
|
70 |
** Here is how the client/server approach works: The database server
|
sl@0
|
71 |
** thread is started on this procedure:
|
sl@0
|
72 |
**
|
sl@0
|
73 |
** void *sqlite3_server(void *NotUsed);
|
sl@0
|
74 |
**
|
sl@0
|
75 |
** The sqlite_server procedure runs as long as the g.serverHalt variable
|
sl@0
|
76 |
** is false. A mutex is used to make sure no more than one server runs
|
sl@0
|
77 |
** at a time. The server waits for messages to arrive on a message
|
sl@0
|
78 |
** queue and processes the messages in order.
|
sl@0
|
79 |
**
|
sl@0
|
80 |
** Two convenience routines are provided for starting and stopping the
|
sl@0
|
81 |
** server thread:
|
sl@0
|
82 |
**
|
sl@0
|
83 |
** void sqlite3_server_start(void);
|
sl@0
|
84 |
** void sqlite3_server_stop(void);
|
sl@0
|
85 |
**
|
sl@0
|
86 |
** Both of the convenience routines return immediately. Neither will
|
sl@0
|
87 |
** ever give an error. If a server is already started or already halted,
|
sl@0
|
88 |
** then the routines are effectively no-ops.
|
sl@0
|
89 |
**
|
sl@0
|
90 |
** Clients use the following interfaces:
|
sl@0
|
91 |
**
|
sl@0
|
92 |
** sqlite3_client_open
|
sl@0
|
93 |
** sqlite3_client_prepare
|
sl@0
|
94 |
** sqlite3_client_step
|
sl@0
|
95 |
** sqlite3_client_reset
|
sl@0
|
96 |
** sqlite3_client_finalize
|
sl@0
|
97 |
** sqlite3_client_close
|
sl@0
|
98 |
**
|
sl@0
|
99 |
** These interfaces work exactly like the standard core SQLite interfaces
|
sl@0
|
100 |
** having the same names without the "_client_" infix. Many other SQLite
|
sl@0
|
101 |
** interfaces can be used directly without having to send messages to the
|
sl@0
|
102 |
** server as long as SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined.
|
sl@0
|
103 |
** The following interfaces fall into this second category:
|
sl@0
|
104 |
**
|
sl@0
|
105 |
** sqlite3_bind_*
|
sl@0
|
106 |
** sqlite3_changes
|
sl@0
|
107 |
** sqlite3_clear_bindings
|
sl@0
|
108 |
** sqlite3_column_*
|
sl@0
|
109 |
** sqlite3_complete
|
sl@0
|
110 |
** sqlite3_create_collation
|
sl@0
|
111 |
** sqlite3_create_function
|
sl@0
|
112 |
** sqlite3_data_count
|
sl@0
|
113 |
** sqlite3_db_handle
|
sl@0
|
114 |
** sqlite3_errcode
|
sl@0
|
115 |
** sqlite3_errmsg
|
sl@0
|
116 |
** sqlite3_last_insert_rowid
|
sl@0
|
117 |
** sqlite3_total_changes
|
sl@0
|
118 |
** sqlite3_transfer_bindings
|
sl@0
|
119 |
**
|
sl@0
|
120 |
** A single SQLite connection (an sqlite3* object) or an SQLite statement
|
sl@0
|
121 |
** (an sqlite3_stmt* object) should only be passed to a single interface
|
sl@0
|
122 |
** function at a time. The connections and statements can be passed from
|
sl@0
|
123 |
** any thread to any of the functions listed in the second group above as
|
sl@0
|
124 |
** long as the same connection is not in use by two threads at once and
|
sl@0
|
125 |
** as long as SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined. Additional
|
sl@0
|
126 |
** information about the SQLITE_ENABLE_MEMORY_MANAGEMENT constraint is
|
sl@0
|
127 |
** below.
|
sl@0
|
128 |
**
|
sl@0
|
129 |
** The busy handler for all database connections should remain turned
|
sl@0
|
130 |
** off. That means that any lock contention will cause the associated
|
sl@0
|
131 |
** sqlite3_client_step() call to return immediately with an SQLITE_BUSY
|
sl@0
|
132 |
** error code. If a busy handler is enabled and lock contention occurs,
|
sl@0
|
133 |
** then the entire server thread will block. This will cause not only
|
sl@0
|
134 |
** the requesting client to block but every other database client as
|
sl@0
|
135 |
** well. It is possible to enhance the code below so that lock
|
sl@0
|
136 |
** contention will cause the message to be placed back on the top of
|
sl@0
|
137 |
** the queue to be tried again later. But such enhanced processing is
|
sl@0
|
138 |
** not included here, in order to keep the example simple.
|
sl@0
|
139 |
**
|
sl@0
|
140 |
** This example code assumes the use of pthreads. Pthreads
|
sl@0
|
141 |
** implementations are available for windows. (See, for example
|
sl@0
|
142 |
** http://sourceware.org/pthreads-win32/announcement.html.) Or, you
|
sl@0
|
143 |
** can translate the locking and thread synchronization code to use
|
sl@0
|
144 |
** windows primitives easily enough. The details are left as an
|
sl@0
|
145 |
** exercise to the reader.
|
sl@0
|
146 |
**
|
sl@0
|
147 |
**** Restrictions Associated With SQLITE_ENABLE_MEMORY_MANAGEMENT ****
|
sl@0
|
148 |
**
|
sl@0
|
149 |
** If you compile with SQLITE_ENABLE_MEMORY_MANAGEMENT defined, then
|
sl@0
|
150 |
** SQLite includes code that tracks how much memory is being used by
|
sl@0
|
151 |
** each thread. These memory counts can become confused if memory
|
sl@0
|
152 |
** is allocated by one thread and then freed by another. For that
|
sl@0
|
153 |
** reason, when SQLITE_ENABLE_MEMORY_MANAGEMENT is used, all operations
|
sl@0
|
154 |
** that might allocate or free memory should be performanced in the same
|
sl@0
|
155 |
** thread that originally created the database connection. In that case,
|
sl@0
|
156 |
** many of the operations that are listed above as safe to be performed
|
sl@0
|
157 |
** in separate threads would need to be sent over to the server to be
|
sl@0
|
158 |
** done there. If SQLITE_ENABLE_MEMORY_MANAGEMENT is defined, then
|
sl@0
|
159 |
** the following functions can be used safely from different threads
|
sl@0
|
160 |
** without messing up the allocation counts:
|
sl@0
|
161 |
**
|
sl@0
|
162 |
** sqlite3_bind_parameter_name
|
sl@0
|
163 |
** sqlite3_bind_parameter_index
|
sl@0
|
164 |
** sqlite3_changes
|
sl@0
|
165 |
** sqlite3_column_blob
|
sl@0
|
166 |
** sqlite3_column_count
|
sl@0
|
167 |
** sqlite3_complete
|
sl@0
|
168 |
** sqlite3_data_count
|
sl@0
|
169 |
** sqlite3_db_handle
|
sl@0
|
170 |
** sqlite3_errcode
|
sl@0
|
171 |
** sqlite3_errmsg
|
sl@0
|
172 |
** sqlite3_last_insert_rowid
|
sl@0
|
173 |
** sqlite3_total_changes
|
sl@0
|
174 |
**
|
sl@0
|
175 |
** The remaining functions are not thread-safe when memory management
|
sl@0
|
176 |
** is enabled. So one would have to define some new interface routines
|
sl@0
|
177 |
** along the following lines:
|
sl@0
|
178 |
**
|
sl@0
|
179 |
** sqlite3_client_bind_*
|
sl@0
|
180 |
** sqlite3_client_clear_bindings
|
sl@0
|
181 |
** sqlite3_client_column_*
|
sl@0
|
182 |
** sqlite3_client_create_collation
|
sl@0
|
183 |
** sqlite3_client_create_function
|
sl@0
|
184 |
** sqlite3_client_transfer_bindings
|
sl@0
|
185 |
**
|
sl@0
|
186 |
** The example code in this file is intended for use with memory
|
sl@0
|
187 |
** management turned off. So the implementation of these additional
|
sl@0
|
188 |
** client interfaces is left as an exercise to the reader.
|
sl@0
|
189 |
**
|
sl@0
|
190 |
** It may seem surprising to the reader that the list of safe functions
|
sl@0
|
191 |
** above does not include things like sqlite3_bind_int() or
|
sl@0
|
192 |
** sqlite3_column_int(). But those routines might, in fact, allocate
|
sl@0
|
193 |
** or deallocate memory. In the case of sqlite3_bind_int(), if the
|
sl@0
|
194 |
** parameter was previously bound to a string that string might need
|
sl@0
|
195 |
** to be deallocated before the new integer value is inserted. In
|
sl@0
|
196 |
** the case of sqlite3_column_int(), the value of the column might be
|
sl@0
|
197 |
** a UTF-16 string which will need to be converted to UTF-8 then into
|
sl@0
|
198 |
** an integer.
|
sl@0
|
199 |
*/
|
sl@0
|
200 |
|
sl@0
|
201 |
/* Include this to get the definition of SQLITE_THREADSAFE, in the
|
sl@0
|
202 |
** case that default values are used.
|
sl@0
|
203 |
*/
|
sl@0
|
204 |
#include "sqliteInt.h"
|
sl@0
|
205 |
|
sl@0
|
206 |
/*
|
sl@0
|
207 |
** Only compile the code in this file on UNIX with a SQLITE_THREADSAFE build
|
sl@0
|
208 |
** and only if the SQLITE_SERVER macro is defined.
|
sl@0
|
209 |
*/
|
sl@0
|
210 |
#if defined(SQLITE_SERVER) && !defined(SQLITE_OMIT_SHARED_CACHE)
|
sl@0
|
211 |
#if defined(SQLITE_OS_UNIX) && OS_UNIX && SQLITE_THREADSAFE
|
sl@0
|
212 |
|
sl@0
|
213 |
/*
|
sl@0
|
214 |
** We require only pthreads and the public interface of SQLite.
|
sl@0
|
215 |
*/
|
sl@0
|
216 |
#include <pthread.h>
|
sl@0
|
217 |
#include "sqlite3.h"
|
sl@0
|
218 |
|
sl@0
|
219 |
/*
|
sl@0
|
220 |
** Messages are passed from client to server and back again as
|
sl@0
|
221 |
** instances of the following structure.
|
sl@0
|
222 |
*/
|
sl@0
|
223 |
typedef struct SqlMessage SqlMessage;
|
sl@0
|
224 |
struct SqlMessage {
|
sl@0
|
225 |
int op; /* Opcode for the message */
|
sl@0
|
226 |
sqlite3 *pDb; /* The SQLite connection */
|
sl@0
|
227 |
sqlite3_stmt *pStmt; /* A specific statement */
|
sl@0
|
228 |
int errCode; /* Error code returned */
|
sl@0
|
229 |
const char *zIn; /* Input filename or SQL statement */
|
sl@0
|
230 |
int nByte; /* Size of the zIn parameter for prepare() */
|
sl@0
|
231 |
const char *zOut; /* Tail of the SQL statement */
|
sl@0
|
232 |
SqlMessage *pNext; /* Next message in the queue */
|
sl@0
|
233 |
SqlMessage *pPrev; /* Previous message in the queue */
|
sl@0
|
234 |
pthread_mutex_t clientMutex; /* Hold this mutex to access the message */
|
sl@0
|
235 |
pthread_cond_t clientWakeup; /* Signal to wake up the client */
|
sl@0
|
236 |
};
|
sl@0
|
237 |
|
sl@0
|
238 |
/*
|
sl@0
|
239 |
** Legal values for SqlMessage.op
|
sl@0
|
240 |
*/
|
sl@0
|
241 |
#define MSG_Open 1 /* sqlite3_open(zIn, &pDb) */
|
sl@0
|
242 |
#define MSG_Prepare 2 /* sqlite3_prepare(pDb, zIn, nByte, &pStmt, &zOut) */
|
sl@0
|
243 |
#define MSG_Step 3 /* sqlite3_step(pStmt) */
|
sl@0
|
244 |
#define MSG_Reset 4 /* sqlite3_reset(pStmt) */
|
sl@0
|
245 |
#define MSG_Finalize 5 /* sqlite3_finalize(pStmt) */
|
sl@0
|
246 |
#define MSG_Close 6 /* sqlite3_close(pDb) */
|
sl@0
|
247 |
#define MSG_Done 7 /* Server has finished with this message */
|
sl@0
|
248 |
|
sl@0
|
249 |
|
sl@0
|
250 |
/*
|
sl@0
|
251 |
** State information about the server is stored in a static variable
|
sl@0
|
252 |
** named "g" as follows:
|
sl@0
|
253 |
*/
|
sl@0
|
254 |
static struct ServerState {
|
sl@0
|
255 |
pthread_mutex_t queueMutex; /* Hold this mutex to access the msg queue */
|
sl@0
|
256 |
pthread_mutex_t serverMutex; /* Held by the server while it is running */
|
sl@0
|
257 |
pthread_cond_t serverWakeup; /* Signal this condvar to wake up the server */
|
sl@0
|
258 |
volatile int serverHalt; /* Server halts itself when true */
|
sl@0
|
259 |
SqlMessage *pQueueHead; /* Head of the message queue */
|
sl@0
|
260 |
SqlMessage *pQueueTail; /* Tail of the message queue */
|
sl@0
|
261 |
} g = {
|
sl@0
|
262 |
PTHREAD_MUTEX_INITIALIZER,
|
sl@0
|
263 |
PTHREAD_MUTEX_INITIALIZER,
|
sl@0
|
264 |
PTHREAD_COND_INITIALIZER,
|
sl@0
|
265 |
};
|
sl@0
|
266 |
|
sl@0
|
267 |
/*
|
sl@0
|
268 |
** Send a message to the server. Block until we get a reply.
|
sl@0
|
269 |
**
|
sl@0
|
270 |
** The mutex and condition variable in the message are uninitialized
|
sl@0
|
271 |
** when this routine is called. This routine takes care of
|
sl@0
|
272 |
** initializing them and destroying them when it has finished.
|
sl@0
|
273 |
*/
|
sl@0
|
274 |
static void sendToServer(SqlMessage *pMsg){
|
sl@0
|
275 |
/* Initialize the mutex and condition variable on the message
|
sl@0
|
276 |
*/
|
sl@0
|
277 |
pthread_mutex_init(&pMsg->clientMutex, 0);
|
sl@0
|
278 |
pthread_cond_init(&pMsg->clientWakeup, 0);
|
sl@0
|
279 |
|
sl@0
|
280 |
/* Add the message to the head of the server's message queue.
|
sl@0
|
281 |
*/
|
sl@0
|
282 |
pthread_mutex_lock(&g.queueMutex);
|
sl@0
|
283 |
pMsg->pNext = g.pQueueHead;
|
sl@0
|
284 |
if( g.pQueueHead==0 ){
|
sl@0
|
285 |
g.pQueueTail = pMsg;
|
sl@0
|
286 |
}else{
|
sl@0
|
287 |
g.pQueueHead->pPrev = pMsg;
|
sl@0
|
288 |
}
|
sl@0
|
289 |
pMsg->pPrev = 0;
|
sl@0
|
290 |
g.pQueueHead = pMsg;
|
sl@0
|
291 |
pthread_mutex_unlock(&g.queueMutex);
|
sl@0
|
292 |
|
sl@0
|
293 |
/* Signal the server that the new message has be queued, then
|
sl@0
|
294 |
** block waiting for the server to process the message.
|
sl@0
|
295 |
*/
|
sl@0
|
296 |
pthread_mutex_lock(&pMsg->clientMutex);
|
sl@0
|
297 |
pthread_cond_signal(&g.serverWakeup);
|
sl@0
|
298 |
while( pMsg->op!=MSG_Done ){
|
sl@0
|
299 |
pthread_cond_wait(&pMsg->clientWakeup, &pMsg->clientMutex);
|
sl@0
|
300 |
}
|
sl@0
|
301 |
pthread_mutex_unlock(&pMsg->clientMutex);
|
sl@0
|
302 |
|
sl@0
|
303 |
/* Destroy the mutex and condition variable of the message.
|
sl@0
|
304 |
*/
|
sl@0
|
305 |
pthread_mutex_destroy(&pMsg->clientMutex);
|
sl@0
|
306 |
pthread_cond_destroy(&pMsg->clientWakeup);
|
sl@0
|
307 |
}
|
sl@0
|
308 |
|
sl@0
|
309 |
/*
|
sl@0
|
310 |
** The following 6 routines are client-side implementations of the
|
sl@0
|
311 |
** core SQLite interfaces:
|
sl@0
|
312 |
**
|
sl@0
|
313 |
** sqlite3_open
|
sl@0
|
314 |
** sqlite3_prepare
|
sl@0
|
315 |
** sqlite3_step
|
sl@0
|
316 |
** sqlite3_reset
|
sl@0
|
317 |
** sqlite3_finalize
|
sl@0
|
318 |
** sqlite3_close
|
sl@0
|
319 |
**
|
sl@0
|
320 |
** Clients should use the following client-side routines instead of
|
sl@0
|
321 |
** the core routines above.
|
sl@0
|
322 |
**
|
sl@0
|
323 |
** sqlite3_client_open
|
sl@0
|
324 |
** sqlite3_client_prepare
|
sl@0
|
325 |
** sqlite3_client_step
|
sl@0
|
326 |
** sqlite3_client_reset
|
sl@0
|
327 |
** sqlite3_client_finalize
|
sl@0
|
328 |
** sqlite3_client_close
|
sl@0
|
329 |
**
|
sl@0
|
330 |
** Each of these routines creates a message for the desired operation,
|
sl@0
|
331 |
** sends that message to the server, waits for the server to process
|
sl@0
|
332 |
** then message and return a response.
|
sl@0
|
333 |
*/
|
sl@0
|
334 |
int sqlite3_client_open(const char *zDatabaseName, sqlite3 **ppDb){
|
sl@0
|
335 |
SqlMessage msg;
|
sl@0
|
336 |
msg.op = MSG_Open;
|
sl@0
|
337 |
msg.zIn = zDatabaseName;
|
sl@0
|
338 |
sendToServer(&msg);
|
sl@0
|
339 |
*ppDb = msg.pDb;
|
sl@0
|
340 |
return msg.errCode;
|
sl@0
|
341 |
}
|
sl@0
|
342 |
int sqlite3_client_prepare(
|
sl@0
|
343 |
sqlite3 *pDb,
|
sl@0
|
344 |
const char *zSql,
|
sl@0
|
345 |
int nByte,
|
sl@0
|
346 |
sqlite3_stmt **ppStmt,
|
sl@0
|
347 |
const char **pzTail
|
sl@0
|
348 |
){
|
sl@0
|
349 |
SqlMessage msg;
|
sl@0
|
350 |
msg.op = MSG_Prepare;
|
sl@0
|
351 |
msg.pDb = pDb;
|
sl@0
|
352 |
msg.zIn = zSql;
|
sl@0
|
353 |
msg.nByte = nByte;
|
sl@0
|
354 |
sendToServer(&msg);
|
sl@0
|
355 |
*ppStmt = msg.pStmt;
|
sl@0
|
356 |
if( pzTail ) *pzTail = msg.zOut;
|
sl@0
|
357 |
return msg.errCode;
|
sl@0
|
358 |
}
|
sl@0
|
359 |
int sqlite3_client_step(sqlite3_stmt *pStmt){
|
sl@0
|
360 |
SqlMessage msg;
|
sl@0
|
361 |
msg.op = MSG_Step;
|
sl@0
|
362 |
msg.pStmt = pStmt;
|
sl@0
|
363 |
sendToServer(&msg);
|
sl@0
|
364 |
return msg.errCode;
|
sl@0
|
365 |
}
|
sl@0
|
366 |
int sqlite3_client_reset(sqlite3_stmt *pStmt){
|
sl@0
|
367 |
SqlMessage msg;
|
sl@0
|
368 |
msg.op = MSG_Reset;
|
sl@0
|
369 |
msg.pStmt = pStmt;
|
sl@0
|
370 |
sendToServer(&msg);
|
sl@0
|
371 |
return msg.errCode;
|
sl@0
|
372 |
}
|
sl@0
|
373 |
int sqlite3_client_finalize(sqlite3_stmt *pStmt){
|
sl@0
|
374 |
SqlMessage msg;
|
sl@0
|
375 |
msg.op = MSG_Finalize;
|
sl@0
|
376 |
msg.pStmt = pStmt;
|
sl@0
|
377 |
sendToServer(&msg);
|
sl@0
|
378 |
return msg.errCode;
|
sl@0
|
379 |
}
|
sl@0
|
380 |
int sqlite3_client_close(sqlite3 *pDb){
|
sl@0
|
381 |
SqlMessage msg;
|
sl@0
|
382 |
msg.op = MSG_Close;
|
sl@0
|
383 |
msg.pDb = pDb;
|
sl@0
|
384 |
sendToServer(&msg);
|
sl@0
|
385 |
return msg.errCode;
|
sl@0
|
386 |
}
|
sl@0
|
387 |
|
sl@0
|
388 |
/*
|
sl@0
|
389 |
** This routine implements the server. To start the server, first
|
sl@0
|
390 |
** make sure g.serverHalt is false, then create a new detached thread
|
sl@0
|
391 |
** on this procedure. See the sqlite3_server_start() routine below
|
sl@0
|
392 |
** for an example. This procedure loops until g.serverHalt becomes
|
sl@0
|
393 |
** true.
|
sl@0
|
394 |
*/
|
sl@0
|
395 |
void *sqlite3_server(void *NotUsed){
|
sl@0
|
396 |
if( pthread_mutex_trylock(&g.serverMutex) ){
|
sl@0
|
397 |
return 0; /* Another server is already running */
|
sl@0
|
398 |
}
|
sl@0
|
399 |
sqlite3_enable_shared_cache(1);
|
sl@0
|
400 |
while( !g.serverHalt ){
|
sl@0
|
401 |
SqlMessage *pMsg;
|
sl@0
|
402 |
|
sl@0
|
403 |
/* Remove the last message from the message queue.
|
sl@0
|
404 |
*/
|
sl@0
|
405 |
pthread_mutex_lock(&g.queueMutex);
|
sl@0
|
406 |
while( g.pQueueTail==0 && g.serverHalt==0 ){
|
sl@0
|
407 |
pthread_cond_wait(&g.serverWakeup, &g.queueMutex);
|
sl@0
|
408 |
}
|
sl@0
|
409 |
pMsg = g.pQueueTail;
|
sl@0
|
410 |
if( pMsg ){
|
sl@0
|
411 |
if( pMsg->pPrev ){
|
sl@0
|
412 |
pMsg->pPrev->pNext = 0;
|
sl@0
|
413 |
}else{
|
sl@0
|
414 |
g.pQueueHead = 0;
|
sl@0
|
415 |
}
|
sl@0
|
416 |
g.pQueueTail = pMsg->pPrev;
|
sl@0
|
417 |
}
|
sl@0
|
418 |
pthread_mutex_unlock(&g.queueMutex);
|
sl@0
|
419 |
if( pMsg==0 ) break;
|
sl@0
|
420 |
|
sl@0
|
421 |
/* Process the message just removed
|
sl@0
|
422 |
*/
|
sl@0
|
423 |
pthread_mutex_lock(&pMsg->clientMutex);
|
sl@0
|
424 |
switch( pMsg->op ){
|
sl@0
|
425 |
case MSG_Open: {
|
sl@0
|
426 |
pMsg->errCode = sqlite3_open(pMsg->zIn, &pMsg->pDb);
|
sl@0
|
427 |
break;
|
sl@0
|
428 |
}
|
sl@0
|
429 |
case MSG_Prepare: {
|
sl@0
|
430 |
pMsg->errCode = sqlite3_prepare(pMsg->pDb, pMsg->zIn, pMsg->nByte,
|
sl@0
|
431 |
&pMsg->pStmt, &pMsg->zOut);
|
sl@0
|
432 |
break;
|
sl@0
|
433 |
}
|
sl@0
|
434 |
case MSG_Step: {
|
sl@0
|
435 |
pMsg->errCode = sqlite3_step(pMsg->pStmt);
|
sl@0
|
436 |
break;
|
sl@0
|
437 |
}
|
sl@0
|
438 |
case MSG_Reset: {
|
sl@0
|
439 |
pMsg->errCode = sqlite3_reset(pMsg->pStmt);
|
sl@0
|
440 |
break;
|
sl@0
|
441 |
}
|
sl@0
|
442 |
case MSG_Finalize: {
|
sl@0
|
443 |
pMsg->errCode = sqlite3_finalize(pMsg->pStmt);
|
sl@0
|
444 |
break;
|
sl@0
|
445 |
}
|
sl@0
|
446 |
case MSG_Close: {
|
sl@0
|
447 |
pMsg->errCode = sqlite3_close(pMsg->pDb);
|
sl@0
|
448 |
break;
|
sl@0
|
449 |
}
|
sl@0
|
450 |
}
|
sl@0
|
451 |
|
sl@0
|
452 |
/* Signal the client that the message has been processed.
|
sl@0
|
453 |
*/
|
sl@0
|
454 |
pMsg->op = MSG_Done;
|
sl@0
|
455 |
pthread_mutex_unlock(&pMsg->clientMutex);
|
sl@0
|
456 |
pthread_cond_signal(&pMsg->clientWakeup);
|
sl@0
|
457 |
}
|
sl@0
|
458 |
sqlite3_thread_cleanup();
|
sl@0
|
459 |
pthread_mutex_unlock(&g.serverMutex);
|
sl@0
|
460 |
return 0;
|
sl@0
|
461 |
}
|
sl@0
|
462 |
|
sl@0
|
463 |
/*
|
sl@0
|
464 |
** Start a server thread if one is not already running. If there
|
sl@0
|
465 |
** is aleady a server thread running, the new thread will quickly
|
sl@0
|
466 |
** die and this routine is effectively a no-op.
|
sl@0
|
467 |
*/
|
sl@0
|
468 |
void sqlite3_server_start(void){
|
sl@0
|
469 |
pthread_t x;
|
sl@0
|
470 |
int rc;
|
sl@0
|
471 |
g.serverHalt = 0;
|
sl@0
|
472 |
rc = pthread_create(&x, 0, sqlite3_server, 0);
|
sl@0
|
473 |
if( rc==0 ){
|
sl@0
|
474 |
pthread_detach(x);
|
sl@0
|
475 |
}
|
sl@0
|
476 |
}
|
sl@0
|
477 |
|
sl@0
|
478 |
/*
|
sl@0
|
479 |
** If a server thread is running, then stop it. If no server is
|
sl@0
|
480 |
** running, this routine is effectively a no-op.
|
sl@0
|
481 |
**
|
sl@0
|
482 |
** This routine waits until the server has actually stopped before
|
sl@0
|
483 |
** returning.
|
sl@0
|
484 |
*/
|
sl@0
|
485 |
void sqlite3_server_stop(void){
|
sl@0
|
486 |
g.serverHalt = 1;
|
sl@0
|
487 |
pthread_cond_broadcast(&g.serverWakeup);
|
sl@0
|
488 |
pthread_mutex_lock(&g.serverMutex);
|
sl@0
|
489 |
pthread_mutex_unlock(&g.serverMutex);
|
sl@0
|
490 |
}
|
sl@0
|
491 |
|
sl@0
|
492 |
#endif /* defined(SQLITE_OS_UNIX) && OS_UNIX && SQLITE_THREADSAFE */
|
sl@0
|
493 |
#endif /* defined(SQLITE_SERVER) */
|