Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSTestSuitesValidation
0007  *
0008  * @brief This source file contains the implementation of
0009  *   SetPreemptionIntervention().
0010  */
0011 
0012 /*
0013  * Copyright (C) 2021 embedded brains GmbH & Co. KG
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifdef HAVE_CONFIG_H
0038 #include "config.h"
0039 #endif
0040 
0041 #include "tx-support.h"
0042 
0043 #include <rtems/sysinit.h>
0044 #include <rtems/score/chainimpl.h>
0045 #include <rtems/score/percpu.h>
0046 #include <rtems/score/threadimpl.h>
0047 
0048 #include <rtems/test.h>
0049 
0050 #if defined(RTEMS_SMP)
0051 typedef struct {
0052   void           ( *handler )( void * );
0053   void             *arg;
0054   Scheduler_Context scheduler_context;
0055   Scheduler_Node    scheduler_node;
0056   Thread_Control    thread;
0057 } PreemptionInterventionContext;
0058 
0059 static PreemptionInterventionContext preemption_intervention_instance;
0060 
0061 static bool PreemptionInterventionAskForHelp(
0062   const Scheduler_Control *scheduler,
0063   Thread_Control          *thread,
0064   Scheduler_Node          *node
0065 )
0066 {
0067   PreemptionInterventionContext *ctx;
0068   void                        ( *handler )( void * );
0069   void                          *arg;
0070 
0071   (void) scheduler;
0072   (void) thread;
0073   (void) node;
0074 
0075   ctx = &preemption_intervention_instance;
0076   handler = ctx->handler;
0077   arg = ctx->arg;
0078   ctx->handler = NULL;
0079   ctx->arg = NULL;
0080   ( *handler )( arg );
0081 
0082   return true;
0083 }
0084 
0085 static const Scheduler_Control preemption_intervention_scheduler = {
0086   .context = &preemption_intervention_instance.scheduler_context,
0087   .Operations = {
0088     .ask_for_help = PreemptionInterventionAskForHelp
0089   }
0090 };
0091 
0092 static void PreemptionInterventionInitialize( void )
0093 {
0094   PreemptionInterventionContext *ctx;
0095 
0096   ctx = &preemption_intervention_instance;
0097 
0098   _Chain_Initialize_node( &ctx->thread.Scheduler.Help_node );
0099   _Thread_queue_Initialize(
0100     &ctx->thread.Join_queue,
0101     "Preemption Intervention"
0102   );
0103   _ISR_lock_Initialize(
0104     &ctx->scheduler_context.Lock,
0105     "Preemption Intervention"
0106   );
0107   _Scheduler_Node_do_initialize(
0108     &preemption_intervention_scheduler,
0109     &ctx->scheduler_node,
0110     &ctx->thread,
0111     0
0112   );
0113   _Chain_Initialize_one(
0114     &ctx->thread.Scheduler.Scheduler_nodes,
0115     &ctx->scheduler_node.Thread.Scheduler_node.Chain
0116   );
0117 }
0118 
0119 RTEMS_SYSINIT_ITEM(
0120   PreemptionInterventionInitialize,
0121   RTEMS_SYSINIT_DEVICE_DRIVERS,
0122   RTEMS_SYSINIT_ORDER_MIDDLE
0123 );
0124 #endif
0125 
0126 void SetPreemptionIntervention(
0127   struct Per_CPU_Control *cpu,
0128   void                 ( *handler )( void * ),
0129   void                   *arg
0130 )
0131 {
0132 #if defined(RTEMS_SMP)
0133   PreemptionInterventionContext *ctx;
0134   rtems_interrupt_level          level;
0135   ISR_lock_Context               lock_context;
0136 
0137   ctx = &preemption_intervention_instance;
0138   T_quiet_assert_null( ctx->handler );
0139   ctx->handler = handler;
0140   ctx->arg = arg;
0141 
0142   rtems_interrupt_local_disable( level );
0143   _Per_CPU_Acquire( cpu, &lock_context );
0144   _Chain_Append_unprotected(
0145     &cpu->Threads_in_need_for_help,
0146     &ctx->thread.Scheduler.Help_node
0147   );
0148   _Per_CPU_Release( cpu, &lock_context );
0149   rtems_interrupt_local_enable( level );
0150 #else
0151   (void) cpu;
0152   (void) handler;
0153   (void) arg;
0154 #endif
0155 }