4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief Recurrent task module implementation.
35 * \author Luca Ottaviano <lottaviano@develer.com>
36 * \author Francesco Sacchi <batt@develer.com>
41 #include "cfg/cfg_rtask.h"
42 #include <cfg/module.h> // MOD_CHECK
44 #define LOG_LEVEL RTASK_LOG_LEVEL
45 #define LOG_FORMAT RTASK_LOG_FORMAT
48 #include <cpu/types.h>
50 #include <drv/timer.h>
52 #include <struct/pool.h>
53 #include <struct/list.h>
55 #include <kern/proc.h>
56 #include <kern/signal.h>
59 #define NEW_TASK SIG_USER0
61 // TODO: Mixing static and dynamic tests in kernel must be tested with care,
62 // until then use this workaround
64 #define PROC_NEW() proc_new(rtask_proc, NULL, CONFIG_RTASK_STACK, NULL)
66 PROC_DEFINE_STACK(rtask_stack, CONFIG_RTASK_STACK);
67 #define PROC_NEW() proc_new(rtask_proc, NULL, sizeof(rtask_stack), rtask_stack)
78 DEFINE_POOL_STATIC(rtask_pool, RTask, CONFIG_RTASK_POOL_SIZE);
79 static Process *process = NULL;
81 static Semaphore rtask_sem;
82 #define RTASK_ATOMIC(code) \
84 sem_obtain(&rtask_sem); \
86 sem_release(&rtask_sem); \
89 #warning "Remove synctimer_poll and use a list directly"
90 static NORETURN void rtask_proc(void)
96 empty = LIST_EMPTY(&rt_list);
103 RTASK_ATOMIC(delay = synctimer_nextTimeout(&rt_list));
104 timer_delayTicks(delay);
105 RTASK_ATOMIC(synctimer_poll(&rt_list));
109 static void rtask_trampoline(void *_rtask)
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);
117 pool_free(&rtask_pool, rtask);
120 RTask *rtask_add(rtask_cb_t cb, mtime_t delay, void *cb_data)
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.
126 // The semaphore is not yet initialized, disable preemption
129 if (UNLIKELY(process == NULL))
134 pool_init(rtask_pool, NULL);
135 sem_init(&rtask_sem);
136 process = PROC_NEW();
142 RTASK_ATOMIC(rt = (RTask *)pool_alloc(&rtask_pool));
146 rt->user_data = cb_data;
147 timer_setSoftint(&rt->t, rtask_trampoline, rt);
148 timer_setDelay(&rt->t, ms_to_ticks(delay));
149 RTASK_ATOMIC(synctimer_add(&rt->t, &rt_list));
150 sig_send(process, NEW_TASK);
153 LOG_ERR("Failed to allocate RTask\n");