@@ -257,9 +257,11 @@ namespace db {
257
257
qr = std::move (sql_query_queue.front ());
258
258
sql_query_queue.pop ();
259
259
}
260
- auto results = query (qr.format , qr.parameters );
261
- if (qr.callback ) {
262
- qr.callback (results);
260
+ if (!qr.format .empty ()) {
261
+ auto results = query (qr.format , qr.parameters );
262
+ if (qr.callback ) {
263
+ qr.callback (results);
264
+ }
263
265
}
264
266
/* *
265
267
* If a transaction is waiting to be executed, fit it atomically into
@@ -269,7 +271,7 @@ namespace db {
269
271
* transaction_in_progress atomic bool is true. This prevents other threads
270
272
* running queries that end up inside the transaction.
271
273
*/
272
- if (transaction_in_progress. load () && transaction_function) {
274
+ if (transaction_in_progress && transaction_function) {
273
275
holds_transaction_lock = true ;
274
276
transaction_function ();
275
277
holds_transaction_lock = false ;
@@ -309,7 +311,7 @@ namespace db {
309
311
310
312
void transaction (std::function<bool ()> closure) {
311
313
312
- if (transaction_in_progress. load () ) {
314
+ if (transaction_in_progress) {
313
315
throw std::runtime_error (" Transaction already in progress" );
314
316
}
315
317
@@ -336,15 +338,17 @@ namespace db {
336
338
/* *
337
339
* Re-enable the SQL queue so that queries can happen again
338
340
*/
339
- transaction_in_progress. exchange ( false , std::memory_order_relaxed) ;
341
+ transaction_in_progress = false ;
340
342
};
341
343
342
344
/* *
343
345
* Set the atomic bool that indicates a transaction should be executed.
344
346
* The transaction is inserted into the stream of SQL queries inside the
345
- * SQL thread as one atomic operation.
347
+ * SQL thread as one atomic operation. We also have to send a blank callback
348
+ * query to signal the condition variable and make the SQL queue advance.
346
349
*/
347
- transaction_in_progress.exchange (true , std::memory_order_relaxed);
350
+ transaction_in_progress = true ;
351
+ query_callback (" " , {}, [](auto ){});
348
352
}
349
353
350
354
bool close () {
@@ -404,7 +408,7 @@ namespace db {
404
408
* If any thread except the queue thread attempts to run a synchronous query whilst
405
409
* a transaction is running, it must wait until the transaction completes.
406
410
*/
407
- while (transaction_in_progress. load () && !holds_transaction_lock) {
411
+ while (transaction_in_progress && !holds_transaction_lock) {
408
412
std::this_thread::sleep_for (1ms);
409
413
}
410
414
0 commit comments