Skip to content

Commit 7942b8a

Browse files
committed
Fix of the last merge.
Plus slightly optimized allocating/deallocating of internally used Stmt handles.
1 parent e61572c commit 7942b8a

File tree

7 files changed

+120
-109
lines changed

7 files changed

+120
-109
lines changed

driver/ma_api_internal.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,21 +279,19 @@ SQLRETURN MA_SQLCancel(SQLHSTMT StatementHandle)
279279

280280
// Technically, if the application does not do good syncronization of stmt use here, we can have
281281
// Stmt already freed at this point, and crash right away on reading the mutex in Stmt
282-
EnterCriticalSection(&globalLock);
282+
std::unique_lock<std::mutex> localScopeLock(globalLock);
283283
if (CheckDeletedStmt(Stmt) != NULL)
284284
{
285-
LeaveCriticalSection(&globalLock);
286285
return ret;// i.e. SQL_SUCCESS. Stmt has been deleted, nothing to cancel already
287286
}
288-
if (!TryEnterCriticalSection(&Stmt->CancelDropSwitch))
287+
if (!Stmt->CancelDropSwitch.try_lock())
289288
{
290-
LeaveCriticalSection(&globalLock);
291289
// This is not clear what is right here - success or error
292290
return SQL_SUCCESS;
293291
}
294292
// We can release the lock. SQL_DROP will get it, but stop at the Stmt->CancelDropSwitch
295293
// that we already own
296-
LeaveCriticalSection(&globalLock);
294+
localScopeLock.unlock();
297295
MADB_CLEAR_ERROR(&Stmt->Error);
298296

299297
MDBUG_C_ENTER(Stmt->Connection, "SQLCancel");
@@ -337,7 +335,7 @@ SQLRETURN MA_SQLCancel(SQLHSTMT StatementHandle)
337335
} // Else we are canceling function running in other thread.
338336
else if (lock.try_lock())
339337
{
340-
Stmt->canceled= '\1';
338+
Stmt->canceled= true;
341339
try
342340
{
343341
/* "If a SQL statement is being executed when SQLCancel is called on another thread to cancel the
@@ -362,7 +360,7 @@ SQLRETURN MA_SQLCancel(SQLHSTMT StatementHandle)
362360
ret= MADB_KillAtServer(Stmt->Connection, &Stmt->Error);
363361
}
364362
}
365-
LeaveCriticalSection(&Stmt->CancelDropSwitch);
363+
Stmt->CancelDropSwitch.unlock();
366364
MDBUG_C_RETURN(Stmt->Connection, ret, &Stmt->Error);
367365
}
368366
/* }}} */

driver/ma_driver.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
*/
2525
/************************************* Driver wide stuff ************************************************/
2626

27-
CRITICAL_SECTION globalLock;
27+
std::mutex globalLock;
2828
static MADB_List *deletedStmt= NULL;
29-
static unsigned int envCount= 0;
29+
//sles12 does not have atomic_uint32 defined
30+
static std::atomic<uint32_t> envCount(0U);
3031

3132
#ifndef _WIN32
3233
__attribute__((constructor))
@@ -43,53 +44,48 @@ extern "C" {
4344
/* {{{ DriverGlobalInit */
4445
void DriverGlobalInit()
4546
{
46-
InitializeCriticalSection(&globalLock);
4747
}
4848
/* }}} */
4949

5050
/* {{{ DriverGlobalClean()*/
5151
void DriverGlobalClean(void)
5252
{
53-
EnterCriticalSection(&globalLock);
53+
// There is no need to lock here at least the while it used the way it used now -
54+
// only called when library is unloaded
5455
if (deletedStmt)
5556
{
5657
MADB_ListFree(deletedStmt, FALSE);
5758
}
58-
LeaveCriticalSection(&globalLock);
59-
DeleteCriticalSection(&globalLock);
6059
}
6160
/* }}} */
6261
}
6362
/* {{{ IncrementEnvCount */
6463
// Normally there should be 1 Env, but nothing forbids app have more than 1.
6564
void IncrementEnvCount()
6665
{
67-
EnterCriticalSection(&globalLock);
6866
++envCount;
69-
LeaveCriticalSection(&globalLock);
7067
}
7168
/*}}}*/
7269

7370
/* {{{ DecrementEnvCount */
7471
// If the last Env has been freed - we should probably clean the list
7572
void DecrementEnvCount()
7673
{
77-
EnterCriticalSection(&globalLock);
7874
--envCount;
7975
if (!envCount)
8076
{
77+
std::lock_guard<std::mutex> localScopeLock(globalLock);
8178
MADB_ListFree(deletedStmt, FALSE);
8279
deletedStmt= NULL;
8380
}
84-
LeaveCriticalSection(&globalLock);
8581
}
8682
/*}}}*/
8783

8884
/* {{{ CheckDeletedStmt */
8985
// If the last Env has been freed - we should probably clean the list
9086
MADB_List* CheckDeletedStmt(void* stmtObjAddr)
9187
{
92-
MADB_List* item= deletedStmt;
88+
MADB_List *item= deletedStmt;
9389
while (item != NULL)
9490
{
9591
if (item->data == stmtObjAddr)
@@ -104,18 +100,17 @@ MADB_List* CheckDeletedStmt(void* stmtObjAddr)
104100

105101
/* {{{ RemoveStmtFromDeleted */
106102
// If the last Env has been freed - we should probably clean the list
107-
BOOL RemoveStmtFromDeleted(void* stmtObjAddr)
103+
bool RemoveStmtFromDeleted(void* stmtObjAddr)
108104
{
109-
BOOL result= FALSE;
110-
EnterCriticalSection(&globalLock);
105+
bool result= false;
106+
std::lock_guard<std::mutex> localScopeLock(globalLock);
111107
MADB_List* found= CheckDeletedStmt(stmtObjAddr);
112108
if (found)
113109
{
114110
deletedStmt= MADB_ListDelete(deletedStmt, found);
115111
free(found);
116-
result= TRUE;
112+
result= true;
117113
}
118-
LeaveCriticalSection(&globalLock);
119114
return result;
120115
}
121116
/*}}}*/

driver/ma_driver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
#ifndef _ma_driver_h_
3131
#define _ma_driver_h_
3232

33-
extern CRITICAL_SECTION globalLock;
33+
extern std::mutex globalLock;
3434
extern "C" {
3535
void DriverGlobalInit();
3636
void DriverGlobalClean(void);
3737
}
3838
void IncrementEnvCount();
3939
void DecrementEnvCount();
4040
MADB_List* CheckDeletedStmt(void* stmtObjAddr);
41-
BOOL RemoveStmtFromDeleted(void* stmtObjAddr);
41+
bool RemoveStmtFromDeleted(void* stmtObjAddr);
4242
void RememberDeletedStmt(void* stmtObjAddr);
4343

4444
typedef struct {

driver/ma_helper.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,10 +1141,11 @@ SQLRETURN MADB_DaeStmt(MADB_Stmt *Stmt, SQLUSMALLINT Operation)
11411141
MADB_CLEAR_ERROR(&Stmt->Error);
11421142

11431143
if (Stmt->DaeStmt)
1144-
Stmt->Methods->StmtFree(Stmt->DaeStmt, SQL_DROP);
1145-
Stmt->DaeStmt= nullptr;
1144+
{
1145+
MADB_DeleteDaeStmt(Stmt);
1146+
}
11461147

1147-
if (!SQL_SUCCEEDED(MA_SQLAllocHandle(SQL_HANDLE_STMT, (SQLHANDLE)Stmt->Connection, (SQLHANDLE *)&Stmt->DaeStmt)))
1148+
if (!SQL_SUCCEEDED(MADB_StmtInit(Stmt->Connection, (SQLHANDLE *)&Stmt->DaeStmt, false)))
11481149
{
11491150
return MADB_CopyError(&Stmt->Error, &Stmt->Connection->Error);
11501151
}
@@ -1183,7 +1184,7 @@ SQLRETURN MADB_DaeStmt(MADB_Stmt *Stmt, SQLUSMALLINT Operation)
11831184
if (!SQL_SUCCEEDED(Stmt->DaeStmt->Prepare(Query.c_str(), (SQLINTEGER)Query.length(), true)))
11841185
{
11851186
MADB_CopyError(&Stmt->Error, &Stmt->DaeStmt->Error);
1186-
Stmt->Methods->StmtFree(Stmt->DaeStmt, SQL_DROP);
1187+
MADB_DeleteDaeStmt(Stmt);
11871188
}
11881189
return Stmt->Error.ReturnValue;
11891190
}

driver/ma_odbc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,14 @@ struct MADB_Env {
287287
// Stmt has to know few things about connection and descriptor
288288
#include "ma_connection.h"
289289
#include "ma_desc.h"
290-
290+
#define xxx sizeof(std::mutex)
291291
struct MADB_Stmt
292292
{
293293
MADB_QUERY Query;
294294
MADB_StmtOptions Options;
295295
MADB_Error Error;
296-
CRITICAL_SECTION CancelDropSwitch; /* mutex for SQLCancel/SQLFreeStmt(SQL_DROP) */
296+
std::mutex CancelDropSwitch; /* mutex for SQLCancel/SQLFreeStmt(SQL_DROP) */
297+
std::atomic_bool canceled;
297298
MADB_Cursor Cursor;
298299
MADB_List ListItem;
299300
long long AffectedRows= 0;
@@ -339,7 +340,6 @@ struct MADB_Stmt
339340
bool PositionedCommand= false;
340341
bool RebindParams= false;
341342
bool bind_done= false;
342-
std::atomic_bool canceled;
343343

344344
MADB_Stmt(MADB_Dbc *Connection);
345345
SQLRETURN Prepare(const char* StatementText, SQLINTEGER TextLength, bool ServerSide= true, bool DirectExecution= false);

0 commit comments

Comments
 (0)