Add rtask module.
[bertos.git] / bertos / kern / rtask.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Recurrent task module implementation.
34  *
35  * \author Luca Ottaviano <lottaviano@develer.com>
36  * \author Francesco Sacchi <batt@develer.com>
37  *
38  */
39
40 #include "rtask.h"
41 #include "cfg/cfg_rtask.h"
42 #include <cfg/module.h> // MOD_CHECK
43
44 #define LOG_LEVEL RTASK_LOG_LEVEL
45 #define LOG_FORMAT RTASK_LOG_FORMAT
46 #include <cfg/log.h>
47
48 #include <drv/timer.h>
49
50 #include <struct/pool.h>
51 #include <struct/list.h>
52
53 #include <kern/proc.h>
54 #include <kern/signal.h>
55 #include <kern/sem.h>
56
57 #define NEW_TASK SIG_USER0
58
59 // TODO: Mixing static and dynamic tests in kernel must be tested with care,
60 // until then use this workaround
61 #if CONFIG_KERN_HEAP
62         #define PROC_NEW() proc_new(rtask_proc, NULL, CONFIG_RTASK_STACK, NULL)
63 #else
64         PROC_DEFINE_STACK(rtask_stack, CONFIG_RTASK_STACK);
65         #define PROC_NEW() proc_new(rtask_proc, NULL, sizeof(rtask_stack), rtask_stack)
66 #endif
67
68
69 struct RTask
70 {
71         Timer t;
72         rtask_cb_t callback;
73         void *user_data;
74 };
75
76 DECLARE_POOL_STATIC(rtask_pool, RTask, CONFIG_RTASK_POOL_SIZE);
77 static Process *process = NULL;
78 static List rt_list;
79 static Semaphore rtask_sem;
80 #define RTASK_ATOMIC(code) \
81         do {                         \
82                 sem_obtain(&rtask_sem);  \
83                 code;                    \
84                 sem_release(&rtask_sem); \
85         } while (0)
86
87 #warning "Remove synctimer_poll and use a list directly"
88 static NORETURN void rtask_proc(void)
89 {
90         while (1)
91         {
92                 bool empty;
93                 RTASK_ATOMIC(
94                         empty = LIST_EMPTY(&rt_list);
95                         sig_check(NEW_TASK);
96                 );
97                 if (empty)
98                         sig_wait(NEW_TASK);
99
100                 ticks_t delay;
101                 RTASK_ATOMIC(delay = synctimer_nextTimeout(&rt_list));
102                 timer_delayTicks(delay);
103                 RTASK_ATOMIC(synctimer_poll(&rt_list));
104         }
105 }
106
107 static void rtask_trampoline(void *_rtask)
108 {
109         // Access the pool and the list freely since this callback is called
110         // with the semaphore held in rtask_proc.
111         RTask *rtask = _rtask;
112         if (rtask->callback(rtask->user_data))
113                 synctimer_readd(&rtask->t, &rt_list);
114         else
115                 pool_free(&rtask_pool, rtask);
116 }
117
118 RTask *rtask_add(rtask_cb_t cb, mtime_t delay, void *cb_data)
119 {
120         // Beware: this function is called from a different process
121         // than rtask_proc, so each access to rtask_pool and rt_list
122         // must be protected with a semaphore.
123
124         // The semaphore is not yet initialized, disable preemption
125         // altogether.
126         proc_forbid();
127         if (UNLIKELY(process == NULL))
128         {
129                 MOD_CHECK(proc);
130
131                 LIST_INIT(&rt_list);
132                 pool_init(rtask_pool, NULL);
133                 sem_init(&rtask_sem);
134                 process = PROC_NEW();
135                 ASSERT(process);
136         }
137         proc_permit();
138
139         RTask *rt = NULL;
140         RTASK_ATOMIC(rt = (RTask *)pool_alloc(&rtask_pool));
141         if (rt)
142         {
143                 rt->callback = cb;
144                 rt->user_data = cb_data;
145                 timer_setSoftint(&rt->t, rtask_trampoline, rt);
146                 timer_setDelay(&rt->t, delay);
147                 RTASK_ATOMIC(synctimer_add(&rt->t, &rt_list));
148                 sig_send(process, NEW_TASK);
149         }
150         else
151                 LOG_ERR("Failed to allocate RTask\n");
152         return rt;
153 }