Commit 88931373 authored by Xiang, Haihao's avatar Xiang, Haihao

i965_drv_video: thread safety for object allocation

Signed-off-by: default avatarXiang, Haihao <haihao.xiang@intel.com>
parent 1587cb78
...@@ -75,6 +75,7 @@ int object_heap_init( object_heap_p heap, int object_size, int id_offset) ...@@ -75,6 +75,7 @@ int object_heap_init( object_heap_p heap, int object_size, int id_offset)
heap->heap_increment = 16; heap->heap_increment = 16;
heap->heap_index = NULL; heap->heap_index = NULL;
heap->next_free = LAST_FREE; heap->next_free = LAST_FREE;
_i965InitMutex(&heap->mutex);
return object_heap_expand(heap); return object_heap_expand(heap);
} }
...@@ -85,10 +86,13 @@ int object_heap_init( object_heap_p heap, int object_size, int id_offset) ...@@ -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 ) int object_heap_allocate( object_heap_p heap )
{ {
object_base_p obj; object_base_p obj;
_i965LockMutex(&heap->mutex);
if ( LAST_FREE == heap->next_free ) if ( LAST_FREE == heap->next_free )
{ {
if( -1 == object_heap_expand( heap ) ) if( -1 == object_heap_expand( heap ) )
{ {
_i965UnlockMutex(&heap->mutex);
return -1; /* Out of memory */ return -1; /* Out of memory */
} }
} }
...@@ -96,6 +100,8 @@ int object_heap_allocate( object_heap_p heap ) ...@@ -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); obj = (object_base_p) (heap->heap_index + heap->next_free * heap->object_size);
heap->next_free = obj->next_free; heap->next_free = obj->next_free;
_i965UnlockMutex(&heap->mutex);
obj->next_free = ALLOCATED; obj->next_free = ALLOCATED;
return obj->id; return obj->id;
} }
...@@ -107,12 +113,16 @@ int object_heap_allocate( object_heap_p heap ) ...@@ -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 object_heap_lookup( object_heap_p heap, int id )
{ {
object_base_p obj; object_base_p obj;
_i965LockMutex(&heap->mutex);
if ( (id < heap->id_offset) || (id > (heap->heap_size+heap->id_offset)) ) if ( (id < heap->id_offset) || (id > (heap->heap_size+heap->id_offset)) )
{ {
_i965UnlockMutex(&heap->mutex);
return NULL; return NULL;
} }
id &= OBJECT_HEAP_ID_MASK; id &= OBJECT_HEAP_ID_MASK;
obj = (object_base_p) (heap->heap_index + id * heap->object_size); obj = (object_base_p) (heap->heap_index + id * heap->object_size);
_i965UnlockMutex(&heap->mutex);
/* Check if the object has in fact been allocated */ /* Check if the object has in fact been allocated */
if ( obj->next_free != ALLOCATED ) if ( obj->next_free != ALLOCATED )
...@@ -140,16 +150,19 @@ object_base_p object_heap_next( object_heap_p heap, object_heap_iterator *iter ) ...@@ -140,16 +150,19 @@ object_base_p object_heap_next( object_heap_p heap, object_heap_iterator *iter )
{ {
object_base_p obj; object_base_p obj;
int i = *iter + 1; int i = *iter + 1;
_i965LockMutex(&heap->mutex);
while ( i < heap->heap_size) while ( i < heap->heap_size)
{ {
obj = (object_base_p) (heap->heap_index + i * heap->object_size); obj = (object_base_p) (heap->heap_index + i * heap->object_size);
if (obj->next_free == ALLOCATED) if (obj->next_free == ALLOCATED)
{ {
_i965UnlockMutex(&heap->mutex);
*iter = i; *iter = i;
return obj; return obj;
} }
i++; i++;
} }
_i965UnlockMutex(&heap->mutex);
*iter = i; *iter = i;
return NULL; return NULL;
} }
...@@ -167,8 +180,10 @@ void object_heap_free( object_heap_p heap, object_base_p obj ) ...@@ -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 */ /* Check if the object has in fact been allocated */
ASSERT( obj->next_free == ALLOCATED ); ASSERT( obj->next_free == ALLOCATED );
_i965LockMutex(&heap->mutex);
obj->next_free = heap->next_free; obj->next_free = heap->next_free;
heap->next_free = obj->id & OBJECT_HEAP_ID_MASK; heap->next_free = obj->id & OBJECT_HEAP_ID_MASK;
_i965UnlockMutex(&heap->mutex);
} }
} }
...@@ -179,6 +194,9 @@ void object_heap_destroy( object_heap_p heap ) ...@@ -179,6 +194,9 @@ void object_heap_destroy( object_heap_p heap )
{ {
object_base_p obj; object_base_p obj;
int i; int i;
_i965DestroyMutex(&heap->mutex);
/* Check if heap is empty */ /* Check if heap is empty */
for (i = 0; i < heap->heap_size; i++) for (i = 0; i < heap->heap_size; i++)
{ {
......
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
#ifndef _OBJECT_HEAP_H_ #ifndef _OBJECT_HEAP_H_
#define _OBJECT_HEAP_H_ #define _OBJECT_HEAP_H_
#include "i965_mutext.h"
#define OBJECT_HEAP_OFFSET_MASK 0x7F000000 #define OBJECT_HEAP_OFFSET_MASK 0x7F000000
#define OBJECT_HEAP_ID_MASK 0x00FFFFFF #define OBJECT_HEAP_ID_MASK 0x00FFFFFF
...@@ -43,6 +45,7 @@ struct object_heap { ...@@ -43,6 +45,7 @@ struct object_heap {
int next_free; int next_free;
int heap_size; int heap_size;
int heap_increment; int heap_increment;
_I965Mutex mutex;
}; };
typedef int object_heap_iterator; typedef int object_heap_iterator;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment