#include "sqliteInt.h"
#if SQLITE_OS_WIN
# include "os_win.h"
#endif
#if SQLITE_MAX_WORKER_THREADS>0
#if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0
#define SQLITE_THREADS_IMPLEMENTED 1
#include <pthread.h>
struct SQLiteThread {
pthread_t tid;
int done;
void *pOut;
void *(*xTask)(void*);
void *pIn;
};
int sqlite3ThreadCreate(
SQLiteThread **ppThread,
void *(*xTask)(void*),
void *pIn
){
SQLiteThread *p;
int rc;
assert( ppThread!=0 );
assert( xTask!=0 );
assert( sqlite3GlobalConfig.bCoreMutex!=0 );
*ppThread = 0;
p = sqlite3Malloc(sizeof(*p));
if( p==0 ) return SQLITE_NOMEM_BKPT;
memset(p, 0, sizeof(*p));
p->xTask = xTask;
p->pIn = pIn;
if( sqlite3FaultSim(200) ){
rc = 1;
}else{
rc = pthread_create(&p->tid, 0, xTask, pIn);
}
if( rc ){
p->done = 1;
p->pOut = xTask(pIn);
}
*ppThread = p;
return SQLITE_OK;
}
int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
int rc;
assert( ppOut!=0 );
if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
if( p->done ){
*ppOut = p->pOut;
rc = SQLITE_OK;
}else{
rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
}
sqlite3_free(p);
return rc;
}
#endif
#if SQLITE_OS_WIN_THREADS
#define SQLITE_THREADS_IMPLEMENTED 1
#include <process.h>
struct SQLiteThread {
void *tid;
unsigned id;
void *(*xTask)(void*);
void *pIn;
void *pResult;
};
static unsigned __stdcall sqlite3ThreadProc(
void *pArg
){
SQLiteThread *p = (SQLiteThread *)pArg;
assert( p!=0 );
#if 0#endif
assert( p->xTask!=0 );
p->pResult = p->xTask(p->pIn);
_endthreadex(0);
return 0;
}
int sqlite3ThreadCreate(
SQLiteThread **ppThread,
void *(*xTask)(void*),
void *pIn
){
SQLiteThread *p;
assert( ppThread!=0 );
assert( xTask!=0 );
*ppThread = 0;
p = sqlite3Malloc(sizeof(*p));
if( p==0 ) return SQLITE_NOMEM_BKPT;
if( sqlite3GlobalConfig.bCoreMutex==0 || sqlite3FaultSim(200) ){
memset(p, 0, sizeof(*p));
}else{
p->xTask = xTask;
p->pIn = pIn;
p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
if( p->tid==0 ){
memset(p, 0, sizeof(*p));
}
}
if( p->xTask==0 ){
p->id = GetCurrentThreadId();
p->pResult = xTask(pIn);
}
*ppThread = p;
return SQLITE_OK;
}
DWORD sqlite3Win32Wait(HANDLE hObject);
int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
DWORD rc;
BOOL bRc;
assert( ppOut!=0 );
if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
if( p->xTask==0 ){
rc = WAIT_OBJECT_0;
assert( p->tid==0 );
}else{
assert( p->id!=0 && p->id!=GetCurrentThreadId() );
rc = sqlite3Win32Wait((HANDLE)p->tid);
assert( rc!=WAIT_IO_COMPLETION );
bRc = CloseHandle((HANDLE)p->tid);
assert( bRc );
}
if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
sqlite3_free(p);
return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
}
#endif
#ifndef SQLITE_THREADS_IMPLEMENTED
struct SQLiteThread {
void *(*xTask)(void*);
void *pIn;
void *pResult;
};
int sqlite3ThreadCreate(
SQLiteThread **ppThread,
void *(*xTask)(void*),
void *pIn
){
SQLiteThread *p;
assert( ppThread!=0 );
assert( xTask!=0 );
*ppThread = 0;
p = sqlite3Malloc(sizeof(*p));
if( p==0 ) return SQLITE_NOMEM_BKPT;
if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
p->xTask = xTask;
p->pIn = pIn;
}else{
p->xTask = 0;
p->pResult = xTask(pIn);
}
*ppThread = p;
return SQLITE_OK;
}
int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
assert( ppOut!=0 );
if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
if( p->xTask ){
*ppOut = p->xTask(p->pIn);
}else{
*ppOut = p->pResult;
}
sqlite3_free(p);
#if defined(SQLITE_TEST)
{
void *pTstAlloc = sqlite3Malloc(10);
if (!pTstAlloc) return SQLITE_NOMEM_BKPT;
sqlite3_free(pTstAlloc);
}
#endif
return SQLITE_OK;
}
#endif
#endif