2424 */
2525/* ************************************ Driver wide stuff ************************************************/
2626
27- CRITICAL_SECTION globalLock;
27+ std::mutex globalLock;
2828static MADB_List *deletedStmt= NULL ;
29- static unsigned int envCount= 0 ;
29+ static std:: atomic_uint32_t envCount ( 0U ) ;
3030
3131#ifndef _WIN32
3232__attribute__ ((constructor))
@@ -43,53 +43,48 @@ extern "C" {
4343 /* {{{ DriverGlobalInit */
4444 void DriverGlobalInit ()
4545 {
46- InitializeCriticalSection (&globalLock);
4746 }
4847 /* }}} */
4948
5049 /* {{{ DriverGlobalClean()*/
5150 void DriverGlobalClean (void )
5251 {
53- EnterCriticalSection (&globalLock);
52+ // There is no need to lock here at least the while it used the way it used now -
53+ // only called when library is unloaded
5454 if (deletedStmt)
5555 {
5656 MADB_ListFree (deletedStmt, FALSE );
5757 }
58- LeaveCriticalSection (&globalLock);
59- DeleteCriticalSection (&globalLock);
6058 }
6159 /* }}} */
6260}
6361/* {{{ IncrementEnvCount */
6462// Normally there should be 1 Env, but nothing forbids app have more than 1.
6563void IncrementEnvCount ()
6664{
67- EnterCriticalSection (&globalLock);
6865 ++envCount;
69- LeaveCriticalSection (&globalLock);
7066}
7167/* }}}*/
7268
7369/* {{{ DecrementEnvCount */
7470// If the last Env has been freed - we should probably clean the list
7571void DecrementEnvCount ()
7672{
77- EnterCriticalSection (&globalLock);
7873 --envCount;
7974 if (!envCount)
8075 {
76+ std::lock_guard<std::mutex> localScopeLock (globalLock);
8177 MADB_ListFree (deletedStmt, FALSE );
8278 deletedStmt= NULL ;
8379 }
84- LeaveCriticalSection (&globalLock);
8580}
8681/* }}}*/
8782
8883/* {{{ CheckDeletedStmt */
8984// If the last Env has been freed - we should probably clean the list
9085MADB_List* CheckDeletedStmt (void * stmtObjAddr)
9186{
92- MADB_List* item= deletedStmt;
87+ MADB_List * item= deletedStmt;
9388 while (item != NULL )
9489 {
9590 if (item->data == stmtObjAddr)
@@ -104,18 +99,17 @@ MADB_List* CheckDeletedStmt(void* stmtObjAddr)
10499
105100/* {{{ RemoveStmtFromDeleted */
106101// If the last Env has been freed - we should probably clean the list
107- BOOL RemoveStmtFromDeleted (void * stmtObjAddr)
102+ bool RemoveStmtFromDeleted (void * stmtObjAddr)
108103{
109- BOOL result= FALSE ;
110- EnterCriticalSection (& globalLock);
104+ bool result= false ;
105+ std::lock_guard<std::mutex> localScopeLock ( globalLock);
111106 MADB_List* found= CheckDeletedStmt (stmtObjAddr);
112107 if (found)
113108 {
114109 deletedStmt= MADB_ListDelete (deletedStmt, found);
115110 free (found);
116- result= TRUE ;
111+ result= true ;
117112 }
118- LeaveCriticalSection (&globalLock);
119113 return result;
120114}
121115/* }}}*/
0 commit comments