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_Seize() and the Event Manager multiprocessing (MP) support system
0010  *   initialization.
0011  */
0012 
0013 /*
0014  *  COPYRIGHT (c) 1989-2008.
0015  *  On-Line Applications Research Corporation (OAR).
0016  *
0017  * Redistribution and use in source and binary forms, with or without
0018  * modification, are permitted provided that the following conditions
0019  * are met:
0020  * 1. Redistributions of source code must retain the above copyright
0021  *    notice, this list of conditions and the following disclaimer.
0022  * 2. Redistributions in binary form must reproduce the above copyright
0023  *    notice, this list of conditions and the following disclaimer in the
0024  *    documentation and/or other materials provided with the distribution.
0025  *
0026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0036  * POSSIBILITY OF SUCH DAMAGE.
0037  */
0038 
0039 #ifdef HAVE_CONFIG_H
0040 #include "config.h"
0041 #endif
0042 
0043 #include <rtems/sysinit.h>
0044 #include <rtems/rtems/eventimpl.h>
0045 #include <rtems/rtems/optionsimpl.h>
0046 #include <rtems/rtems/statusimpl.h>
0047 #include <rtems/score/threadimpl.h>
0048 #include <rtems/score/watchdogimpl.h>
0049 
0050 rtems_status_code _Event_Seize(
0051   rtems_event_set    event_in,
0052   rtems_option       option_set,
0053   rtems_interval     ticks,
0054   rtems_event_set   *event_out,
0055   Thread_Control    *executing,
0056   Event_Control     *event,
0057   Thread_Wait_flags  wait_class,
0058   States_Control     block_state,
0059   ISR_lock_Context  *lock_context
0060 )
0061 {
0062   rtems_event_set    seized_events;
0063   rtems_event_set    pending_events;
0064   bool               success;
0065   Thread_Wait_flags  intend_to_block;
0066   Per_CPU_Control   *cpu_self;
0067 
0068   pending_events = event->pending_events;
0069   seized_events  = _Event_sets_Get( pending_events, event_in );
0070 
0071   if ( !_Event_sets_Is_empty( seized_events ) &&
0072        (seized_events == event_in || _Options_Is_any( option_set )) ) {
0073     event->pending_events =
0074       _Event_sets_Clear( pending_events, seized_events );
0075     _Thread_Wait_release_default( executing, lock_context );
0076     *event_out = seized_events;
0077     return RTEMS_SUCCESSFUL;
0078   }
0079 
0080   if ( _Options_Is_no_wait( option_set ) ) {
0081     _Thread_Wait_release_default( executing, lock_context );
0082     *event_out = seized_events;
0083     return RTEMS_UNSATISFIED;
0084   }
0085 
0086   intend_to_block = wait_class | THREAD_WAIT_STATE_INTEND_TO_BLOCK;
0087 
0088   /*
0089    *  Note what we are waiting for BEFORE we enter the critical section.
0090    *  The interrupt critical section management code needs this to be
0091    *  set properly when we are marked as in the event critical section.
0092    *
0093    *  NOTE: Since interrupts are disabled, this isn't that much of an
0094    *        issue but better safe than sorry.
0095    */
0096   executing->Wait.return_code     = STATUS_SUCCESSFUL;
0097   executing->Wait.option          = option_set;
0098   executing->Wait.count           = event_in;
0099   executing->Wait.return_argument = event_out;
0100   _Thread_Wait_flags_set( executing, intend_to_block );
0101 
0102   cpu_self = _Thread_Dispatch_disable_critical( lock_context );
0103   _Thread_Wait_release_default( executing, lock_context );
0104 
0105   if ( ticks ) {
0106     _Thread_Add_timeout_ticks( executing, cpu_self, ticks );
0107   }
0108 
0109   _Thread_Set_state( executing, block_state );
0110 
0111   success = _Thread_Wait_flags_try_change_acquire(
0112     executing,
0113     intend_to_block,
0114     wait_class | THREAD_WAIT_STATE_BLOCKED
0115   );
0116   if ( !success ) {
0117     _Thread_Timer_remove( executing );
0118     _Thread_Unblock( executing );
0119   }
0120 
0121   _Thread_Dispatch_direct( cpu_self );
0122   return _Status_Get_after_wait( executing );
0123 }
0124 
0125 #if defined(RTEMS_MULTIPROCESSING)
0126 static void _Event_MP_Initialize( void )
0127 {
0128   _MPCI_Register_packet_processor( MP_PACKET_EVENT, _Event_MP_Process_packet );
0129 }
0130 
0131 RTEMS_SYSINIT_ITEM(
0132   _Event_MP_Initialize,
0133   RTEMS_SYSINIT_CLASSIC_EVENT_MP,
0134   RTEMS_SYSINIT_ORDER_MIDDLE
0135 );
0136 #endif