rtems: Add and use _Region_Get_and_lock()

Get region and lock allocator in _Region_Get_and_lock() in case the
region exists and unlock it in _Region_Unlock().
This commit is contained in:
Sebastian Huber
2016-04-08 06:56:46 +02:00
parent 572cb62429
commit 1142f5593a
10 changed files with 172 additions and 177 deletions

View File

@@ -19,7 +19,6 @@
#include <rtems/rtems/region.h>
#include <rtems/score/apimutex.h>
#include <rtems/score/assert.h>
#include <rtems/score/heapimpl.h>
#include <rtems/score/objectimpl.h>
#include <rtems/score/threadqimpl.h>
@@ -67,11 +66,28 @@ RTEMS_INLINE_ROUTINE void _Region_Free (
_Objects_Free( &_Region_Information, &the_region->Object );
}
RTEMS_INLINE_ROUTINE Region_Control *_Region_Get( Objects_Id id )
RTEMS_INLINE_ROUTINE Region_Control *_Region_Get_and_lock( Objects_Id id )
{
_Assert( _RTEMS_Allocator_is_owner() );
return (Region_Control *)
Region_Control *the_region;
_RTEMS_Lock_allocator();
the_region = (Region_Control *)
_Objects_Get_no_protection( &_Region_Information, id );
if ( the_region != NULL ) {
/* Keep allocator lock */
return the_region;
}
_RTEMS_Unlock_allocator();
return NULL;
}
RTEMS_INLINE_ROUTINE void _Region_Unlock( Region_Control *the_region )
{
(void) the_region;
_RTEMS_Unlock_allocator();
}
/**

View File

@@ -28,11 +28,14 @@ rtems_status_code rtems_region_delete(
Region_Control *the_region;
_Objects_Allocator_lock();
_RTEMS_Lock_allocator();
the_region = _Region_Get( id );
the_region = _Region_Get_and_lock( id );
if ( the_region == NULL ) {
_Objects_Allocator_unlock();
return RTEMS_INVALID_ID;
}
if ( the_region != NULL ) {
if ( the_region->number_of_used_blocks != 0 ) {
status = RTEMS_RESOURCE_IN_USE;
} else {
@@ -40,11 +43,8 @@ rtems_status_code rtems_region_delete(
_Region_Free( the_region );
status = RTEMS_SUCCESSFUL;
}
} else {
status = RTEMS_INVALID_ID;
}
_RTEMS_Unlock_allocator();
_Region_Unlock( the_region );
_Objects_Allocator_unlock();
return status;
}

View File

@@ -34,11 +34,12 @@ rtems_status_code rtems_region_extend(
return RTEMS_INVALID_ADDRESS;
}
_RTEMS_Lock_allocator();
the_region = _Region_Get_and_lock( id );
the_region = _Region_Get( id );
if ( the_region == NULL ) {
return RTEMS_INVALID_ID;
}
if ( the_region != NULL ) {
amount_extended = _Heap_Extend(
&the_region->Memory,
starting_address,
@@ -53,10 +54,7 @@ rtems_status_code rtems_region_extend(
} else {
status = RTEMS_INVALID_ADDRESS;
}
} else {
status = RTEMS_INVALID_ID;
}
_RTEMS_Unlock_allocator();
_Region_Unlock( the_region );
return status;
}

View File

@@ -27,25 +27,21 @@ rtems_status_code rtems_region_get_free_information(
Heap_Information_block *the_info
)
{
rtems_status_code status;
Region_Control *the_region;
if ( the_info == NULL ) {
return RTEMS_INVALID_ADDRESS;
}
_RTEMS_Lock_allocator();
the_region = _Region_Get_and_lock( id );
the_region = _Region_Get( id );
if ( the_region != NULL ) {
memset( &the_info->Used, 0, sizeof( the_info->Used ) );
_Heap_Get_free_information( &the_region->Memory, &the_info->Free );
status = RTEMS_SUCCESSFUL;
} else {
status = RTEMS_INVALID_ID;
if ( the_region == NULL ) {
return RTEMS_INVALID_ID;
}
_RTEMS_Unlock_allocator();
return status;
memset( &the_info->Used, 0, sizeof( the_info->Used ) );
_Heap_Get_free_information( &the_region->Memory, &the_info->Free );
_Region_Unlock( the_region );
return RTEMS_SUCCESSFUL;
}

View File

@@ -25,24 +25,20 @@ rtems_status_code rtems_region_get_information(
Heap_Information_block *the_info
)
{
rtems_status_code status;
Region_Control *the_region;
if ( the_info == NULL ) {
return RTEMS_INVALID_ADDRESS;
}
_RTEMS_Lock_allocator();
the_region = _Region_Get_and_lock( id );
the_region = _Region_Get( id );
if ( the_region != NULL ) {
_Heap_Get_information( &the_region->Memory, the_info );
status = RTEMS_SUCCESSFUL;
} else {
status = RTEMS_INVALID_ID;
if ( the_region == NULL ) {
return RTEMS_INVALID_ID;
}
_RTEMS_Unlock_allocator();
return status;
_Heap_Get_information( &the_region->Memory, the_info );
_Region_Unlock( the_region );
return RTEMS_SUCCESSFUL;
}

View File

@@ -44,11 +44,12 @@ rtems_status_code rtems_region_get_segment(
return RTEMS_INVALID_SIZE;
}
_RTEMS_Lock_allocator();
the_region = _Region_Get_and_lock( id );
the_region = _Region_Get( id );
if ( the_region == NULL ) {
return RTEMS_INVALID_ID;
}
if ( the_region != NULL ) {
if ( size > the_region->maximum_segment_size ) {
status = RTEMS_INVALID_SIZE;
} else {
@@ -73,7 +74,7 @@ rtems_status_code rtems_region_get_segment(
*/
/* FIXME: This is a home grown condition variable */
cpu_self = _Thread_Dispatch_disable();
_RTEMS_Unlock_allocator();
_Region_Unlock( the_region );
executing = _Per_CPU_Get_executing( cpu_self );
@@ -94,10 +95,7 @@ rtems_status_code rtems_region_get_segment(
return (rtems_status_code) executing->Wait.return_code;
}
}
} else {
status = RTEMS_INVALID_ID;
}
_RTEMS_Unlock_allocator();
_Region_Unlock( the_region );
return status;
}

View File

@@ -37,20 +37,18 @@ rtems_status_code rtems_region_get_segment_size(
return RTEMS_INVALID_ADDRESS;
}
_RTEMS_Lock_allocator();
the_region = _Region_Get_and_lock( id );
the_region = _Region_Get( id );
if ( the_region == NULL ) {
return RTEMS_INVALID_ID;
}
if ( the_region != NULL ) {
if ( _Heap_Size_of_alloc_area( &the_region->Memory, segment, size ) ) {
status = RTEMS_SUCCESSFUL;
} else {
status = RTEMS_INVALID_ADDRESS;
}
} else {
status = RTEMS_INVALID_ID;
}
_RTEMS_Unlock_allocator();
_Region_Unlock( the_region );
return status;
}

View File

@@ -40,7 +40,7 @@ void _Region_Process_queue(
* switch could occur.
*/
cpu_self = _Thread_Dispatch_disable();
_RTEMS_Unlock_allocator();
_Region_Unlock( the_region );
/*
* NOTE: The following loop is O(n) where n is the number of

View File

@@ -37,11 +37,12 @@ rtems_status_code rtems_region_resize_segment(
return RTEMS_INVALID_ADDRESS;
}
_RTEMS_Lock_allocator();
the_region = _Region_Get_and_lock( id );
the_region = _Region_Get( id );
if ( the_region == NULL ) {
return RTEMS_INVALID_ID;
}
if ( the_region != NULL ) {
resize_status = _Heap_Resize_block(
&the_region->Memory,
segment,
@@ -65,10 +66,7 @@ rtems_status_code rtems_region_resize_segment(
status = RTEMS_INVALID_ADDRESS;
break;
}
} else {
status = RTEMS_INVALID_ID;
}
_RTEMS_Unlock_allocator();
_Region_Unlock( the_region );
return status;
}

View File

@@ -25,27 +25,22 @@ rtems_status_code rtems_region_return_segment(
void *segment
)
{
rtems_status_code status;
Region_Control *the_region;
_RTEMS_Lock_allocator();
the_region = _Region_Get_and_lock( id );
the_region = _Region_Get( id );
if ( the_region == NULL ) {
return RTEMS_INVALID_ID;
}
if ( the_region != NULL ) {
if ( _Region_Free_segment( the_region, segment ) ) {
the_region->number_of_used_blocks -= 1;
/* Unlocks allocator */
_Region_Process_queue( the_region );
return RTEMS_SUCCESSFUL;
} else {
status = RTEMS_INVALID_ADDRESS;
}
} else {
status = RTEMS_INVALID_ID;
}
_RTEMS_Unlock_allocator();
return status;
_Region_Unlock( the_region );
return RTEMS_INVALID_ADDRESS;
}