12d02a67dSdrh /*
22d02a67dSdrh ** 2006 January 07
32d02a67dSdrh **
42d02a67dSdrh ** The author disclaims copyright to this source code. In place of
52d02a67dSdrh ** a legal notice, here is a blessing:
62d02a67dSdrh **
72d02a67dSdrh ** May you do good and not evil.
82d02a67dSdrh ** May you find forgiveness for yourself and forgive others.
92d02a67dSdrh ** May you share freely, never taking more than you give.
102d02a67dSdrh **
112d02a67dSdrh ******************************************************************************
122d02a67dSdrh **
132d02a67dSdrh ** This file contains demonstration code. Nothing in this file gets compiled
142d02a67dSdrh ** or linked into the SQLite library unless you use a non-standard option:
152d02a67dSdrh **
162d02a67dSdrh ** -DSQLITE_SERVER=1
172d02a67dSdrh **
182d02a67dSdrh ** The configure script will never generate a Makefile with the option
192d02a67dSdrh ** above. You will need to manually modify the Makefile if you want to
202d02a67dSdrh ** include any of the code from this file in your project. Or, at your
212d02a67dSdrh ** option, you may copy and paste the code from this file and
222d02a67dSdrh ** thereby avoiding a recompile of SQLite.
232d02a67dSdrh **
242d02a67dSdrh **
252d02a67dSdrh ** This source file demonstrates how to use SQLite to create an SQL database
262d02a67dSdrh ** server thread in a multiple-threaded program. One or more client threads
272d02a67dSdrh ** send messages to the server thread and the server thread processes those
282d02a67dSdrh ** messages in the order received and returns the results to the client.
292d02a67dSdrh **
302d02a67dSdrh ** One might ask: "Why bother? Why not just let each thread connect
312d02a67dSdrh ** to the database directly?" There are a several of reasons to
322d02a67dSdrh ** prefer the client/server approach.
332d02a67dSdrh **
342d02a67dSdrh ** (1) Some systems (ex: Redhat9) have broken threading implementations
352d02a67dSdrh ** that prevent SQLite database connections from being used in
362d02a67dSdrh ** a thread different from the one where they were created. With
372d02a67dSdrh ** the client/server approach, all database connections are created
382d02a67dSdrh ** and used within the server thread. Client calls to the database
392d02a67dSdrh ** can be made from multiple threads (though not at the same time!)
402d02a67dSdrh **
412d02a67dSdrh ** (2) Beginning with SQLite version 3.3.0, when two or more
422d02a67dSdrh ** connections to the same database occur within the same thread,
432d02a67dSdrh ** they can optionally share their database cache. This reduces
442d02a67dSdrh ** I/O and memory requirements. Cache shared is controlled using
452d02a67dSdrh ** the sqlite3_enable_shared_cache() API.
462d02a67dSdrh **
472d02a67dSdrh ** (3) Database connections on a shared cache use table-level locking
482d02a67dSdrh ** instead of file-level locking for improved concurrency.
492d02a67dSdrh **
502d02a67dSdrh ** (4) Database connections on a shared cache can by optionally
512d02a67dSdrh ** set to READ UNCOMMITTED isolation. (The default isolation for
522d02a67dSdrh ** SQLite is SERIALIZABLE.) When this occurs, readers will
532d02a67dSdrh ** never be blocked by a writer and writers will not be
542d02a67dSdrh ** blocked by readers. There can still only be a single writer
552d02a67dSdrh ** at a time, but multiple readers can simultaneously exist with
562d02a67dSdrh ** that writer. This is a huge increase in concurrency.
572d02a67dSdrh **
582d02a67dSdrh ** To summarize the rational for using a client/server approach: prior
592d02a67dSdrh ** to SQLite version 3.3.0 it probably was not worth the trouble. But
602d02a67dSdrh ** with SQLite version 3.3.0 and beyond you can get significant performance
612d02a67dSdrh ** and concurrency improvements and memory usage reductions by going
622d02a67dSdrh ** client/server.
632d02a67dSdrh **
642d02a67dSdrh ** Note: The extra features of version 3.3.0 described by points (2)
652d02a67dSdrh ** through (4) above are only available if you compile without the
662d02a67dSdrh ** option -DSQLITE_OMIT_SHARED_CACHE.
672d02a67dSdrh **
682d02a67dSdrh ** Here is how the client/server approach works: The database server
692d02a67dSdrh ** thread is started on this procedure:
702d02a67dSdrh **
712d02a67dSdrh ** void *sqlite3_server(void *NotUsed);
722d02a67dSdrh **
732d02a67dSdrh ** The sqlite_server procedure runs as long as the g.serverHalt variable
742d02a67dSdrh ** is false. A mutex is used to make sure no more than one server runs
752d02a67dSdrh ** at a time. The server waits for messages to arrive on a message
762d02a67dSdrh ** queue and processes the messages in order.
772d02a67dSdrh **
782d02a67dSdrh ** Two convenience routines are provided for starting and stopping the
792d02a67dSdrh ** server thread:
802d02a67dSdrh **
812d02a67dSdrh ** void sqlite3_server_start(void);
822d02a67dSdrh ** void sqlite3_server_stop(void);
832d02a67dSdrh **
842d02a67dSdrh ** Both of the convenience routines return immediately. Neither will
852d02a67dSdrh ** ever give an error. If a server is already started or already halted,
862d02a67dSdrh ** then the routines are effectively no-ops.
872d02a67dSdrh **
882d02a67dSdrh ** Clients use the following interfaces:
892d02a67dSdrh **
902d02a67dSdrh ** sqlite3_client_open
912d02a67dSdrh ** sqlite3_client_prepare
922d02a67dSdrh ** sqlite3_client_step
932d02a67dSdrh ** sqlite3_client_reset
942d02a67dSdrh ** sqlite3_client_finalize
952d02a67dSdrh ** sqlite3_client_close
962d02a67dSdrh **
972d02a67dSdrh ** These interfaces work exactly like the standard core SQLite interfaces
982d02a67dSdrh ** having the same names without the "_client_" infix. Many other SQLite
992d02a67dSdrh ** interfaces can be used directly without having to send messages to the
1002d02a67dSdrh ** server as long as SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined.
1012d02a67dSdrh ** The following interfaces fall into this second category:
1022d02a67dSdrh **
1032d02a67dSdrh ** sqlite3_bind_*
1042d02a67dSdrh ** sqlite3_changes
1052d02a67dSdrh ** sqlite3_clear_bindings
1062d02a67dSdrh ** sqlite3_column_*
1072d02a67dSdrh ** sqlite3_complete
1082d02a67dSdrh ** sqlite3_create_collation
1092d02a67dSdrh ** sqlite3_create_function
1102d02a67dSdrh ** sqlite3_data_count
1112d02a67dSdrh ** sqlite3_db_handle
1122d02a67dSdrh ** sqlite3_errcode
1132d02a67dSdrh ** sqlite3_errmsg
1142d02a67dSdrh ** sqlite3_last_insert_rowid
1152d02a67dSdrh ** sqlite3_total_changes
1162d02a67dSdrh ** sqlite3_transfer_bindings
1172d02a67dSdrh **
1182d02a67dSdrh ** A single SQLite connection (an sqlite3* object) or an SQLite statement
1192d02a67dSdrh ** (an sqlite3_stmt* object) should only be passed to a single interface
1202d02a67dSdrh ** function at a time. The connections and statements can be passed from
1212d02a67dSdrh ** any thread to any of the functions listed in the second group above as
1222d02a67dSdrh ** long as the same connection is not in use by two threads at once and
1232d02a67dSdrh ** as long as SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined. Additional
1242d02a67dSdrh ** information about the SQLITE_ENABLE_MEMORY_MANAGEMENT constraint is
1252d02a67dSdrh ** below.
1262d02a67dSdrh **
1272d02a67dSdrh ** The busy handler for all database connections should remain turned
1282d02a67dSdrh ** off. That means that any lock contention will cause the associated
1292d02a67dSdrh ** sqlite3_client_step() call to return immediately with an SQLITE_BUSY
1302d02a67dSdrh ** error code. If a busy handler is enabled and lock contention occurs,
1312d02a67dSdrh ** then the entire server thread will block. This will cause not only
1322d02a67dSdrh ** the requesting client to block but every other database client as
1332d02a67dSdrh ** well. It is possible to enhance the code below so that lock
1342d02a67dSdrh ** contention will cause the message to be placed back on the top of
1352d02a67dSdrh ** the queue to be tried again later. But such enhanced processing is
1362d02a67dSdrh ** not included here, in order to keep the example simple.
1372d02a67dSdrh **
1382d02a67dSdrh ** This example code assumes the use of pthreads. Pthreads
1392d02a67dSdrh ** implementations are available for windows. (See, for example
1402d02a67dSdrh ** http://sourceware.org/pthreads-win32/announcement.html.) Or, you
1412d02a67dSdrh ** can translate the locking and thread synchronization code to use
1422d02a67dSdrh ** windows primitives easily enough. The details are left as an
1432d02a67dSdrh ** exercise to the reader.
1442d02a67dSdrh **
1452d02a67dSdrh **** Restrictions Associated With SQLITE_ENABLE_MEMORY_MANAGEMENT ****
1462d02a67dSdrh **
1472d02a67dSdrh ** If you compile with SQLITE_ENABLE_MEMORY_MANAGEMENT defined, then
1482d02a67dSdrh ** SQLite includes code that tracks how much memory is being used by
1492d02a67dSdrh ** each thread. These memory counts can become confused if memory
1502d02a67dSdrh ** is allocated by one thread and then freed by another. For that
1512d02a67dSdrh ** reason, when SQLITE_ENABLE_MEMORY_MANAGEMENT is used, all operations
1522d02a67dSdrh ** that might allocate or free memory should be performanced in the same
1532d02a67dSdrh ** thread that originally created the database connection. In that case,
1542d02a67dSdrh ** many of the operations that are listed above as safe to be performed
1552d02a67dSdrh ** in separate threads would need to be sent over to the server to be
1562d02a67dSdrh ** done there. If SQLITE_ENABLE_MEMORY_MANAGEMENT is defined, then
1572d02a67dSdrh ** the following functions can be used safely from different threads
1582d02a67dSdrh ** without messing up the allocation counts:
1592d02a67dSdrh **
1602d02a67dSdrh ** sqlite3_bind_parameter_name
1612d02a67dSdrh ** sqlite3_bind_parameter_index
1622d02a67dSdrh ** sqlite3_changes
1632d02a67dSdrh ** sqlite3_column_blob
1642d02a67dSdrh ** sqlite3_column_count
1652d02a67dSdrh ** sqlite3_complete
1662d02a67dSdrh ** sqlite3_data_count
1672d02a67dSdrh ** sqlite3_db_handle
1682d02a67dSdrh ** sqlite3_errcode
1692d02a67dSdrh ** sqlite3_errmsg
1702d02a67dSdrh ** sqlite3_last_insert_rowid
1712d02a67dSdrh ** sqlite3_total_changes
1722d02a67dSdrh **
1732d02a67dSdrh ** The remaining functions are not thread-safe when memory management
1742d02a67dSdrh ** is enabled. So one would have to define some new interface routines
1752d02a67dSdrh ** along the following lines:
1762d02a67dSdrh **
1772d02a67dSdrh ** sqlite3_client_bind_*
1782d02a67dSdrh ** sqlite3_client_clear_bindings
1792d02a67dSdrh ** sqlite3_client_column_*
1802d02a67dSdrh ** sqlite3_client_create_collation
1812d02a67dSdrh ** sqlite3_client_create_function
1822d02a67dSdrh ** sqlite3_client_transfer_bindings
1832d02a67dSdrh **
1842d02a67dSdrh ** The example code in this file is intended for use with memory
1852d02a67dSdrh ** management turned off. So the implementation of these additional
1862d02a67dSdrh ** client interfaces is left as an exercise to the reader.
1872d02a67dSdrh **
1882d02a67dSdrh ** It may seem surprising to the reader that the list of safe functions
1892d02a67dSdrh ** above does not include things like sqlite3_bind_int() or
1902d02a67dSdrh ** sqlite3_column_int(). But those routines might, in fact, allocate
1912d02a67dSdrh ** or deallocate memory. In the case of sqlite3_bind_int(), if the
1922d02a67dSdrh ** parameter was previously bound to a string that string might need
1932d02a67dSdrh ** to be deallocated before the new integer value is inserted. In
1942d02a67dSdrh ** the case of sqlite3_column_int(), the value of the column might be
1952d02a67dSdrh ** a UTF-16 string which will need to be converted to UTF-8 then into
1962d02a67dSdrh ** an integer.
1972d02a67dSdrh */
1982d02a67dSdrh
1994152e677Sdanielk1977 /* Include this to get the definition of SQLITE_THREADSAFE, in the
2004152e677Sdanielk1977 ** case that default values are used.
2014152e677Sdanielk1977 */
2024152e677Sdanielk1977 #include "sqliteInt.h"
2034152e677Sdanielk1977
2042d02a67dSdrh /*
205d677b3d6Sdrh ** Only compile the code in this file on UNIX with a SQLITE_THREADSAFE build
2062d02a67dSdrh ** and only if the SQLITE_SERVER macro is defined.
2072d02a67dSdrh */
208a154dcd7Sdrh #if defined(SQLITE_SERVER) && !defined(SQLITE_OMIT_SHARED_CACHE)
2093a2d29f8Sshaneh #if SQLITE_OS_UNIX && SQLITE_THREADSAFE
2102d02a67dSdrh
2112d02a67dSdrh /*
2122d02a67dSdrh ** We require only pthreads and the public interface of SQLite.
2132d02a67dSdrh */
2142d02a67dSdrh #include <pthread.h>
2152d02a67dSdrh #include "sqlite3.h"
2162d02a67dSdrh
2172d02a67dSdrh /*
2182d02a67dSdrh ** Messages are passed from client to server and back again as
2192d02a67dSdrh ** instances of the following structure.
2202d02a67dSdrh */
2212d02a67dSdrh typedef struct SqlMessage SqlMessage;
2222d02a67dSdrh struct SqlMessage {
2232d02a67dSdrh int op; /* Opcode for the message */
2242d02a67dSdrh sqlite3 *pDb; /* The SQLite connection */
2252d02a67dSdrh sqlite3_stmt *pStmt; /* A specific statement */
2262d02a67dSdrh int errCode; /* Error code returned */
2272d02a67dSdrh const char *zIn; /* Input filename or SQL statement */
2282d02a67dSdrh int nByte; /* Size of the zIn parameter for prepare() */
2292d02a67dSdrh const char *zOut; /* Tail of the SQL statement */
2302d02a67dSdrh SqlMessage *pNext; /* Next message in the queue */
2312d02a67dSdrh SqlMessage *pPrev; /* Previous message in the queue */
2322d02a67dSdrh pthread_mutex_t clientMutex; /* Hold this mutex to access the message */
2332d02a67dSdrh pthread_cond_t clientWakeup; /* Signal to wake up the client */
2342d02a67dSdrh };
2352d02a67dSdrh
2362d02a67dSdrh /*
2372d02a67dSdrh ** Legal values for SqlMessage.op
2382d02a67dSdrh */
2392d02a67dSdrh #define MSG_Open 1 /* sqlite3_open(zIn, &pDb) */
2402d02a67dSdrh #define MSG_Prepare 2 /* sqlite3_prepare(pDb, zIn, nByte, &pStmt, &zOut) */
2412d02a67dSdrh #define MSG_Step 3 /* sqlite3_step(pStmt) */
2422d02a67dSdrh #define MSG_Reset 4 /* sqlite3_reset(pStmt) */
2432d02a67dSdrh #define MSG_Finalize 5 /* sqlite3_finalize(pStmt) */
2442d02a67dSdrh #define MSG_Close 6 /* sqlite3_close(pDb) */
2452d02a67dSdrh #define MSG_Done 7 /* Server has finished with this message */
2462d02a67dSdrh
2472d02a67dSdrh
2482d02a67dSdrh /*
2492d02a67dSdrh ** State information about the server is stored in a static variable
2502d02a67dSdrh ** named "g" as follows:
2512d02a67dSdrh */
2522d02a67dSdrh static struct ServerState {
2532d02a67dSdrh pthread_mutex_t queueMutex; /* Hold this mutex to access the msg queue */
2542d02a67dSdrh pthread_mutex_t serverMutex; /* Held by the server while it is running */
2552d02a67dSdrh pthread_cond_t serverWakeup; /* Signal this condvar to wake up the server */
2562d02a67dSdrh volatile int serverHalt; /* Server halts itself when true */
2572d02a67dSdrh SqlMessage *pQueueHead; /* Head of the message queue */
2582d02a67dSdrh SqlMessage *pQueueTail; /* Tail of the message queue */
2592d02a67dSdrh } g = {
2602d02a67dSdrh PTHREAD_MUTEX_INITIALIZER,
2612d02a67dSdrh PTHREAD_MUTEX_INITIALIZER,
2622d02a67dSdrh PTHREAD_COND_INITIALIZER,
2632d02a67dSdrh };
2642d02a67dSdrh
2652d02a67dSdrh /*
2662d02a67dSdrh ** Send a message to the server. Block until we get a reply.
2672d02a67dSdrh **
2682d02a67dSdrh ** The mutex and condition variable in the message are uninitialized
2692d02a67dSdrh ** when this routine is called. This routine takes care of
2702d02a67dSdrh ** initializing them and destroying them when it has finished.
2712d02a67dSdrh */
sendToServer(SqlMessage * pMsg)2722d02a67dSdrh static void sendToServer(SqlMessage *pMsg){
2732d02a67dSdrh /* Initialize the mutex and condition variable on the message
2742d02a67dSdrh */
2752d02a67dSdrh pthread_mutex_init(&pMsg->clientMutex, 0);
2762d02a67dSdrh pthread_cond_init(&pMsg->clientWakeup, 0);
2772d02a67dSdrh
2782d02a67dSdrh /* Add the message to the head of the server's message queue.
2792d02a67dSdrh */
2802d02a67dSdrh pthread_mutex_lock(&g.queueMutex);
2812d02a67dSdrh pMsg->pNext = g.pQueueHead;
2822d02a67dSdrh if( g.pQueueHead==0 ){
2832d02a67dSdrh g.pQueueTail = pMsg;
2842d02a67dSdrh }else{
2852d02a67dSdrh g.pQueueHead->pPrev = pMsg;
2862d02a67dSdrh }
2872d02a67dSdrh pMsg->pPrev = 0;
2882d02a67dSdrh g.pQueueHead = pMsg;
2892d02a67dSdrh pthread_mutex_unlock(&g.queueMutex);
2902d02a67dSdrh
2912d02a67dSdrh /* Signal the server that the new message has be queued, then
2922d02a67dSdrh ** block waiting for the server to process the message.
2932d02a67dSdrh */
2942d02a67dSdrh pthread_mutex_lock(&pMsg->clientMutex);
2952d02a67dSdrh pthread_cond_signal(&g.serverWakeup);
2962d02a67dSdrh while( pMsg->op!=MSG_Done ){
2972d02a67dSdrh pthread_cond_wait(&pMsg->clientWakeup, &pMsg->clientMutex);
2982d02a67dSdrh }
2992d02a67dSdrh pthread_mutex_unlock(&pMsg->clientMutex);
3002d02a67dSdrh
3012d02a67dSdrh /* Destroy the mutex and condition variable of the message.
3022d02a67dSdrh */
3032d02a67dSdrh pthread_mutex_destroy(&pMsg->clientMutex);
3042d02a67dSdrh pthread_cond_destroy(&pMsg->clientWakeup);
3052d02a67dSdrh }
3062d02a67dSdrh
3072d02a67dSdrh /*
3082d02a67dSdrh ** The following 6 routines are client-side implementations of the
3092d02a67dSdrh ** core SQLite interfaces:
3102d02a67dSdrh **
3112d02a67dSdrh ** sqlite3_open
3122d02a67dSdrh ** sqlite3_prepare
3132d02a67dSdrh ** sqlite3_step
3142d02a67dSdrh ** sqlite3_reset
3152d02a67dSdrh ** sqlite3_finalize
3162d02a67dSdrh ** sqlite3_close
3172d02a67dSdrh **
3182d02a67dSdrh ** Clients should use the following client-side routines instead of
3192d02a67dSdrh ** the core routines above.
3202d02a67dSdrh **
3212d02a67dSdrh ** sqlite3_client_open
3222d02a67dSdrh ** sqlite3_client_prepare
3232d02a67dSdrh ** sqlite3_client_step
3242d02a67dSdrh ** sqlite3_client_reset
3252d02a67dSdrh ** sqlite3_client_finalize
3262d02a67dSdrh ** sqlite3_client_close
3272d02a67dSdrh **
3282d02a67dSdrh ** Each of these routines creates a message for the desired operation,
3292d02a67dSdrh ** sends that message to the server, waits for the server to process
3302d02a67dSdrh ** then message and return a response.
3312d02a67dSdrh */
sqlite3_client_open(const char * zDatabaseName,sqlite3 ** ppDb)3322d02a67dSdrh int sqlite3_client_open(const char *zDatabaseName, sqlite3 **ppDb){
3332d02a67dSdrh SqlMessage msg;
3342d02a67dSdrh msg.op = MSG_Open;
3352d02a67dSdrh msg.zIn = zDatabaseName;
3362d02a67dSdrh sendToServer(&msg);
3372d02a67dSdrh *ppDb = msg.pDb;
3382d02a67dSdrh return msg.errCode;
3392d02a67dSdrh }
sqlite3_client_prepare(sqlite3 * pDb,const char * zSql,int nByte,sqlite3_stmt ** ppStmt,const char ** pzTail)3402d02a67dSdrh int sqlite3_client_prepare(
3412d02a67dSdrh sqlite3 *pDb,
3422d02a67dSdrh const char *zSql,
3432d02a67dSdrh int nByte,
3442d02a67dSdrh sqlite3_stmt **ppStmt,
3452d02a67dSdrh const char **pzTail
3462d02a67dSdrh ){
3472d02a67dSdrh SqlMessage msg;
3482d02a67dSdrh msg.op = MSG_Prepare;
3492d02a67dSdrh msg.pDb = pDb;
3502d02a67dSdrh msg.zIn = zSql;
3512d02a67dSdrh msg.nByte = nByte;
3522d02a67dSdrh sendToServer(&msg);
3532d02a67dSdrh *ppStmt = msg.pStmt;
3542d02a67dSdrh if( pzTail ) *pzTail = msg.zOut;
3552d02a67dSdrh return msg.errCode;
3562d02a67dSdrh }
sqlite3_client_step(sqlite3_stmt * pStmt)3572d02a67dSdrh int sqlite3_client_step(sqlite3_stmt *pStmt){
3582d02a67dSdrh SqlMessage msg;
3592d02a67dSdrh msg.op = MSG_Step;
3602d02a67dSdrh msg.pStmt = pStmt;
3612d02a67dSdrh sendToServer(&msg);
3622d02a67dSdrh return msg.errCode;
3632d02a67dSdrh }
sqlite3_client_reset(sqlite3_stmt * pStmt)3642d02a67dSdrh int sqlite3_client_reset(sqlite3_stmt *pStmt){
3652d02a67dSdrh SqlMessage msg;
3662d02a67dSdrh msg.op = MSG_Reset;
3672d02a67dSdrh msg.pStmt = pStmt;
3682d02a67dSdrh sendToServer(&msg);
3692d02a67dSdrh return msg.errCode;
3702d02a67dSdrh }
sqlite3_client_finalize(sqlite3_stmt * pStmt)3712d02a67dSdrh int sqlite3_client_finalize(sqlite3_stmt *pStmt){
3722d02a67dSdrh SqlMessage msg;
3732d02a67dSdrh msg.op = MSG_Finalize;
3742d02a67dSdrh msg.pStmt = pStmt;
3752d02a67dSdrh sendToServer(&msg);
3762d02a67dSdrh return msg.errCode;
3772d02a67dSdrh }
sqlite3_client_close(sqlite3 * pDb)3782d02a67dSdrh int sqlite3_client_close(sqlite3 *pDb){
3792d02a67dSdrh SqlMessage msg;
3802d02a67dSdrh msg.op = MSG_Close;
3812d02a67dSdrh msg.pDb = pDb;
3822d02a67dSdrh sendToServer(&msg);
3832d02a67dSdrh return msg.errCode;
3842d02a67dSdrh }
3852d02a67dSdrh
3862d02a67dSdrh /*
3872d02a67dSdrh ** This routine implements the server. To start the server, first
3882d02a67dSdrh ** make sure g.serverHalt is false, then create a new detached thread
3892d02a67dSdrh ** on this procedure. See the sqlite3_server_start() routine below
3902d02a67dSdrh ** for an example. This procedure loops until g.serverHalt becomes
3912d02a67dSdrh ** true.
3922d02a67dSdrh */
sqlite3_server(void * NotUsed)3932d02a67dSdrh void *sqlite3_server(void *NotUsed){
3942d02a67dSdrh if( pthread_mutex_trylock(&g.serverMutex) ){
3952d02a67dSdrh return 0; /* Another server is already running */
3962d02a67dSdrh }
397df12a9bcSdrh sqlite3_enable_shared_cache(1);
3982d02a67dSdrh while( !g.serverHalt ){
3992d02a67dSdrh SqlMessage *pMsg;
4002d02a67dSdrh
4012d02a67dSdrh /* Remove the last message from the message queue.
4022d02a67dSdrh */
4032d02a67dSdrh pthread_mutex_lock(&g.queueMutex);
4042d02a67dSdrh while( g.pQueueTail==0 && g.serverHalt==0 ){
4052d02a67dSdrh pthread_cond_wait(&g.serverWakeup, &g.queueMutex);
4062d02a67dSdrh }
4072d02a67dSdrh pMsg = g.pQueueTail;
4082d02a67dSdrh if( pMsg ){
4092d02a67dSdrh if( pMsg->pPrev ){
4102d02a67dSdrh pMsg->pPrev->pNext = 0;
4112d02a67dSdrh }else{
4122d02a67dSdrh g.pQueueHead = 0;
4132d02a67dSdrh }
4142d02a67dSdrh g.pQueueTail = pMsg->pPrev;
4152d02a67dSdrh }
4162d02a67dSdrh pthread_mutex_unlock(&g.queueMutex);
4172d02a67dSdrh if( pMsg==0 ) break;
4182d02a67dSdrh
4192d02a67dSdrh /* Process the message just removed
4202d02a67dSdrh */
4212d02a67dSdrh pthread_mutex_lock(&pMsg->clientMutex);
4222d02a67dSdrh switch( pMsg->op ){
4232d02a67dSdrh case MSG_Open: {
4242d02a67dSdrh pMsg->errCode = sqlite3_open(pMsg->zIn, &pMsg->pDb);
4252d02a67dSdrh break;
4262d02a67dSdrh }
4272d02a67dSdrh case MSG_Prepare: {
4282d02a67dSdrh pMsg->errCode = sqlite3_prepare(pMsg->pDb, pMsg->zIn, pMsg->nByte,
4292d02a67dSdrh &pMsg->pStmt, &pMsg->zOut);
4302d02a67dSdrh break;
4312d02a67dSdrh }
4322d02a67dSdrh case MSG_Step: {
4332d02a67dSdrh pMsg->errCode = sqlite3_step(pMsg->pStmt);
4342d02a67dSdrh break;
4352d02a67dSdrh }
4362d02a67dSdrh case MSG_Reset: {
4372d02a67dSdrh pMsg->errCode = sqlite3_reset(pMsg->pStmt);
4382d02a67dSdrh break;
4392d02a67dSdrh }
4402d02a67dSdrh case MSG_Finalize: {
4412d02a67dSdrh pMsg->errCode = sqlite3_finalize(pMsg->pStmt);
4422d02a67dSdrh break;
4432d02a67dSdrh }
4442d02a67dSdrh case MSG_Close: {
4452d02a67dSdrh pMsg->errCode = sqlite3_close(pMsg->pDb);
4462d02a67dSdrh break;
4472d02a67dSdrh }
4482d02a67dSdrh }
4492d02a67dSdrh
4502d02a67dSdrh /* Signal the client that the message has been processed.
4512d02a67dSdrh */
4522d02a67dSdrh pMsg->op = MSG_Done;
4532d02a67dSdrh pthread_mutex_unlock(&pMsg->clientMutex);
4542d02a67dSdrh pthread_cond_signal(&pMsg->clientWakeup);
4552d02a67dSdrh }
4563586110bSdanielk1977 pthread_mutex_unlock(&g.serverMutex);
4572d02a67dSdrh return 0;
4582d02a67dSdrh }
4592d02a67dSdrh
4602d02a67dSdrh /*
4612d02a67dSdrh ** Start a server thread if one is not already running. If there
4622d02a67dSdrh ** is aleady a server thread running, the new thread will quickly
4632d02a67dSdrh ** die and this routine is effectively a no-op.
4642d02a67dSdrh */
sqlite3_server_start(void)4652d02a67dSdrh void sqlite3_server_start(void){
4662d02a67dSdrh pthread_t x;
4672d02a67dSdrh int rc;
4682d02a67dSdrh g.serverHalt = 0;
4692d02a67dSdrh rc = pthread_create(&x, 0, sqlite3_server, 0);
4702d02a67dSdrh if( rc==0 ){
4712d02a67dSdrh pthread_detach(x);
4722d02a67dSdrh }
4732d02a67dSdrh }
4742d02a67dSdrh
4752d02a67dSdrh /*
476*cfe111b1Sdan ** A wrapper around sqlite3_server() that decrements the int variable
477*cfe111b1Sdan ** pointed to by the first argument after the sqlite3_server() call
478*cfe111b1Sdan ** returns.
479*cfe111b1Sdan */
serverWrapper(void * pnDecr)480*cfe111b1Sdan static void *serverWrapper(void *pnDecr){
481*cfe111b1Sdan void *p = sqlite3_server(0);
482*cfe111b1Sdan (*(int*)pnDecr)--;
483*cfe111b1Sdan return p;
484*cfe111b1Sdan }
485*cfe111b1Sdan
486*cfe111b1Sdan /*
487*cfe111b1Sdan ** This function is the similar to sqlite3_server_start(), except that
488*cfe111b1Sdan ** the integer pointed to by the first argument is decremented when
489*cfe111b1Sdan ** the server thread exits.
490*cfe111b1Sdan */
sqlite3_server_start2(int * pnDecr)491*cfe111b1Sdan void sqlite3_server_start2(int *pnDecr){
492*cfe111b1Sdan pthread_t x;
493*cfe111b1Sdan int rc;
494*cfe111b1Sdan g.serverHalt = 0;
495*cfe111b1Sdan rc = pthread_create(&x, 0, serverWrapper, (void*)pnDecr);
496*cfe111b1Sdan if( rc==0 ){
497*cfe111b1Sdan pthread_detach(x);
498*cfe111b1Sdan }
499*cfe111b1Sdan }
500*cfe111b1Sdan
501*cfe111b1Sdan /*
5022d02a67dSdrh ** If a server thread is running, then stop it. If no server is
5032d02a67dSdrh ** running, this routine is effectively a no-op.
5042d02a67dSdrh **
5053586110bSdanielk1977 ** This routine waits until the server has actually stopped before
5063586110bSdanielk1977 ** returning.
5072d02a67dSdrh */
sqlite3_server_stop(void)5082d02a67dSdrh void sqlite3_server_stop(void){
5092d02a67dSdrh g.serverHalt = 1;
5102d02a67dSdrh pthread_cond_broadcast(&g.serverWakeup);
5113586110bSdanielk1977 pthread_mutex_lock(&g.serverMutex);
5123586110bSdanielk1977 pthread_mutex_unlock(&g.serverMutex);
5132d02a67dSdrh }
5142d02a67dSdrh
5153a2d29f8Sshaneh #endif /* SQLITE_OS_UNIX && SQLITE_THREADSAFE */
5162d02a67dSdrh #endif /* defined(SQLITE_SERVER) */
517