drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 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 | |
dan | b3f56fd | 2014-03-31 19:57:34 +0000 | [diff] [blame] | 30 | #if SQLITE_MAX_WORKER_THREADS>0 |
| 31 | |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 32 | /********************************* Unix Pthreads ****************************/ |
drh | 3c86363 | 2014-03-25 14:12:16 +0000 | [diff] [blame] | 33 | #if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0 |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 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 { |
drh | 3de4df2 | 2014-04-24 12:28:28 +0000 | [diff] [blame] | 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 */ |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 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; |
drh | b92284d | 2014-07-29 18:46:30 +0000 | [diff] [blame] | 54 | int rc; |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 55 | |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 56 | assert( ppThread!=0 ); |
| 57 | assert( xTask!=0 ); |
drh | cb6effa | 2014-05-20 19:11:50 +0000 | [diff] [blame] | 58 | /* This routine is never used in single-threaded mode */ |
| 59 | assert( sqlite3GlobalConfig.bCoreMutex!=0 ); |
| 60 | |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 61 | *ppThread = 0; |
| 62 | p = sqlite3Malloc(sizeof(*p)); |
| 63 | if( p==0 ) return SQLITE_NOMEM; |
drh | 6ddecb7 | 2012-08-21 17:36:44 +0000 | [diff] [blame] | 64 | memset(p, 0, sizeof(*p)); |
drh | 3de4df2 | 2014-04-24 12:28:28 +0000 | [diff] [blame] | 65 | p->xTask = xTask; |
| 66 | p->pIn = pIn; |
drh | b92284d | 2014-07-29 18:46:30 +0000 | [diff] [blame] | 67 | if( sqlite3FaultSim(200) ){ |
| 68 | rc = 1; |
| 69 | }else{ |
| 70 | rc = pthread_create(&p->tid, 0, xTask, pIn); |
| 71 | } |
| 72 | if( rc ){ |
drh | 6ddecb7 | 2012-08-21 17:36:44 +0000 | [diff] [blame] | 73 | p->done = 1; |
| 74 | p->pOut = xTask(pIn); |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 75 | } |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 76 | *ppThread = p; |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 77 | return SQLITE_OK; |
| 78 | } |
| 79 | |
| 80 | /* Get the results of the thread */ |
| 81 | int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ |
| 82 | int rc; |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 83 | |
| 84 | assert( ppOut!=0 ); |
drh | b92284d | 2014-07-29 18:46:30 +0000 | [diff] [blame] | 85 | if( NEVER(p==0) ) return SQLITE_NOMEM; |
drh | 6ddecb7 | 2012-08-21 17:36:44 +0000 | [diff] [blame] | 86 | if( p->done ){ |
| 87 | *ppOut = p->pOut; |
| 88 | rc = SQLITE_OK; |
| 89 | }else{ |
drh | cb6effa | 2014-05-20 19:11:50 +0000 | [diff] [blame] | 90 | rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK; |
drh | 6ddecb7 | 2012-08-21 17:36:44 +0000 | [diff] [blame] | 91 | } |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 92 | sqlite3_free(p); |
drh | cb6effa | 2014-05-20 19:11:50 +0000 | [diff] [blame] | 93 | return rc; |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | #endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */ |
| 97 | /******************************** End Unix Pthreads *************************/ |
| 98 | |
| 99 | |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 100 | /********************************* Win32 Threads ****************************/ |
drh | 3c86363 | 2014-03-25 14:12:16 +0000 | [diff] [blame] | 101 | #if SQLITE_OS_WIN && !SQLITE_OS_WINRT && SQLITE_THREADSAFE>0 |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 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 */ |
mistachkin | b1ac2bc | 2014-07-29 16:37:53 +0000 | [diff] [blame] | 109 | unsigned id; /* The thread identifier */ |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 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 */ |
mistachkin | b1ac2bc | 2014-07-29 16:37:53 +0000 | [diff] [blame] | 116 | static unsigned __stdcall sqlite3ThreadProc( |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 117 | void *pArg /* IN: Pointer to the SQLiteThread structure */ |
| 118 | ){ |
| 119 | SQLiteThread *p = (SQLiteThread *)pArg; |
| 120 | |
| 121 | assert( p!=0 ); |
mistachkin | 0479c6a | 2014-07-29 21:44:13 +0000 | [diff] [blame] | 122 | #if 0 |
| 123 | /* |
| 124 | ** This assert appears to trigger spuriously on certain |
| 125 | ** versions of Windows, possibly due to _beginthreadex() |
| 126 | ** and/or CreateThread() not fully setting their thread |
| 127 | ** ID parameter before starting the thread. |
| 128 | */ |
mistachkin | b1ac2bc | 2014-07-29 16:37:53 +0000 | [diff] [blame] | 129 | assert( p->id==GetCurrentThreadId() ); |
mistachkin | 0479c6a | 2014-07-29 21:44:13 +0000 | [diff] [blame] | 130 | #endif |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 131 | assert( p->xTask!=0 ); |
| 132 | p->pResult = p->xTask(p->pIn); |
mistachkin | b1ac2bc | 2014-07-29 16:37:53 +0000 | [diff] [blame] | 133 | |
| 134 | _endthreadex(0); |
| 135 | return 0; /* NOT REACHED */ |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* Create a new thread */ |
| 139 | int sqlite3ThreadCreate( |
| 140 | SQLiteThread **ppThread, /* OUT: Write the thread object here */ |
| 141 | void *(*xTask)(void*), /* Routine to run in a separate thread */ |
| 142 | void *pIn /* Argument passed into xTask() */ |
| 143 | ){ |
| 144 | SQLiteThread *p; |
| 145 | |
| 146 | assert( ppThread!=0 ); |
| 147 | assert( xTask!=0 ); |
| 148 | *ppThread = 0; |
| 149 | p = sqlite3Malloc(sizeof(*p)); |
| 150 | if( p==0 ) return SQLITE_NOMEM; |
drh | 6ddecb7 | 2012-08-21 17:36:44 +0000 | [diff] [blame] | 151 | if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
| 152 | memset(p, 0, sizeof(*p)); |
| 153 | }else{ |
| 154 | p->xTask = xTask; |
| 155 | p->pIn = pIn; |
mistachkin | b1ac2bc | 2014-07-29 16:37:53 +0000 | [diff] [blame] | 156 | p->tid = _beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id); |
mistachkin | acd57ea | 2014-07-29 19:00:43 +0000 | [diff] [blame] | 157 | if( p->tid==0 ){ |
drh | 6ddecb7 | 2012-08-21 17:36:44 +0000 | [diff] [blame] | 158 | memset(p, 0, sizeof(*p)); |
| 159 | } |
| 160 | } |
| 161 | if( p->xTask==0 ){ |
mistachkin | b1ac2bc | 2014-07-29 16:37:53 +0000 | [diff] [blame] | 162 | p->id = GetCurrentThreadId(); |
drh | 6ddecb7 | 2012-08-21 17:36:44 +0000 | [diff] [blame] | 163 | p->pResult = xTask(pIn); |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 164 | } |
| 165 | *ppThread = p; |
| 166 | return SQLITE_OK; |
| 167 | } |
| 168 | |
mistachkin | b1ac2bc | 2014-07-29 16:37:53 +0000 | [diff] [blame] | 169 | DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */ |
drh | 2b49327 | 2014-07-29 00:23:08 +0000 | [diff] [blame] | 170 | |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 171 | /* Get the results of the thread */ |
| 172 | int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ |
| 173 | DWORD rc; |
mistachkin | b1ac2bc | 2014-07-29 16:37:53 +0000 | [diff] [blame] | 174 | BOOL bRc; |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 175 | |
| 176 | assert( ppOut!=0 ); |
drh | b92284d | 2014-07-29 18:46:30 +0000 | [diff] [blame] | 177 | if( NEVER(p==0) ) return SQLITE_NOMEM; |
drh | 6ddecb7 | 2012-08-21 17:36:44 +0000 | [diff] [blame] | 178 | if( p->xTask==0 ){ |
mistachkin | 7c2231c | 2014-07-29 18:53:01 +0000 | [diff] [blame] | 179 | assert( p->id==GetCurrentThreadId() ); |
mistachkin | f7da555 | 2014-04-04 21:40:38 +0000 | [diff] [blame] | 180 | rc = WAIT_OBJECT_0; |
mistachkin | 7c2231c | 2014-07-29 18:53:01 +0000 | [diff] [blame] | 181 | assert( p->tid==0 ); |
drh | 6ddecb7 | 2012-08-21 17:36:44 +0000 | [diff] [blame] | 182 | }else{ |
mistachkin | b1ac2bc | 2014-07-29 16:37:53 +0000 | [diff] [blame] | 183 | assert( p->id!=0 && p->id!=GetCurrentThreadId() ); |
drh | 6ddecb7 | 2012-08-21 17:36:44 +0000 | [diff] [blame] | 184 | rc = sqlite3Win32Wait((HANDLE)p->tid); |
| 185 | assert( rc!=WAIT_IO_COMPLETION ); |
mistachkin | b1ac2bc | 2014-07-29 16:37:53 +0000 | [diff] [blame] | 186 | bRc = CloseHandle((HANDLE)p->tid); |
| 187 | assert( bRc ); |
drh | 6ddecb7 | 2012-08-21 17:36:44 +0000 | [diff] [blame] | 188 | } |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 189 | if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult; |
| 190 | sqlite3_free(p); |
| 191 | return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR; |
| 192 | } |
| 193 | |
| 194 | #endif /* SQLITE_OS_WIN && !SQLITE_OS_WINRT */ |
| 195 | /******************************** End Win32 Threads *************************/ |
| 196 | |
| 197 | |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 198 | /********************************* Single-Threaded **************************/ |
| 199 | #ifndef SQLITE_THREADS_IMPLEMENTED |
| 200 | /* |
| 201 | ** This implementation does not actually create a new thread. It does the |
| 202 | ** work of the thread in the main thread, when either the thread is created |
| 203 | ** or when it is joined |
| 204 | */ |
| 205 | |
| 206 | /* A running thread */ |
| 207 | struct SQLiteThread { |
| 208 | void *(*xTask)(void*); /* The routine to run as a thread */ |
| 209 | void *pIn; /* Argument to xTask */ |
| 210 | void *pResult; /* Result of xTask */ |
| 211 | }; |
| 212 | |
| 213 | /* Create a new thread */ |
| 214 | int sqlite3ThreadCreate( |
| 215 | SQLiteThread **ppThread, /* OUT: Write the thread object here */ |
| 216 | void *(*xTask)(void*), /* Routine to run in a separate thread */ |
| 217 | void *pIn /* Argument passed into xTask() */ |
| 218 | ){ |
| 219 | SQLiteThread *p; |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 220 | |
| 221 | assert( ppThread!=0 ); |
| 222 | assert( xTask!=0 ); |
| 223 | *ppThread = 0; |
| 224 | p = sqlite3Malloc(sizeof(*p)); |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 225 | if( p==0 ) return SQLITE_NOMEM; |
| 226 | if( (SQLITE_PTR_TO_INT(p)/17)&1 ){ |
| 227 | p->xTask = xTask; |
| 228 | p->pIn = pIn; |
| 229 | }else{ |
| 230 | p->xTask = 0; |
| 231 | p->pResult = xTask(pIn); |
| 232 | } |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 233 | *ppThread = p; |
| 234 | return SQLITE_OK; |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* Get the results of the thread */ |
| 238 | int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ |
dan | d94d4ee | 2014-05-05 09:08:54 +0000 | [diff] [blame] | 239 | |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 240 | assert( ppOut!=0 ); |
drh | b92284d | 2014-07-29 18:46:30 +0000 | [diff] [blame] | 241 | if( NEVER(p==0) ) return SQLITE_NOMEM; |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 242 | if( p->xTask ){ |
mistachkin | da0e471 | 2012-07-21 22:49:08 +0000 | [diff] [blame] | 243 | *ppOut = p->xTask(p->pIn); |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 244 | }else{ |
| 245 | *ppOut = p->pResult; |
| 246 | } |
| 247 | sqlite3_free(p); |
dan | d94d4ee | 2014-05-05 09:08:54 +0000 | [diff] [blame] | 248 | |
| 249 | #if defined(SQLITE_TEST) |
| 250 | { |
| 251 | void *pTstAlloc = sqlite3Malloc(10); |
| 252 | if (!pTstAlloc) return SQLITE_NOMEM; |
| 253 | sqlite3_free(pTstAlloc); |
| 254 | } |
| 255 | #endif |
| 256 | |
drh | f51446a | 2012-07-21 19:40:42 +0000 | [diff] [blame] | 257 | return SQLITE_OK; |
| 258 | } |
| 259 | |
| 260 | #endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */ |
| 261 | /****************************** End Single-Threaded *************************/ |
dan | b3f56fd | 2014-03-31 19:57:34 +0000 | [diff] [blame] | 262 | #endif /* SQLITE_MAX_WORKER_THREADS>0 */ |