[pulseaudio-commits] r1343 - in /trunk/src: pulsecore/cli-command.c pulsecore/memblock.c pulsecore/memblock.h pulsecore/protocol-native.c tests/memblock-test.c

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Mon Aug 28 19:01:42 PDT 2006


Author: lennart
Date: Tue Aug 29 04:01:39 2006
New Revision: 1343

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=1343&root=pulseaudio&view=rev
Log:
make pa_mempool_stat thread-safe/lock-free

Modified:
    trunk/src/pulsecore/cli-command.c
    trunk/src/pulsecore/memblock.c
    trunk/src/pulsecore/memblock.h
    trunk/src/pulsecore/protocol-native.c
    trunk/src/tests/memblock-test.c

Modified: trunk/src/pulsecore/cli-command.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/pulsecore/cli-command.c?rev=1343&root=pulseaudio&r1=1342&r2=1343&view=diff
==============================================================================
--- trunk/src/pulsecore/cli-command.c (original)
+++ trunk/src/pulsecore/cli-command.c Tue Aug 29 04:01:39 2006
@@ -259,20 +259,20 @@
     stat = pa_mempool_get_stat(c->mempool);
     
     pa_strbuf_printf(buf, "Memory blocks currently allocated: %u, size: %s.\n",
-                     stat->n_allocated,
-                     pa_bytes_snprint(s, sizeof(s), stat->allocated_size));
+                     (unsigned) AO_load_acquire_read((AO_t*) &stat->n_allocated),
+                     pa_bytes_snprint(s, sizeof(s), (size_t) AO_load_acquire_read((AO_t*) &stat->allocated_size)));
 
     pa_strbuf_printf(buf, "Memory blocks allocated during the whole lifetime: %u, size: %s.\n",
-                     stat->n_accumulated,
-                     pa_bytes_snprint(s, sizeof(s), stat->accumulated_size));
+                     (unsigned) AO_load_acquire_read((AO_t*) &stat->n_accumulated),
+                     pa_bytes_snprint(s, sizeof(s), (size_t) AO_load_acquire_read((AO_t*) &stat->accumulated_size)));
 
     pa_strbuf_printf(buf, "Memory blocks imported from other processes: %u, size: %s.\n",
-                     stat->n_imported,
-                     pa_bytes_snprint(s, sizeof(s), stat->imported_size));
+                     (unsigned) AO_load_acquire_read((AO_t*) &stat->n_imported),
+                     pa_bytes_snprint(s, sizeof(s), (size_t) AO_load_acquire_read((AO_t*) &stat->imported_size)));
 
     pa_strbuf_printf(buf, "Memory blocks exported to other processes: %u, size: %s.\n",
-                     stat->n_exported,
-                     pa_bytes_snprint(s, sizeof(s), stat->exported_size));
+                     (unsigned) AO_load_acquire_read((AO_t*) &stat->n_exported),
+                     pa_bytes_snprint(s, sizeof(s), (size_t) AO_load_acquire_read((AO_t*) &stat->exported_size)));
 
     pa_strbuf_printf(buf, "Total sample cache size: %s.\n",
                      pa_bytes_snprint(s, sizeof(s), pa_scache_total_size(c)));
@@ -289,8 +289,8 @@
         pa_strbuf_printf(buf,
                          "Memory blocks of type %s: %u allocated/%u accumulated.\n",
                          type_table[k],
-                         stat->n_allocated_by_type[k],
-                         stat->n_accumulated_by_type[k]);
+                         (unsigned) AO_load_acquire_read(&stat->n_allocated_by_type[k]),
+                         (unsigned) AO_load_acquire_read(&stat->n_accumulated_by_type[k]));
     
     return 0;
 }

Modified: trunk/src/pulsecore/memblock.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/pulsecore/memblock.c?rev=1343&root=pulseaudio&r1=1342&r2=1343&view=diff
==============================================================================
--- trunk/src/pulsecore/memblock.c (original)
+++ trunk/src/pulsecore/memblock.c Tue Aug 29 04:01:39 2006
@@ -112,39 +112,40 @@
     assert(b);
     assert(b->pool);
 
-    b->pool->stat.n_allocated ++;
-    b->pool->stat.n_accumulated ++;
-    b->pool->stat.allocated_size += b->length;
-    b->pool->stat.accumulated_size += b->length;
+    AO_fetch_and_add1_release_write(&b->pool->stat.n_allocated);
+    AO_fetch_and_add_release_write(&b->pool->stat.allocated_size, (AO_t) b->length);
+
+    AO_fetch_and_add1_release_write(&b->pool->stat.n_accumulated);
+    AO_fetch_and_add_release_write(&b->pool->stat.accumulated_size, (AO_t) b->length);
 
     if (b->type == PA_MEMBLOCK_IMPORTED) {
-        b->pool->stat.n_imported++;
-        b->pool->stat.imported_size += b->length;
-    }
-
-    b->pool->stat.n_allocated_by_type[b->type]++;
-    b->pool->stat.n_accumulated_by_type[b->type]++;
+        AO_fetch_and_add1_release_write(&b->pool->stat.n_imported);
+        AO_fetch_and_add_release_write(&b->pool->stat.imported_size, (AO_t) b->length);
+    }
+
+    AO_fetch_and_add1_release_write(&b->pool->stat.n_allocated_by_type[b->type]);
+    AO_fetch_and_add1_release_write(&b->pool->stat.n_accumulated_by_type[b->type]);
 }
 
 static void stat_remove(pa_memblock *b) {
     assert(b);
     assert(b->pool);
 
-    assert(b->pool->stat.n_allocated > 0);
-    assert(b->pool->stat.allocated_size >= b->length);
+    assert(AO_load_acquire_read(&b->pool->stat.n_allocated) > 0);
+    assert(AO_load_acquire_read(&b->pool->stat.allocated_size) >= (AO_t) b->length);
            
-    b->pool->stat.n_allocated --;
-    b->pool->stat.allocated_size -= b->length;
+    AO_fetch_and_sub1_release_write(&b->pool->stat.n_allocated);
+    AO_fetch_and_add_release_write(&b->pool->stat.allocated_size,  (AO_t) (-b->length));
 
     if (b->type == PA_MEMBLOCK_IMPORTED) {
-        assert(b->pool->stat.n_imported > 0);
-        assert(b->pool->stat.imported_size >= b->length);
+        assert(AO_load_acquire_read(&b->pool->stat.n_imported) > 0);
+        assert(AO_load_acquire_read(&b->pool->stat.imported_size) >= (AO_t) b->length);
         
-        b->pool->stat.n_imported --;
-        b->pool->stat.imported_size -= b->length;
-    }
-
-    b->pool->stat.n_allocated_by_type[b->type]--;
+        AO_fetch_and_sub1_release_write(&b->pool->stat.n_imported);
+        AO_fetch_and_add_release_write(&b->pool->stat.imported_size, (AO_t)  (-b->length));
+    }
+
+    AO_fetch_and_sub1_release_write(&b->pool->stat.n_allocated_by_type[b->type]);
 }
 
 static pa_memblock *memblock_new_appended(pa_mempool *p, size_t length);
@@ -190,7 +191,7 @@
         slot = (struct mempool_slot*) ((uint8_t*) p->memory.ptr + (p->block_size * p->n_init++));
     else {
         pa_log_debug("Pool full");
-        p->stat.n_pool_full++;
+        AO_fetch_and_add1_release_write(&p->stat.n_pool_full);
         return NULL;
     }
 
@@ -247,7 +248,7 @@
         b->data = mempool_slot_data(slot);
     } else {
         pa_log_debug("Memory block too large for pool: %u > %u", length, p->block_size - sizeof(struct mempool_slot));
-        p->stat.n_too_large_for_pool++;
+        AO_fetch_and_add1_release_write(&p->stat.n_too_large_for_pool);
         return NULL;
     }
 
@@ -371,7 +372,7 @@
 static void memblock_make_local(pa_memblock *b) {
     assert(b);
 
-    b->pool->stat.n_allocated_by_type[b->type]--;
+    AO_fetch_and_sub1_release_write(&b->pool->stat.n_allocated_by_type[b->type]);
 
     if (b->length <= b->pool->block_size - sizeof(struct mempool_slot)) {
         struct mempool_slot *slot;
@@ -397,8 +398,8 @@
     b->data = pa_xmemdup(b->data, b->length);
 
 finish:
-    b->pool->stat.n_allocated_by_type[b->type]++;
-    b->pool->stat.n_accumulated_by_type[b->type]++;
+    AO_fetch_and_add1_release_write(&b->pool->stat.n_allocated_by_type[b->type]);
+    AO_fetch_and_add1_release_write(&b->pool->stat.n_accumulated_by_type[b->type]);
 }
 
 void pa_memblock_unref_fixed(pa_memblock *b) {
@@ -418,10 +419,10 @@
     assert(b);
     assert(b->type == PA_MEMBLOCK_IMPORTED);
 
-    assert(b->pool->stat.n_imported > 0);
-    assert(b->pool->stat.imported_size >= b->length);
-    b->pool->stat.n_imported --;
-    b->pool->stat.imported_size -= b->length;
+    assert(AO_load_acquire_read(&b->pool->stat.n_imported) > 0);
+    assert(AO_load_acquire_read(&b->pool->stat.imported_size) >= (AO_t) b->length);
+    AO_fetch_and_sub1_release_write(&b->pool->stat.n_imported);
+    AO_fetch_and_add_release_write(&b->pool->stat.imported_size, (AO_t) - b->length);
 
     seg = b->per_type.imported.segment;
     assert(seg);
@@ -486,7 +487,7 @@
     while (p->exports)
         pa_memexport_free(p->exports);
 
-    if (p->stat.n_allocated > 0)
+    if (AO_load_acquire_read(&p->stat.n_allocated) > 0)
         pa_log_warn("WARNING! Memory pool destroyed but not all memory blocks freed!");
     
     pa_shm_free(&p->memory);
@@ -685,11 +686,11 @@
 
 /*     pa_log("Processing release for %u", id); */
 
-    assert(e->pool->stat.n_exported > 0);
-    assert(e->pool->stat.exported_size >= e->slots[id].block->length);
-    
-    e->pool->stat.n_exported --;
-    e->pool->stat.exported_size -= e->slots[id].block->length;
+    assert(AO_load_acquire_read(&e->pool->stat.n_exported) > 0);
+    assert(AO_load_acquire_read(&e->pool->stat.exported_size) >= (AO_t) e->slots[id].block->length);
+    
+    AO_fetch_and_sub1_release_write(&e->pool->stat.n_exported);
+    AO_fetch_and_add_release_write(&e->pool->stat.exported_size, (AO_t) -e->slots[id].block->length);
     
     pa_memblock_unref(e->slots[id].block);
     e->slots[id].block = NULL;
@@ -786,8 +787,8 @@
     *offset = (uint8_t*) b->data - (uint8_t*) memory->ptr;
     *size = b->length;
 
-    e->pool->stat.n_exported ++;
-    e->pool->stat.exported_size += b->length;
+    AO_fetch_and_add1_release_write(&e->pool->stat.n_exported);
+    AO_fetch_and_add_release_write(&e->pool->stat.exported_size, (AO_t) b->length);
 
     return 0;
 }

Modified: trunk/src/pulsecore/memblock.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/pulsecore/memblock.h?rev=1343&root=pulseaudio&r1=1342&r2=1343&view=diff
==============================================================================
--- trunk/src/pulsecore/memblock.h (original)
+++ trunk/src/pulsecore/memblock.h Tue Aug 29 04:01:39 2006
@@ -74,21 +74,25 @@
     } per_type;
 };
 
+/* Please note that updates to this structure are not locked,
+ * i.e. n_allocated might be updated at a point in time where
+ * n_accumulated is not yet. Take these values with a grain of salt,
+ * threy are here for purely statistical reasons.*/
 struct pa_mempool_stat {
-    unsigned n_allocated;
-    unsigned n_accumulated;
-    unsigned n_imported;
-    unsigned n_exported;
-    size_t allocated_size;
-    size_t accumulated_size;
-    size_t imported_size;
-    size_t exported_size;
+    AO_t n_allocated;
+    AO_t n_accumulated;
+    AO_t n_imported;
+    AO_t n_exported;
+    AO_t allocated_size;
+    AO_t accumulated_size;
+    AO_t imported_size;
+    AO_t exported_size;
 
-    unsigned n_too_large_for_pool;
-    unsigned n_pool_full;
+    AO_t n_too_large_for_pool;
+    AO_t n_pool_full;
 
-    unsigned n_allocated_by_type[PA_MEMBLOCK_TYPE_MAX];
-    unsigned n_accumulated_by_type[PA_MEMBLOCK_TYPE_MAX];
+    AO_t n_allocated_by_type[PA_MEMBLOCK_TYPE_MAX];
+    AO_t n_accumulated_by_type[PA_MEMBLOCK_TYPE_MAX];
 };
 
 /* Allocate a new memory block of type PA_MEMBLOCK_MEMPOOL or PA_MEMBLOCK_APPENDED, depending on the size */

Modified: trunk/src/pulsecore/protocol-native.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/pulsecore/protocol-native.c?rev=1343&root=pulseaudio&r1=1342&r2=1343&view=diff
==============================================================================
--- trunk/src/pulsecore/protocol-native.c (original)
+++ trunk/src/pulsecore/protocol-native.c Tue Aug 29 04:01:39 2006
@@ -1112,10 +1112,10 @@
     stat = pa_mempool_get_stat(c->protocol->core->mempool);
     
     reply = reply_new(tag);
-    pa_tagstruct_putu32(reply, stat->n_allocated);
-    pa_tagstruct_putu32(reply, stat->allocated_size);
-    pa_tagstruct_putu32(reply, stat->n_accumulated);
-    pa_tagstruct_putu32(reply, stat->accumulated_size);
+    pa_tagstruct_putu32(reply, (uint32_t) AO_load_acquire_read((AO_t*) &stat->n_allocated));
+    pa_tagstruct_putu32(reply, (uint32_t) AO_load_acquire_read((AO_t*) &stat->allocated_size));
+    pa_tagstruct_putu32(reply, (uint32_t) AO_load_acquire_read((AO_t*) &stat->n_accumulated));
+    pa_tagstruct_putu32(reply, (uint32_t) AO_load_acquire_read((AO_t*) &stat->accumulated_size));
     pa_tagstruct_putu32(reply, pa_scache_total_size(c->protocol->core));
     pa_pstream_send_tagstruct(c->pstream, reply);
 }

Modified: trunk/src/tests/memblock-test.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/tests/memblock-test.c?rev=1343&root=pulseaudio&r1=1342&r2=1343&view=diff
==============================================================================
--- trunk/src/tests/memblock-test.c (original)
+++ trunk/src/tests/memblock-test.c Tue Aug 29 04:01:39 2006
@@ -54,16 +54,16 @@
            "n_pool_full = %u\n"
            "}\n",
            text,
-           s->n_allocated,
-           s->n_accumulated,
-           s->n_imported,
-           s->n_exported,
-           (unsigned long) s->allocated_size,
-           (unsigned long) s->accumulated_size,
-           (unsigned long) s->imported_size,
-           (unsigned long) s->exported_size,
-           s->n_too_large_for_pool,
-           s->n_pool_full);
+           (unsigned) AO_load_acquire_read((AO_t*) &s->n_allocated),
+           (unsigned) AO_load_acquire_read((AO_t*) &s->n_accumulated),
+           (unsigned) AO_load_acquire_read((AO_t*) &s->n_imported),
+           (unsigned) AO_load_acquire_read((AO_t*) &s->n_exported),
+           (unsigned long) AO_load_acquire_read((AO_t*) &s->allocated_size),
+           (unsigned long) AO_load_acquire_read((AO_t*) &s->accumulated_size),
+           (unsigned long) AO_load_acquire_read((AO_t*) &s->imported_size),
+           (unsigned long) AO_load_acquire_read((AO_t*) &s->exported_size),
+           (unsigned) AO_load_acquire_read((AO_t*) &s->n_too_large_for_pool),
+           (unsigned) AO_load_acquire_read((AO_t*) &s->n_pool_full));
 }
 
 int main(int argc, char *argv[]) {




More information about the pulseaudio-commits mailing list