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
/*
 * Copyright (C) 2014 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.
 */
#ifndef _BOILERPLATE_LIBC_H
#define _BOILERPLATE_LIBC_H
 
#include <limits.h>
 
#ifdef __IN_XENO__
/*
 * Quirks for dealing with outdated libc* issues.  This header will be
 * parsed by the Xenomai implementation only, applications based on it
 * have to provide their own set of wrappers as they should decide by
 * themselves what to do when a feature is missing.
 */
#include <xeno_config.h>
#include <errno.h>
#include <boilerplate/compiler.h>
 
#if !HAVE_DECL_PTHREAD_PRIO_NONE
enum {
   PTHREAD_PRIO_NONE,
   PTHREAD_PRIO_INHERIT,
   PTHREAD_PRIO_PROTECT
};
#endif /* !HAVE_DECL_PTHREAD_PRIO_NONE */
 
#ifndef HAVE_FORK
static inline int fork(void)
{
   errno = ENOSYS;
   return -1;
}
#endif
 
#ifndef HAVE_PTHREAD_ATFORK
#ifndef HAVE_FORK
static inline
int pthread_atfork(void (*prepare)(void), void (*parent)(void),
          void (*child)(void))
{
   return 0;
}
#else
#error "fork() without pthread_atfork()"
#endif
#endif /* !HAVE_PTHREAD_ATFORK */
 
#ifndef HAVE_PTHREAD_GETATTR_NP
static inline
int pthread_getattr_np(pthread_t th, pthread_attr_t *attr)
{
   return ENOSYS;
}
#endif /* !HAVE_PTHREAD_GETATTR_NP */
 
#ifndef HAVE_PTHREAD_CONDATTR_SETCLOCK
static inline
int pthread_condattr_setclock(pthread_condattr_t *__restrict__ attr,
                 clockid_t clock_id)
{
   return clock_id == CLOCK_REALTIME ? 0 : ENOSYS;
}
#endif    /* !HAVE_PTHREAD_CONDATTR_SETCLOCK */
 
#ifndef HAVE_PTHREAD_CONDATTR_GETCLOCK
static inline
int pthread_condattr_getclock(const pthread_condattr_t *__restrict__ attr,
                 clockid_t *__restrict__ clock_id)
{
   *clock_id = CLOCK_REALTIME;
 
   return 0;
}
#endif    /* !HAVE_PTHREAD_CONDATTR_GETCLOCK */
 
#ifndef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL
static inline
int pthread_mutexattr_setprotocol(pthread_mutexattr_t *__restrict__ attr,
                 int protocol)
{
   return protocol == PTHREAD_PRIO_NONE ? 0 : ENOSYS;
}
#endif /* !HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL */
 
#ifndef HAVE_PTHREAD_MUTEXATTR_GETPROTOCOL
static inline
int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *
                 __restrict__ attr, int *__restrict__ protocol)
{
   *protocol = PTHREAD_PRIO_NONE;
 
   return 0;
}
#endif /* !HAVE_PTHREAD_MUTEXATTR_GETPROTOCOL */
 
#ifndef HAVE_PTHREAD_MUTEXATTR_SETPRIOCEILING
static inline
int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr,
                    int prioceiling)
{
   return ENOSYS;
}
#endif /* !HAVE_PTHREAD_MUTEXATTR_SETPRIOCEILING */
 
#ifndef HAVE_PTHREAD_MUTEXATTR_GETPRIOCEILING
static inline
int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *
                     __restrict attr,
                    int *__restrict prioceiling)
{
   return ENOSYS;
}
#endif /* !HAVE_PTHREAD_MUTEXATTR_GETPRIOCEILING */
 
#ifndef HAVE_PTHREAD_MUTEX_SETPRIOCEILING
static inline
int pthread_mutex_setprioceiling(pthread_mutex_t *__restrict attr,
                int prioceiling,
                int *__restrict old_ceiling)
{
   return ENOSYS;
}
#endif /* !HAVE_PTHREAD_MUTEXATTR_SETPRIOCEILING */
 
#ifndef HAVE_PTHREAD_MUTEX_GETPRIOCEILING
static inline
int pthread_mutex_getprioceiling(pthread_mutex_t *__restrict attr,
                int *__restrict prioceiling)
{
   return ENOSYS;
}
#endif /* !HAVE_PTHREAD_MUTEXATTR_GETPRIOCEILING */
 
#ifndef HAVE_PTHREAD_ATTR_SETAFFINITY_NP
#include <sched.h>
static inline
int pthread_attr_setaffinity_np(pthread_attr_t *attr,
               size_t cpusetsize, const cpu_set_t *cpuset)
{
   if (CPU_ISSET(0, cpuset) && CPU_COUNT(cpuset) == 1)
       return 0;
   return ENOSYS;
}
#endif /* !HAVE_PTHREAD_ATTR_SETAFFINITY_NP */
 
#ifndef HAVE_PTHREAD_SETAFFINITY_NP
static inline
int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
              const cpu_set_t *cpuset)
{
   if (CPU_ISSET(0, cpuset) && CPU_COUNT(cpuset) == 1)
       return 0;
   return ENOSYS;
}
#endif /* !HAVE_PTHREAD_SETAFFINITY_NP */
 
#ifndef HAVE_PTHREAD_SETSCHEDPRIO
 
static inline
int pthread_setschedprio(pthread_t thread, int prio)
{
   struct sched_param param;
   int policy, ret;
 
   ret = pthread_getschedparam(thread, &policy, &param);
   if (ret)
       return ret;
 
   param.sched_priority = prio;
 
   return pthread_setschedparam(thread, policy, &param);
}
 
#endif /* !HAVE_PTHREAD_SETSCHEDPRIO */
 
#if !defined(HAVE_CLOCK_NANOSLEEP) && defined(CONFIG_XENO_MERCURY)
/*
 * Best effort for a Mercury setup based on an outdated libc lacking
 * "advanced" real-time support.  Too bad if the system clock is set
 * during sleep time. This is a non-issue for Cobalt, as the libcobalt
 * implementation will always be picked instead.
 */
__weak int clock_nanosleep(clockid_t clock_id, int flags,
              const struct timespec *request,
              struct timespec *remain)
{
   struct timespec now, tmp;
 
   tmp = *request;
   if (flags) {
       clock_gettime(CLOCK_REALTIME, &now);
       tmp.tv_sec -= now.tv_sec;
       tmp.tv_nsec -= now.tv_nsec;
       if (tmp.tv_nsec < 0) {
           tmp.tv_sec--;
           tmp.tv_nsec += 1000000000;
       }
   }
 
   return nanosleep(&tmp, remain);
}
#endif /* !HAVE_CLOCK_NANOSLEEP && MERCURY */
 
#ifndef HAVE_SCHED_GETCPU
/*
 * Might be declared in uClibc headers but not actually implemented,
 * so we make the placeholder a weak symbol.
 */
__weak int sched_getcpu(void)
{
   return 0;   /* outdated uClibc: assume uniprocessor. */
}
#endif /* !HAVE_SCHED_GETCPU */
 
#ifndef HAVE_SHM_OPEN
__weak int shm_open(const char *name, int oflag, mode_t mode)
{
   errno = ENOSYS;
   return -1;
}
#endif    /* !HAVE_SHM_OPEN */
 
#ifndef HAVE_SHM_UNLINK
__weak int shm_unlink(const char *name)
{
   errno = ENOSYS;
   return -1;
}
#endif    /* !HAVE_SHM_UNLINK */
 
#ifndef HAVE_PTHREAD_MUTEXATTR_SETROBUST
#ifdef HAVE_PTHREAD_MUTEXATTR_SETROBUST_NP
#define pthread_mutexattr_setrobust    pthread_mutexattr_setrobust_np
#else
static inline
int pthread_mutexattr_setrobust(pthread_mutexattr_t *attr,
               int robustness)
{
   return ENOSYS;
}
#endif /* !HAVE_PTHREAD_MUTEXATTR_SETROBUST_NP */
#endif /* !HAVE_PTHREAD_MUTEXATTR_SETROBUST */
 
#if !defined(HAVE_PTHREAD_SETNAME_NP) && defined(CONFIG_XENO_MERCURY)
static inline
int pthread_setname_np(pthread_t thread, const char *name)
{
   return ENOSYS;
}
#endif /* !HAVE_PTHREAD_SETNAME_NP && MERCURY */
 
#endif /* __IN_XENO__ */
 
#if defined(__COBALT_WRAP__) || defined(__IN_XENO__)
/*
 * clock_nanosleep() and pthread_setname_np() must be declared when the libc
 * does not declare them, both for compiling xenomai, and for compiling
 * applications wrapping these symbols to the libcobalt versions.
 */
#ifndef HAVE_CLOCK_NANOSLEEP
int clock_nanosleep(clockid_t clock_id, int flags,
           const struct timespec *request,
           struct timespec *remain);
#endif /* !HAVE_CLOCK_NANOSLEEP */
 
#ifndef HAVE_PTHREAD_SETNAME_NP
int pthread_setname_np(pthread_t thread, const char *name);
#endif /* !HAVE_PTHREAD_SETNAME_NP */
#endif /* __COBALT_WRAP__ || __IN_XENO__ */
 
#ifndef PTHREAD_STACK_DEFAULT
#define PTHREAD_STACK_DEFAULT            \
   ({                    \
       int __ret = PTHREAD_STACK_MIN;    \
       if (__ret < 65536)        \
           __ret = 65536;        \
       __ret;                \
   })
#endif /* !PTHREAD_STACK_DEFAULT */
 
#endif /* _BOILERPLATE_LIBC_H */