[Libva] [PATCH] Thread safety for object allocation

Krzysztof Kotlenga pocek at users.sf.net
Wed Sep 19 07:28:41 PDT 2012


Straight port of 76ea06bab9c3e3ae9abb6150296504019d36fe7e from the
intel-driver.
---
 src/object_heap.c |   14 ++++++++++++++
 src/object_heap.h |    1 +
 2 files changed, 15 insertions(+)

diff --git a/src/object_heap.c b/src/object_heap.c
index f437edd..6ad93c5 100644
--- a/src/object_heap.c
+++ b/src/object_heap.c
@@ -103,6 +103,7 @@ int object_heap_init( object_heap_p heap, int object_size, int id_offset)
     heap->heap_size = 0;
     heap->heap_increment = 16;
     heap->next_free = LAST_FREE;
+    pthread_mutex_init(&heap->mutex, NULL);
     heap->num_buckets = 0;
     heap->bucket = NULL;
     return object_heap_expand(heap);
@@ -117,10 +118,12 @@ int object_heap_allocate( object_heap_p heap )
     object_base_p obj;
     int bucket_index, obj_index;
 
+    pthread_mutex_lock(&heap->mutex);
     if ( LAST_FREE == heap->next_free )
     {
         if( -1 == object_heap_expand( heap ) )
         {
+            pthread_mutex_unlock(&heap->mutex);
             return -1; /* Out of memory */
         }
     }
@@ -131,6 +134,7 @@ int object_heap_allocate( object_heap_p heap )
 
     obj = (object_base_p) (heap->bucket[bucket_index] + obj_index * heap->object_size);
     heap->next_free = obj->next_free;
+    pthread_mutex_unlock(&heap->mutex);
     obj->next_free = ALLOCATED;
     return obj->id;
 }
@@ -144,14 +148,17 @@ object_base_p object_heap_lookup( object_heap_p heap, int id )
     object_base_p obj;
     int bucket_index, obj_index;
 
+    pthread_mutex_lock(&heap->mutex);
     if ( (id < heap->id_offset) || (id > (heap->heap_size+heap->id_offset)) )
     {
+        pthread_mutex_unlock(&heap->mutex);
         return NULL;
     }
     id &= OBJECT_HEAP_ID_MASK;
     bucket_index = id / heap->heap_increment;
     obj_index = id % heap->heap_increment;
     obj = (object_base_p) (heap->bucket[bucket_index] + obj_index * heap->object_size);
+    pthread_mutex_unlock(&heap->mutex);
 
 	/* Check if the object has in fact been allocated */
 	if ( obj->next_free != ALLOCATED )
@@ -181,6 +188,7 @@ object_base_p object_heap_next( object_heap_p heap, object_heap_iterator *iter )
     int bucket_index, obj_index;
     int i = *iter + 1;
 
+    pthread_mutex_lock(&heap->mutex);
     while ( i < heap->heap_size)
     {
         bucket_index = i / heap->heap_increment;
@@ -189,11 +197,13 @@ object_base_p object_heap_next( object_heap_p heap, object_heap_iterator *iter )
         obj = (object_base_p) (heap->bucket[bucket_index] + obj_index * heap->object_size);
         if (obj->next_free == ALLOCATED)
         {
+            pthread_mutex_unlock(&heap->mutex);
             *iter = i;
             return obj;
         }
         i++;
     }
+    pthread_mutex_unlock(&heap->mutex);
     *iter = i;
     return NULL;
 }
@@ -211,8 +221,10 @@ void object_heap_free( object_heap_p heap, object_base_p obj )
         /* Check if the object has in fact been allocated */
         ASSERT( obj->next_free == ALLOCATED );
     
+        pthread_mutex_lock(&heap->mutex);
         obj->next_free = heap->next_free;
         heap->next_free = obj->id & OBJECT_HEAP_ID_MASK;
+        pthread_mutex_unlock(&heap->mutex);
     }
 }
 
@@ -224,6 +236,8 @@ void object_heap_destroy( object_heap_p heap )
     object_base_p obj;
     int bucket_index, obj_index, i;
 
+    pthread_mutex_destroy(&heap->mutex);
+
     /* Check if heap is empty */
     for (i = 0; i < heap->heap_size; i++)
     {
diff --git a/src/object_heap.h b/src/object_heap.h
index 4e512fd..c345541 100644
--- a/src/object_heap.h
+++ b/src/object_heap.h
@@ -38,6 +38,7 @@ struct object_heap {
     int next_free;
     int heap_size;
     int heap_increment;
+    pthread_mutex_t mutex;
     void **bucket;
     int num_buckets;
 };
-- 
1.7.9.5



More information about the Libva mailing list