summaryrefslogtreecommitdiffstats
path: root/cpukit/rtems/src/signalcatch.c
blob: 6924f34428bd3da09520b16bea3a1698282171d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
 * @file
 *
 * @ingroup RTEMSImplClassicSignal
 *
 * @brief This source file contains the implementation of
 *   rtems_signal_catch() and the Signal Manager multiprocessing (MP) support
 *   system initialization.
 */

/*
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/rtems/signalimpl.h>
#include <rtems/rtems/modesimpl.h>
#include <rtems/rtems/tasksdata.h>
#include <rtems/score/threadimpl.h>

RTEMS_STATIC_ASSERT( RTEMS_DEFAULT_MODES == 0, _ASR_Create_mode_set );

rtems_status_code rtems_signal_catch(
  rtems_asr_entry   asr_handler,
  rtems_mode        mode_set
)
{
  Thread_Control     *executing;
  RTEMS_API_Control  *api;
  ASR_Information    *asr;
  ISR_lock_Context    lock_context;

#if defined(RTEMS_SMP) || CPU_ENABLE_ROBUST_THREAD_DISPATCH == TRUE
  if (
    asr_handler != NULL &&
    !_Modes_Is_interrupt_level_supported( mode_set )
  ) {
    return RTEMS_NOT_IMPLEMENTED;
  }
#endif

  executing = _Thread_State_acquire_for_executing( &lock_context );

#if defined(RTEMS_SMP)
  if (
    asr_handler != NULL &&
    !_Modes_Is_preempt_mode_supported( mode_set, executing )
  ) {
    _Thread_State_release( executing, &lock_context );
    return RTEMS_NOT_IMPLEMENTED;
  }
#endif

  api = executing->API_Extensions[ THREAD_API_RTEMS ];
  asr = &api->Signal;
  asr->handler = asr_handler;
  asr->mode_set = mode_set;

#if defined(RTEMS_SMP)
  if ( asr_handler == NULL ) {
    Chain_Node *node;

    /*
     * In SMP configurations, signals may be sent on other processors
     * (interrupts or threads) in parallel.  This will cause an inter-processor
     * interrupt which may be blocked by the above interrupt disable.
     */

    node = &api->Signal_action.Node;
    _Assert( asr->signals_pending == 0 || !_Chain_Is_node_off_chain( node ) );
    _Assert( asr->signals_pending != 0 || _Chain_Is_node_off_chain( node ) );

    if ( !_Chain_Is_node_off_chain( node ) ) {
      asr->signals_pending = 0;
      _Chain_Extract_unprotected( node );
      _Chain_Set_off_chain( node );
    }
  }
#else
  /*
   * In uniprocessor configurations, as soon as interrupts are disabled above
   * nobody can send signals to the executing thread.  So, pending signals at
   * this point cannot appear.
   */
  _Assert( asr->signals_pending == 0 );
  _Assert( _Chain_Is_node_off_chain( &api->Signal_action.Node ) );
#endif

  _Thread_State_release( executing, &lock_context );
  return RTEMS_SUCCESSFUL;
}