1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 /**************************************************************************/
12 /**************************************************************************/
13 /**                                                                       */
14 /**   ThreadX Component                                                   */
15 /**                                                                       */
16 /**   POSIX Compliancy Wrapper (POSIX)                                    */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 /**************************************************************************/
22 /*                                                                        */
23 /*  EKP DEFINITIONS                                        RELEASE        */
24 /*                                                                        */
25 /*    signal.h                                            PORTABLE C      */
26 /*                                                           6.2.0        */
27 /*  AUTHOR                                                                */
28 /*                                                                        */
29 /*    William E. Lamie, Microsoft Corporation                             */
30 /*                                                                        */
31 /*  DESCRIPTION                                                           */
32 /*                                                                        */
33 /*    This file defines the constants, structures, etc.needed to          */
34 /*    implement signals functionality for POSIX Users (POSIX)             */
35 /*                                                                        */
36 /*                                                                        */
37 /*  RELEASE HISTORY                                                       */
38 /*                                                                        */
39 /*    DATE              NAME                      DESCRIPTION             */
40 /*                                                                        */
41 /*  06-02-2021      William E. Lamie        Initial Version 6.1.7         */
42 /*  10-31-2022      Scott Larson            Update pthread_kill argument  */
43 /*                                            type,                       */
44 /*                                            resulting in version 6.2.0  */
45 /*                                                                        */
46 /**************************************************************************/
47 
48 #ifndef _SIGNAL_H
49 #define _SIGNAL_H
50 
51 
52 /* The POSIX wrapper for ThreadX supports a maximum of 32 signals, from 0
53    through 31, inclusive. In this implemenation, signals are NOT queued.  */
54 
55 
56 /* Define constants for the signal implementation.  */
57 
58 #define MAX_SIGNALS 32
59 #define SIGRTMIN    0
60 #define SIGRTMAX    31
61 #define SIG_DFL     (void *) 0
62 #define SIG_IGN     (void *) 0
63 #define SIG_BLOCK   1
64 #define SIG_SETMASK 2
65 #define SIG_UNBLOCK 3
66 
67 
68 /* Define the typdefs for this signal handling implementation.  */
69 
70 
71 /* Define the type that holds the desired signals.  */
72 
73 typedef struct sigset_t_struct
74 {
75     unsigned long   signal_set;
76 } sigset_t;
77 
78 
79 /* Define the type that keeps track of information in the POSIX thread control block. POSIX threads are used to simulate the behavior
80    of POSIX signals in this implemenation.  */
81 
82 struct pthread_control_block;
83 struct pthread_t_variable;
84 
85 typedef struct signal_info_struct
86 {
87 
88     UINT                            signal_handler;                         /* This is a flag. If TRUE, this thread is being used as a signal handler. If FALSE, it is a regular thread. */
89     UINT                            signal_nesting_depth;                   /* A positive value indicates the level of nested signal handling the POSIX thread is currently processing.  */
90     sigset_t                        signal_pending;                         /* Bit map of signals pending.                                                                               */
91     sigset_t                        signal_mask;                            /* Signal mask, bit blocks the signal until cleared.                                                         */
92     UINT                            saved_thread_state;                     /* Saved ThreadX state of the POSIX thread, at the time of the first signal.                                 */
93     struct  pthread_control_block  *base_thread_ptr;                        /* Pointer to the thread associated with the signal.                                                         */
94     struct  pthread_control_block  *top_signal_thread;                      /* Pointer to the top (most recent) signal thread.                                                           */
95     struct  pthread_control_block  *next_signal_thread;                     /* Pointer to the next most recent signal thread.                                                            */
96     void                            (*signal_func[MAX_SIGNALS])(int);       /* Array of signal handlers for this thread.                                                                 */
97     TX_EVENT_FLAGS_GROUP            signal_event_flags;                     /* ThreadX event flag group used for sigwait                                                                 */
98 
99 } signal_info;
100 
101 
102 /* Define public POSIX routines.  */
103 
104 int   signal(int signo, void (*func)(int));
105 int   pthread_kill(ALIGN_TYPE thread, int sig);
106 int   sigwait(const sigset_t *set, int *sig);
107 int   sigemptyset(sigset_t *set);
108 int   sigaddset(sigset_t *set, int signo);
109 int   sigfillset(sigset_t *set);
110 int   sigdelset(sigset_t *set, int signo);
111 int   pthread_sigmask(int how, const sigset_t *newmask, sigset_t *oldmask);
112 
113 
114 /* Define internal POSIX routines.  */
115 
116 void  internal_signal_dispatch(ULONG id);
117 
118 
119 #endif
120