Silen some warning.
[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 DECLARE_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 #warning "Remove synctimer_poll and use a list directly"
90 static NORETURN void rtask_proc(void)
91 {
92         while (1)
93         {
94                 bool empty;
95                 RTASK_ATOMIC(
96                         empty = LIST_EMPTY(&rt_list);
97                         sig_check(NEW_TASK);
98                 );
99                 if (empty)
100                         sig_wait(NEW_TASK);
101
102                 ticks_t delay;
103                 RTASK_ATOMIC(delay = synctimer_nextTimeout(&rt_list));
104                 timer_delayTicks(delay);
105                 RTASK_ATOMIC(synctimer_poll(&rt_list));
106         }
107 }
108
109 static void rtask_trampoline(void *_rtask)
110 {
111         // Access the pool and the list freely since this callback is called
112         // with the semaphore held in rtask_proc.
113         RTask *rtask = _rtask;
114         if (rtask->callback(rtask->user_data))
115                 synctimer_readd(&rtask->t, &rt_list);
116         else
117                 pool_free(&rtask_pool, rtask);
118 }
119
120 RTask *rtask_add(rtask_cb_t cb, mtime_t delay, void *cb_data)
121 {
122         // Beware: this function is called from a different process
123         // than rtask_proc, so each access to rtask_pool and rt_list
124         // must be protected with a semaphore.
125
126         // The semaphore is not yet initialized, disable preemption
127         // altogether.
128         proc_forbid();
129         if (UNLIKELY(process == NULL))
130         {
131                 MOD_CHECK(proc);
132
133                 LIST_INIT(&rt_list);
134                 pool_init(rtask_pool, NULL);
135                 sem_init(&rtask_sem);
136                 process = PROC_NEW();
137                 ASSERT(process);
138         }
139         proc_permit();
140
141         RTask *rt = NULL;
142         RTASK_ATOMIC(rt = (RTask *)pool_alloc(&rtask_pool));
143         if (rt)
144         {
145                 rt->callback = cb;
146                 rt->user_data = cb_data;
147                 timer_setSoftint(&rt->t, rtask_trampoline, rt);
148                 timer_setDelay(&rt->t, delay);
149                 RTASK_ATOMIC(synctimer_add(&rt->t, &rt_list));
150                 sig_send(process, NEW_TASK);
151         }
152         else
153                 LOG_ERR("Failed to allocate RTask\n");
154         return rt;
155 }