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 <drv/timer.h>
50 #include <struct/pool.h>
51 #include <struct/list.h>
53 #include <kern/proc.h>
54 #include <kern/signal.h>
57 #define NEW_TASK SIG_USER0
59 // TODO: Mixing static and dynamic tests in kernel must be tested with care,
60 // until then use this workaround
62 #define PROC_NEW() proc_new(rtask_proc, NULL, CONFIG_RTASK_STACK, NULL)
64 PROC_DEFINE_STACK(rtask_stack, CONFIG_RTASK_STACK);
65 #define PROC_NEW() proc_new(rtask_proc, NULL, sizeof(rtask_stack), rtask_stack)
76 DECLARE_POOL_STATIC(rtask_pool, RTask, CONFIG_RTASK_POOL_SIZE);
77 static Process *process = NULL;
79 static Semaphore rtask_sem;
80 #define RTASK_ATOMIC(code) \
82 sem_obtain(&rtask_sem); \
84 sem_release(&rtask_sem); \
87 #warning "Remove synctimer_poll and use a list directly"
88 static NORETURN void rtask_proc(void)
94 empty = LIST_EMPTY(&rt_list);
101 RTASK_ATOMIC(delay = synctimer_nextTimeout(&rt_list));
102 timer_delayTicks(delay);
103 RTASK_ATOMIC(synctimer_poll(&rt_list));
107 static void rtask_trampoline(void *_rtask)
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);
115 pool_free(&rtask_pool, rtask);
118 RTask *rtask_add(rtask_cb_t cb, mtime_t delay, void *cb_data)
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.
124 // The semaphore is not yet initialized, disable preemption
127 if (UNLIKELY(process == NULL))
132 pool_init(rtask_pool, NULL);
133 sem_init(&rtask_sem);
134 process = PROC_NEW();
140 RTASK_ATOMIC(rt = (RTask *)pool_alloc(&rtask_pool));
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);
151 LOG_ERR("Failed to allocate RTask\n");