From 7cc8d6c3b1a207684892d207055ceff9f37d5731 Mon Sep 17 00:00:00 2001 From: Joel Sherrill Date: Tue, 2 Nov 1999 21:48:15 +0000 Subject: Split core mutex and semaphore handlers into separate files. --- c/src/exec/score/src/coremutexflush.c | 52 ++++++++++ c/src/exec/score/src/coremutexseize.c | 156 ++++++++++++++++++++++++++++++ c/src/exec/score/src/coremutexsurrender.c | 139 ++++++++++++++++++++++++++ c/src/exec/score/src/coresemflush.c | 58 +++++++++++ c/src/exec/score/src/coresemseize.c | 81 ++++++++++++++++ c/src/exec/score/src/coresemsurrender.c | 75 ++++++++++++++ 6 files changed, 561 insertions(+) create mode 100644 c/src/exec/score/src/coremutexflush.c create mode 100644 c/src/exec/score/src/coremutexseize.c create mode 100644 c/src/exec/score/src/coremutexsurrender.c create mode 100644 c/src/exec/score/src/coresemflush.c create mode 100644 c/src/exec/score/src/coresemseize.c create mode 100644 c/src/exec/score/src/coresemsurrender.c (limited to 'c') diff --git a/c/src/exec/score/src/coremutexflush.c b/c/src/exec/score/src/coremutexflush.c new file mode 100644 index 0000000000..27ba369acf --- /dev/null +++ b/c/src/exec/score/src/coremutexflush.c @@ -0,0 +1,52 @@ +/* + * Mutex Handler + * + * DESCRIPTION: + * + * This package is the implementation of the Mutex Handler. + * This handler provides synchronization and mutual exclusion capabilities. + * + * COPYRIGHT (c) 1989-1998. + * On-Line Applications Research Corporation (OAR). + * Copyright assigned to U.S. Government, 1994. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.OARcorp.com/rtems/license.html. + * + * $Id$ + */ + +#include +#include +#include +#include +#include +#include + +/*PAGE + * + * _CORE_mutex_Flush + * + * This function a flushes the mutex's task wait queue. + * + * Input parameters: + * the_mutex - the mutex to be flushed + * remote_extract_callout - function to invoke remotely + * status - status to pass to thread + * + * Output parameters: NONE + */ + +void _CORE_mutex_Flush( + CORE_mutex_Control *the_mutex, + Thread_queue_Flush_callout remote_extract_callout, + unsigned32 status +) +{ + _Thread_queue_Flush( + &the_mutex->Wait_queue, + remote_extract_callout, + status + ); +} diff --git a/c/src/exec/score/src/coremutexseize.c b/c/src/exec/score/src/coremutexseize.c new file mode 100644 index 0000000000..a2c8a37d71 --- /dev/null +++ b/c/src/exec/score/src/coremutexseize.c @@ -0,0 +1,156 @@ +/* + * Mutex Handler + * + * DESCRIPTION: + * + * This package is the implementation of the Mutex Handler. + * This handler provides synchronization and mutual exclusion capabilities. + * + * COPYRIGHT (c) 1989-1998. + * On-Line Applications Research Corporation (OAR). + * Copyright assigned to U.S. Government, 1994. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.OARcorp.com/rtems/license.html. + * + * $Id$ + */ + +#include +#include +#include +#include +#include +#include + +/*PAGE + * + * _CORE_mutex_Seize + * + * This routine attempts to allocate a mutex to the calling thread. + * + * Input parameters: + * the_mutex - pointer to mutex control block + * id - id of object to wait on + * wait - TRUE if wait is allowed, FALSE otherwise + * timeout - number of ticks to wait (0 means forever) + * + * Output parameters: NONE + * + * INTERRUPT LATENCY: + * available + * wait + */ + +void _CORE_mutex_Seize( + CORE_mutex_Control *the_mutex, + Objects_Id id, + boolean wait, + Watchdog_Interval timeout +) +{ + Thread_Control *executing; + ISR_Level level; + + executing = _Thread_Executing; + switch ( the_mutex->Attributes.discipline ) { + case CORE_MUTEX_DISCIPLINES_FIFO: + case CORE_MUTEX_DISCIPLINES_PRIORITY: + case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: + break; + case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: + if ( executing->current_priority < + the_mutex->Attributes.priority_ceiling) { + executing->Wait.return_code = CORE_MUTEX_STATUS_CEILING_VIOLATED; + return; + } + } + executing->Wait.return_code = CORE_MUTEX_STATUS_SUCCESSFUL; + _ISR_Disable( level ); + if ( ! _CORE_mutex_Is_locked( the_mutex ) ) { + the_mutex->lock = CORE_MUTEX_LOCKED; + the_mutex->holder = executing; + the_mutex->holder_id = executing->Object.id; + the_mutex->nest_count = 1; + executing->resource_count++; + _ISR_Enable( level ); + switch ( the_mutex->Attributes.discipline ) { + case CORE_MUTEX_DISCIPLINES_FIFO: + case CORE_MUTEX_DISCIPLINES_PRIORITY: + case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: + /* already the highest priority */ + break; + case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: + if ( the_mutex->Attributes.priority_ceiling < + executing->current_priority ) { + _Thread_Change_priority( + the_mutex->holder, + the_mutex->Attributes.priority_ceiling, + FALSE + ); + } + } + return; + } + + if ( _Objects_Are_ids_equal( + _Thread_Executing->Object.id, the_mutex->holder_id ) ) { + if ( _CORE_mutex_Is_nesting_allowed( &the_mutex->Attributes ) ) + the_mutex->nest_count++; + else + executing->Wait.return_code = CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED; + + _ISR_Enable( level ); + return; + } + + if ( !wait ) { + _ISR_Enable( level ); + executing->Wait.return_code = CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT; + return; + } + + _Thread_queue_Enter_critical_section( &the_mutex->Wait_queue ); + executing->Wait.queue = &the_mutex->Wait_queue; + executing->Wait.id = id; + _ISR_Enable( level ); + + switch ( the_mutex->Attributes.discipline ) { + case CORE_MUTEX_DISCIPLINES_FIFO: + case CORE_MUTEX_DISCIPLINES_PRIORITY: + case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: + break; + case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: + if ( the_mutex->holder->current_priority > executing->current_priority ) { + _Thread_Change_priority( + the_mutex->holder, + executing->current_priority, + FALSE + ); + } + break; + } + + _Thread_queue_Enqueue( &the_mutex->Wait_queue, timeout ); + + if ( _Thread_Executing->Wait.return_code == CORE_MUTEX_STATUS_SUCCESSFUL ) { + switch ( the_mutex->Attributes.discipline ) { + case CORE_MUTEX_DISCIPLINES_FIFO: + case CORE_MUTEX_DISCIPLINES_PRIORITY: + case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: + break; + case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: + if ( the_mutex->Attributes.priority_ceiling < + executing->current_priority ) { + _Thread_Change_priority( + executing, + the_mutex->Attributes.priority_ceiling, + FALSE + ); + }; + break; + } + } +} + diff --git a/c/src/exec/score/src/coremutexsurrender.c b/c/src/exec/score/src/coremutexsurrender.c new file mode 100644 index 0000000000..b7f38eb2b0 --- /dev/null +++ b/c/src/exec/score/src/coremutexsurrender.c @@ -0,0 +1,139 @@ +/* + * Mutex Handler + * + * DESCRIPTION: + * + * This package is the implementation of the Mutex Handler. + * This handler provides synchronization and mutual exclusion capabilities. + * + * COPYRIGHT (c) 1989-1998. + * On-Line Applications Research Corporation (OAR). + * Copyright assigned to U.S. Government, 1994. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.OARcorp.com/rtems/license.html. + * + * $Id$ + */ + +#include +#include +#include +#include +#include +#include + +/* + * _CORE_mutex_Surrender + * + * DESCRIPTION: + * + * This routine frees a unit to the mutex. If a task was blocked waiting for + * a unit from this mutex, then that task will be readied and the unit + * given to that task. Otherwise, the unit will be returned to the mutex. + * + * Input parameters: + * the_mutex - the mutex to be flushed + * id - id of parent mutex + * api_mutex_mp_support - api dependent MP support actions + * + * Output parameters: + * CORE_MUTEX_STATUS_SUCCESSFUL - if successful + * core error code - if unsuccessful + */ + +CORE_mutex_Status _CORE_mutex_Surrender( + CORE_mutex_Control *the_mutex, + Objects_Id id, + CORE_mutex_API_mp_support_callout api_mutex_mp_support +) +{ + Thread_Control *the_thread; + Thread_Control *executing; + + executing = _Thread_Executing; + + /* + * The following code allows a thread (or ISR) other than the thread + * which acquired the mutex to release that mutex. This is only + * allowed when the mutex in quetion is FIFO or simple Priority + * discipline. But Priority Ceiling or Priority Inheritance mutexes + * must be released by the thread which acquired them. + */ + + if ( !_Objects_Are_ids_equal( + _Thread_Executing->Object.id, the_mutex->holder_id ) ) { + + switch ( the_mutex->Attributes.discipline ) { + case CORE_MUTEX_DISCIPLINES_FIFO: + case CORE_MUTEX_DISCIPLINES_PRIORITY: + break; + case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: + case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: + return( CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE ); + break; + } + } + + the_mutex->nest_count--; + + if ( the_mutex->nest_count != 0 ) + return( CORE_MUTEX_STATUS_SUCCESSFUL ); + + _Thread_Executing->resource_count--; + the_mutex->holder = NULL; + the_mutex->holder_id = 0; + + /* + * Whether or not someone is waiting for the mutex, an + * inherited priority must be lowered if this is the last + * mutex (i.e. resource) this task has. + */ + + switch ( the_mutex->Attributes.discipline ) { + case CORE_MUTEX_DISCIPLINES_FIFO: + case CORE_MUTEX_DISCIPLINES_PRIORITY: + break; + case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: + case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: + if ( executing->resource_count == 0 && + executing->real_priority != executing->current_priority ) { + _Thread_Change_priority( executing, executing->real_priority, TRUE ); + } + break; + } + + + if ( ( the_thread = _Thread_queue_Dequeue( &the_mutex->Wait_queue ) ) ) { + +#if defined(RTEMS_MULTIPROCESSING) + if ( !_Objects_Is_local_id( the_thread->Object.id ) ) { + + the_mutex->holder = NULL; + the_mutex->holder_id = the_thread->Object.id; + the_mutex->nest_count = 1; + + ( *api_mutex_mp_support)( the_thread, id ); + + } else +#endif + { + + the_mutex->holder = the_thread; + the_mutex->holder_id = the_thread->Object.id; + the_thread->resource_count++; + the_mutex->nest_count = 1; + + /* + * No special action for priority inheritance or priority ceiling + * because the_thread is guaranteed to be the highest priority + * thread waiting for the mutex. + */ + } + } else + the_mutex->lock = CORE_MUTEX_UNLOCKED; + + return( CORE_MUTEX_STATUS_SUCCESSFUL ); +} + diff --git a/c/src/exec/score/src/coresemflush.c b/c/src/exec/score/src/coresemflush.c new file mode 100644 index 0000000000..c456b661e5 --- /dev/null +++ b/c/src/exec/score/src/coresemflush.c @@ -0,0 +1,58 @@ +/* + * CORE Semaphore Handler + * + * DESCRIPTION: + * + * This package is the implementation of the CORE Semaphore Handler. + * This core object utilizes standard Dijkstra counting semaphores to provide + * synchronization and mutual exclusion capabilities. + * + * COPYRIGHT (c) 1989-1998. + * On-Line Applications Research Corporation (OAR). + * Copyright assigned to U.S. Government, 1994. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.OARcorp.com/rtems/license.html. + * + * $Id$ + */ + +#include +#include +#include +#include +#include +#include +#if defined(RTEMS_MULTIPROCESSING) +#include +#endif + +/*PAGE + * + * _CORE_semaphore_Flush + * + * This function a flushes the semaphore's task wait queue. + * + * Input parameters: + * the_semaphore - the semaphore to be flushed + * remote_extract_callout - function to invoke remotely + * status - status to pass to thread + * + * Output parameters: NONE + */ + +void _CORE_semaphore_Flush( + CORE_semaphore_Control *the_semaphore, + Thread_queue_Flush_callout remote_extract_callout, + unsigned32 status +) +{ + + _Thread_queue_Flush( + &the_semaphore->Wait_queue, + remote_extract_callout, + status + ); + +} diff --git a/c/src/exec/score/src/coresemseize.c b/c/src/exec/score/src/coresemseize.c new file mode 100644 index 0000000000..f08b40ca5b --- /dev/null +++ b/c/src/exec/score/src/coresemseize.c @@ -0,0 +1,81 @@ +/* + * CORE Semaphore Handler + * + * DESCRIPTION: + * + * This package is the implementation of the CORE Semaphore Handler. + * This core object utilizes standard Dijkstra counting semaphores to provide + * synchronization and mutual exclusion capabilities. + * + * COPYRIGHT (c) 1989-1998. + * On-Line Applications Research Corporation (OAR). + * Copyright assigned to U.S. Government, 1994. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.OARcorp.com/rtems/license.html. + * + * $Id$ + */ + +#include +#include +#include +#include +#include +#include +#if defined(RTEMS_MULTIPROCESSING) +#include +#endif + +/*PAGE + * + * _CORE_semaphore_Seize + * + * This routine attempts to allocate a core semaphore to the calling thread. + * + * Input parameters: + * the_semaphore - pointer to semaphore control block + * id - id of object to wait on + * wait - TRUE if wait is allowed, FALSE otherwise + * timeout - number of ticks to wait (0 means forever) + * + * Output parameters: NONE + * + * INTERRUPT LATENCY: + * available + * wait + */ + +void _CORE_semaphore_Seize( + CORE_semaphore_Control *the_semaphore, + Objects_Id id, + boolean wait, + Watchdog_Interval timeout +) +{ + Thread_Control *executing; + ISR_Level level; + + executing = _Thread_Executing; + executing->Wait.return_code = CORE_SEMAPHORE_STATUS_SUCCESSFUL; + _ISR_Disable( level ); + if ( the_semaphore->count != 0 ) { + the_semaphore->count -= 1; + _ISR_Enable( level ); + return; + } + + if ( !wait ) { + _ISR_Enable( level ); + executing->Wait.return_code = CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT; + return; + } + + _Thread_queue_Enter_critical_section( &the_semaphore->Wait_queue ); + executing->Wait.queue = &the_semaphore->Wait_queue; + executing->Wait.id = id; + _ISR_Enable( level ); + + _Thread_queue_Enqueue( &the_semaphore->Wait_queue, timeout ); +} diff --git a/c/src/exec/score/src/coresemsurrender.c b/c/src/exec/score/src/coresemsurrender.c new file mode 100644 index 0000000000..dd4839182c --- /dev/null +++ b/c/src/exec/score/src/coresemsurrender.c @@ -0,0 +1,75 @@ +/* + * CORE Semaphore Handler + * + * DESCRIPTION: + * + * This package is the implementation of the CORE Semaphore Handler. + * This core object utilizes standard Dijkstra counting semaphores to provide + * synchronization and mutual exclusion capabilities. + * + * COPYRIGHT (c) 1989-1998. + * On-Line Applications Research Corporation (OAR). + * Copyright assigned to U.S. Government, 1994. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.OARcorp.com/rtems/license.html. + * + * $Id$ + */ + +#include +#include +#include +#include +#include +#include +#if defined(RTEMS_MULTIPROCESSING) +#include +#endif + +/*PAGE + * + * _CORE_semaphore_Surrender + * + * Input parameters: + * the_semaphore - the semaphore to be flushed + * id - id of parent semaphore + * api_semaphore_mp_support - api dependent MP support actions + * + * Output parameters: + * CORE_SEMAPHORE_STATUS_SUCCESSFUL - if successful + * core error code - if unsuccessful + * + * Output parameters: + */ + +CORE_semaphore_Status _CORE_semaphore_Surrender( + CORE_semaphore_Control *the_semaphore, + Objects_Id id, + CORE_semaphore_API_mp_support_callout api_semaphore_mp_support +) +{ + Thread_Control *the_thread; + ISR_Level level; + CORE_semaphore_Status status; + + status = CORE_SEMAPHORE_STATUS_SUCCESSFUL; + + if ( (the_thread = _Thread_queue_Dequeue(&the_semaphore->Wait_queue)) ) { + + if ( !_Objects_Is_local_id( the_thread->Object.id ) ) + (*api_semaphore_mp_support) ( the_thread, id ); + + } else { + _ISR_Disable( level ); + if ( the_semaphore->count <= the_semaphore->Attributes.maximum_count ) + the_semaphore->count += 1; + else + status = CORE_SEMAPHORE_MAXIMUM_COUNT_EXCEEDED; + _ISR_Enable( level ); + } + + return status; +} + -- cgit v1.2.3