[HB] Fix blob to use a actual mutex

This commit is contained in:
Behdad Esfahbod 2009-08-06 13:33:51 -04:00
parent a794ebf4be
commit 7f3d5c8166
3 changed files with 106 additions and 43 deletions

View File

@ -36,11 +36,15 @@
struct _hb_blob_t { struct _hb_blob_t {
hb_reference_count_t ref_count; hb_reference_count_t ref_count;
hb_reference_count_t lock; unsigned int length;
hb_mutex_t lock;
/* the rest are protected by lock */
unsigned int lock_count;
hb_memory_mode_t mode;
const char *data; const char *data;
unsigned int length;
hb_memory_mode_t mode;
hb_destroy_func_t destroy; hb_destroy_func_t destroy;
void *user_data; void *user_data;
@ -48,11 +52,14 @@ struct _hb_blob_t {
static hb_blob_t _hb_blob_nil = { static hb_blob_t _hb_blob_nil = {
HB_REFERENCE_COUNT_INVALID, /* ref_count */ HB_REFERENCE_COUNT_INVALID, /* ref_count */
HB_REFERENCE_COUNT_INVALID, /* lock */ 0, /* length */
HB_MUTEX_INIT, /* lock */
0, /* lock_count */
HB_MEMORY_MODE_READONLY_NEVER_DUPLICATE, /* mode */
NULL, /* data */ NULL, /* data */
0, /* length */
HB_MEMORY_MODE_READONLY_NEVER_DUPLICATE, /* mode */
NULL, /* destroy */ NULL, /* destroy */
NULL /* user_data */ NULL /* user_data */
@ -90,7 +97,8 @@ hb_blob_create (const char *data,
return &_hb_blob_nil; return &_hb_blob_nil;
} }
HB_REFERENCE_COUNT_INIT (blob->lock, 0); hb_mutex_init (blob->lock);
blob->lock_count = 0;
blob->data = data; blob->data = data;
blob->length = length; blob->length = length;
@ -125,7 +133,10 @@ hb_blob_create_sub_blob (hb_blob_t *parent,
blob->data = pdata + offset; blob->data = pdata + offset;
blob->length = MIN (length, parent->length - offset); blob->length = MIN (length, parent->length - offset);
hb_mutex_lock (parent->lock);
blob->mode = parent->mode; blob->mode = parent->mode;
hb_mutex_unlock (parent->lock);
blob->destroy = (hb_destroy_func_t) _hb_blob_unlock_and_destroy; blob->destroy = (hb_destroy_func_t) _hb_blob_unlock_and_destroy;
blob->user_data = hb_blob_reference (parent); blob->user_data = hb_blob_reference (parent);
@ -170,40 +181,67 @@ hb_blob_get_length (hb_blob_t *blob)
const char * const char *
hb_blob_lock (hb_blob_t *blob) hb_blob_lock (hb_blob_t *blob)
{ {
if (!HB_OBJECT_IS_INERT (blob)) if (HB_OBJECT_IS_INERT (blob))
(void) hb_reference_count_inc (blob->lock); return NULL;
hb_mutex_lock (blob->lock);
blob->lock_count++;
#if HB_DEBUG #if HB_DEBUG
fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__, fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
HB_REFERENCE_COUNT_GET_VALUE (blob->lock), blob->data); blob->lock_count, blob->data);
#endif #endif
hb_mutex_unlock (blob->lock);
return blob->data; return blob->data;
} }
void void
hb_blob_unlock (hb_blob_t *blob) hb_blob_unlock (hb_blob_t *blob)
{ {
if (!HB_OBJECT_IS_INERT (blob)) { if (HB_OBJECT_IS_INERT (blob))
int old_lock = hb_reference_count_dec (blob->lock); return;
assert (old_lock > 0);
}
hb_mutex_lock (blob->lock);
assert (blob->lock_count > 0);
blob->lock_count--;
#if HB_DEBUG #if HB_DEBUG
fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__, fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
HB_REFERENCE_COUNT_GET_VALUE (blob->lock), blob->data); hb_atomic_int_get (blob->lock_count), blob->data);
#endif #endif
hb_mutex_unlock (blob->lock);
} }
hb_bool_t hb_bool_t
hb_blob_is_writeable (hb_blob_t *blob) hb_blob_is_writeable (hb_blob_t *blob)
{ {
return blob->mode == HB_MEMORY_MODE_WRITEABLE; hb_memory_mode_t mode;
if (HB_OBJECT_IS_INERT (blob))
return FALSE;
hb_mutex_lock (blob->lock);
mode = blob->mode;
hb_mutex_unlock (blob->lock);
return mode == HB_MEMORY_MODE_WRITEABLE;
} }
hb_bool_t hb_bool_t
hb_blob_try_writeable_inplace (hb_blob_t *blob) hb_blob_try_writeable_inplace (hb_blob_t *blob)
{ {
hb_memory_mode_t mode;
if (HB_OBJECT_IS_INERT (blob))
return FALSE;
hb_mutex_lock (blob->lock);
if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITEABLE) { if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITEABLE) {
unsigned int pagesize, mask, length; unsigned int pagesize, mask, length;
const char *addr; const char *addr;
@ -216,7 +254,7 @@ hb_blob_try_writeable_inplace (hb_blob_t *blob)
#if HB_DEBUG #if HB_DEBUG
fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno)); fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno));
#endif #endif
return FALSE; goto done;
} }
#if HB_DEBUG #if HB_DEBUG
fprintf (stderr, "%p %s: pagesize is %u\n", blob, __FUNCTION__, pagesize); fprintf (stderr, "%p %s: pagesize is %u\n", blob, __FUNCTION__, pagesize);
@ -234,10 +272,11 @@ hb_blob_try_writeable_inplace (hb_blob_t *blob)
#if HB_DEBUG #if HB_DEBUG
fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno)); fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno));
#endif #endif
return FALSE; goto done;
} }
blob->mode = HB_MEMORY_MODE_WRITEABLE; blob->mode = HB_MEMORY_MODE_WRITEABLE;
#if HB_DEBUG #if HB_DEBUG
fprintf (stderr, "%p %s: successfully made [%p..%p] (%d bytes) writeable\n", fprintf (stderr, "%p %s: successfully made [%p..%p] (%d bytes) writeable\n",
blob, __FUNCTION__, blob, __FUNCTION__,
@ -245,25 +284,38 @@ hb_blob_try_writeable_inplace (hb_blob_t *blob)
#endif #endif
} }
return blob->mode == HB_MEMORY_MODE_WRITEABLE; done:
mode = blob->mode;
hb_mutex_unlock (blob->lock);
return mode == HB_MEMORY_MODE_WRITEABLE;
} }
hb_bool_t hb_bool_t
hb_blob_try_writeable (hb_blob_t *blob) hb_blob_try_writeable (hb_blob_t *blob)
{ {
if (blob->mode == HB_MEMORY_MODE_READONLY_NEVER_DUPLICATE) hb_memory_mode_t mode;
if (HB_OBJECT_IS_INERT (blob))
return FALSE; return FALSE;
hb_mutex_lock (blob->lock);
if (blob->mode == HB_MEMORY_MODE_READONLY_NEVER_DUPLICATE)
goto done;
if (blob->mode == HB_MEMORY_MODE_READONLY) if (blob->mode == HB_MEMORY_MODE_READONLY)
{ {
char *new_data; char *new_data;
#if HB_DEBUG #if HB_DEBUG
fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__, fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
HB_REFERENCE_COUNT_GET_VALUE (blob->lock), blob->data); blob->lock_count, blob->data);
#endif #endif
if (HB_REFERENCE_COUNT_HAS_REFERENCE (blob->lock))
return FALSE; if (blob->lock_count)
goto done;
new_data = malloc (blob->length); new_data = malloc (blob->length);
if (new_data) { if (new_data) {
@ -274,11 +326,16 @@ hb_blob_try_writeable (hb_blob_t *blob)
blob->data = new_data; blob->data = new_data;
blob->mode = HB_MEMORY_MODE_WRITEABLE; blob->mode = HB_MEMORY_MODE_WRITEABLE;
_hb_blob_destroy_user_data (blob); _hb_blob_destroy_user_data (blob);
return TRUE; }
} else
return FALSE;
} }
return done:
hb_blob_try_writeable_inplace (blob); mode = blob->mode;
hb_mutex_unlock (blob->lock);
if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITEABLE)
return hb_blob_try_writeable_inplace (blob);
return mode == HB_MEMORY_MODE_WRITEABLE;
} }

View File

@ -36,8 +36,8 @@ typedef struct {
hb_atomic_int_t ref_count; hb_atomic_int_t ref_count;
} hb_reference_count_t; } hb_reference_count_t;
#define hb_reference_count_inc(RC) hb_atomic_fetch_and_add ((RC).ref_count, 1) #define hb_reference_count_inc(RC) hb_atomic_int_fetch_and_add ((RC).ref_count, 1)
#define hb_reference_count_dec(RC) hb_atomic_fetch_and_add ((RC).ref_count, -1) #define hb_reference_count_dec(RC) hb_atomic_int_fetch_and_add ((RC).ref_count, -1)
#define HB_REFERENCE_COUNT_INIT(RC, VALUE) ((RC).ref_count = (VALUE)) #define HB_REFERENCE_COUNT_INIT(RC, VALUE) ((RC).ref_count = (VALUE))

View File

@ -51,8 +51,6 @@
#define hb_be_uint16(v) ((uint16_t) hb_be_int16 ((uint16_t) v)) #define hb_be_uint16(v) ((uint16_t) hb_be_int16 ((uint16_t) v))
#define hb_be_uint32(v) ((uint32_t) hb_be_int32 ((uint32_t) v)) #define hb_be_uint32(v) ((uint32_t) hb_be_int32 ((uint32_t) v))
typedef int hb_atomic_int_t;
/* We need external help for these */ /* We need external help for these */
#if HAVE_GLIB #if HAVE_GLIB
@ -63,10 +61,18 @@ typedef int hb_atomic_int_t;
#define hb_be_int16(v) GINT16_FROM_BE (v) #define hb_be_int16(v) GINT16_FROM_BE (v)
#define hb_be_int32(v) GINT32_FROM_BE (v) #define hb_be_int32(v) GINT32_FROM_BE (v)
#define hb_atomic_fetch_and_add(AI, V) g_atomic_int_exchange_and_add (&(AI), V) typedef int hb_atomic_int_t;
#define hb_atomic_int_fetch_and_add(AI, V) g_atomic_int_exchange_and_add (&(AI), V)
#define hb_atomic_int_get(AI) g_atomic_int_get (&(AI)) #define hb_atomic_int_get(AI) g_atomic_int_get (&(AI))
#define hb_atomic_int_set(AI, V) g_atomic_int_set (&(AI), V) #define hb_atomic_int_set(AI, V) g_atomic_int_set (&(AI), V)
typedef GStaticMutex hb_mutex_t;
#define HB_MUTEX_INIT G_STATIC_MUTEX_INIT
#define hb_mutex_init(M) g_static_mutex_init (&M)
#define hb_mutex_lock(M) g_static_mutex_lock (&M)
#define hb_mutex_trylock(M) g_static_mutex_trylock (&M)
#define hb_mutex_unlock(M) g_static_mutex_unlock (&M)
#else #else
#error "Could not find any system to define platform macros, see hb-private.h" #error "Could not find any system to define platform macros, see hb-private.h"
#endif #endif