Mistake on this page?
Report an issue in GitHub or email us
Task.h
1 /*
2  * Copyright (c) 2018-2019, Arm Limited and affiliates.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef MBED_TASK_H
19 #define MBED_TASK_H
20 
21 #include "events/EventQueue.h"
22 #include "events/TaskBase.h"
23 #include "platform/mbed_assert.h"
24 #include "platform/Callback.h"
25 
26 namespace events {
27 /** \addtogroup events */
28 
29 
30 template<typename F, typename A1 = void, typename A2 = void, typename A3 = void, typename A4 = void, typename A5 = void>
31 struct AllArgs;
32 
33 template<typename B0>
34 struct AllArgs<B0> {
35  typedef AllArgs<B0> Self;
36  B0 b0;
37 
38  AllArgs(B0 b0 = B0()): b0(b0) {}
39 
40  template <typename T, typename _>
41  struct Operations {
42  static void copy(void *_dest, void *_src)
43  {
44  new (_dest) Self(*(Self *)_src);
45  }
46 
47  static void call(void *data)
48  {
49  Self *s = static_cast<Self *>(data);
50  s->b0();
51  s->~Self();
52  }
53  };
54 
55  typedef Operations<B0, void> ops;
56 };
57 
58 template<typename B0, typename B1>
59 struct AllArgs<B0, B1> {
60  typedef AllArgs<B0, B1> Self;
61  B0 b0;
62  B1 b1;
63 
64  AllArgs(B0 b0 = B0(), B1 b1 = B1()): b0(b0), b1(b1) {}
65 
66  template <typename T, typename _>
67  struct Operations {
68  static void copy(void *_dest, void *_src)
69  {
70  new (_dest) Self(*(Self *)_src);
71  }
72 
73  static void call(void *data)
74  {
75  Self *s = static_cast<Self *>(data);
76  s->b0(s->b1);
77  s->~Self();
78  }
79  };
80 
81  template <typename T, typename R, typename U>
82  struct Operations<T *, R(U::*)()> {
83  static void copy(void *_dest, void *_src)
84  {
85  new (_dest) Self(*(Self *)_src);
86  }
87 
88  static void call(void *data)
89  {
90  Self *s = static_cast<Self *>(data);
91  ((s->b0)->*(s->b1))();
92  s->~Self();
93  }
94  };
95 
96  template <typename T, typename R, typename U>
97  struct Operations<T, R(U::*)() const> {
98  static void copy(void *_dest, void *_src)
99  {
100  new (_dest) Self(*(Self *)_src);
101  }
102 
103  static void call(void *data)
104  {
105  Self *s = static_cast<Self *>(data);
106  ((s->b0)->*(s->b1))();
107  s->~Self();
108  }
109  };
110 
111  template <typename T, typename R, typename U>
112  struct Operations<T, R(U::*)() volatile> {
113  static void copy(void *_dest, void *_src)
114  {
115  new (_dest) Self(*(Self *)_src);
116  }
117 
118  static void call(void *data)
119  {
120  Self *s = static_cast<Self *>(data);
121  ((s->b0)->*(s->b1))();
122  s->~Self();
123  }
124  };
125 
126  template <typename T, typename R, typename U>
127  struct Operations<T, R(U::*)() const volatile> {
128  static void copy(void *_dest, void *_src)
129  {
130  new (_dest) Self(*(Self *)_src);
131  }
132 
133  static void call(void *data)
134  {
135  Self *s = static_cast<Self *>(data);
136  ((s->b0)->*(s->b1))();
137  s->~Self();
138  }
139  };
140 
141  typedef Operations<B0, B1> ops;
142 };
143 
144 template<typename B0, typename B1, typename B2>
145 struct AllArgs<B0, B1, B2> {
146  typedef AllArgs<B0, B1, B2> Self;
147  B0 b0;
148  B1 b1;
149  B2 b2;
150 
151 
152  AllArgs(B0 b0 = B0(), B1 b1 = B1(), B2 b2 = B2()): b0(b0), b1(b1), b2(b2) {}
153 
154  template <typename T, typename _>
155  struct Operations {
156  static void copy(void *_dest, void *_src)
157  {
158  new (_dest) Self(*(Self *)_src);
159  }
160 
161  static void call(void *data)
162  {
163  Self *s = static_cast<Self *>(data);
164  s->b0(s->b1, s->b2);
165  s->~Self();
166  }
167  };
168 
169  template <typename T, typename R, typename U>
170  struct Operations<T *, R(U::*)(B2)> {
171  static void copy(void *_dest, void *_src)
172  {
173  new (_dest) Self(*(Self *)_src);
174  }
175 
176  static void call(void *data)
177  {
178  Self *s = static_cast<Self *>(data);
179  ((s->b0)->*(s->b1))(s->b2);
180  s->~Self();
181  }
182  };
183 
184  template <typename T, typename R, typename U>
185  struct Operations<T, R(U::*)(B2) const> {
186  static void copy(void *_dest, void *_src)
187  {
188  new (_dest) Self(*(Self *)_src);
189  }
190 
191  static void call(void *data)
192  {
193  Self *s = static_cast<Self *>(data);
194  ((s->b0)->*(s->b1))(s->b2);
195  s->~Self();
196  }
197  };
198 
199  template <typename T, typename R, typename U>
200  struct Operations<T, R(U::*)(B2) volatile> {
201  static void copy(void *_dest, void *_src)
202  {
203  new (_dest) Self(*(Self *)_src);
204  }
205 
206  static void call(void *data)
207  {
208  Self *s = static_cast<Self *>(data);
209  ((s->b0)->*(s->b1))(s->b2);
210  s->~Self();
211  }
212  };
213 
214  template <typename T, typename R, typename U>
215  struct Operations<T, R(U::*)(B2) const volatile> {
216  static void copy(void *_dest, void *_src)
217  {
218  new (_dest) Self(*(Self *)_src);
219  }
220 
221  static void call(void *data)
222  {
223  Self *s = static_cast<Self *>(data);
224  ((s->b0)->*(s->b1))(s->b2);
225  s->~Self();
226  }
227  };
228 
229  typedef Operations<B0, B1> ops;
230 };
231 
232 template<typename B0, typename B1, typename B2, typename B3>
233 struct AllArgs<B0, B1, B2, B3> {
235  B0 b0;
236  B1 b1;
237  B2 b2;
238  B3 b3;
239 
240 
241  AllArgs(B0 b0 = B0(), B1 b1 = B1(), B2 b2 = B2(), B3 b3 = B3()): b0(b0), b1(b1), b2(b2), b3(b3) {}
242 
243  template <typename T, typename _>
244  struct Operations {
245  static void copy(void *_dest, void *_src)
246  {
247  new (_dest) Self(*(Self *)_src);
248  }
249 
250  static void call(void *data)
251  {
252  Self *s = static_cast<Self *>(data);
253  s->b0(s->b1, s->b2, s->b3);
254  s->~Self();
255  }
256  };
257 
258  template <typename T, typename R, typename U>
259  struct Operations<T *, R(U::*)(B2, B3)> {
260  static void copy(void *_dest, void *_src)
261  {
262  new (_dest) Self(*(Self *)_src);
263  }
264 
265  static void call(void *data)
266  {
267  Self *s = static_cast<Self *>(data);
268  ((s->b0)->*(s->b1))(s->b2, s->b3);
269  s->~Self();
270  }
271  };
272 
273  template <typename T, typename R, typename U>
274  struct Operations<T, R(U::*)(B2, B3) const> {
275  static void copy(void *_dest, void *_src)
276  {
277  new (_dest) Self(*(Self *)_src);
278  }
279 
280  static void call(void *data)
281  {
282  Self *s = static_cast<Self *>(data);
283  ((s->b0)->*(s->b1))(s->b2, s->b3);
284  s->~Self();
285  }
286  };
287 
288  template <typename T, typename R, typename U>
289  struct Operations<T, R(U::*)(B2, B3) volatile> {
290  static void copy(void *_dest, void *_src)
291  {
292  new (_dest) Self(*(Self *)_src);
293  }
294 
295  static void call(void *data)
296  {
297  Self *s = static_cast<Self *>(data);
298  ((s->b0)->*(s->b1))(s->b2, s->b3);
299  s->~Self();
300  }
301  };
302 
303  template <typename T, typename R, typename U>
304  struct Operations<T, R(U::*)(B2, B3) const volatile> {
305  static void copy(void *_dest, void *_src)
306  {
307  new (_dest) Self(*(Self *)_src);
308  }
309 
310  static void call(void *data)
311  {
312  Self *s = static_cast<Self *>(data);
313  ((s->b0)->*(s->b1))(s->b2, s->b3);
314  s->~Self();
315  }
316  };
317 
318  typedef Operations<B0, B1> ops;
319 };
320 
321 template<typename B0, typename B1, typename B2, typename B3, typename B4>
322 struct AllArgs<B0, B1, B2, B3, B4> {
324  B0 b0;
325  B1 b1;
326  B2 b2;
327  B3 b3;
328  B4 b4;
329 
330 
331  AllArgs(B0 b0 = B0(), B1 b1 = B1(), B2 b2 = B2(), B3 b3 = B3(), B4 b4 = B4()): b0(b0), b1(b1), b2(b2), b3(b3), b4(b4) {}
332 
333  template <typename T, typename _>
334  struct Operations {
335  static void copy(void *_dest, void *_src)
336  {
337  new (_dest) Self(*(Self *)_src);
338  }
339 
340  static void call(void *data)
341  {
342  Self *s = static_cast<Self *>(data);
343  s->b0(s->b1, s->b2, s->b3, s->b4);
344  s->~Self();
345  }
346  };
347 
348  template <typename T, typename R, typename U>
349  struct Operations<T *, R(U::*)(B2, B3, B4)> {
350  static void copy(void *_dest, void *_src)
351  {
352  new (_dest) Self(*(Self *)_src);
353  }
354 
355  static void call(void *data)
356  {
357  Self *s = static_cast<Self *>(data);
358  ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4);
359  s->~Self();
360  }
361  };
362 
363  template <typename T, typename R, typename U>
364  struct Operations<T, R(U::*)(B2, B3, B4) const> {
365  static void copy(void *_dest, void *_src)
366  {
367  new (_dest) Self(*(Self *)_src);
368  }
369 
370  static void call(void *data)
371  {
372  Self *s = static_cast<Self *>(data);
373  ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4);
374  s->~Self();
375  }
376  };
377 
378  template <typename T, typename R, typename U>
379  struct Operations<T, R(U::*)(B2, B3, B4) volatile> {
380  static void copy(void *_dest, void *_src)
381  {
382  new (_dest) Self(*(Self *)_src);
383  }
384 
385  static void call(void *data)
386  {
387  Self *s = static_cast<Self *>(data);
388  ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4);
389  s->~Self();
390  }
391  };
392 
393  template <typename T, typename R, typename U>
394  struct Operations<T, R(U::*)(B2, B3, B4) const volatile> {
395  static void copy(void *_dest, void *_src)
396  {
397  new (_dest) Self(*(Self *)_src);
398  }
399 
400  static void call(void *data)
401  {
402  Self *s = static_cast<Self *>(data);
403  ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4);
404  s->~Self();
405  }
406  };
407 
408  typedef Operations<B0, B1> ops;
409 };
410 
411 template<typename B0, typename B1, typename B2, typename B3, typename B4, typename B5>
412 struct AllArgs {
414  B0 b0;
415  B1 b1;
416  B2 b2;
417  B3 b3;
418  B4 b4;
419  B5 b5;
420 
421 
422  AllArgs(B0 b0 = B0(), B1 b1 = B1(), B2 b2 = B2(), B3 b3 = B3(), B4 b4 = B4(), B5 b5 = B5()): b0(b0), b1(b1), b2(b2), b3(b3), b4(b4), b5(b5) {}
423 
424  template <typename T, typename _>
425  struct Operations {
426  static void copy(void *_dest, void *_src)
427  {
428  new (_dest) Self(*(Self *)_src);
429  }
430 
431  static void call(void *data)
432  {
433  Self *s = static_cast<Self *>(data);
434  s->b0(s->b1, s->b2, s->b3, s->b4, s->b5);
435  s->~Self();
436  }
437  };
438 
439  template <typename T, typename R, typename U>
440  struct Operations<T *, R(U::*)(B2, B3, B4, B5)> {
441  static void copy(void *_dest, void *_src)
442  {
443  new (_dest) Self(*(Self *)_src);
444  }
445 
446  static void call(void *data)
447  {
448  Self *s = static_cast<Self *>(data);
449  ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4, s->b5);
450  s->~Self();
451  }
452  };
453 
454  template <typename T, typename R, typename U>
455  struct Operations<T, R(U::*)(B2, B3, B4, B5) const> {
456  static void copy(void *_dest, void *_src)
457  {
458  new (_dest) Self(*(Self *)_src);
459  }
460 
461  static void call(void *data)
462  {
463  Self *s = static_cast<Self *>(data);
464  ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4, s->b5);
465  s->~Self();
466  }
467  };
468 
469  template <typename T, typename R, typename U>
470  struct Operations<T, R(U::*)(B2, B3, B4, B5) volatile> {
471  static void copy(void *_dest, void *_src)
472  {
473  new (_dest) Self(*(Self *)_src);
474  }
475 
476  static void call(void *data)
477  {
478  Self *s = static_cast<Self *>(data);
479  ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4, s->b5);
480  s->~Self();
481  }
482  };
483 
484  template <typename T, typename R, typename U>
485  struct Operations<T, R(U::*)(B2, B3, B4, B5) const volatile> {
486  static void copy(void *_dest, void *_src)
487  {
488  new (_dest) Self(*(Self *)_src);
489  }
490 
491  static void call(void *data)
492  {
493  Self *s = static_cast<Self *>(data);
494  ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4, s->b5);
495  s->~Self();
496  }
497  };
498 
499  typedef Operations<B0, B1> ops;
500 };
501 
502 
503 template <typename F>
504 class Task;
505 
506 template <typename R>
507 class Task<R()>: public TaskBase {
508 public:
509 
510  Task(TaskQueue *q = NULL, mbed::Callback<R()> cb = mbed::Callback<R()>())
511  : TaskBase(q), _args(cb)
512  {
513  }
514 
515  Task &operator=(mbed::Callback<R()> cb)
516  {
517  _args.b0 = cb;
518  return *this;
519  }
520 
521  void call()
522  {
523  post();
524  }
525 
526 protected:
527 
528  virtual uint32_t size()
529  {
530  return sizeof(_args);
531  }
532 
533  virtual run_callback_t start(void *data, uint32_t max_size)
534  {
535  All::ops::copy(data, (void *)&_args);
536  return &All::ops::call;
537  }
538 
539 private:
541  All _args;
542 };
543 
544 template <typename R, typename A0>
545 class Task<R(A0)>: public TaskBase {
546 public:
547 
548  Task(TaskQueue *q = NULL, mbed::Callback<R(A0)> cb = mbed::Callback<R(A0)>())
549  : TaskBase(q), _args(cb)
550  {
551  }
552 
553  Task &operator=(mbed::Callback<R(A0)> cb)
554  {
555  _args.b0 = cb;
556  return *this;
557  }
558 
559  void call(A0 a0)
560  {
561  _args.b1 = a0;
562  post();
563  }
564 
565 protected:
566 
567  virtual uint32_t size()
568  {
569  return sizeof(_args);
570  }
571 
572  virtual run_callback_t start(void *data, uint32_t max_size)
573  {
574  All::ops::copy(data, (void *)&_args);
575  return &All::ops::call;
576  }
577 
578 private:
579  typedef AllArgs<mbed::Callback<R(A0)>, A0> All;
580  All _args;
581 };
582 
583 /** Task
584  *
585  * Representation of a postable task
586  * @ingroup events
587  */
588 template <typename R, typename A0, typename A1>
589 class Task<R(A0, A1)>: public TaskBase {
590 public:
591 
592  /**
593  * Construct a new task
594  *
595  * @param q TaskQueue to post to
596  * @param cb Callback to run
597  */
598  Task(TaskQueue *q = NULL, mbed::Callback<R(A0, A1)> cb = mbed::Callback<R(A0, A1)>())
599  : TaskBase(q), _args(cb)
600  {
601  }
602 
603  /**
604  * Set the callback of this task
605  *
606  * @param cb Callback to run
607  */
608  Task &operator=(mbed::Callback<R(A0, A1)> cb)
609  {
610  _args.b0 = cb;
611  return *this;
612  }
613 
614  /**
615  * Post this task for execution
616  *
617  * The number of arguments to call should match
618  * the type of the callback. For example Task<void(int, int)>
619  * expects two integers as arguments to call, while Task<void()>
620  * expects no arguments.
621  *
622  * @param a0 First callback parameter
623  * @param a1 Second callback parameter
624  */
625  void call(A0 a0, A1 a1)
626  {
627  _args.b1 = a0;
628  _args.b2 = a1;
629  post();
630  }
631 
632 protected:
633 
634  virtual uint32_t size()
635  {
636  return sizeof(_args);
637  }
638 
639  virtual run_callback_t start(void *data, uint32_t max_size)
640  {
641  All::ops::copy(data, (void *)&_args);
642  return &All::ops::call;
643  }
644 
645 private:
646  typedef AllArgs<mbed::Callback<R(A0, A1)>, A0, A1> All;
647  All _args;
648 };
649 
650 }
651 
652 /** @}*/
653 
654 #endif
virtual run_callback_t start(void *data, uint32_t max_size)
Copy any callback data and return a callback to run.
Definition: Task.h:533
TaskBase.
Definition: TaskBase.h:40
void call(A0 a0, A1 a1)
Post this task for execution.
Definition: Task.h:625
Task(TaskQueue *q=NULL, mbed::Callback< R(A0, A1)> cb=mbed::Callback< R(A0, A1)>())
Construct a new task.
Definition: Task.h:598
Task & operator=(mbed::Callback< R(A0, A1)> cb)
Set the callback of this task.
Definition: Task.h:608
virtual run_callback_t start(void *data, uint32_t max_size)
Copy any callback data and return a callback to run.
Definition: Task.h:572
virtual uint32_t size()
Size of buffer required for TaskBase::start.
Definition: Task.h:528
virtual run_callback_t start(void *data, uint32_t max_size)
Copy any callback data and return a callback to run.
Definition: Task.h:639
TaskQueue.
Definition: TaskQueue.h:37
virtual uint32_t size()
Size of buffer required for TaskBase::start.
Definition: Task.h:634
Callback class based on template specialization.
Definition: Callback.h:39
virtual uint32_t size()
Size of buffer required for TaskBase::start.
Definition: Task.h:567
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.