blob: 8957f0aedc33b689fde58888b2314daec52eaca0 (
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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
/***************************************************************************
* 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 */
/* */
/* pthread_sigmask PORTABLE C */
/* 6.2.0 */
/* AUTHOR */
/* */
/* William E. Lamie, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function shall examine or change (or both) the calling */
/* thread's signal mask. */
/* */
/* INPUT */
/* */
/* how how the set will be changed */
/* newmask pointer to new set of signals */
/* oldmask pointer to store previous signals */
/* */
/* OUTPUT */
/* */
/* OK If successful */
/* EINVAL If error occurs */
/* */
/* CALLS */
/* */
/* posix_set_pthread_errno */
/* tx_thread_identify */
/* pthread_kill */
/* _tx_thread_system_preempt_check */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
int pthread_sigmask(int how, const sigset_t *newmask, sigset_t *oldmask)
{
TX_INTERRUPT_SAVE_AREA
ULONG blocked_signals;
ULONG released_signals;
ULONG signal_number;
ULONG previous_mask;
POSIX_TCB *base_thread;
ULONG reissue_flag;
/* Check for a valid how parameter. */
if ((how != SIG_BLOCK) && (how != SIG_SETMASK) & (how != SIG_UNBLOCK))
{
/* Return an error. */
posix_set_pthread_errno(EINVAL);
return(EINVAL);
}
/* Check for valid signal masks. */
if ((newmask == NULL) || (oldmask == NULL))
{
/* Return an error. */
posix_set_pthread_errno(EINVAL);
return(EINVAL);
}
/* 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_set_pthread_errno(ESRCH);
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;
}
/* Save the current signal mask for return. */
previous_mask = base_thread -> signals.signal_mask.signal_set;
/* Now process based on how the mask is to be changed. */
if (how == SIG_BLOCK)
{
/* Simply set the mask to block the signal(s). */
base_thread -> signals.signal_mask.signal_set = base_thread -> signals.signal_mask.signal_set | newmask -> signal_set;
}
else
{
/* Now calculate the set of currently pending signals there are waiting based on the current mask. */
blocked_signals = base_thread -> signals.signal_mask.signal_set & base_thread -> signals.signal_pending.signal_set;
/* Now modify the singal mask correspondingly. */
if (how == SIG_UNBLOCK)
{
/* Clear only the signals specified in the new signal mask. */
base_thread -> signals.signal_mask.signal_set = base_thread -> signals.signal_mask.signal_set & ~(newmask -> signal_set);
}
else
{
/* Simply set the signal mask to the new signal mask value. */
base_thread -> signals.signal_mask.signal_set = newmask -> signal_set;
}
/* Now determine if there are any signals that need to be activated. */
released_signals = blocked_signals & ~(base_thread -> signals.signal_mask.signal_set);
/* Are there any signals that need to be activated? */
if (released_signals)
{
/* Temporarily disable interrupts. */
TX_DISABLE
/* Temporarily disable preemption. */
_tx_thread_preempt_disable++;
/* Restore interrupts. */
TX_RESTORE
/* Set the reissue flag to false. */
reissue_flag = TX_FALSE;
/* Loop to process all the blocked signals. */
signal_number = 0;
while ((released_signals) && (signal_number < 32))
{
/* Determine if this signal was released. */
if (released_signals & 1)
{
/* Yes, this signal was released. We need to make it active again. */
/* Clear the pending bit so the pthread_kill call will not discard the signal (signals are not queued in this implementation). */
base_thread -> signals.signal_pending.signal_set = base_thread -> signals.signal_pending.signal_set & ~(((unsigned long) 1) << signal_number);
/* Call pthread_kill to reissue the signal. */
pthread_kill((ALIGN_TYPE) base_thread, signal_number);
/* Set the reissue flag. */
reissue_flag = TX_TRUE;
}
/* Look for next signal. */
released_signals = released_signals >> 1;
signal_number++;
}
/* Temporarily disable interrupts. */
TX_DISABLE
/* Release preemption. */
_tx_thread_preempt_disable--;
/* Restore interrupts. */
TX_RESTORE
/* Check for a preemption condition. */
_tx_thread_system_preempt_check();
/* Determine if the reissue flag is set. */
if (reissue_flag == TX_TRUE)
{
/* Relinquish to allow signal thread at same priority to run before we return. */
_tx_thread_relinquish();
}
}
}
/* Setup return mask. */
oldmask -> signal_set = previous_mask;
return(OK);
}
|