/ Check-in [4e75897e]
Login

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

Overview
Comment:Ensure that "static" mutexes are deleted on shutdown so that they are not leaked across restarts. (CVS 6700)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4e75897ee177bd24145f9dbfccd41a03c108440f
User & Date: shane 2009-06-01 17:06:08
Context
2009-06-01
17:10
Added assert to verify winMutex system is inited before "static" mutexes are used. (CVS 6701) check-in: b126013b user: shane tags: trunk
17:06
Ensure that "static" mutexes are deleted on shutdown so that they are not leaked across restarts. (CVS 6700) check-in: 4e75897e user: shane tags: trunk
16:53
Fix compiler warnings with MSVC build. (CVS 6699) check-in: 07915885 user: shane tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/mutex_w32.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains the C functions that implement mutexes for win32
    13     13   **
    14         -** $Id: mutex_w32.c,v 1.15 2009/01/30 16:09:23 shane Exp $
           14  +** $Id: mutex_w32.c,v 1.16 2009/06/01 17:06:08 shane Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   
    18     18   /*
    19     19   ** The code in this file is only used if we are compiling multithreaded
    20     20   ** on a win32 system.
    21     21   */
................................................................................
    78     78   }
    79     79   #endif
    80     80   
    81     81   
    82     82   /*
    83     83   ** Initialize and deinitialize the mutex subsystem.
    84     84   */
    85         -static int winMutexInit(void){ return SQLITE_OK; }
    86         -static int winMutexEnd(void){ return SQLITE_OK; }
           85  +static sqlite3_mutex winMutex_staticMutexes[6];
           86  +static int winMutex_isInit = 0;
           87  +/* As winMutexInit() and winMutexEnd() are called as part
           88  +** of the sqlite3_initialize and sqlite3_shutdown()
           89  +** processing, the "interlocked" magic is probably not
           90  +** strictly necessary.
           91  +*/
           92  +static long winMutex_lock = 0;
           93  +
           94  +static int winMutexInit(void){ 
           95  +  /* The first to increment to 1 does actual initialization */
           96  +  if( InterlockedIncrement(&winMutex_lock)==1 ){
           97  +    int i;
           98  +    for(i=0; i<sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes[0]); i++){
           99  +      InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
          100  +    }
          101  +    winMutex_isInit = 1;
          102  +  }else{
          103  +    while( !winMutex_isInit ){
          104  +      Sleep(1);
          105  +    }
          106  +  }
          107  +  return SQLITE_OK; 
          108  +}
          109  +
          110  +static int winMutexEnd(void){ 
          111  +  /* The first to decrement to 0 does actual shutdown 
          112  +  ** (which should be the last to shutdown.) */
          113  +  if( InterlockedDecrement(&winMutex_lock)==0 ){
          114  +    if( winMutex_isInit==1 ){
          115  +      int i;
          116  +      for(i=0; i<sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes[0]); i++){
          117  +        DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
          118  +      }
          119  +      winMutex_isInit = 0;
          120  +    }
          121  +  }
          122  +  return SQLITE_OK; 
          123  +}
    87    124   
    88    125   /*
    89    126   ** The sqlite3_mutex_alloc() routine allocates a new
    90    127   ** mutex and returns a pointer to it.  If it returns NULL
    91    128   ** that means that a mutex could not be allocated.  SQLite
    92    129   ** will unwind its stack and return an error.  The argument
    93    130   ** to sqlite3_mutex_alloc() is one of these integer constants:
................................................................................
   127    164   static sqlite3_mutex *winMutexAlloc(int iType){
   128    165     sqlite3_mutex *p;
   129    166   
   130    167     switch( iType ){
   131    168       case SQLITE_MUTEX_FAST:
   132    169       case SQLITE_MUTEX_RECURSIVE: {
   133    170         p = sqlite3MallocZero( sizeof(*p) );
   134         -      if( p ){
          171  +      if( p ){  
   135    172           p->id = iType;
   136    173           InitializeCriticalSection(&p->mutex);
   137    174         }
   138    175         break;
   139    176       }
   140    177       default: {
   141         -      static sqlite3_mutex staticMutexes[6];
   142         -      static int isInit = 0;
   143         -      while( !isInit ){
   144         -        static long lock = 0;
   145         -        if( InterlockedIncrement(&lock)==1 ){
   146         -          int i;
   147         -          for(i=0; i<sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++){
   148         -            InitializeCriticalSection(&staticMutexes[i].mutex);
   149         -          }
   150         -          isInit = 1;
   151         -        }else{
   152         -          Sleep(1);
   153         -        }
   154         -      }
   155    178         assert( iType-2 >= 0 );
   156         -      assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
   157         -      p = &staticMutexes[iType-2];
          179  +      assert( iType-2 < sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes[0]) );
          180  +      p = &winMutex_staticMutexes[iType-2];
   158    181         p->id = iType;
   159    182         break;
   160    183       }
   161    184     }
   162    185     return p;
   163    186   }
   164    187