Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Task.h Source File

Task.h

00001 /*
00002  * Copyright (c) 2018-2019, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef MBED_TASK_H
00019 #define MBED_TASK_H
00020 
00021 #include "events/EventQueue.h"
00022 #include "drivers/internal/TaskBase.h"
00023 #include "platform/mbed_assert.h"
00024 #include "platform/Callback.h"
00025 
00026 namespace events {
00027 /** \addtogroup drivers-internal-api-usb
00028  * @{
00029  */
00030 
00031 
00032 template<typename F, typename A1 = void, typename A2 = void, typename A3 = void, typename A4 = void, typename A5 = void>
00033 struct AllArgs;
00034 
00035 template<typename B0>
00036 struct AllArgs<B0> {
00037     typedef AllArgs<B0> Self;
00038     B0 b0;
00039 
00040     AllArgs(B0 b0 = B0()): b0(b0) {}
00041 
00042     template <typename T, typename _>
00043     struct Operations {
00044         static void copy(void *_dest, void *_src)
00045         {
00046             new (_dest) Self(*(Self *)_src);
00047         }
00048 
00049         static void call(void *data)
00050         {
00051             Self *s = static_cast<Self *>(data);
00052             s->b0();
00053             s->~Self();
00054         }
00055     };
00056 
00057     typedef Operations<B0, void> ops;
00058 };
00059 
00060 template<typename B0, typename B1>
00061 struct AllArgs<B0, B1> {
00062     typedef AllArgs<B0, B1> Self;
00063     B0 b0;
00064     B1 b1;
00065 
00066     AllArgs(B0 b0 = B0(), B1 b1 = B1()): b0(b0), b1(b1) {}
00067 
00068     template <typename T, typename _>
00069     struct Operations {
00070         static void copy(void *_dest, void *_src)
00071         {
00072             new (_dest) Self(*(Self *)_src);
00073         }
00074 
00075         static void call(void *data)
00076         {
00077             Self *s = static_cast<Self *>(data);
00078             s->b0(s->b1);
00079             s->~Self();
00080         }
00081     };
00082 
00083     template <typename T, typename R, typename U>
00084     struct Operations<T *, R(U::*)()> {
00085         static void copy(void *_dest, void *_src)
00086         {
00087             new (_dest) Self(*(Self *)_src);
00088         }
00089 
00090         static void call(void *data)
00091         {
00092             Self *s = static_cast<Self *>(data);
00093             ((s->b0)->*(s->b1))();
00094             s->~Self();
00095         }
00096     };
00097 
00098     template <typename T, typename R, typename U>
00099     struct Operations<T, R(U::*)() const> {
00100         static void copy(void *_dest, void *_src)
00101         {
00102             new (_dest) Self(*(Self *)_src);
00103         }
00104 
00105         static void call(void *data)
00106         {
00107             Self *s = static_cast<Self *>(data);
00108             ((s->b0)->*(s->b1))();
00109             s->~Self();
00110         }
00111     };
00112 
00113     template <typename T, typename R, typename U>
00114     struct Operations<T, R(U::*)() volatile> {
00115         static void copy(void *_dest, void *_src)
00116         {
00117             new (_dest) Self(*(Self *)_src);
00118         }
00119 
00120         static void call(void *data)
00121         {
00122             Self *s = static_cast<Self *>(data);
00123             ((s->b0)->*(s->b1))();
00124             s->~Self();
00125         }
00126     };
00127 
00128     template <typename T, typename R, typename U>
00129     struct Operations<T, R(U::*)() const volatile> {
00130         static void copy(void *_dest, void *_src)
00131         {
00132             new (_dest) Self(*(Self *)_src);
00133         }
00134 
00135         static void call(void *data)
00136         {
00137             Self *s = static_cast<Self *>(data);
00138             ((s->b0)->*(s->b1))();
00139             s->~Self();
00140         }
00141     };
00142 
00143     typedef Operations<B0, B1> ops;
00144 };
00145 
00146 template<typename B0, typename B1, typename B2>
00147 struct AllArgs<B0, B1, B2> {
00148     typedef AllArgs<B0, B1, B2> Self;
00149     B0 b0;
00150     B1 b1;
00151     B2 b2;
00152 
00153 
00154     AllArgs(B0 b0 = B0(), B1 b1 = B1(), B2 b2 = B2()): b0(b0), b1(b1), b2(b2) {}
00155 
00156     template <typename T, typename _>
00157     struct Operations {
00158         static void copy(void *_dest, void *_src)
00159         {
00160             new (_dest) Self(*(Self *)_src);
00161         }
00162 
00163         static void call(void *data)
00164         {
00165             Self *s = static_cast<Self *>(data);
00166             s->b0(s->b1, s->b2);
00167             s->~Self();
00168         }
00169     };
00170 
00171     template <typename T, typename R, typename U>
00172     struct Operations<T *, R(U::*)(B2)> {
00173         static void copy(void *_dest, void *_src)
00174         {
00175             new (_dest) Self(*(Self *)_src);
00176         }
00177 
00178         static void call(void *data)
00179         {
00180             Self *s = static_cast<Self *>(data);
00181             ((s->b0)->*(s->b1))(s->b2);
00182             s->~Self();
00183         }
00184     };
00185 
00186     template <typename T, typename R, typename U>
00187     struct Operations<T, R(U::*)(B2) const> {
00188         static void copy(void *_dest, void *_src)
00189         {
00190             new (_dest) Self(*(Self *)_src);
00191         }
00192 
00193         static void call(void *data)
00194         {
00195             Self *s = static_cast<Self *>(data);
00196             ((s->b0)->*(s->b1))(s->b2);
00197             s->~Self();
00198         }
00199     };
00200 
00201     template <typename T, typename R, typename U>
00202     struct Operations<T, R(U::*)(B2) volatile> {
00203         static void copy(void *_dest, void *_src)
00204         {
00205             new (_dest) Self(*(Self *)_src);
00206         }
00207 
00208         static void call(void *data)
00209         {
00210             Self *s = static_cast<Self *>(data);
00211             ((s->b0)->*(s->b1))(s->b2);
00212             s->~Self();
00213         }
00214     };
00215 
00216     template <typename T, typename R, typename U>
00217     struct Operations<T, R(U::*)(B2) const volatile> {
00218         static void copy(void *_dest, void *_src)
00219         {
00220             new (_dest) Self(*(Self *)_src);
00221         }
00222 
00223         static void call(void *data)
00224         {
00225             Self *s = static_cast<Self *>(data);
00226             ((s->b0)->*(s->b1))(s->b2);
00227             s->~Self();
00228         }
00229     };
00230 
00231     typedef Operations<B0, B1> ops;
00232 };
00233 
00234 template<typename B0, typename B1, typename B2, typename B3>
00235 struct AllArgs<B0, B1, B2, B3> {
00236     typedef AllArgs<B0, B1, B2, B3> Self;
00237     B0 b0;
00238     B1 b1;
00239     B2 b2;
00240     B3 b3;
00241 
00242 
00243     AllArgs(B0 b0 = B0(), B1 b1 = B1(), B2 b2 = B2(), B3 b3 = B3()): b0(b0), b1(b1), b2(b2), b3(b3) {}
00244 
00245     template <typename T, typename _>
00246     struct Operations {
00247         static void copy(void *_dest, void *_src)
00248         {
00249             new (_dest) Self(*(Self *)_src);
00250         }
00251 
00252         static void call(void *data)
00253         {
00254             Self *s = static_cast<Self *>(data);
00255             s->b0(s->b1, s->b2, s->b3);
00256             s->~Self();
00257         }
00258     };
00259 
00260     template <typename T, typename R, typename U>
00261     struct Operations<T *, R(U::*)(B2, B3)> {
00262         static void copy(void *_dest, void *_src)
00263         {
00264             new (_dest) Self(*(Self *)_src);
00265         }
00266 
00267         static void call(void *data)
00268         {
00269             Self *s = static_cast<Self *>(data);
00270             ((s->b0)->*(s->b1))(s->b2, s->b3);
00271             s->~Self();
00272         }
00273     };
00274 
00275     template <typename T, typename R, typename U>
00276     struct Operations<T, R(U::*)(B2, B3) const> {
00277         static void copy(void *_dest, void *_src)
00278         {
00279             new (_dest) Self(*(Self *)_src);
00280         }
00281 
00282         static void call(void *data)
00283         {
00284             Self *s = static_cast<Self *>(data);
00285             ((s->b0)->*(s->b1))(s->b2, s->b3);
00286             s->~Self();
00287         }
00288     };
00289 
00290     template <typename T, typename R, typename U>
00291     struct Operations<T, R(U::*)(B2, B3) volatile> {
00292         static void copy(void *_dest, void *_src)
00293         {
00294             new (_dest) Self(*(Self *)_src);
00295         }
00296 
00297         static void call(void *data)
00298         {
00299             Self *s = static_cast<Self *>(data);
00300             ((s->b0)->*(s->b1))(s->b2, s->b3);
00301             s->~Self();
00302         }
00303     };
00304 
00305     template <typename T, typename R, typename U>
00306     struct Operations<T, R(U::*)(B2, B3) const volatile> {
00307         static void copy(void *_dest, void *_src)
00308         {
00309             new (_dest) Self(*(Self *)_src);
00310         }
00311 
00312         static void call(void *data)
00313         {
00314             Self *s = static_cast<Self *>(data);
00315             ((s->b0)->*(s->b1))(s->b2, s->b3);
00316             s->~Self();
00317         }
00318     };
00319 
00320     typedef Operations<B0, B1> ops;
00321 };
00322 
00323 template<typename B0, typename B1, typename B2, typename B3, typename B4>
00324 struct AllArgs<B0, B1, B2, B3, B4> {
00325     typedef AllArgs<B0, B1, B2, B3, B4> Self;
00326     B0 b0;
00327     B1 b1;
00328     B2 b2;
00329     B3 b3;
00330     B4 b4;
00331 
00332 
00333     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) {}
00334 
00335     template <typename T, typename _>
00336     struct Operations {
00337         static void copy(void *_dest, void *_src)
00338         {
00339             new (_dest) Self(*(Self *)_src);
00340         }
00341 
00342         static void call(void *data)
00343         {
00344             Self *s = static_cast<Self *>(data);
00345             s->b0(s->b1, s->b2, s->b3, s->b4);
00346             s->~Self();
00347         }
00348     };
00349 
00350     template <typename T, typename R, typename U>
00351     struct Operations<T *, R(U::*)(B2, B3, B4)> {
00352         static void copy(void *_dest, void *_src)
00353         {
00354             new (_dest) Self(*(Self *)_src);
00355         }
00356 
00357         static void call(void *data)
00358         {
00359             Self *s = static_cast<Self *>(data);
00360             ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4);
00361             s->~Self();
00362         }
00363     };
00364 
00365     template <typename T, typename R, typename U>
00366     struct Operations<T, R(U::*)(B2, B3, B4) const> {
00367         static void copy(void *_dest, void *_src)
00368         {
00369             new (_dest) Self(*(Self *)_src);
00370         }
00371 
00372         static void call(void *data)
00373         {
00374             Self *s = static_cast<Self *>(data);
00375             ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4);
00376             s->~Self();
00377         }
00378     };
00379 
00380     template <typename T, typename R, typename U>
00381     struct Operations<T, R(U::*)(B2, B3, B4) volatile> {
00382         static void copy(void *_dest, void *_src)
00383         {
00384             new (_dest) Self(*(Self *)_src);
00385         }
00386 
00387         static void call(void *data)
00388         {
00389             Self *s = static_cast<Self *>(data);
00390             ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4);
00391             s->~Self();
00392         }
00393     };
00394 
00395     template <typename T, typename R, typename U>
00396     struct Operations<T, R(U::*)(B2, B3, B4) const volatile> {
00397         static void copy(void *_dest, void *_src)
00398         {
00399             new (_dest) Self(*(Self *)_src);
00400         }
00401 
00402         static void call(void *data)
00403         {
00404             Self *s = static_cast<Self *>(data);
00405             ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4);
00406             s->~Self();
00407         }
00408     };
00409 
00410     typedef Operations<B0, B1> ops;
00411 };
00412 
00413 template<typename B0, typename B1, typename B2, typename B3, typename B4, typename B5>
00414 struct AllArgs {
00415     typedef AllArgs<B0, B1, B2, B3, B4, B5> Self;
00416     B0 b0;
00417     B1 b1;
00418     B2 b2;
00419     B3 b3;
00420     B4 b4;
00421     B5 b5;
00422 
00423 
00424     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) {}
00425 
00426     template <typename T, typename _>
00427     struct Operations {
00428         static void copy(void *_dest, void *_src)
00429         {
00430             new (_dest) Self(*(Self *)_src);
00431         }
00432 
00433         static void call(void *data)
00434         {
00435             Self *s = static_cast<Self *>(data);
00436             s->b0(s->b1, s->b2, s->b3, s->b4, s->b5);
00437             s->~Self();
00438         }
00439     };
00440 
00441     template <typename T, typename R, typename U>
00442     struct Operations<T *, R(U::*)(B2, B3, B4, B5)> {
00443         static void copy(void *_dest, void *_src)
00444         {
00445             new (_dest) Self(*(Self *)_src);
00446         }
00447 
00448         static void call(void *data)
00449         {
00450             Self *s = static_cast<Self *>(data);
00451             ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4, s->b5);
00452             s->~Self();
00453         }
00454     };
00455 
00456     template <typename T, typename R, typename U>
00457     struct Operations<T, R(U::*)(B2, B3, B4, B5) const> {
00458         static void copy(void *_dest, void *_src)
00459         {
00460             new (_dest) Self(*(Self *)_src);
00461         }
00462 
00463         static void call(void *data)
00464         {
00465             Self *s = static_cast<Self *>(data);
00466             ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4, s->b5);
00467             s->~Self();
00468         }
00469     };
00470 
00471     template <typename T, typename R, typename U>
00472     struct Operations<T, R(U::*)(B2, B3, B4, B5) volatile> {
00473         static void copy(void *_dest, void *_src)
00474         {
00475             new (_dest) Self(*(Self *)_src);
00476         }
00477 
00478         static void call(void *data)
00479         {
00480             Self *s = static_cast<Self *>(data);
00481             ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4, s->b5);
00482             s->~Self();
00483         }
00484     };
00485 
00486     template <typename T, typename R, typename U>
00487     struct Operations<T, R(U::*)(B2, B3, B4, B5) const volatile> {
00488         static void copy(void *_dest, void *_src)
00489         {
00490             new (_dest) Self(*(Self *)_src);
00491         }
00492 
00493         static void call(void *data)
00494         {
00495             Self *s = static_cast<Self *>(data);
00496             ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4, s->b5);
00497             s->~Self();
00498         }
00499     };
00500 
00501     typedef Operations<B0, B1> ops;
00502 };
00503 
00504 
00505 template <typename F>
00506 class Task;
00507 
00508 template <typename R>
00509 class Task<R()>: public TaskBase {
00510 public:
00511 
00512     Task(TaskQueue *q = NULL, mbed::Callback<R()> cb = mbed::Callback<R()>())
00513         : TaskBase(q), _args(cb)
00514     {
00515     }
00516 
00517     Task &operator=(mbed::Callback<R()> cb)
00518     {
00519         _args.b0 = cb;
00520         return *this;
00521     }
00522 
00523     void call()
00524     {
00525         post();
00526     }
00527 
00528 protected:
00529 
00530     virtual uint32_t size()
00531     {
00532         return sizeof(_args);
00533     }
00534 
00535     virtual run_callback_t start(void *data, uint32_t max_size)
00536     {
00537         All::ops::copy(data, (void *)&_args);
00538         return &All::ops::call;
00539     }
00540 
00541 private:
00542     typedef AllArgs<mbed::Callback<R()> > All;
00543     All _args;
00544 };
00545 
00546 template <typename R, typename A0>
00547 class Task<R(A0)>: public TaskBase {
00548 public:
00549 
00550     Task(TaskQueue *q = NULL, mbed::Callback<R(A0)> cb = mbed::Callback<R(A0)>())
00551         : TaskBase(q), _args(cb)
00552     {
00553     }
00554 
00555     Task &operator=(mbed::Callback<R(A0)> cb)
00556     {
00557         _args.b0 = cb;
00558         return *this;
00559     }
00560 
00561     void call(A0 a0)
00562     {
00563         _args.b1 = a0;
00564         post();
00565     }
00566 
00567 protected:
00568 
00569     virtual uint32_t size()
00570     {
00571         return sizeof(_args);
00572     }
00573 
00574     virtual run_callback_t start(void *data, uint32_t max_size)
00575     {
00576         All::ops::copy(data, (void *)&_args);
00577         return &All::ops::call;
00578     }
00579 
00580 private:
00581     typedef AllArgs<mbed::Callback<R(A0)>, A0> All;
00582     All _args;
00583 };
00584 
00585 /** Task
00586  *
00587  *  Representation of a postable task
00588  */
00589 template <typename R, typename A0, typename A1>
00590 class Task<R(A0, A1)>: public TaskBase {
00591 public:
00592 
00593     /**
00594      * Construct a new task
00595      *
00596      * @param q TaskQueue to post to
00597      * @param cb Callback to run
00598      */
00599     Task(TaskQueue *q = NULL, mbed::Callback<R(A0, A1)> cb = mbed::Callback<R(A0, A1)>())
00600         : TaskBase(q), _args(cb)
00601     {
00602     }
00603 
00604     /**
00605      * Set the callback of this task
00606      *
00607      * @param cb Callback to run
00608      */
00609     Task &operator=(mbed::Callback<R(A0, A1)> cb)
00610     {
00611         _args.b0 = cb;
00612         return *this;
00613     }
00614 
00615     /**
00616      * Post this task for execution
00617      *
00618      * The number of arguments to call should match
00619      * the type of the callback. For example Task<void(int, int)>
00620      * expects two integers as arguments to call, while Task<void()>
00621      * expects no arguments.
00622      *
00623      * @param a0 First callback parameter
00624      * @param a1 Second callback parameter
00625      */
00626     void call(A0 a0, A1 a1)
00627     {
00628         _args.b1 = a0;
00629         _args.b2 = a1;
00630         post();
00631     }
00632 
00633 protected:
00634 
00635     virtual uint32_t size()
00636     {
00637         return sizeof(_args);
00638     }
00639 
00640     virtual run_callback_t start(void *data, uint32_t max_size)
00641     {
00642         All::ops::copy(data, (void *)&_args);
00643         return &All::ops::call;
00644     }
00645 
00646 private:
00647     typedef AllArgs<mbed::Callback<R(A0, A1)>, A0, A1> All;
00648     All _args;
00649 };
00650 
00651 /** @}*/
00652 
00653 }
00654 
00655 #endif