4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 ******************************************************************************
13 ** This file presents a simple cross-platform threading interface for
14 ** use internally by SQLite.
16 ** A "thread" can be created using sqlite3ThreadCreate(). This thread
17 ** runs independently of its creator until it is joined using
18 ** sqlite3ThreadJoin(), at which point it terminates.
20 ** Threads do not have to be real. It could be that the work of the
21 ** "thread" is done by the main thread at either the sqlite3ThreadCreate()
22 ** or sqlite3ThreadJoin() call. This is, in fact, what happens in
23 ** single threaded systems. Nothing in SQLite requires multiple threads.
24 ** This interface exists so that applications that want to take advantage
25 ** of multiple cores can do so, while also allowing applications to stay
26 ** single-threaded if desired.
28 #include "sqliteInt.h"
33 #if SQLITE_MAX_WORKER_THREADS>0
35 /********************************* Unix Pthreads ****************************/
36 #if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0
38 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
41 /* A running thread */
43 pthread_t tid
; /* Thread ID */
44 int done
; /* Set to true when thread finishes */
45 void *pOut
; /* Result returned by the thread */
46 void *(*xTask
)(void*); /* The thread routine */
47 void *pIn
; /* Argument to the thread */
50 /* Create a new thread */
51 int sqlite3ThreadCreate(
52 SQLiteThread
**ppThread
, /* OUT: Write the thread object here */
53 void *(*xTask
)(void*), /* Routine to run in a separate thread */
54 void *pIn
/* Argument passed into xTask() */
59 assert( ppThread
!=0 );
61 /* This routine is never used in single-threaded mode */
62 assert( sqlite3GlobalConfig
.bCoreMutex
!=0 );
65 p
= sqlite3Malloc(sizeof(*p
));
66 if( p
==0 ) return SQLITE_NOMEM_BKPT
;
67 memset(p
, 0, sizeof(*p
));
70 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
71 ** function that returns SQLITE_ERROR when passed the argument 200, that
72 ** forces worker threads to run sequentially and deterministically
73 ** for testing purposes. */
74 if( sqlite3FaultSim(200) ){
77 rc
= pthread_create(&p
->tid
, 0, xTask
, pIn
);
87 /* Get the results of the thread */
88 int sqlite3ThreadJoin(SQLiteThread
*p
, void **ppOut
){
92 if( NEVER(p
==0) ) return SQLITE_NOMEM_BKPT
;
97 rc
= pthread_join(p
->tid
, ppOut
) ? SQLITE_ERROR
: SQLITE_OK
;
103 #endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
104 /******************************** End Unix Pthreads *************************/
107 /********************************* Win32 Threads ****************************/
108 #if SQLITE_OS_WIN_THREADS
110 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
113 /* A running thread */
114 struct SQLiteThread
{
115 void *tid
; /* The thread handle */
116 unsigned id
; /* The thread identifier */
117 void *(*xTask
)(void*); /* The routine to run as a thread */
118 void *pIn
; /* Argument to xTask */
119 void *pResult
; /* Result of xTask */
122 /* Thread procedure Win32 compatibility shim */
123 static unsigned __stdcall
sqlite3ThreadProc(
124 void *pArg
/* IN: Pointer to the SQLiteThread structure */
126 SQLiteThread
*p
= (SQLiteThread
*)pArg
;
131 ** This assert appears to trigger spuriously on certain
132 ** versions of Windows, possibly due to _beginthreadex()
133 ** and/or CreateThread() not fully setting their thread
134 ** ID parameter before starting the thread.
136 assert( p
->id
==GetCurrentThreadId() );
138 assert( p
->xTask
!=0 );
139 p
->pResult
= p
->xTask(p
->pIn
);
142 return 0; /* NOT REACHED */
145 /* Create a new thread */
146 int sqlite3ThreadCreate(
147 SQLiteThread
**ppThread
, /* OUT: Write the thread object here */
148 void *(*xTask
)(void*), /* Routine to run in a separate thread */
149 void *pIn
/* Argument passed into xTask() */
153 assert( ppThread
!=0 );
156 p
= sqlite3Malloc(sizeof(*p
));
157 if( p
==0 ) return SQLITE_NOMEM_BKPT
;
158 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
159 ** function that returns SQLITE_ERROR when passed the argument 200, that
160 ** forces worker threads to run sequentially and deterministically
161 ** (via the sqlite3FaultSim() term of the conditional) for testing
163 if( sqlite3GlobalConfig
.bCoreMutex
==0 || sqlite3FaultSim(200) ){
164 memset(p
, 0, sizeof(*p
));
168 p
->tid
= (void*)_beginthreadex(0, 0, sqlite3ThreadProc
, p
, 0, &p
->id
);
170 memset(p
, 0, sizeof(*p
));
174 p
->id
= GetCurrentThreadId();
175 p
->pResult
= xTask(pIn
);
181 DWORD
sqlite3Win32Wait(HANDLE hObject
); /* os_win.c */
183 /* Get the results of the thread */
184 int sqlite3ThreadJoin(SQLiteThread
*p
, void **ppOut
){
189 if( NEVER(p
==0) ) return SQLITE_NOMEM_BKPT
;
191 /* assert( p->id==GetCurrentThreadId() ); */
195 assert( p
->id
!=0 && p
->id
!=GetCurrentThreadId() );
196 rc
= sqlite3Win32Wait((HANDLE
)p
->tid
);
197 assert( rc
!=WAIT_IO_COMPLETION
);
198 bRc
= CloseHandle((HANDLE
)p
->tid
);
201 if( rc
==WAIT_OBJECT_0
) *ppOut
= p
->pResult
;
203 return (rc
==WAIT_OBJECT_0
) ? SQLITE_OK
: SQLITE_ERROR
;
206 #endif /* SQLITE_OS_WIN_THREADS */
207 /******************************** End Win32 Threads *************************/
210 /********************************* Single-Threaded **************************/
211 #ifndef SQLITE_THREADS_IMPLEMENTED
213 ** This implementation does not actually create a new thread. It does the
214 ** work of the thread in the main thread, when either the thread is created
215 ** or when it is joined
218 /* A running thread */
219 struct SQLiteThread
{
220 void *(*xTask
)(void*); /* The routine to run as a thread */
221 void *pIn
; /* Argument to xTask */
222 void *pResult
; /* Result of xTask */
225 /* Create a new thread */
226 int sqlite3ThreadCreate(
227 SQLiteThread
**ppThread
, /* OUT: Write the thread object here */
228 void *(*xTask
)(void*), /* Routine to run in a separate thread */
229 void *pIn
/* Argument passed into xTask() */
233 assert( ppThread
!=0 );
236 p
= sqlite3Malloc(sizeof(*p
));
237 if( p
==0 ) return SQLITE_NOMEM_BKPT
;
238 if( (SQLITE_PTR_TO_INT(p
)/17)&1 ){
243 p
->pResult
= xTask(pIn
);
249 /* Get the results of the thread */
250 int sqlite3ThreadJoin(SQLiteThread
*p
, void **ppOut
){
253 if( NEVER(p
==0) ) return SQLITE_NOMEM_BKPT
;
255 *ppOut
= p
->xTask(p
->pIn
);
261 #if defined(SQLITE_TEST)
263 void *pTstAlloc
= sqlite3Malloc(10);
264 if (!pTstAlloc
) return SQLITE_NOMEM_BKPT
;
265 sqlite3_free(pTstAlloc
);
272 #endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
273 /****************************** End Single-Threaded *************************/
274 #endif /* SQLITE_MAX_WORKER_THREADS>0 */