-
Notifications
You must be signed in to change notification settings - Fork 26
/
os.h
119 lines (102 loc) · 3.07 KB
/
os.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ========================================================================
* Copyright 2012 Chris Moos
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ========================================================================
*/
#include <stdint.h>
typedef void(*os_task_function)(void *arg);
/**
* Initializes the OS
*
* This should be the first call in the
* application's main method.
*/
void os_init();
/**
* Starts the OS loop.
*
* This will execute tasks.
* Make sure to create at least one task before
* starting the loop.
*/
void os_loop();
/**
* Schedules a task for execution.
*
* @param function The function that will execute when the task is started.
* @param arg A user defined value that will be passed to the task.
* @param start_delay_secs The amount of seconds before starting this task or
* 0 if the task should be started immediately.
*/
int os_schedule_task(os_task_function function, void *arg, uint16_t start_delay_secs);
/**
* This MUST be called at the end of a task.
*/
void os_exit_task();
/**
* Switches to the next available task.
*/
void os_yield();
/**
* Suspends the current task for the specified time.
*/
void os_sleep(uint16_t millis);
/**
* Returns the amount of seconds since the OS has started.
*/
uint32_t os_get_uptime();
/**
* Returns the amount of milliseconds since the OS has started.
*
* This will overflow after about
*/
uint64_t os_get_uptime_millis();
typedef volatile int spinlock_t;
void spinlock_init(spinlock_t *lock);
void spinlock_acquire(spinlock_t *lock);
void spinlock_release(spinlock_t *lock);
struct mutex_s {
// This is set to 1 if the mutex is currently held or 0 if available.
uint8_t value;
// This is set to the next task waiting for the mutex.
// A queue could be used but this should give a good balance of fairness
// and performance.
//
// NOTE: Tasks must always release their mutexes before exiting or a deadlock
// may occur.
uint8_t wait;
};
typedef volatile struct mutex_s mutex_t;
/**
* Initialize a mutex.
*
* After initializiation the mutex is available to acquire.
*/
void mutex_init(mutex_t *mutex);
/**
* Acquires the specified mutex.
*
* If the mutex is being held by another task this call
* will block and another task will be executed.
*
* When this function returns, the mutex is held by the current task.
*/
void mutex_acquire(mutex_t *mutex);
/**
* Releases the specified mutex.
*
* This should only be called after a call to mutex_acquire();
* In addition, calls to acquire/release should be balanced.
*/
void mutex_release(mutex_t *mutex);