xref: /sqlite-3.40.0/src/threads.c (revision cb6effaf)
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 /* Get the results of the thread */
151 int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
152   DWORD rc;
153 
154   assert( ppOut!=0 );
155   if( p==0 ) return SQLITE_NOMEM;
156   if( p->xTask==0 ){
157     rc = WAIT_OBJECT_0;
158   }else{
159     rc = sqlite3Win32Wait((HANDLE)p->tid);
160     assert( rc!=WAIT_IO_COMPLETION );
161   }
162   if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
163   sqlite3_free(p);
164   return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
165 }
166 
167 #endif /* SQLITE_OS_WIN && !SQLITE_OS_WINRT */
168 /******************************** End Win32 Threads *************************/
169 
170 
171 /********************************* Single-Threaded **************************/
172 #ifndef SQLITE_THREADS_IMPLEMENTED
173 /*
174 ** This implementation does not actually create a new thread.  It does the
175 ** work of the thread in the main thread, when either the thread is created
176 ** or when it is joined
177 */
178 
179 /* A running thread */
180 struct SQLiteThread {
181   void *(*xTask)(void*);   /* The routine to run as a thread */
182   void *pIn;               /* Argument to xTask */
183   void *pResult;           /* Result of xTask */
184 };
185 
186 /* Create a new thread */
187 int sqlite3ThreadCreate(
188   SQLiteThread **ppThread,  /* OUT: Write the thread object here */
189   void *(*xTask)(void*),    /* Routine to run in a separate thread */
190   void *pIn                 /* Argument passed into xTask() */
191 ){
192   SQLiteThread *p;
193 
194   assert( ppThread!=0 );
195   assert( xTask!=0 );
196   *ppThread = 0;
197   p = sqlite3Malloc(sizeof(*p));
198   if( p==0 ) return SQLITE_NOMEM;
199   if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
200     p->xTask = xTask;
201     p->pIn = pIn;
202   }else{
203     p->xTask = 0;
204     p->pResult = xTask(pIn);
205   }
206   *ppThread = p;
207   return SQLITE_OK;
208 }
209 
210 /* Get the results of the thread */
211 int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
212 
213   assert( ppOut!=0 );
214   if( p==0 ) return SQLITE_NOMEM;
215   if( p->xTask ){
216     *ppOut = p->xTask(p->pIn);
217   }else{
218     *ppOut = p->pResult;
219   }
220   sqlite3_free(p);
221 
222 #if defined(SQLITE_TEST)
223   {
224     void *pTstAlloc = sqlite3Malloc(10);
225     if (!pTstAlloc) return SQLITE_NOMEM;
226     sqlite3_free(pTstAlloc);
227   }
228 #endif
229 
230   return SQLITE_OK;
231 }
232 
233 #endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
234 /****************************** End Single-Threaded *************************/
235 #endif /* SQLITE_MAX_WORKER_THREADS>0 */
236