summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof Kotlenga <pocek@users.sf.net>2012-09-19 16:28:41 +0200
committerGwenole Beauchesne <gwenole.beauchesne@intel.com>2012-09-28 14:22:48 +0200
commit0e0725cb9d139bf42190d3572dc6350cedbfb22f (patch)
tree5f02bdc999777fbb9644ddab5a6744261d3fa764
parenta970574139eb783316264e607dfe58798467ed89 (diff)
objects: fix threading issues.
Make base VA objects thread-safe. This is a straightforward port of 76ea06bab9c3e3ae9abb6150296504019d36fe7e from the VA intel-driver. Signed-off-by: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
-rw-r--r--src/object_heap.c65
-rw-r--r--src/object_heap.h1
2 files changed, 54 insertions, 12 deletions
diff --git a/src/object_heap.c b/src/object_heap.c
index f437edd..615cd38 100644
--- a/src/object_heap.c
+++ b/src/object_heap.c
@@ -19,6 +19,7 @@
*/
#include "sysdeps.h"
+#include <pthread.h>
#include "object_heap.h"
/* This code is:
@@ -98,6 +99,7 @@ static int object_heap_expand( object_heap_p heap )
*/
int object_heap_init( object_heap_p heap, int object_size, int id_offset)
{
+ pthread_mutex_init(&heap->mutex, NULL);
heap->object_size = object_size;
heap->id_offset = id_offset & OBJECT_HEAP_OFFSET_MASK;
heap->heap_size = 0;
@@ -112,7 +114,7 @@ int object_heap_init( object_heap_p heap, int object_size, int id_offset)
* Allocates an object
* Returns the object ID on success, returns -1 on error
*/
-int object_heap_allocate( object_heap_p heap )
+static int object_heap_allocate_unlocked( object_heap_p heap )
{
object_base_p obj;
int bucket_index, obj_index;
@@ -135,11 +137,21 @@ int object_heap_allocate( object_heap_p heap )
return obj->id;
}
+int object_heap_allocate( object_heap_p heap )
+{
+ int ret;
+
+ pthread_mutex_lock(&heap->mutex);
+ ret = object_heap_allocate_unlocked(heap);
+ pthread_mutex_unlock(&heap->mutex);
+ return ret;
+}
+
/*
* Lookup an object by object ID
* Returns a pointer to the object on success, returns NULL on error
*/
-object_base_p object_heap_lookup( object_heap_p heap, int id )
+static object_base_p object_heap_lookup_unlocked( object_heap_p heap, int id )
{
object_base_p obj;
int bucket_index, obj_index;
@@ -161,6 +173,16 @@ object_base_p object_heap_lookup( object_heap_p heap, int id )
return obj;
}
+object_base_p object_heap_lookup( object_heap_p heap, int id )
+{
+ object_base_p obj;
+
+ pthread_mutex_lock(&heap->mutex);
+ obj = object_heap_lookup_unlocked(heap, id);
+ pthread_mutex_unlock(&heap->mutex);
+ return obj;
+}
+
/*
* Iterate over all objects in the heap.
* Returns a pointer to the first object on the heap, returns NULL if heap is empty.
@@ -175,7 +197,8 @@ object_base_p object_heap_first( object_heap_p heap, object_heap_iterator *iter
* Iterate over all objects in the heap.
* Returns a pointer to the next object on the heap, returns NULL if heap is empty.
*/
-object_base_p object_heap_next( object_heap_p heap, object_heap_iterator *iter )
+static object_base_p
+object_heap_next_unlocked( object_heap_p heap, object_heap_iterator *iter )
{
object_base_p obj;
int bucket_index, obj_index;
@@ -198,22 +221,38 @@ object_base_p object_heap_next( object_heap_p heap, object_heap_iterator *iter )
return NULL;
}
+object_base_p
+object_heap_next( object_heap_p heap, object_heap_iterator *iter )
+{
+ object_base_p obj;
+ pthread_mutex_lock(&heap->mutex);
+ obj = object_heap_next_unlocked(heap, iter);
+ pthread_mutex_unlock(&heap->mutex);
+ return obj;
+}
/*
* Frees an object
*/
-void object_heap_free( object_heap_p heap, object_base_p obj )
+static void
+object_heap_free_unlocked( object_heap_p heap, object_base_p obj )
{
- /* Don't complain about NULL pointers */
- if (NULL != obj)
- {
- /* Check if the object has in fact been allocated */
- ASSERT( obj->next_free == ALLOCATED );
+ /* Check if the object has in fact been allocated */
+ ASSERT( obj->next_free == ALLOCATED );
- obj->next_free = heap->next_free;
- heap->next_free = obj->id & OBJECT_HEAP_ID_MASK;
- }
+ obj->next_free = heap->next_free;
+ heap->next_free = obj->id & OBJECT_HEAP_ID_MASK;
+}
+
+void
+object_heap_free( object_heap_p heap, object_base_p obj )
+{
+ if (!obj)
+ return;
+ pthread_mutex_lock(&heap->mutex);
+ object_heap_free_unlocked(heap, obj);
+ pthread_mutex_unlock(&heap->mutex);
}
/*
@@ -238,6 +277,8 @@ void object_heap_destroy( object_heap_p heap )
free(heap->bucket[i]);
}
+ pthread_mutex_destroy(&heap->mutex);
+
free(heap->bucket);
heap->bucket = NULL;
heap->heap_size = 0;
diff --git a/src/object_heap.h b/src/object_heap.h
index 4e512fd..e3301b4 100644
--- a/src/object_heap.h
+++ b/src/object_heap.h
@@ -33,6 +33,7 @@ struct object_base {
};
struct object_heap {
+ pthread_mutex_t mutex;
int object_size;
int id_offset;
int next_free;