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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/***************************************************************************
* 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 */
/* */
/* sigwait PORTABLE C */
/* 6.1.7 */
/* AUTHOR */
/* */
/* William E. Lamie, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function selects a pending signal from set, atomically */
/* clears it from the system's set of pending signals, and returns the */
/* signal number in the location referenced by sig. */
/* */
/* INPUT */
/* */
/* set Pointer to set of signals */
/* sig Pointer to returned signal number */
/* */
/* OUTPUT */
/* */
/* OK If successful */
/* EINVAL If error occurs */
/* */
/* CALLS */
/* */
/* tx_thread_identify */
/* posix_internal_error */
/* pthread_sigmask */
/* tx_event_flags_get */
/* TX_LOWEST_SET_BIT_CALCULATE */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
int sigwait(const sigset_t *set, int *sig)
{
UINT status;
ULONG signal_bit_map;
ULONG signal_number;
ULONG saved_mask;
ULONG changed_mask;
ULONG pending_signals;
sigset_t original_set;
POSIX_TCB *base_thread;
/* Pickup base thread, since the base thread and all signal threads will pend off the same
event flag group. */
base_thread = (POSIX_TCB *) tx_thread_identify();
/* Is it non-NULL? */
if (!base_thread)
{
/* System error! */
posix_internal_error(444);
return(EINVAL);
}
/* Determine if the current thread is a signal handler thread. */
if (base_thread -> signals.signal_handler)
{
/* Pickup target thread. */
base_thread = base_thread -> signals.base_thread_ptr;
}
/* Initialize the saved and changed mask values to zero. */
saved_mask = 0;
changed_mask = 0;
/* Determine if there are any pending signals that are pertinent to this request. */
pending_signals = base_thread -> signals.signal_mask.signal_set & base_thread -> signals.signal_pending.signal_set & set -> signal_set;
/* Are there any. */
if (pending_signals)
{
/* Yes, there are signals being masked currently that would satisfy this request. */
/* Save the current mask. */
saved_mask = base_thread -> signals.signal_mask.signal_set;
/* Calculate the changed mask. */
changed_mask = saved_mask & ~(set -> signal_set);
/* Call pthread_sigmask to temporarily unblock these signals which will release them as well. */
pthread_sigmask(SIG_UNBLOCK, set, &original_set);
/* Now determine if the changed mask is still in effect, i.e., there wasn't a pthread_sigmask call from any subsequent signal handlers. */
if (base_thread -> signals.signal_mask.signal_set == changed_mask)
{
/* Yes, restore the previous signal mask. */
base_thread -> signals.signal_mask.signal_set = saved_mask;
}
/* Derived the signal number from the bit map. */
TX_LOWEST_SET_BIT_CALCULATE(pending_signals, signal_number);
/* Return the signal number. */
*sig = (int) signal_number;
/* Return success! */
return(OK);
}
/* Determine if there are any signals that have to be temporarily cleared. */
if (base_thread -> signals.signal_mask.signal_set & set -> signal_set)
{
/* Yes, there are signals being masked needed to satisfy this request. */
/* Save the current mask. */
saved_mask = base_thread -> signals.signal_mask.signal_set;
/* Calculate the changed mask. */
changed_mask = saved_mask & ~(set -> signal_set);
/* Apply the changed signal mask. */
base_thread -> signals.signal_mask.signal_set = changed_mask;
}
/* Suspend on the signal specified by the input. */
status = tx_event_flags_get(&(base_thread -> signals.signal_event_flags), (ULONG) set -> signal_set, TX_OR_CLEAR, &signal_bit_map, TX_WAIT_FOREVER);
/* Determine if we need to restore the signal mask. */
if ((saved_mask) && (changed_mask == base_thread -> signals.signal_mask.signal_set))
{
/* Yes, the signal mask should be restored. */
base_thread -> signals.signal_mask.signal_set = saved_mask;
}
/* Check for successful status. */
if (status == TX_SUCCESS)
{
/* Derived the signal number from the bit map. */
TX_LOWEST_SET_BIT_CALCULATE(signal_bit_map, signal_number);
/* Return the signal number. */
*sig = (int) signal_number;
/* Return success! */
return(OK);
}
else
{
/* Return error! */
return(EINVAL);
}
}
|