rework memory block management to be thread-safe and mostly lock-free.

pa_memblock is now an opaque structure. Access to its fields is now done
through various accessor functions in a thread-safe manner.

pa_memblock_acquire() and pa_memblock_release() are now used to access the
attached audio data. Why? To allow safe manipulation of the memory pointer
maintained by the memory block. Internally _acquire() and _release() maintain a
reference counter. Please do not confuse this reference counter whith the one
maintained by pa_memblock_ref()/_unref()!

As a side effect this patch removes all direct usages of AO_t and replaces it
with pa_atomic_xxx based code.

This stuff needs some serious testing love. Especially if threads are actively
used.



git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1404 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Lennart Poettering 2006-09-26 23:50:56 +00:00
parent 5ad143b3ab
commit d210ebbb09
36 changed files with 991 additions and 500 deletions

View file

@ -25,6 +25,7 @@
#include <sys/types.h>
#include <inttypes.h>
#include <pulse/def.h>
#include <pulsecore/llist.h>
#include <pulsecore/refcnt.h>
@ -54,45 +55,25 @@ typedef struct pa_memexport pa_memexport;
typedef void (*pa_memimport_release_cb_t)(pa_memimport *i, uint32_t block_id, void *userdata);
typedef void (*pa_memexport_revoke_cb_t)(pa_memexport *e, uint32_t block_id, void *userdata);
struct pa_memblock {
pa_memblock_type_t type;
int read_only; /* boolean */
PA_REFCNT_DECLARE; /* the reference counter */
size_t length;
void *data;
pa_mempool *pool;
union {
struct {
void (*free_cb)(void *p); /* If type == PA_MEMBLOCK_USER this points to a function for freeing this memory block */
} user;
struct {
uint32_t id;
pa_memimport_segment *segment;
} imported;
} 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.*/
* they are here for purely statistical reasons.*/
struct pa_mempool_stat {
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;
pa_atomic_int_t n_allocated;
pa_atomic_int_t n_accumulated;
pa_atomic_int_t n_imported;
pa_atomic_int_t n_exported;
pa_atomic_int_t allocated_size;
pa_atomic_int_t accumulated_size;
pa_atomic_int_t imported_size;
pa_atomic_int_t exported_size;
AO_t n_too_large_for_pool;
AO_t n_pool_full;
pa_atomic_int_t n_too_large_for_pool;
pa_atomic_int_t n_pool_full;
AO_t n_allocated_by_type[PA_MEMBLOCK_TYPE_MAX];
AO_t n_accumulated_by_type[PA_MEMBLOCK_TYPE_MAX];
pa_atomic_int_t n_allocated_by_type[PA_MEMBLOCK_TYPE_MAX];
pa_atomic_int_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 */
@ -116,9 +97,17 @@ pa_memblock* pa_memblock_ref(pa_memblock*b);
/* This special unref function has to be called by the owner of the
memory of a static memory block when he wants to release all
references to the memory. This causes the memory to be copied and
converted into a PA_MEMBLOCK_DYNAMIC type memory block */
converted into a pool or malloc'ed memory block. Please note that this
function is not multiple caller safe, i.e. needs to be locked
manually if called from more than one thread at the same time. */
void pa_memblock_unref_fixed(pa_memblock*b);
int pa_memblock_is_read_only(pa_memblock *b);
void* pa_memblock_acquire(pa_memblock *b);
void pa_memblock_release(pa_memblock *b);
size_t pa_memblock_get_length(pa_memblock *b);
pa_mempool * pa_memblock_get_pool(pa_memblock *b);
/* The memory block manager */
pa_mempool* pa_mempool_new(int shared);
void pa_mempool_free(pa_mempool *p);