Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:22

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSImplClassicEvent
0007  *
0008  * @brief This source file contains the implementation of
0009  *   _Event_Surrender().
0010  */
0011 
0012 /*
0013  *  COPYRIGHT (c) 1989-2008.
0014  *  On-Line Applications Research Corporation (OAR).
0015  *
0016  * Redistribution and use in source and binary forms, with or without
0017  * modification, are permitted provided that the following conditions
0018  * are met:
0019  * 1. Redistributions of source code must retain the above copyright
0020  *    notice, this list of conditions and the following disclaimer.
0021  * 2. Redistributions in binary form must reproduce the above copyright
0022  *    notice, this list of conditions and the following disclaimer in the
0023  *    documentation and/or other materials provided with the distribution.
0024  *
0025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0035  * POSSIBILITY OF SUCH DAMAGE.
0036  */
0037 
0038 #ifdef HAVE_CONFIG_H
0039 #include "config.h"
0040 #endif
0041 
0042 #include <rtems/rtems/eventimpl.h>
0043 #include <rtems/rtems/optionsimpl.h>
0044 #include <rtems/score/threadimpl.h>
0045 #include <rtems/score/watchdogimpl.h>
0046 
0047 static void _Event_Satisfy(
0048   Thread_Control  *the_thread,
0049   Event_Control   *event,
0050   rtems_event_set  pending_events,
0051   rtems_event_set  seized_events
0052 )
0053 {
0054   event->pending_events = _Event_sets_Clear( pending_events, seized_events );
0055   *(rtems_event_set *) the_thread->Wait.return_argument = seized_events;
0056 }
0057 
0058 static bool _Event_Is_blocking_on_event(
0059   const Thread_Control *the_thread,
0060   Thread_Wait_flags     wait_class
0061 )
0062 {
0063   Thread_Wait_flags wait_flags;
0064 
0065   wait_flags = _Thread_Wait_flags_get( the_thread );
0066 
0067   return ( wait_flags & THREAD_WAIT_CLASS_MASK ) == wait_class;
0068 }
0069 
0070 static bool _Event_Is_satisfied(
0071   const Thread_Control *the_thread,
0072   rtems_event_set       pending_events,
0073   rtems_event_set      *seized_events
0074 )
0075 {
0076   rtems_option    option_set;
0077   rtems_event_set event_condition;
0078 
0079   option_set = the_thread->Wait.option;
0080   event_condition = the_thread->Wait.count;
0081   *seized_events = _Event_sets_Get( pending_events, event_condition );
0082 
0083   return !_Event_sets_Is_empty( *seized_events )
0084     && ( *seized_events == event_condition || _Options_Is_any( option_set ) );
0085 }
0086 
0087 rtems_status_code _Event_Surrender(
0088   Thread_Control    *the_thread,
0089   rtems_event_set    event_in,
0090   Event_Control     *event,
0091   Thread_Wait_flags  wait_class,
0092   ISR_lock_Context  *lock_context
0093 )
0094 {
0095   rtems_event_set pending_events;
0096   rtems_event_set seized_events;
0097   bool            unblock;
0098 
0099   _Thread_Wait_acquire_default_critical( the_thread, lock_context );
0100 
0101   _Event_sets_Post( event_in, &event->pending_events );
0102   pending_events = event->pending_events;
0103 
0104   if (
0105     _Event_Is_blocking_on_event( the_thread, wait_class )
0106       && _Event_Is_satisfied( the_thread, pending_events, &seized_events )
0107   ) {
0108     bool success;
0109 
0110     _Event_Satisfy( the_thread, event, pending_events, seized_events );
0111 
0112     success = _Thread_Wait_flags_try_change_release(
0113       the_thread,
0114       wait_class | THREAD_WAIT_STATE_INTEND_TO_BLOCK,
0115       THREAD_WAIT_STATE_READY
0116     );
0117 
0118     if ( success ) {
0119       unblock = false;
0120     } else {
0121       _Assert(
0122         _Thread_Wait_flags_get( the_thread )
0123           == ( wait_class | THREAD_WAIT_STATE_BLOCKED )
0124       );
0125       _Thread_Wait_flags_set( the_thread, THREAD_WAIT_STATE_READY );
0126       unblock = true;
0127     }
0128   } else {
0129     unblock = false;
0130   }
0131 
0132   if ( unblock ) {
0133     Per_CPU_Control *cpu_self;
0134 
0135     cpu_self = _Thread_Dispatch_disable_critical( lock_context );
0136     _Thread_Wait_release_default( the_thread, lock_context );
0137 
0138     _Thread_Timer_remove( the_thread );
0139     _Thread_Unblock( the_thread );
0140 
0141     _Thread_Dispatch_enable( cpu_self );
0142   } else {
0143     _Thread_Wait_release_default( the_thread, lock_context );
0144   }
0145 
0146   return RTEMS_SUCCESSFUL;
0147 }