summaryrefslogtreecommitdiffstats
path: root/cpukit
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2016-04-08 06:56:46 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2016-04-12 07:36:19 +0200
commit1142f5593a5b6ab65d47bafb749e0709c7d2daa2 (patch)
tree44d5b886a880307bd6f38550a7275071f38965a7 /cpukit
parentscore: Simplify _Objects_Get_no_protection() (diff)
downloadrtems-1142f5593a5b6ab65d47bafb749e0709c7d2daa2.tar.bz2
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().
Diffstat (limited to 'cpukit')
-rw-r--r--cpukit/rtems/include/rtems/rtems/regionimpl.h24
-rw-r--r--cpukit/rtems/src/regiondelete.c28
-rw-r--r--cpukit/rtems/src/regionextend.c40
-rw-r--r--cpukit/rtems/src/regiongetfreeinfo.c22
-rw-r--r--cpukit/rtems/src/regiongetinfo.c20
-rw-r--r--cpukit/rtems/src/regiongetsegment.c96
-rw-r--r--cpukit/rtems/src/regiongetsegmentsize.c18
-rw-r--r--cpukit/rtems/src/regionprocessqueue.c2
-rw-r--r--cpukit/rtems/src/regionresizesegment.c50
-rw-r--r--cpukit/rtems/src/regionreturnsegment.c29
10 files changed, 162 insertions, 167 deletions
diff --git a/cpukit/rtems/include/rtems/rtems/regionimpl.h b/cpukit/rtems/include/rtems/rtems/regionimpl.h
index 60cf50c2fd..4db65997a7 100644
--- a/cpukit/rtems/include/rtems/rtems/regionimpl.h
+++ b/cpukit/rtems/include/rtems/rtems/regionimpl.h
@@ -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();
}
/**
diff --git a/cpukit/rtems/src/regiondelete.c b/cpukit/rtems/src/regiondelete.c
index 3927a545d6..684c2a9b96 100644
--- a/cpukit/rtems/src/regiondelete.c
+++ b/cpukit/rtems/src/regiondelete.c
@@ -28,23 +28,23 @@ rtems_status_code rtems_region_delete(
Region_Control *the_region;
_Objects_Allocator_lock();
- _RTEMS_Lock_allocator();
-
- the_region = _Region_Get( id );
-
- if ( the_region != NULL ) {
- if ( the_region->number_of_used_blocks != 0 ) {
- status = RTEMS_RESOURCE_IN_USE;
- } else {
- _Objects_Close( &_Region_Information, &the_region->Object );
- _Region_Free( the_region );
- status = RTEMS_SUCCESSFUL;
- }
+
+ the_region = _Region_Get_and_lock( id );
+
+ if ( the_region == NULL ) {
+ _Objects_Allocator_unlock();
+ return RTEMS_INVALID_ID;
+ }
+
+ if ( the_region->number_of_used_blocks != 0 ) {
+ status = RTEMS_RESOURCE_IN_USE;
} else {
- status = RTEMS_INVALID_ID;
+ _Objects_Close( &_Region_Information, &the_region->Object );
+ _Region_Free( the_region );
+ status = RTEMS_SUCCESSFUL;
}
- _RTEMS_Unlock_allocator();
+ _Region_Unlock( the_region );
_Objects_Allocator_unlock();
return status;
}
diff --git a/cpukit/rtems/src/regionextend.c b/cpukit/rtems/src/regionextend.c
index b1df066f25..e6cadf8750 100644
--- a/cpukit/rtems/src/regionextend.c
+++ b/cpukit/rtems/src/regionextend.c
@@ -34,29 +34,27 @@ rtems_status_code rtems_region_extend(
return RTEMS_INVALID_ADDRESS;
}
- _RTEMS_Lock_allocator();
-
- the_region = _Region_Get( id );
-
- if ( the_region != NULL ) {
- amount_extended = _Heap_Extend(
- &the_region->Memory,
- starting_address,
- length,
- 0
- );
-
- if ( amount_extended > 0 ) {
- the_region->length += amount_extended;
- the_region->maximum_segment_size += amount_extended;
- status = RTEMS_SUCCESSFUL;
- } else {
- status = RTEMS_INVALID_ADDRESS;
- }
+ the_region = _Region_Get_and_lock( id );
+
+ if ( the_region == NULL ) {
+ return RTEMS_INVALID_ID;
+ }
+
+ amount_extended = _Heap_Extend(
+ &the_region->Memory,
+ starting_address,
+ length,
+ 0
+ );
+
+ if ( amount_extended > 0 ) {
+ the_region->length += amount_extended;
+ the_region->maximum_segment_size += amount_extended;
+ status = RTEMS_SUCCESSFUL;
} else {
- status = RTEMS_INVALID_ID;
+ status = RTEMS_INVALID_ADDRESS;
}
- _RTEMS_Unlock_allocator();
+ _Region_Unlock( the_region );
return status;
}
diff --git a/cpukit/rtems/src/regiongetfreeinfo.c b/cpukit/rtems/src/regiongetfreeinfo.c
index 7924c0f90e..c35d888082 100644
--- a/cpukit/rtems/src/regiongetfreeinfo.c
+++ b/cpukit/rtems/src/regiongetfreeinfo.c
@@ -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;
+ 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;
}
diff --git a/cpukit/rtems/src/regiongetinfo.c b/cpukit/rtems/src/regiongetinfo.c
index 2ab3d40e30..8226dca59a 100644
--- a/cpukit/rtems/src/regiongetinfo.c
+++ b/cpukit/rtems/src/regiongetinfo.c
@@ -25,24 +25,20 @@ rtems_status_code rtems_region_get_information(
Heap_Information_block *the_info
)
{
- rtems_status_code status;
- Region_Control *the_region;
+ 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;
}
diff --git a/cpukit/rtems/src/regiongetsegment.c b/cpukit/rtems/src/regiongetsegment.c
index 5a98e85ba4..ecc6378383 100644
--- a/cpukit/rtems/src/regiongetsegment.c
+++ b/cpukit/rtems/src/regiongetsegment.c
@@ -44,60 +44,58 @@ 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 ( size > the_region->maximum_segment_size ) {
+ status = RTEMS_INVALID_SIZE;
+ } else {
+ void *the_segment;
- if ( the_region != NULL ) {
- if ( size > the_region->maximum_segment_size ) {
- status = RTEMS_INVALID_SIZE;
+ the_segment = _Region_Allocate_segment( the_region, size );
+
+ if ( the_segment != NULL ) {
+ the_region->number_of_used_blocks += 1;
+ *segment = the_segment;
+ status = RTEMS_SUCCESSFUL;
+ } else if ( _Options_Is_no_wait( option_set ) ) {
+ status = RTEMS_UNSATISFIED;
} else {
- void *the_segment;
-
- the_segment = _Region_Allocate_segment( the_region, size );
-
- if ( the_segment != NULL ) {
- the_region->number_of_used_blocks += 1;
- *segment = the_segment;
- status = RTEMS_SUCCESSFUL;
- } else if ( _Options_Is_no_wait( option_set ) ) {
- status = RTEMS_UNSATISFIED;
- } else {
- Per_CPU_Control *cpu_self;
- Thread_Control *executing;
-
- /*
- * Switch from using the memory allocation mutex to using a
- * dispatching disabled critical section. We have to do this
- * because this thread is going to block.
- */
- /* FIXME: This is a home grown condition variable */
- cpu_self = _Thread_Dispatch_disable();
- _RTEMS_Unlock_allocator();
-
- executing = _Per_CPU_Get_executing( cpu_self );
-
- executing->Wait.count = size;
- executing->Wait.return_argument = segment;
-
- _Thread_queue_Enqueue(
- &the_region->Wait_queue,
- the_region->wait_operations,
- executing,
- STATES_WAITING_FOR_SEGMENT,
- timeout,
- RTEMS_TIMEOUT
- );
-
- _Thread_Dispatch_enable( cpu_self );
-
- return (rtems_status_code) executing->Wait.return_code;
- }
+ Per_CPU_Control *cpu_self;
+ Thread_Control *executing;
+
+ /*
+ * Switch from using the memory allocation mutex to using a
+ * dispatching disabled critical section. We have to do this
+ * because this thread is going to block.
+ */
+ /* FIXME: This is a home grown condition variable */
+ cpu_self = _Thread_Dispatch_disable();
+ _Region_Unlock( the_region );
+
+ executing = _Per_CPU_Get_executing( cpu_self );
+
+ executing->Wait.count = size;
+ executing->Wait.return_argument = segment;
+
+ _Thread_queue_Enqueue(
+ &the_region->Wait_queue,
+ the_region->wait_operations,
+ executing,
+ STATES_WAITING_FOR_SEGMENT,
+ timeout,
+ RTEMS_TIMEOUT
+ );
+
+ _Thread_Dispatch_enable( cpu_self );
+
+ return (rtems_status_code) executing->Wait.return_code;
}
- } else {
- status = RTEMS_INVALID_ID;
}
- _RTEMS_Unlock_allocator();
+ _Region_Unlock( the_region );
return status;
}
diff --git a/cpukit/rtems/src/regiongetsegmentsize.c b/cpukit/rtems/src/regiongetsegmentsize.c
index 21a2bb4bde..bb7481fcad 100644
--- a/cpukit/rtems/src/regiongetsegmentsize.c
+++ b/cpukit/rtems/src/regiongetsegmentsize.c
@@ -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;
- }
+ if ( _Heap_Size_of_alloc_area( &the_region->Memory, segment, size ) ) {
+ status = RTEMS_SUCCESSFUL;
} else {
- status = RTEMS_INVALID_ID;
+ status = RTEMS_INVALID_ADDRESS;
}
- _RTEMS_Unlock_allocator();
+ _Region_Unlock( the_region );
return status;
}
diff --git a/cpukit/rtems/src/regionprocessqueue.c b/cpukit/rtems/src/regionprocessqueue.c
index a28d68c03e..1c9d273b05 100644
--- a/cpukit/rtems/src/regionprocessqueue.c
+++ b/cpukit/rtems/src/regionprocessqueue.c
@@ -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
diff --git a/cpukit/rtems/src/regionresizesegment.c b/cpukit/rtems/src/regionresizesegment.c
index a21d1b9d44..303fce496f 100644
--- a/cpukit/rtems/src/regionresizesegment.c
+++ b/cpukit/rtems/src/regionresizesegment.c
@@ -37,38 +37,36 @@ 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,
- (uint32_t) size,
- &osize,
- &avail_size
- );
- *old_size = (uint32_t) osize;
+ resize_status = _Heap_Resize_block(
+ &the_region->Memory,
+ segment,
+ (uint32_t) size,
+ &osize,
+ &avail_size
+ );
+ *old_size = (uint32_t) osize;
- switch ( resize_status ) {
- case HEAP_RESIZE_SUCCESSFUL:
- /* Unlocks allocator */
- _Region_Process_queue( the_region );
- return RTEMS_SUCCESSFUL;
+ switch ( resize_status ) {
+ case HEAP_RESIZE_SUCCESSFUL:
+ /* Unlocks allocator */
+ _Region_Process_queue( the_region );
+ return RTEMS_SUCCESSFUL;
- case HEAP_RESIZE_UNSATISFIED:
- status = RTEMS_UNSATISFIED;
- break;
+ case HEAP_RESIZE_UNSATISFIED:
+ status = RTEMS_UNSATISFIED;
+ break;
- default:
- status = RTEMS_INVALID_ADDRESS;
- break;
- }
- } else {
- status = RTEMS_INVALID_ID;
+ default:
+ status = RTEMS_INVALID_ADDRESS;
+ break;
}
- _RTEMS_Unlock_allocator();
+ _Region_Unlock( the_region );
return status;
}
diff --git a/cpukit/rtems/src/regionreturnsegment.c b/cpukit/rtems/src/regionreturnsegment.c
index c7147bbe19..8ec84fb9c4 100644
--- a/cpukit/rtems/src/regionreturnsegment.c
+++ b/cpukit/rtems/src/regionreturnsegment.c
@@ -25,27 +25,22 @@ rtems_status_code rtems_region_return_segment(
void *segment
)
{
- rtems_status_code status;
- Region_Control *the_region;
+ 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;
+ 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;
+ /* Unlocks allocator */
+ _Region_Process_queue( the_region );
+ return RTEMS_SUCCESSFUL;
}
- _RTEMS_Unlock_allocator();
- return status;
+ _Region_Unlock( the_region );
+ return RTEMS_INVALID_ADDRESS;
}