summaryrefslogtreecommitdiff
path: root/utility/rtos_compatibility_layers/posix/px_internal_signal_dispatch.c
blob: f23afb608e70c2365f729333e60c10897284e295 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** POSIX wrapper for THREADX                                             */
/**                                                                       */
/**                                                                       */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

/* Include necessary system files.  */

#include "tx_api.h"     /* Threadx API */
#include "pthread.h"    /* Posix API */
#include "px_int.h"     /* Posix helper functions */
#include "tx_thread.h"  /* Internal ThreadX thread management.  */

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    internal_signal_dispatch                            PORTABLE C      */
/*                                                           6.1.7        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*     Signal handler thread.                                             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    id                             Signal                               */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*     none                                                               */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    posix_internal_error                  Generic error Handler         */
/*    tx_thread_identify                                                  */
/*    tx_event_flags_set                                                  */
/*    tx_thread_resume                                                    */
/*    posix_destroy_pthread                                               */
/*    tx_thread_suspend                                                   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Internal Code                                                       */
/*                                                                        */
/**************************************************************************/
void  internal_signal_dispatch(ULONG id)
{

TX_INTERRUPT_SAVE_AREA

POSIX_TCB   *signal_thread;
POSIX_TCB   *target_thread;
VOID        (*handler)(int);


    /* Determine if the desired signal is valid.  */
    if (id > SIGRTMAX)
    {

        /* System error!  */
        posix_internal_error(444);
        return;
    }

    /* Pickup signal thread.  */
    signal_thread =  (POSIX_TCB *) tx_thread_identify();

    /* Is it non-NULL?  */
    if (!signal_thread)
    {

        /* System error!  */
        posix_internal_error(444);
        return;
    }

    /* Pickup target thread.  */
    target_thread =  signal_thread -> signals.base_thread_ptr;

    /* Pickup signal handler function pointer.  */
    handler =  target_thread -> signals.signal_func[id];

    /* See if there is a signal handler setup for this signal.  */
    if (handler)
    {

        /* Yes, there is a signal handler - call it!  */
        (handler)((int) id);
    }

    /* Set the event flag corresponding the signal.  */
    tx_event_flags_set(&(target_thread -> signals.signal_event_flags), (((ULONG) 1) << id), TX_OR);

    /* Ensure the flag is left in a clear state.  */
    tx_event_flags_set(&(target_thread -> signals.signal_event_flags), ~(((ULONG) 1) << id), TX_AND);

    /* Now we need to clear this signal and terminate this signal handler thread.  */

    /* Disable interrupts.  */
    TX_DISABLE

    /* Clear this signal from the pending list.  */
    target_thread -> signals.signal_pending.signal_set =  target_thread -> signals.signal_pending.signal_set & ~(((unsigned long) 1) << id);

    /* Decrement the signal nesting count.  */
    target_thread -> signals.signal_nesting_depth--;

    /* Is this the last nested signal leaving?  */
    if (target_thread -> signals.signal_nesting_depth == 0)
    {

        /* Clear the top signal thread link and resume the target thread.  */
        target_thread -> signals.top_signal_thread =  NULL;

        /* Restore interrupts.  */
        TX_RESTORE

        /* Resume the target thread.  */
        tx_thread_resume((TX_THREAD *) target_thread);
    }
    else
    {

        /* Otherwise, there are more signal threads still active.   */

        /* Setup the new top signal thread pointer.  */
        target_thread -> signals.top_signal_thread =  signal_thread -> signals.next_signal_thread;

        /* Restore interrupts.  */
        TX_RESTORE

        /* Resume the signal handler thread.  */
        tx_thread_resume((TX_THREAD *) signal_thread -> signals.next_signal_thread);
    }

    /* Now we need to mark this signal thread for destruction.  */
    posix_destroy_pthread(signal_thread,(VOID *) 0);

    /* Self-suspend the current signal thread.  */
    tx_thread_suspend((TX_THREAD *) signal_thread);
}