BRISCITS  0.1
RISC "Real-Time" Scheduler
brisc_thread.h
Go to the documentation of this file.
1 /******************************************************************************
2  ________ ________ ___ ________ ________ ___ _________ ________
3 |\ __ \|\ __ \|\ \|\ ____\|\ ____\|\ \|\___ ___\\ ____\
4 \ \ \|\ /\ \ \|\ \ \ \ \ \___|\ \ \___|\ \ \|___ \ \_\ \ \___|_
5  \ \ __ \ \ _ _\ \ \ \_____ \ \ \ \ \ \ \ \ \ \ \_____ \
6  \ \ \|\ \ \ \\ \\ \ \|____|\ \ \ \____\ \ \ \ \ \ \|____|\ \
7  \ \_______\ \__\\ _\\ \__\____\_\ \ \_______\ \__\ \ \__\ ____\_\ \
8  \|_______|\|__|\|__|\|__|\_________\|_______|\|__| \|__| |\_________\
9  \|_________| \|_________|
10 
11 MIT License
12 
13 Copyright (c) 2021 Mike Sharkey
14 
15 Permission is hereby granted, free of charge, to any person obtaining a copy
16 of this software and associated documentation files (the "Software"), to deal
17 in the Software without restriction, including without limitation the rights
18 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 copies of the Software, and to permit persons to whom the Software is
20 furnished to do so, subject to the following conditions:
21 
22 The above copyright notice and this permission notice shall be included in all
23 copies or substantial portions of the Software.
24 
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 SOFTWARE.
32 
33 ******************************************************************************/
34 #ifndef _BRISC_THREAD_H_
35 #define _BRISC_THREAD_H_
36 
37 #include <cpu.h>
38 #include <stdint.h>
39 #include <stddef.h>
40 #include <stdbool.h>
41 
42 #ifdef __cplusplus
43 extern "C"
44 {
45 #endif
46 
147 #ifndef BRISC_THREAD_MAX
148  #define BRISC_THREAD_MAX 8
149 #endif
150 
151 #ifndef BRISC_THREAD_NAME_MAX
152  #define BRISC_THREAD_NAME_MAX 8
153 #endif
154 
155 #define BRISC_THREAD_PRIO_INVALID (-1)
156 #define BRISC_THREAD_PRIO_SUSPEND (0)
157 #define BRISC_THREAD_PRIO_MIN (1)
158 #define BRISC_THREAD_PRIO_MAX (127)
160 #define brisc_systick_t uint32_t
161 
162 typedef struct brisc_thread
163 {
165  int8_t prio;
166  cpu_state_t* cpu_state;
167 } brisc_thread_t __attribute__ ((aligned (8)));
168 
169 #define b_int_enabled() cpu_int_enabled()
170 #define b_int_enable() cpu_int_enable()
171 #define b_int_disable() cpu_int_disable()
172 #define b_int_set(s) cpu_int_set((s))
174 #define b_atomic_acquire(s) cpu_atomic_acquire((s))
175 #define b_atomic_release(s) cpu_atomic_release((s))
177 #define b_wfi() cpu_wfi();
183 #define b_thread_block_while(cond) while((cond)) b_thread_yield()
184 
189 #define b_thread_prio_clear() (brisc_scheduler_state.prio = 0)
190 
196 extern int b_thread_init( const char* name );
197 
210 extern int b_thread_create( const char* name, void (*thread_fn)(void*), void* arg, cpu_reg_t* stack, size_t n_stack_words );
211 
218 extern void b_thread_start( int id );
219 
225 extern void b_thread_stop( int id );
226 
231 extern void b_thread_lock( void );
232 
236 extern void b_thread_unlock( void );
237 
246 extern int b_thread_set_prio( int id, int8_t prio );
247 
252 extern int8_t b_thread_priority( int id );
253 
257 extern void b_thread_yield( void );
258 
262 extern brisc_systick_t b_thread_systick( void );
263 
267 extern volatile brisc_thread_t* b_thread_current( void );
268 
272 extern void b_thread_set_systick_fn(void (*systick_fn)(void) );
273 
277 extern void b_thread_set_yield_fn(void (*yield_fn)(void) );
278 
279 #ifdef __cplusplus
280 }
281 #endif
282 
283 #endif
int8_t b_thread_priority(int id)
Definition: brisc_thread.c:143
void(* yield_fn)(void)
Definition: brisc_sched.h:83
#define brisc_systick_t
Definition: brisc_thread.h:160
struct brisc_thread __attribute__((aligned(8)))
int b_thread_set_prio(int id, int8_t prio)
set a thread priority. < 0 is inactive, = 0 is active but suspended. > 0 indicates the maximum number...
Definition: brisc_thread.c:132
int b_thread_init(const char *name)
Called by the "main()" thread, initializes briscits and inserts an entry in the thread table for the ...
Definition: brisc_thread.c:41
void b_thread_lock(void)
Lock the scheduler such that the current thread will nut yield until thread_unlock() is called...
Definition: brisc_thread.c:77
void b_thread_set_yield_fn(void(*yield_fn)(void))
Insert a callback on yield interrupt.
Definition: brisc_thread.c:56
#define BRISC_THREAD_NAME_MAX
Definition: brisc_thread.h:152
int b_thread_create(const char *name, void(*thread_fn)(void *), void *arg, cpu_reg_t *stack, size_t n_stack_words)
Allocate a new thread in the run queue.
Definition: brisc_thread.c:98
void b_thread_stop(int id)
Stop a thread. The thread remains active in the run queue, yet receives no run time.
Definition: brisc_thread.c:67
void b_thread_yield(void)
Yield the remainder of the current thread&#39;s time slice(s).
Definition: brisc_thread.c:87
void b_thread_start(int id)
Start a thread with the default priority of BRISC_THREAD_PRIO_MIN. TO start thread with other priorit...
Definition: brisc_thread.c:72
cpu_state_t * cpu_state
Definition: brisc_thread.h:166
char name[BRISC_THREAD_NAME_MAX+1]
Definition: brisc_thread.h:164
brisc_systick_t b_thread_systick(void)
Definition: brisc_thread.c:62
void b_thread_set_systick_fn(void(*systick_fn)(void))
Insert a callback on systick interrupt.
Definition: brisc_thread.c:51
void(* systick_fn)(void)
Definition: brisc_sched.h:82
volatile brisc_thread_t * b_thread_current(void)
Definition: brisc_thread.c:167
void b_thread_unlock(void)
Unlock the current thread (see thread_lock())
Definition: brisc_thread.c:82