xref: /sqlite-3.40.0/src/threads.c (revision 2b49327d)
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 
55   assert( ppThread!=0 );
56   assert( xTask!=0 );
57   /* This routine is never used in single-threaded mode */
58   assert( sqlite3GlobalConfig.bCoreMutex!=0 );
59 
60   *ppThread = 0;
61   p = sqlite3Malloc(sizeof(*p));
62   if( p==0 ) return SQLITE_NOMEM;
63   memset(p, 0, sizeof(*p));
64   p->xTask = xTask;
65   p->pIn = pIn;
66   if( sqlite3FaultSim(200) ? 1 : pthread_create(&p->tid, 0, xTask, pIn) ){
67     p->done = 1;
68     p->pOut = xTask(pIn);
69   }
70   *ppThread = p;
71   return SQLITE_OK;
72 }
73 
74 /* Get the results of the thread */
75 int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
76   int rc;
77 
78   assert( ppOut!=0 );
79   if( p==0 ) return SQLITE_NOMEM;
80   if( p->done ){
81     *ppOut = p->pOut;
82     rc = SQLITE_OK;
83   }else{
84     rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
85   }
86   sqlite3_free(p);
87   return rc;
88 }
89 
90 #endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
91 /******************************** End Unix Pthreads *************************/
92 
93 
94 /********************************* Win32 Threads ****************************/
95 #if SQLITE_OS_WIN && !SQLITE_OS_WINRT && SQLITE_THREADSAFE>0
96 
97 #define SQLITE_THREADS_IMPLEMENTED 1  /* Prevent the single-thread code below */
98 #include <process.h>
99 
100 /* A running thread */
101 struct SQLiteThread {
102   uintptr_t tid;           /* The thread handle */
103   void *(*xTask)(void*);   /* The routine to run as a thread */
104   void *pIn;               /* Argument to xTask */
105   void *pResult;           /* Result of xTask */
106 };
107 
108 /* Thread procedure Win32 compatibility shim */
109 static void sqlite3ThreadProc(
110   void *pArg  /* IN: Pointer to the SQLiteThread structure */
111 ){
112   SQLiteThread *p = (SQLiteThread *)pArg;
113 
114   assert( p!=0 );
115   assert( p->xTask!=0 );
116   p->pResult = p->xTask(p->pIn);
117   _endthread();
118 }
119 
120 /* Create a new thread */
121 int sqlite3ThreadCreate(
122   SQLiteThread **ppThread,  /* OUT: Write the thread object here */
123   void *(*xTask)(void*),    /* Routine to run in a separate thread */
124   void *pIn                 /* Argument passed into xTask() */
125 ){
126   SQLiteThread *p;
127 
128   assert( ppThread!=0 );
129   assert( xTask!=0 );
130   *ppThread = 0;
131   p = sqlite3Malloc(sizeof(*p));
132   if( p==0 ) return SQLITE_NOMEM;
133   if( sqlite3GlobalConfig.bCoreMutex==0 ){
134     memset(p, 0, sizeof(*p));
135   }else{
136     p->xTask = xTask;
137     p->pIn = pIn;
138     p->tid = _beginthread(sqlite3ThreadProc, 0, p);
139     if( p->tid==(uintptr_t)-1 ){
140       memset(p, 0, sizeof(*p));
141     }
142   }
143   if( p->xTask==0 ){
144     p->pResult = xTask(pIn);
145   }
146   *ppThread = p;
147   return SQLITE_OK;
148 }
149 
150 /* Wait on an object */
151 DWORD sqlite3Win32Wait(HANDLE hObject){
152   DWORD rc;
153   while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
154                                        TRUE))==WAIT_IO_COMPLETION ){}
155   return rc;
156 }
157 
158 /* Get the results of the thread */
159 int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
160   DWORD rc;
161 
162   assert( ppOut!=0 );
163   if( p==0 ) return SQLITE_NOMEM;
164   if( p->xTask==0 ){
165     rc = WAIT_OBJECT_0;
166   }else{
167     rc = sqlite3Win32Wait((HANDLE)p->tid);
168     assert( rc!=WAIT_IO_COMPLETION );
169   }
170   if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
171   sqlite3_free(p);
172   return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
173 }
174 
175 #endif /* SQLITE_OS_WIN && !SQLITE_OS_WINRT */
176 /******************************** End Win32 Threads *************************/
177 
178 
179 /********************************* Single-Threaded **************************/
180 #ifndef SQLITE_THREADS_IMPLEMENTED
181 /*
182 ** This implementation does not actually create a new thread.  It does the
183 ** work of the thread in the main thread, when either the thread is created
184 ** or when it is joined
185 */
186 
187 /* A running thread */
188 struct SQLiteThread {
189   void *(*xTask)(void*);   /* The routine to run as a thread */
190   void *pIn;               /* Argument to xTask */
191   void *pResult;           /* Result of xTask */
192 };
193 
194 /* Create a new thread */
195 int sqlite3ThreadCreate(
196   SQLiteThread **ppThread,  /* OUT: Write the thread object here */
197   void *(*xTask)(void*),    /* Routine to run in a separate thread */
198   void *pIn                 /* Argument passed into xTask() */
199 ){
200   SQLiteThread *p;
201 
202   assert( ppThread!=0 );
203   assert( xTask!=0 );
204   *ppThread = 0;
205   p = sqlite3Malloc(sizeof(*p));
206   if( p==0 ) return SQLITE_NOMEM;
207   if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
208     p->xTask = xTask;
209     p->pIn = pIn;
210   }else{
211     p->xTask = 0;
212     p->pResult = xTask(pIn);
213   }
214   *ppThread = p;
215   return SQLITE_OK;
216 }
217 
218 /* Get the results of the thread */
219 int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
220 
221   assert( ppOut!=0 );
222   if( p==0 ) return SQLITE_NOMEM;
223   if( p->xTask ){
224     *ppOut = p->xTask(p->pIn);
225   }else{
226     *ppOut = p->pResult;
227   }
228   sqlite3_free(p);
229 
230 #if defined(SQLITE_TEST)
231   {
232     void *pTstAlloc = sqlite3Malloc(10);
233     if (!pTstAlloc) return SQLITE_NOMEM;
234     sqlite3_free(pTstAlloc);
235   }
236 #endif
237 
238   return SQLITE_OK;
239 }
240 
241 #endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
242 /****************************** End Single-Threaded *************************/
243 #endif /* SQLITE_MAX_WORKER_THREADS>0 */
244