summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXiang, Haihao <haihao.xiang@intel.com>2011-05-16 09:49:01 +0800
committerXiang, Haihao <haihao.xiang@intel.com>2011-05-16 11:58:48 +0800
commit88931373a169a30f73837fde2739fdf1042f4820 (patch)
tree409164c447bb1eb24b69ff277ba562385bc22f87
parent1587cb7821d6e9f4ae52288c30d45e5b3a7ab7a1 (diff)
i965_drv_video: thread safety for object allocation
Signed-off-by: Xiang, Haihao <haihao.xiang@intel.com>
-rw-r--r--i965_drv_video/object_heap.c18
-rw-r--r--i965_drv_video/object_heap.h3
2 files changed, 21 insertions, 0 deletions
diff --git a/i965_drv_video/object_heap.c b/i965_drv_video/object_heap.c
index e867139..46062fb 100644
--- a/i965_drv_video/object_heap.c
+++ b/i965_drv_video/object_heap.c
@@ -75,6 +75,7 @@ int object_heap_init( object_heap_p heap, int object_size, int id_offset)
heap->heap_increment = 16;
heap->heap_index = NULL;
heap->next_free = LAST_FREE;
+ _i965InitMutex(&heap->mutex);
return object_heap_expand(heap);
}
@@ -85,10 +86,13 @@ int object_heap_init( object_heap_p heap, int object_size, int id_offset)
int object_heap_allocate( object_heap_p heap )
{
object_base_p obj;
+
+ _i965LockMutex(&heap->mutex);
if ( LAST_FREE == heap->next_free )
{
if( -1 == object_heap_expand( heap ) )
{
+ _i965UnlockMutex(&heap->mutex);
return -1; /* Out of memory */
}
}
@@ -96,6 +100,8 @@ int object_heap_allocate( object_heap_p heap )
obj = (object_base_p) (heap->heap_index + heap->next_free * heap->object_size);
heap->next_free = obj->next_free;
+ _i965UnlockMutex(&heap->mutex);
+
obj->next_free = ALLOCATED;
return obj->id;
}
@@ -107,12 +113,16 @@ int object_heap_allocate( object_heap_p heap )
object_base_p object_heap_lookup( object_heap_p heap, int id )
{
object_base_p obj;
+
+ _i965LockMutex(&heap->mutex);
if ( (id < heap->id_offset) || (id > (heap->heap_size+heap->id_offset)) )
{
+ _i965UnlockMutex(&heap->mutex);
return NULL;
}
id &= OBJECT_HEAP_ID_MASK;
obj = (object_base_p) (heap->heap_index + id * heap->object_size);
+ _i965UnlockMutex(&heap->mutex);
/* Check if the object has in fact been allocated */
if ( obj->next_free != ALLOCATED )
@@ -140,16 +150,19 @@ object_base_p object_heap_next( object_heap_p heap, object_heap_iterator *iter )
{
object_base_p obj;
int i = *iter + 1;
+ _i965LockMutex(&heap->mutex);
while ( i < heap->heap_size)
{
obj = (object_base_p) (heap->heap_index + i * heap->object_size);
if (obj->next_free == ALLOCATED)
{
+ _i965UnlockMutex(&heap->mutex);
*iter = i;
return obj;
}
i++;
}
+ _i965UnlockMutex(&heap->mutex);
*iter = i;
return NULL;
}
@@ -167,8 +180,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 );
+ _i965LockMutex(&heap->mutex);
obj->next_free = heap->next_free;
heap->next_free = obj->id & OBJECT_HEAP_ID_MASK;
+ _i965UnlockMutex(&heap->mutex);
}
}
@@ -179,6 +194,9 @@ void object_heap_destroy( object_heap_p heap )
{
object_base_p obj;
int i;
+
+ _i965DestroyMutex(&heap->mutex);
+
/* Check if heap is empty */
for (i = 0; i < heap->heap_size; i++)
{
diff --git a/i965_drv_video/object_heap.h b/i965_drv_video/object_heap.h
index 154fddb..82a6917 100644
--- a/i965_drv_video/object_heap.h
+++ b/i965_drv_video/object_heap.h
@@ -25,6 +25,8 @@
#ifndef _OBJECT_HEAP_H_
#define _OBJECT_HEAP_H_
+#include "i965_mutext.h"
+
#define OBJECT_HEAP_OFFSET_MASK 0x7F000000
#define OBJECT_HEAP_ID_MASK 0x00FFFFFF
@@ -43,6 +45,7 @@ struct object_heap {
int next_free;
int heap_size;
int heap_increment;
+ _I965Mutex mutex;
};
typedef int object_heap_iterator;