xref: /sqlite-3.40.0/src/threads.c (revision acd57ea0)
1 /*
2 ** 2012 July 21
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
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.
10 **
11 ******************************************************************************
12 **
13 ** This file presents a simple cross-platform threading interface for
14 ** use internally by SQLite.
15 **
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.
19 **
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.
27 */
28 #include "sqliteInt.h"
29 
30 #if SQLITE_MAX_WORKER_THREADS>0
31 
32 /********************************* Unix Pthreads ****************************/
33 #if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0
34 
35 #define SQLITE_THREADS_IMPLEMENTED 1  /* Prevent the single-thread code below */
36 #include <pthread.h>
37 
38 /* A running thread */
39 struct SQLiteThread {
40   pthread_t tid;                 /* Thread ID */
41   int done;                      /* Set to true when thread finishes */
42   void *pOut;                    /* Result returned by the thread */
43   void *(*xTask)(void*);         /* The thread routine */
44   void *pIn;                     /* Argument to the thread */
45 };
46 
47 /* Create a new thread */
48 int sqlite3ThreadCreate(
49   SQLiteThread **ppThread,  /* OUT: Write the thread object here */
50   void *(*xTask)(void*),    /* Routine to run in a separate thread */
51   void *pIn                 /* Argument passed into xTask() */
52 ){
53   SQLiteThread *p;
54   int rc;
55 
56   assert( ppThread!=0 );
57   assert( xTask!=0 );
58   /* This routine is never used in single-threaded mode */
59   assert( sqlite3GlobalConfig.bCoreMutex!=0 );
60 
61   *ppThread = 0;
62   p = sqlite3Malloc(sizeof(*p));
63   if( p==0 ) return SQLITE_NOMEM;
64   memset(p, 0, sizeof(*p));
65   p->xTask = xTask;
66   p->pIn = pIn;
67   if( sqlite3FaultSim(200) ){
68     rc = 1;
69   }else{
70     rc = pthread_create(&p->tid, 0, xTask, pIn);
71   }
72   if( rc ){
73     p->done = 1;
74     p->pOut = xTask(pIn);
75   }
76   *ppThread = p;
77   return SQLITE_OK;
78 }
79 
80 /* Get the results of the thread */
81 int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
82   int rc;
83 
84   assert( ppOut!=0 );
85   if( NEVER(p==0) ) return SQLITE_NOMEM;
86   if( p->done ){
87     *ppOut = p->pOut;
88     rc = SQLITE_OK;
89   }else{
90     rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
91   }
92   sqlite3_free(p);
93   return rc;
94 }
95 
96 #endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
97 /******************************** End Unix Pthreads *************************/
98 
99 
100 /********************************* Win32 Threads ****************************/
101 #if SQLITE_OS_WIN && !SQLITE_OS_WINRT && SQLITE_THREADSAFE>0
102 
103 #define SQLITE_THREADS_IMPLEMENTED 1  /* Prevent the single-thread code below */
104 #include <process.h>
105 
106 /* A running thread */
107 struct SQLiteThread {
108   uintptr_t tid;           /* The thread handle */
109   unsigned id;             /* The thread identifier */
110   void *(*xTask)(void*);   /* The routine to run as a thread */
111   void *pIn;               /* Argument to xTask */
112   void *pResult;           /* Result of xTask */
113 };
114 
115 /* Thread procedure Win32 compatibility shim */
116 static unsigned __stdcall sqlite3ThreadProc(
117   void *pArg  /* IN: Pointer to the SQLiteThread structure */
118 ){
119   SQLiteThread *p = (SQLiteThread *)pArg;
120 
121   assert( p!=0 );
122   assert( p->id==GetCurrentThreadId() );
123   assert( p->xTask!=0 );
124   p->pResult = p->xTask(p->pIn);
125 
126   _endthreadex(0);
127   return 0; /* NOT REACHED */
128 }
129 
130 /* Create a new thread */
131 int sqlite3ThreadCreate(
132   SQLiteThread **ppThread,  /* OUT: Write the thread object here */
133   void *(*xTask)(void*),    /* Routine to run in a separate thread */
134   void *pIn                 /* Argument passed into xTask() */
135 ){
136   SQLiteThread *p;
137 
138   assert( ppThread!=0 );
139   assert( xTask!=0 );
140   *ppThread = 0;
141   p = sqlite3Malloc(sizeof(*p));
142   if( p==0 ) return SQLITE_NOMEM;
143   if( sqlite3GlobalConfig.bCoreMutex==0 ){
144     memset(p, 0, sizeof(*p));
145   }else{
146     p->xTask = xTask;
147     p->pIn = pIn;
148     p->tid = _beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
149     if( p->tid==0 ){
150       memset(p, 0, sizeof(*p));
151     }
152   }
153   if( p->xTask==0 ){
154     p->id = GetCurrentThreadId();
155     p->pResult = xTask(pIn);
156   }
157   *ppThread = p;
158   return SQLITE_OK;
159 }
160 
161 DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
162 
163 /* Get the results of the thread */
164 int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
165   DWORD rc;
166   BOOL bRc;
167 
168   assert( ppOut!=0 );
169   if( NEVER(p==0) ) return SQLITE_NOMEM;
170   if( p->xTask==0 ){
171     assert( p->id==GetCurrentThreadId() );
172     rc = WAIT_OBJECT_0;
173     assert( p->tid==0 );
174   }else{
175     assert( p->id!=0 && p->id!=GetCurrentThreadId() );
176     rc = sqlite3Win32Wait((HANDLE)p->tid);
177     assert( rc!=WAIT_IO_COMPLETION );
178     bRc = CloseHandle((HANDLE)p->tid);
179     assert( bRc );
180   }
181   if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
182   sqlite3_free(p);
183   return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
184 }
185 
186 #endif /* SQLITE_OS_WIN && !SQLITE_OS_WINRT */
187 /******************************** End Win32 Threads *************************/
188 
189 
190 /********************************* Single-Threaded **************************/
191 #ifndef SQLITE_THREADS_IMPLEMENTED
192 /*
193 ** This implementation does not actually create a new thread.  It does the
194 ** work of the thread in the main thread, when either the thread is created
195 ** or when it is joined
196 */
197 
198 /* A running thread */
199 struct SQLiteThread {
200   void *(*xTask)(void*);   /* The routine to run as a thread */
201   void *pIn;               /* Argument to xTask */
202   void *pResult;           /* Result of xTask */
203 };
204 
205 /* Create a new thread */
206 int sqlite3ThreadCreate(
207   SQLiteThread **ppThread,  /* OUT: Write the thread object here */
208   void *(*xTask)(void*),    /* Routine to run in a separate thread */
209   void *pIn                 /* Argument passed into xTask() */
210 ){
211   SQLiteThread *p;
212 
213   assert( ppThread!=0 );
214   assert( xTask!=0 );
215   *ppThread = 0;
216   p = sqlite3Malloc(sizeof(*p));
217   if( p==0 ) return SQLITE_NOMEM;
218   if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
219     p->xTask = xTask;
220     p->pIn = pIn;
221   }else{
222     p->xTask = 0;
223     p->pResult = xTask(pIn);
224   }
225   *ppThread = p;
226   return SQLITE_OK;
227 }
228 
229 /* Get the results of the thread */
230 int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
231 
232   assert( ppOut!=0 );
233   if( NEVER(p==0) ) return SQLITE_NOMEM;
234   if( p->xTask ){
235     *ppOut = p->xTask(p->pIn);
236   }else{
237     *ppOut = p->pResult;
238   }
239   sqlite3_free(p);
240 
241 #if defined(SQLITE_TEST)
242   {
243     void *pTstAlloc = sqlite3Malloc(10);
244     if (!pTstAlloc) return SQLITE_NOMEM;
245     sqlite3_free(pTstAlloc);
246   }
247 #endif
248 
249   return SQLITE_OK;
250 }
251 
252 #endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
253 /****************************** End Single-Threaded *************************/
254 #endif /* SQLITE_MAX_WORKER_THREADS>0 */
255