Refactor to use new protocol module and sipo.
[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 <cpu/types.h>
49
50 #include <drv/timer.h>
51
52 #include <struct/pool.h>
53 #include <struct/list.h>
54
55 #include <kern/proc.h>
56 #include <kern/signal.h>
57 #include <kern/sem.h>
58
59 #define NEW_TASK SIG_USER0
60
61 // TODO: Mixing static and dynamic tests in kernel must be tested with care,
62 // until then use this workaround
63 #if CONFIG_KERN_HEAP
64         #define PROC_NEW() proc_new(rtask_proc, NULL, CONFIG_RTASK_STACK, NULL)
65 #else
66         PROC_DEFINE_STACK(rtask_stack, CONFIG_RTASK_STACK);
67         #define PROC_NEW() proc_new(rtask_proc, NULL, sizeof(rtask_stack), rtask_stack)
68 #endif
69
70
71 struct RTask
72 {
73         Timer t;
74         rtask_cb_t callback;
75         void *user_data;
76 };
77
78 DEFINE_POOL_STATIC(rtask_pool, RTask, CONFIG_RTASK_POOL_SIZE);
79 static Process *process = NULL;
80 static List rt_list;
81 static Semaphore rtask_sem;
82 #define RTASK_ATOMIC(code) \
83         do {                         \
84                 sem_obtain(&rtask_sem);  \
85                 code;                    \
86                 sem_release(&rtask_sem); \
87         } while (0)
88
89
90 //TODO: "Remove synctimer_poll and use a list directly"
91 static NORETURN void rtask_proc(void)
92 {
93         while (1)
94         {
95                 bool empty;
96                 RTASK_ATOMIC(
97                         empty = LIST_EMPTY(&rt_list);
98                         sig_check(NEW_TASK);
99                 );
100                 if (empty)
101                         sig_wait(NEW_TASK);
102
103                 ticks_t delay;
104                 RTASK_ATOMIC(delay = synctimer_nextTimeout(&rt_list));
105                 timer_delayTicks(delay);
106                 RTASK_ATOMIC(synctimer_poll(&rt_list));
107         }
108 }
109
110 static void rtask_trampoline(void *_rtask)
111 {
112         // Access the pool and the list freely since this callback is called
113         // with the semaphore held in rtask_proc.
114         RTask *rtask = _rtask;
115         if (rtask->callback(rtask->user_data))
116                 synctimer_readd(&rtask->t, &rt_list);
117         else
118                 pool_free(&rtask_pool, rtask);
119 }
120
121 RTask *rtask_add(rtask_cb_t cb, mtime_t delay, void *cb_data)
122 {
123         // Beware: this function is called from a different process
124         // than rtask_proc, so each access to rtask_pool and rt_list
125         // must be protected with a semaphore.
126
127         // The semaphore is not yet initialized, disable preemption
128         // altogether.
129         proc_forbid();
130         if (UNLIKELY(process == NULL))
131         {
132                 MOD_CHECK(proc);
133
134                 LIST_INIT(&rt_list);
135                 pool_init(rtask_pool, NULL);
136                 sem_init(&rtask_sem);
137                 process = PROC_NEW();
138                 ASSERT(process);
139         }
140         proc_permit();
141
142         RTask *rt = NULL;
143         RTASK_ATOMIC(rt = (RTask *)pool_alloc(&rtask_pool));
144         if (rt)
145         {
146                 rt->callback = cb;
147                 rt->user_data = cb_data;
148                 timer_setSoftint(&rt->t, rtask_trampoline, rt);
149                 timer_setDelay(&rt->t, ms_to_ticks(delay));
150                 RTASK_ATOMIC(synctimer_add(&rt->t, &rt_list));
151                 sig_send(process, NEW_TASK);
152         }
153         else
154                 LOG_ERR("Failed to allocate RTask\n");
155         return rt;
156 }