Xenomai  3.0.5
smokey.h
1 /*
2  * Copyright (C) 2014 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18 #ifndef _XENOMAI_SMOKEY_SMOKEY_H
19 #define _XENOMAI_SMOKEY_SMOKEY_H
20 
21 #include <stdarg.h>
22 #include <pthread.h>
23 #include <boilerplate/list.h>
24 #include <boilerplate/libc.h>
25 #include <copperplate/clockobj.h>
26 #include <xenomai/init.h>
27 
28 #define SMOKEY_INT(__name) { \
29  .name = # __name, \
30  .parser = smokey_int, \
31  .matched = 0, \
32  }
33 
34 #define SMOKEY_BOOL(__name) { \
35  .name = # __name, \
36  .parser = smokey_bool, \
37  .matched = 0, \
38  }
39 
40 #define SMOKEY_STRING(__name) { \
41  .name = # __name, \
42  .parser = smokey_string, \
43  .matched = 0, \
44  }
45 
46 #define SMOKEY_ARGLIST(__args...) ((struct smokey_arg[]){ __args })
47 
48 #define SMOKEY_NOARGS (((struct smokey_arg[]){ { .name = NULL } }))
49 
50 struct smokey_arg {
51  const char *name;
52  int (*parser)(const char *s,
53  struct smokey_arg *arg);
54  union {
55  int n_val;
56  char *s_val;
57  } u;
58  int matched;
59 };
60 
61 struct smokey_test {
62  const char *name;
63  struct smokey_arg *args;
64  int nargs;
65  const char *description;
66  int (*run)(struct smokey_test *t,
67  int argc, char *const argv[]);
68  struct {
69  int id;
70  struct pvholder next;
71  } __reserved;
72 };
73 
74 #define for_each_smokey_test(__pos) \
75  pvlist_for_each_entry((__pos), &smokey_test_list, __reserved.next)
76 
77 #define __smokey_arg_count(__args) \
78  (sizeof(__args) / sizeof(__args[0]))
79 
80 #define smokey_test_plugin(__plugin, __args, __desc) \
81  static int run_ ## __plugin(struct smokey_test *t, \
82  int argc, char *const argv[]); \
83  static struct smokey_test __plugin = { \
84  .name = #__plugin, \
85  .args = (__args), \
86  .nargs = __smokey_arg_count(__args), \
87  .description = (__desc), \
88  .run = run_ ## __plugin, \
89  }; \
90  __early_ctor void smokey_plugin_ ## __plugin(void); \
91  void smokey_plugin_ ## __plugin(void) \
92  { \
93  smokey_register_plugin(&(__plugin)); \
94  }
95 
96 #define SMOKEY_ARG(__plugin, __arg) (smokey_lookup_arg(&(__plugin), # __arg))
97 #define SMOKEY_ARG_ISSET(__plugin, __arg) (SMOKEY_ARG(__plugin, __arg)->matched)
98 #define SMOKEY_ARG_INT(__plugin, __arg) (SMOKEY_ARG(__plugin, __arg)->u.n_val)
99 #define SMOKEY_ARG_BOOL(__plugin, __arg) (!!SMOKEY_ARG_INT(__plugin, __arg))
100 #define SMOKEY_ARG_STRING(__plugin, __arg) (SMOKEY_ARG(__plugin, __arg)->u.s_val)
101 
102 #define smokey_check_errno(__expr) \
103  ({ \
104  int __ret = (__expr); \
105  if (__ret < 0) { \
106  __ret = -errno; \
107  __smokey_warning(__FILE__, __LINE__, "%s: %s", \
108  #__expr, strerror(errno)); \
109  } \
110  __ret; \
111  })
112 
113 #define smokey_check_status(__expr) \
114  ({ \
115  int __ret = (__expr); \
116  if (__ret) { \
117  __smokey_warning(__FILE__, __LINE__, "%s: %s", \
118  #__expr, strerror(__ret)); \
119  __ret = -__ret; \
120  } \
121  __ret; \
122  })
123 
124 #define smokey_assert(__expr) \
125  ({ \
126  int __ret = (__expr); \
127  if (!__ret) \
128  __smokey_warning(__FILE__, __LINE__, \
129  "assertion failed: %s", #__expr); \
130  __ret; \
131  })
132 
133 #define smokey_warning(__fmt, __args...) \
134  __smokey_warning(__FILE__, __LINE__, __fmt, ##__args)
135 
136 #define __T(__ret, __action) \
137  ({ \
138  (__ret) = (__action); \
139  if (__ret) { \
140  if ((__ret) > 0) \
141  (__ret) = -(__ret); \
142  smokey_warning("FAILED: %s (=%s)", \
143  __stringify(__action), \
144  symerror(__ret)); \
145  } \
146  (__ret) == 0; \
147  })
148 
149 #define __F(__ret, __action) \
150  ({ \
151  (__ret) = (__action); \
152  if ((__ret) == 0) \
153  smokey_warning("FAILED: %s (=0)", \
154  __stringify(__action)); \
155  else if ((__ret) > 0) \
156  (__ret) = -(__ret); \
157  (__ret) != 0; \
158  })
159 
160 #define __Terrno(__ret, __action) \
161  ({ \
162  (__ret) = (__action); \
163  if (__ret) { \
164  (__ret) = -errno; \
165  smokey_warning("FAILED: %s (=%s)", \
166  __stringify(__action), \
167  symerror(__ret)); \
168  } \
169  (__ret) == 0; \
170  })
171 
172 #define __Tassert(__expr) \
173  ({ \
174  int __ret = !!(__expr); \
175  if (!__ret) \
176  smokey_warning("FAILED: %s (=false)", \
177  __stringify(__expr)); \
178  __ret; \
179  })
180 
181 #define __Fassert(__expr) \
182  ({ \
183  int __ret = (__expr); \
184  if (__ret) \
185  smokey_warning("FAILED: %s (=true)", \
186  __stringify(__expr)); \
187  !__ret; \
188  })
189 
190 struct smokey_barrier {
191  pthread_mutex_t lock;
192  pthread_cond_t barrier;
193  int signaled;
194 };
195 
196 #ifdef __cplusplus
197 extern "C" {
198 #endif
199 
200 void smokey_register_plugin(struct smokey_test *t);
201 
202 int smokey_int(const char *s, struct smokey_arg *arg);
203 
204 int smokey_bool(const char *s, struct smokey_arg *arg);
205 
206 int smokey_string(const char *s, struct smokey_arg *arg);
207 
208 struct smokey_arg *smokey_lookup_arg(struct smokey_test *t,
209  const char *arg);
210 
211 int smokey_parse_args(struct smokey_test *t,
212  int argc, char *const argv[]);
213 
214 void smokey_vatrace(const char *fmt, va_list ap);
215 
216 void smokey_trace(const char *fmt, ...);
217 
218 void smokey_note(const char *fmt, ...);
219 
220 void __smokey_warning(const char *file, int lineno,
221  const char *fmt, ...);
222 
223 int smokey_barrier_init(struct smokey_barrier *b);
224 
225 void smokey_barrier_destroy(struct smokey_barrier *b);
226 
227 int smokey_barrier_wait(struct smokey_barrier *b);
228 
229 int smokey_barrier_timedwait(struct smokey_barrier *b,
230  struct timespec *ts);
231 
232 void smokey_barrier_release(struct smokey_barrier *b);
233 
234 #ifdef __cplusplus
235 }
236 #endif
237 
238 extern struct pvlistobj smokey_test_list;
239 
240 extern int smokey_keep_going;
241 
242 extern int smokey_verbose_mode;
243 
244 extern int smokey_on_vm;
245 
246 #endif /* _XENOMAI_SMOKEY_SMOKEY_H */