@@ -478,13 +478,12 @@ void Countly::addEvent(const cly::Event &event) {
478478#else
479479 addEventToSqlite (event);
480480#endif
481- checkAndSendEventToRQ ();
482481 mutex->unlock ();
482+ checkAndSendEventToRQ ();
483483}
484484
485485void Countly::checkAndSendEventToRQ () {
486486 nlohmann::json events = nlohmann::json::array ();
487- mutex->unlock ();
488487 int queueSize = checkEQSize ();
489488 mutex->lock ();
490489#ifdef COUNTLY_USE_SQLITE
@@ -493,7 +492,9 @@ void Countly::checkAndSendEventToRQ() {
493492 std::string event_ids;
494493
495494 // fetch events up to the threshold from the database
495+ mutex->unlock ();
496496 fillEventsIntoJson (events, event_ids);
497+ mutex->lock ();
497498 // send them to request queue
498499 sendEventsToRQ (events);
499500 // remove them from database
@@ -509,6 +510,7 @@ void Countly::checkAndSendEventToRQ() {
509510 event_queue.clear ();
510511 }
511512#endif
513+ mutex->unlock ();
512514}
513515
514516void Countly::setMaxEvents (size_t value) {
@@ -530,8 +532,8 @@ void Countly::setEventsToRQThreshold(int value) {
530532 // set the value
531533 configuration->eventQueueThreshold = value;
532534 // if current queue size is greater than the new threshold, send events to RQ
533- checkAndSendEventToRQ ();
534535 mutex->unlock ();
536+ checkAndSendEventToRQ ();
535537}
536538
537539void Countly::flushEvents (std::chrono::seconds timeout) {
@@ -715,7 +717,9 @@ bool Countly::updateSession() {
715717 // events array
716718 nlohmann::json events = nlohmann::json::array ();
717719 std::string event_ids;
718- bool no_events = isEQEmpty ();
720+ mutex->unlock ();
721+ bool no_events = checkEQSize () > 0 ? false : true ;
722+ mutex->lock ();
719723
720724 if (!no_events) {
721725#ifndef COUNTLY_USE_SQLITE
@@ -724,7 +728,9 @@ bool Countly::updateSession() {
724728 }
725729#else
726730 // TODO: If database_path was empty there was return false here
731+ mutex->unlock ();
727732 fillEventsIntoJson (events, event_ids);
733+ mutex->lock ();
728734#endif
729735 } else {
730736 log (LogLevel::DEBUG, " [Countly][updateSession] EQ empty." );
@@ -772,17 +778,6 @@ void Countly::sendEventsToRQ(const nlohmann::json &events) {
772778 requestModule->addRequestToQueue (data);
773779}
774780
775- bool Countly::isEQEmpty () {
776- log (LogLevel::DEBUG, " [Countly][isEQEmpty] Checking if the event queue is empty." );
777- #ifdef COUNTLY_USE_SQLITE
778- mutex->unlock ();
779- return checkEQSize () > 0 ? false : true ;
780- mutex->lock ();
781- #else
782- return event_queue.empty ();
783- #endif
784- }
785-
786781bool Countly::endSession () {
787782 log (LogLevel::INFO, " [Countly][endSession]" );
788783 const std::chrono::system_clock::time_point now = Countly::getTimestamp ();
@@ -827,7 +822,11 @@ int Countly::checkEQSize() {
827822#ifndef COUNTLY_USE_SQLITE
828823int Countly::checkMemoryEQSize () {
829824 log (LogLevel::DEBUG, " [Countly][checkMemoryEQSize] Checking event queue size in memory" );
830- return event_queue.size ();
825+ int result = 0 ;
826+ mutex->lock ();
827+ result = event_queue.size ();
828+ mutex->unlock ();
829+ return result;
831830}
832831#endif
833832
@@ -861,6 +860,7 @@ void Countly::removeEventWithId(const std::string &event_ids) {
861860}
862861
863862void Countly::fillEventsIntoJson (nlohmann::json &events, std::string &event_ids) {
863+ mutex->lock ();
864864 if (database_path.empty ()) {
865865 mutex->unlock ();
866866 log (LogLevel::FATAL, " [Countly][fillEventsIntoJson] Sqlite database path is not set." );
@@ -911,6 +911,7 @@ void Countly::fillEventsIntoJson(nlohmann::json &events, std::string &event_ids)
911911 log (LogLevel::ERROR, " [Countly][fillEventsIntoJson] Could not open database." );
912912 }
913913 sqlite3_close (database);
914+ mutex->unlock ();
914915}
915916
916917int Countly::checkPersistentEQSize () {
0 commit comments