/ Check-in [555fc07e]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Changes to the thread routines to disable them when threading is turned off using sqlite3_config().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | threads-sort-ex1
Files: files | file ages | folders
SHA1: 555fc07efd1a1bc597804dcacbbcd95e88e75e90
User & Date: drh 2012-08-20 12:36:53
References
2012-08-21
17:36
Cherry-pick [555fc07]: Changes to the thread routines to disable them when threading is turned off using sqlite3_config(). Also merge all recent trunk changes. check-in: c92b0fe1 user: drh tags: threads
Context
2012-08-21
17:46
Merge in all the latest trunk changes. check-in: 45cdc32f user: drh tags: threads-sort-ex1
2012-08-20
12:36
Changes to the thread routines to disable them when threading is turned off using sqlite3_config(). check-in: 555fc07e user: drh tags: threads-sort-ex1
2012-08-16
20:05
Attempt to use two cores to do sorting. Unfortunately, instead of making sorts go faster as was hoped, this changes slows sorting down by about 10%. (Later:) The previous measurement was compiled using -pg. When compiled using -Os, this new code is roughly 10% faster than the original. check-in: 11dd05e5 user: drh tags: threads-sort-ex1
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/threads.c.

    32     32   
    33     33   #define SQLITE_THREADS_IMPLEMENTED 1  /* Prevent the single-thread code below */
    34     34   #include <pthread.h>
    35     35   
    36     36   /* A running thread */
    37     37   struct SQLiteThread {
    38     38     pthread_t tid;
           39  +  int done;
           40  +  void *pOut;
    39     41   };
    40     42   
    41     43   /* Create a new thread */
    42     44   int sqlite3ThreadCreate(
    43     45     SQLiteThread **ppThread,  /* OUT: Write the thread object here */
    44     46     void *(*xTask)(void*),    /* Routine to run in a separate thread */
    45     47     void *pIn                 /* Argument passed into xTask() */
................................................................................
    48     50     int rc;
    49     51   
    50     52     assert( ppThread!=0 );
    51     53     assert( xTask!=0 );
    52     54     *ppThread = 0;
    53     55     p = sqlite3Malloc(sizeof(*p));
    54     56     if( p==0 ) return SQLITE_NOMEM;
    55         -  rc = pthread_create(&p->tid, 0, xTask, pIn);
    56         -  if( rc ){
    57         -    sqlite3_free(p);
    58         -    return SQLITE_ERROR;
           57  +  memset(p, 0, sizeof(*p));
           58  +  if( sqlite3GlobalConfig.bCoreMutex==0
           59  +    || pthread_create(&p->tid, 0, xTask, pIn)!=0 
           60  +  ){
           61  +    p->done = 1;
           62  +    p->pOut = xTask(pIn);
    59     63     }
    60     64     *ppThread = p;
    61     65     return SQLITE_OK;
    62     66   }
    63     67   
    64     68   /* Get the results of the thread */
    65     69   int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
    66     70     int rc;
    67     71   
    68     72     assert( ppOut!=0 );
    69     73     if( p==0 ) return SQLITE_NOMEM;
    70         -  rc = pthread_join(p->tid, ppOut);
           74  +  if( p->done ){
           75  +    *ppOut = p->pOut;
           76  +    rc = SQLITE_OK;
           77  +  }else{
           78  +    rc = pthread_join(p->tid, ppOut);
           79  +  }
    71     80     sqlite3_free(p);
    72     81     return rc ? SQLITE_ERROR : SQLITE_OK;
    73     82   }
    74     83   
    75     84   #endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
    76     85   /******************************** End Unix Pthreads *************************/
    77     86   
................................................................................
   111    120     SQLiteThread *p;
   112    121   
   113    122     assert( ppThread!=0 );
   114    123     assert( xTask!=0 );
   115    124     *ppThread = 0;
   116    125     p = sqlite3Malloc(sizeof(*p));
   117    126     if( p==0 ) return SQLITE_NOMEM;
   118         -  p->xTask = xTask; p->pIn = pIn;
   119         -  p->tid = _beginthread(sqlite3ThreadProc, 0, p);
   120         -  if( p->tid==(uintptr_t)-1 ){
   121         -    sqlite3_free(p);
   122         -    return SQLITE_ERROR;
          127  +  if( sqlite3GlobalConfig.bCoreMutex==0 ){
          128  +    memset(p, 0, sizeof(*p));
          129  +  }else{
          130  +    p->xTask = xTask;
          131  +    p->pIn = pIn;
          132  +    p->tid = _beginthread(sqlite3ThreadProc, 0, p);
          133  +    if( p->tid==(uintptr_t)-1 ){
          134  +      memset(p, 0, sizeof(*p));
          135  +    }
          136  +  }
          137  +  if( p->xTask==0 ){
          138  +    p->pResult = xTask(pIn);
   123    139     }
   124    140     *ppThread = p;
   125    141     return SQLITE_OK;
   126    142   }
   127    143   
   128    144   /* Get the results of the thread */
   129    145   int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
   130    146     DWORD rc;
   131    147   
   132    148     assert( ppOut!=0 );
   133    149     if( p==0 ) return SQLITE_NOMEM;
   134         -  rc = sqlite3Win32Wait((HANDLE)p->tid);
   135         -  assert( rc!=WAIT_IO_COMPLETION );
          150  +  if( p->xTask==0 ){
          151  +    rc = WAIT_OBJECT_O;
          152  +  }else{
          153  +    rc = sqlite3Win32Wait((HANDLE)p->tid);
          154  +    assert( rc!=WAIT_IO_COMPLETION );
          155  +  }
   136    156     if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
   137    157     sqlite3_free(p);
   138    158     return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
   139    159   }
   140    160   
   141    161   #endif /* SQLITE_OS_WIN && !SQLITE_OS_WINRT */
   142    162   /******************************** End Win32 Threads *************************/