diff options
author | drh <drh@noemail.net> | 2012-07-21 19:40:42 +0000 |
---|---|---|
committer | drh <drh@noemail.net> | 2012-07-21 19:40:42 +0000 |
commit | f51446a38cb0db8dcfeade1e30da43de0f80d266 (patch) | |
tree | 60a7bee8f27852d5b5a5f18793290ded62efa149 /src/threads.c | |
parent | 5373b76bdc82139a07b3bd32126eb42bb193b27a (diff) | |
download | sqlite-f51446a38cb0db8dcfeade1e30da43de0f80d266.tar.gz sqlite-f51446a38cb0db8dcfeade1e30da43de0f80d266.zip |
Add an internal interface that allows the code to take advantage of multiple
cores by pushing subcomputations off into separate threads. The interface
is not currently used.
FossilOrigin-Name: 0e4d977a4a07d6de50acbf022c7dd947998b8d96
Diffstat (limited to 'src/threads.c')
-rw-r--r-- | src/threads.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/threads.c b/src/threads.c new file mode 100644 index 000000000..ed9801551 --- /dev/null +++ b/src/threads.c @@ -0,0 +1,120 @@ +/* +** 2012 July 21 +** +** 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 presents a simple cross-platform threading interface for +** use internally by SQLite. +** +** A "thread" can be created using sqlite3ThreadCreate(). This thread +** runs independently of its creator until it is joined using +** sqlite3ThreadJoin(), at which point it terminates. +** +** Threads do not have to be real. It could be that the work of the +** "thread" is done by the main thread at either the sqlite3ThreadCreate() +** or sqlite3ThreadJoin() call. This is, in fact, what happens in +** single threaded systems. Nothing in SQLite requires multiple threads. +** This interface exists so that applications that want to take advantage +** of multiple cores can do so, while also allowing applications to stay +** single-threaded if desired. +*/ +#include "sqliteInt.h" + +/********************************* Unix Pthreads ****************************/ +#if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) + +#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */ +#include <pthread.h> + +/* A running thread */ +struct SQLiteThread { + pthread_t tid; +}; + +/* Create a new thread */ +int sqlite3ThreadCreate( + SQLiteThread **ppThread, /* OUT: Write the thread object here */ + void *(*xTask)(void*), /* Routine to run in a separate thread */ + void *pIn /* Argument passed into xTask() */ +){ + SQLiteThread *p; + int rc; + + *ppThread = p = sqlite3Malloc(sizeof(*p)); + if( p==0 ) return SQLITE_OK; + rc = pthread_create(&p->tid, 0, xTask, pIn); + if( rc ){ + sqlite3_free(p); + return SQLITE_ERROR; + } + return SQLITE_OK; +} + +/* Get the results of the thread */ +int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + int rc; + if( p==0 ) return SQLITE_NOMEM; + rc = pthread_join(p->tid, ppOut); + sqlite3_free(p); + return rc ? SQLITE_ERROR : SQLITE_OK; +} + +#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */ +/******************************** End Unix Pthreads *************************/ + + +/********************************* Single-Threaded **************************/ +#ifndef SQLITE_THREADS_IMPLEMENTED +/* +** This implementation does not actually create a new thread. It does the +** work of the thread in the main thread, when either the thread is created +** or when it is joined +*/ + +/* A running thread */ +struct SQLiteThread { + void *(*xTask)(void*); /* The routine to run as a thread */ + void *pIn; /* Argument to xTask */ + void *pResult; /* Result of xTask */ +}; + +/* Create a new thread */ +int sqlite3ThreadCreate( + SQLiteThread **ppThread, /* OUT: Write the thread object here */ + void *(*xTask)(void*), /* Routine to run in a separate thread */ + void *pIn /* Argument passed into xTask() */ +){ + SQLiteThread *p; + *ppThread = p = sqlite3Malloc(sizeof(*p)); + if( p==0 ) return SQLITE_NOMEM; + if( (SQLITE_PTR_TO_INT(p)/17)&1 ){ + p->xTask = xTask; + p->pIn = pIn; + }else{ + p->xTask = 0; + p->pResult = xTask(pIn); + } + return p; +} + +/* Get the results of the thread */ +int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + if( p==0 ) return SQLITE_NOMEM; + if( p->xTask ){ + *ppOut = = p->xTask(p->pIn); + }else{ + *ppOut = p->pResult; + } + sqlite3_free(p); + return SQLITE_OK; +} + +#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */ +/****************************** End Single-Threaded *************************/ |