]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
fix race cond
authorAnthony Minessale <anthm@freeswitch.org>
Mon, 29 Oct 2012 20:16:55 +0000 (15:16 -0500)
committerAnthony Minessale <anthm@freeswitch.org>
Mon, 29 Oct 2012 20:16:55 +0000 (15:16 -0500)
src/switch_core_sqldb.c

index b250b737dd64da476360ee195204a2364653403c..ba98b02af494e3264bcdd4a3bb19dce71fcd5415 100644 (file)
@@ -1202,6 +1202,7 @@ struct switch_sql_queue_manager {
        const char *name;
        switch_cache_db_handle_t *event_db;
        switch_queue_t **sql_queue;
+       uint32_t *pre_written;
        uint32_t *written;
        uint32_t numq;
        char *dsn;
@@ -1439,6 +1440,7 @@ SWITCH_DECLARE(switch_status_t) switch_sql_queue_manager_init_name(const char *n
        
        qm->sql_queue = switch_core_alloc(qm->pool, sizeof(switch_queue_t *) * numq);
        qm->written = switch_core_alloc(qm->pool, sizeof(uint32_t) * numq);
+       qm->pre_written = switch_core_alloc(qm->pool, sizeof(uint32_t) * numq);
 
        for (i = 0; i < qm->numq; i++) {
                switch_queue_create(&qm->sql_queue[i], SWITCH_SQL_QUEUE_LEN, qm->pool);
@@ -1468,6 +1470,12 @@ static uint32_t do_trans(switch_sql_queue_manager_t *qm)
 
        if (io_mutex) switch_mutex_lock(io_mutex);
 
+       switch_mutex_lock(qm->mutex);
+       for (i = 0; i < qm->numq; i++) {
+               qm->pre_written[i] = 0;
+       }
+       switch_mutex_unlock(qm->mutex);
+
        if (!zstr(qm->pre_trans_execute)) {
                switch_cache_db_execute_sql_real(qm->event_db, qm->pre_trans_execute, &errmsg);
                if (errmsg) {
@@ -1534,9 +1542,7 @@ static uint32_t do_trans(switch_sql_queue_manager_t *qm)
 
                if (pop) {
                        if ((status = switch_cache_db_execute_sql(qm->event_db, (char *) pop, NULL)) == SWITCH_STATUS_SUCCESS) {
-                               switch_mutex_lock(qm->mutex);
-                               qm->written[i]++;
-                               switch_mutex_unlock(qm->mutex);
+                               qm->pre_written[i]++;
                                ttl++;
                        }
                        free(pop);
@@ -1588,6 +1594,14 @@ static uint32_t do_trans(switch_sql_queue_manager_t *qm)
                }
        }
 
+
+       switch_mutex_lock(qm->mutex);
+       for (i = 0; i < qm->numq; i++) {
+               qm->written[i] += qm->pre_written[i];
+       }
+       switch_mutex_unlock(qm->mutex);
+
+
        if (io_mutex) switch_mutex_unlock(io_mutex);
 
        return ttl;