hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright (C) 2011 Philippe Gerum <rpm@xenomai.org>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include <sys/types.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <limits.h>
#include <boilerplate/ancillaries.h>
#include <copperplate/clockobj.h>
#include <copperplate/threadobj.h>
#include "internal.h"
 
static int thread_spawn_prologue(struct corethread_attributes *cta);
 
static int thread_spawn_epilogue(struct corethread_attributes *cta);
 
static void *thread_trampoline(void *arg);
 
#ifdef CONFIG_XENO_COBALT
 
#include "cobalt/internal.h"
 
int copperplate_create_thread(struct corethread_attributes *cta,
                 pthread_t *ptid_r)
{
   pthread_attr_ex_t attr_ex;
   size_t stacksize;
   int ret;
 
   ret = thread_spawn_prologue(cta);
   if (ret)
       return __bt(ret);
 
   stacksize = cta->stacksize;
   if (stacksize < PTHREAD_STACK_DEFAULT)
       stacksize = PTHREAD_STACK_DEFAULT;
 
   pthread_attr_init_ex(&attr_ex);
   pthread_attr_setinheritsched_ex(&attr_ex, PTHREAD_INHERIT_SCHED);
   pthread_attr_setstacksize_ex(&attr_ex, stacksize);
   pthread_attr_setdetachstate_ex(&attr_ex, cta->detachstate);
   ret = -pthread_create_ex(ptid_r, &attr_ex, thread_trampoline, cta);
   pthread_attr_destroy_ex(&attr_ex);
   if (ret)
       return __bt(ret);
 
   return __bt(thread_spawn_epilogue(cta));
}
 
int copperplate_renice_local_thread(pthread_t ptid, int policy,
                   const struct sched_param_ex *param_ex)
{
   return -pthread_setschedparam_ex(ptid, policy, param_ex);
}
 
static inline void prepare_wait_corespec(void)
{
   /*
    * Switch back to primary mode eagerly, so that both the
    * parent and the child threads compete on the same priority
    * scale when handshaking. In addition, this ensures the child
    * thread enters the run() handler over the Xenomai domain,
    * which is a basic assumption for all clients.
    */
   cobalt_thread_harden();
}
 
int copperplate_kill_tid(pid_t tid, int sig)
{
   return __RT(kill(tid, sig)) ? -errno : 0;
}
 
int copperplate_probe_tid(pid_t tid)
{
   return cobalt_thread_probe(tid);
}
 
void copperplate_set_current_name(const char *name)
{
   __RT(pthread_setname_np(pthread_self(), name));
}
 
#else /* CONFIG_XENO_MERCURY */
 
int copperplate_kill_tid(pid_t tid, int sig)
{
   return syscall(__NR_tkill, tid, sig) ? -errno : 0;
}
 
int copperplate_probe_tid(pid_t tid)
{
   return copperplate_kill_tid(tid, 0) && errno != EPERM ? -errno : 0;
}
 
void copperplate_set_current_name(const char *name)
{
   prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);
}
 
int copperplate_create_thread(struct corethread_attributes *cta,
                 pthread_t *ptid_r)
{
   pthread_attr_t attr;
   size_t stacksize;
   int ret;
 
   ret = thread_spawn_prologue(cta);
   if (ret)
       return __bt(ret);
 
   stacksize = cta->stacksize;
   if (stacksize < PTHREAD_STACK_DEFAULT)
       stacksize = PTHREAD_STACK_DEFAULT;
 
   pthread_attr_init(&attr);
   pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
   pthread_attr_setstacksize(&attr, stacksize);
   pthread_attr_setdetachstate(&attr, cta->detachstate);
   ret = -pthread_create(ptid_r, &attr, thread_trampoline, cta);
   pthread_attr_destroy(&attr);
 
   if (ret)
       return __bt(ret);
 
   return __bt(thread_spawn_epilogue(cta));
}
 
int copperplate_renice_local_thread(pthread_t ptid, int policy,
                   const struct sched_param_ex *param_ex)
{
   struct sched_param param = {
       .sched_priority = param_ex->sched_priority,
   };
 
   return -__RT(pthread_setschedparam(ptid, policy, &param));
}
 
static inline void prepare_wait_corespec(void)
{
   /* empty */
}
 
#endif  /* CONFIG_XENO_MERCURY */
 
int copperplate_get_current_name(char *name, size_t maxlen)
{
   if (maxlen < 16)
       return -ENOSPC;
 
   return prctl(PR_GET_NAME, (unsigned long)name, 0, 0, 0);
}
 
static int thread_spawn_prologue(struct corethread_attributes *cta)
{
   int ret;
 
   ret = __RT(sem_init(&cta->__reserved.warm, 0, 0));
   if (ret)
       return __bt(-errno);
 
   cta->__reserved.status = -ENOSYS;
 
   return 0;
}
 
static void thread_spawn_wait(sem_t *sem)
{
   int ret;
 
   for (;;) {
       ret = __RT(sem_wait(sem));
       if (ret && errno == EINTR)
           continue;
       if (ret == 0)
           return;
       ret = -errno;
       panic("sem_wait() failed with %s", symerror(ret));
   }
}
 
static void *thread_trampoline(void *arg)
{
   struct corethread_attributes *cta = arg, _cta;
   sem_t released;
   int ret;
 
   /*
    * cta may be on the parent's stack, so it may be dandling
    * soon after the parent is posted: copy this argument
    * structure early on.
    */
   _cta = *cta;
 
   ret = __RT(sem_init(&released, 0, 0));
   if (ret) {
       ret = __bt(-errno);
       cta->__reserved.status = ret;
       warning("lack of resources for core thread, %s", symerror(ret));
       goto fail;
   }
 
   cta->__reserved.released = &released;
   ret = cta->prologue(cta->arg);
   cta->__reserved.status = ret;
   if (ret) {
       __RT(sem_destroy(&released));
       backtrace_check();
       goto fail;
   }
 
   /*
    * CAUTION: Once the prologue handler has run successfully,
    * the client code may assume that we can't fail spawning the
    * child thread anymore, which guarantees that
    * copperplate_create_thread() will return a success code
    * after this point. This is important so that any thread
    * finalizer installed by the prologue handler won't conflict
    * with the cleanup code the client may run whenever
    * copperplate_create_thread() fails.
    */
   ret = __bt(copperplate_renice_local_thread(pthread_self(),
              _cta.policy, &_cta.param_ex));
   if (ret)
       warning("cannot renice core thread, %s", symerror(ret));
   prepare_wait_corespec();
   __RT(sem_post(&cta->__reserved.warm));
   thread_spawn_wait(&released);
   __RT(sem_destroy(&released));
 
   return _cta.run(_cta.arg);
fail:
   __RT(sem_post(&cta->__reserved.warm));
 
   return (void *)(long)ret;
}
 
static int thread_spawn_epilogue(struct corethread_attributes *cta)
{
   prepare_wait_corespec();
   thread_spawn_wait(&cta->__reserved.warm);
 
   if (cta->__reserved.status == 0)
       __RT(sem_post(cta->__reserved.released));
 
   __RT(sem_destroy(&cta->__reserved.warm));
 
   return __bt(cta->__reserved.status);
}
 
void __panic(const char *fn, const char *fmt, ...)
{
   struct threadobj *thobj = threadobj_current();
   va_list ap;
 
   va_start(ap, fmt);
   ___panic(fn, thobj ? threadobj_get_name(thobj) : NULL, fmt, ap);
}
 
void warning(const char *fmt, ...)
{
   struct threadobj *thobj = threadobj_current();
   va_list ap;
 
   va_start(ap, fmt);
   __warning(thobj ? threadobj_get_name(thobj) : NULL, fmt, ap);
   va_end(ap);
}
 
void notice(const char *fmt, ...)
{
   struct threadobj *thobj = threadobj_current();
   va_list ap;
 
   va_start(ap, fmt);
   __notice(thobj ? threadobj_get_name(thobj) : NULL, fmt, ap);
   va_end(ap);
}