1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

fixed coding conventions (correctly this time)

This commit is contained in:
Oliver Hahm 2013-06-20 18:18:29 +02:00
parent 6ca6ae9811
commit ffeb6f8523
27 changed files with 452 additions and 307 deletions

View File

@ -31,8 +31,10 @@ number_of_highest_bit(unsigned v)
r |= (v >> 1);
#else
r = 0;
while (v >>= 1) // unroll for more speed...
while(v >>= 1) { // unroll for more speed...
r++;
}
#endif
return r;
@ -55,6 +57,7 @@ unsigned
number_of_bits_set(unsigned v)
{
unsigned c; // c accumulates the total bits set in v
for(c = 0; v; c++) {
v &= v - 1; // clear the least significant bit set
}

View File

@ -1,16 +1,19 @@
#include <cib.h>
void cib_init(cib_t *cib, unsigned int size) {
void cib_init(cib_t *cib, unsigned int size)
{
cib->read_count = 0;
cib->write_count = 0;
cib->complement = 0 - size;
}
int cib_avail (cib_t *cib) {
int cib_avail(cib_t *cib)
{
return (int)(cib->write_count - cib->read_count);
}
int cib_get(cib_t *cib) {
int cib_get(cib_t *cib)
{
int avail = cib_avail(cib);
if(avail > 0) {
@ -20,7 +23,8 @@ int cib_get(cib_t *cib) {
return -1;
}
int cib_put(cib_t *cib) {
int cib_put(cib_t *cib)
{
int avail = cib_avail(cib);
if((int)(avail + cib->complement) < 0) {

View File

@ -19,16 +19,19 @@
#include <stdio.h>
/* inserts new_node after node */
void clist_add(clist_node_t** node, clist_node_t* new_node) {
void clist_add(clist_node_t **node, clist_node_t *new_node)
{
if(*node != NULL) {
new_node->next = (*node);
new_node->prev = (*node)->prev;
(*node)->prev->next = new_node;
(*node)->prev = new_node;
if((*node)->prev == *node) {
(*node)->prev = new_node;
}
} else {
}
else {
*node = new_node;
new_node->next = new_node;
new_node->prev = new_node;
@ -36,23 +39,32 @@ void clist_add(clist_node_t** node, clist_node_t* new_node) {
}
/* removes node. */
void clist_remove(clist_node_t** list, clist_node_t *node) {
void clist_remove(clist_node_t **list, clist_node_t *node)
{
if(node->next != node) {
node->prev->next = node->next;
node->next->prev = node->prev;
if (node == *list) *list = node->next;
} else {
if(node == *list) {
*list = node->next;
}
}
else {
*list = NULL;
}
}
void clist_print(clist_node_t* clist) {
void clist_print(clist_node_t *clist)
{
clist_node_t *start = clist;
while(clist != NULL) {
printf("list entry: %u prev=%u next=%u\n", clist->data, clist->prev->data, clist->next->data);
clist = clist->next;
if (clist == start) break;
if(clist == start) {
break;
}
}
}

View File

@ -37,15 +37,16 @@ static int lifo[ARCH_MAXTIMERS+1];
/*---------------------------------------------------------------------------*/
static void multiplexer(int source) {
// printf("\nhwt: trigger %i.\n", source);
static void multiplexer(int source)
{
lifo_insert(lifo, source);
lpm_prevent_sleep--;
timer[source].callback(timer[source].data);
}
static void hwtimer_wakeup(void* ptr) {
static void hwtimer_wakeup(void *ptr)
{
int pid = (int)ptr;
thread_wakeup(pid);
}
@ -53,22 +54,27 @@ static void hwtimer_wakeup(void* ptr) {
void hwtimer_spin(unsigned long ticks)
{
unsigned long co = hwtimer_arch_now() + ticks;
while(hwtimer_arch_now() > co);
while(hwtimer_arch_now() < co);
}
/*---------------------------------------------------------------------------*/
void hwtimer_init(void) {
void hwtimer_init(void)
{
hwtimer_init_comp(F_CPU);
}
/*---------------------------------------------------------------------------*/
void hwtimer_init_comp(uint32_t fcpu) {
void hwtimer_init_comp(uint32_t fcpu)
{
hwtimer_arch_init(multiplexer, fcpu);
lifo_init(lifo, ARCH_MAXTIMERS);
for(int i = 0; i < ARCH_MAXTIMERS; i++) {
lifo_insert(lifo, i);
}
@ -76,7 +82,8 @@ void hwtimer_init_comp(uint32_t fcpu) {
/*---------------------------------------------------------------------------*/
int hwtimer_active(void) {
int hwtimer_active(void)
{
return (! lifo_empty(lifo));
}
@ -98,6 +105,7 @@ void hwtimer_wait(unsigned long ticks)
/* -2 is to adjust the real value */
int res = hwtimer_set(ticks - 2, hwtimer_wakeup, (void*)(unsigned int)(active_thread->pid));
if(res == -1) {
hwtimer_spin(ticks);
return;
@ -116,10 +124,12 @@ static int _hwtimer_set(unsigned long offset, void (*callback)(void*), void *ptr
}
int n = lifo_get(lifo);
if(n == -1) {
if(! inISR()) {
eINT();
}
puts("No hwtimer left.");
return -1;
}
@ -128,11 +138,9 @@ static int _hwtimer_set(unsigned long offset, void (*callback)(void*), void *ptr
timer[n].data = ptr;
if(absolute) {
// printf("hwt: setting %i to %u\n", n, offset);
hwtimer_arch_set_absolute(offset, n);
}
else {
// printf("hwt: setting %i to offset %u\n", n, offset);
hwtimer_arch_set(offset, n);
}
@ -141,14 +149,17 @@ static int _hwtimer_set(unsigned long offset, void (*callback)(void*), void *ptr
if(!inISR()) {
eINT();
}
return n;
}
int hwtimer_set(unsigned long offset, void (*callback)(void*), void *ptr) {
int hwtimer_set(unsigned long offset, void (*callback)(void*), void *ptr)
{
return _hwtimer_set(offset, callback, ptr, false);
}
int hwtimer_set_absolute(unsigned long offset, void (*callback)(void*), void *ptr) {
int hwtimer_set_absolute(unsigned long offset, void (*callback)(void*), void *ptr)
{
return _hwtimer_set(offset, callback, ptr, true);
}
@ -157,7 +168,6 @@ int hwtimer_set_absolute(unsigned long offset, void (*callback)(void*), void *pt
int hwtimer_remove(int n)
{
// printf("hwt: remove %i.\n", n);
hwtimer_arch_disable_interrupt();
hwtimer_arch_unset(n);

View File

@ -34,7 +34,8 @@ void clist_remove(clist_node_t** list, clist_node_t *node);
/* advances the circle list. second list entry will be first, first is last. */
/*void clist_advance(clist_node_t** list);*/
static inline void clist_advance(clist_node_t** list) {
static inline void clist_advance(clist_node_t **list)
{
*list = (*list)->next;
}

View File

@ -39,7 +39,7 @@ extern volatile int thread_pid;
//#define SCHEDSTATISTICS
#if SCHEDSTATISTICS
typedef struct schedstat {
typedef struct {
unsigned int laststart;
unsigned int schedules;
unsigned int runtime;

View File

@ -40,15 +40,16 @@ volatile int lpm_prevent_sleep = 0;
extern int main(void);
static void idle_thread(void) {
static void idle_thread(void)
{
while(1) {
if(lpm_prevent_sleep) {
lpm_set(LPM_IDLE);
}
else {
lpm_set(LPM_IDLE);
// lpm_set(LPM_SLEEP);
// lpm_set(LPM_POWERDOWN);
/* lpm_set(LPM_SLEEP); */
/* lpm_set(LPM_POWERDOWN); */
}
}
}

View File

@ -1,33 +1,40 @@
#include <lifo.h>
int lifo_empty(int *array) {
int lifo_empty(int *array)
{
return array[0] == -1;
}
void lifo_init(int *array, int n) {
void lifo_init(int *array, int n)
{
for(int i = 0; i <= n; i++) {
array[i] = -1;
}
}
void lifo_insert(int *array, int i) {
void lifo_insert(int *array, int i)
{
int index = i + 1;
array[index] = array[0];
array[0] = i;
}
int lifo_get(int *array) {
int lifo_get(int *array)
{
int head = array[0];
if(head != -1) {
array[0] = array[head + 1];
}
return head;
}
#ifdef WITH_MAIN
#include <stdio.h>
int main() {
int main()
{
int array[5];
lifo_init(array, 4);

View File

@ -28,7 +28,8 @@
//#define ENABLE_DEBUG
#include "debug.h"
static int queue_msg(tcb_t *target, msg_t *m) {
static int queue_msg(tcb_t *target, msg_t *m)
{
int n = cib_put(&(target->msg_queue));
if(n != -1) {
@ -39,7 +40,8 @@ static int queue_msg(tcb_t *target, msg_t *m) {
return 0;
}
int msg_send(msg_t* m, unsigned int target_pid, bool block) {
int msg_send(msg_t *m, unsigned int target_pid, bool block)
{
if(inISR()) {
return msg_send_int(m, target_pid);
}
@ -47,6 +49,7 @@ int msg_send(msg_t* m, unsigned int target_pid, bool block) {
tcb_t *target = (tcb_t*) sched_threads[target_pid];
m->sender_pid = thread_pid;
if(m->sender_pid == target_pid) {
return -1;
}
@ -56,6 +59,7 @@ int msg_send(msg_t* m, unsigned int target_pid, bool block) {
}
dINT();
if(target->status != STATUS_RECEIVE_BLOCKED) {
if(target->msg_array && queue_msg(target, m)) {
eINT();
@ -79,16 +83,19 @@ int msg_send(msg_t* m, unsigned int target_pid, bool block) {
active_thread->wait_data = (void*) m;
int newstatus;
if(active_thread->status == STATUS_REPLY_BLOCKED) {
newstatus = STATUS_REPLY_BLOCKED;
} else {
}
else {
newstatus = STATUS_SEND_BLOCKED;
}
sched_set_status((tcb_t*) active_thread, newstatus);
DEBUG("%s: back from send block.\n", active_thread->name);
} else {
}
else {
DEBUG("%s: direct msg copy.\n", active_thread->name);
/* copy msg to target */
msg_t *target_message = (msg_t*) target->wait_data;
@ -102,7 +109,8 @@ int msg_send(msg_t* m, unsigned int target_pid, bool block) {
return 1;
}
int msg_send_int(msg_t* m, unsigned int target_pid) {
int msg_send_int(msg_t *m, unsigned int target_pid)
{
tcb_t *target = (tcb_t *) sched_threads[target_pid];
if(target->status == STATUS_RECEIVE_BLOCKED) {
@ -117,13 +125,15 @@ int msg_send_int(msg_t* m, unsigned int target_pid) {
sched_context_switch_request = 1;
return 1;
} else {
}
else {
DEBUG("msg_send_int: receiver not waiting.\n");
return (queue_msg(target, m));
}
}
int msg_send_receive(msg_t *m, msg_t *reply, unsigned int target_pid) {
int msg_send_receive(msg_t *m, msg_t *reply, unsigned int target_pid)
{
dINT();
tcb_t *me = (tcb_t*) sched_threads[thread_pid];
sched_set_status(me, STATUS_REPLY_BLOCKED);
@ -135,10 +145,12 @@ int msg_send_receive(msg_t *m, msg_t *reply, unsigned int target_pid) {
return 1;
}
int msg_reply(msg_t *m, msg_t *reply) {
int msg_reply(msg_t *m, msg_t *reply)
{
int state = disableIRQ();
tcb_t *target = (tcb_t*) sched_threads[m->sender_pid];
if(target->status != STATUS_REPLY_BLOCKED) {
DEBUG("%s: msg_reply(): target \"%s\" not waiting for reply.", active_thread->name, target->name);
restoreIRQ(state);
@ -156,12 +168,15 @@ int msg_reply(msg_t *m, msg_t *reply) {
return 1;
}
int msg_reply_int(msg_t *m, msg_t *reply) {
int msg_reply_int(msg_t *m, msg_t *reply)
{
tcb_t *target = (tcb_t*) sched_threads[m->sender_pid];
if(target->status != STATUS_REPLY_BLOCKED) {
DEBUG("%s: msg_reply_int(): target \"%s\" not waiting for reply.", active_thread->name, target->name);
return -1;
}
msg_t *target_message = (msg_t*) target->wait_data;
*target_message = *reply;
sched_set_status(target, STATUS_PENDING);
@ -169,13 +184,15 @@ int msg_reply_int(msg_t *m, msg_t *reply) {
return 1;
}
int msg_receive(msg_t* m) {
int msg_receive(msg_t *m)
{
dINT();
DEBUG("%s: msg_receive.\n", active_thread->name);
tcb_t *me = (tcb_t*) sched_threads[thread_pid];
int n = -1;
if(me->msg_array) {
n = cib_get(&(me->msg_queue));
}
@ -183,7 +200,8 @@ int msg_receive(msg_t* m) {
if(n >= 0) {
DEBUG("%s: msg_receive(): We've got a queued message.\n", active_thread->name);
*m = me->msg_array[n];
} else {
}
else {
me->wait_data = (void *) m;
}
@ -191,6 +209,7 @@ int msg_receive(msg_t* m) {
if(node == NULL) {
DEBUG("%s: msg_receive(): No thread in waiting list.\n", active_thread->name);
if(n < 0) {
DEBUG("%s: msg_receive(): No msg in queue. Going blocked.\n", active_thread->name);
sched_set_status(me, STATUS_RECEIVE_BLOCKED);
@ -200,8 +219,10 @@ int msg_receive(msg_t* m) {
/* sender copied message */
}
return 1;
} else {
}
else {
DEBUG("%s: msg_receive(): Wakeing up waiting thread.\n", active_thread->name);
tcb_t *sender = (tcb_t*) node->data;
@ -225,7 +246,8 @@ int msg_receive(msg_t* m) {
}
}
int msg_init_queue(msg_t* array, int num) {
int msg_init_queue(msg_t *array, int num)
{
/* make sure brainfuck condition is met */
if(num && (num & (num - 1)) == 0) {
tcb_t *me = (tcb_t*) active_thread;

View File

@ -26,7 +26,8 @@
//#define ENABLE_DEBUG
#include <debug.h>
int mutex_init(struct mutex_t* mutex) {
int mutex_init(struct mutex_t *mutex)
{
mutex->val = 0;
mutex->queue.priority = 0;
@ -36,30 +37,36 @@ int mutex_init(struct mutex_t* mutex) {
return 1;
}
int mutex_trylock(struct mutex_t* mutex) {
int mutex_trylock(struct mutex_t *mutex)
{
DEBUG("%s: trylocking to get mutex. val: %u\n", active_thread->name, mutex->val);
return (atomic_set_return(&mutex->val, thread_pid) == 0);
}
int prio(void) {
int prio(void)
{
return active_thread->priority;
}
int mutex_lock(struct mutex_t* mutex) {
int mutex_lock(struct mutex_t *mutex)
{
DEBUG("%s: trying to get mutex. val: %u\n", active_thread->name, mutex->val);
if(atomic_set_return(&mutex->val, 1) != 0) {
// mutex was locked.
/* mutex was locked. */
mutex_wait(mutex);
}
return 1;
}
void mutex_wait(struct mutex_t *mutex) {
void mutex_wait(struct mutex_t *mutex)
{
int irqstate = disableIRQ();
DEBUG("%s: Mutex in use. %u\n", active_thread->name, mutex->val);
if(mutex->val == 0) {
// somebody released the mutex. return.
/* somebody released the mutex. return. */
mutex->val = thread_pid;
DEBUG("%s: mutex_wait early out. %u\n", active_thread->name, mutex->val);
restoreIRQ(irqstate);
@ -84,7 +91,8 @@ void mutex_wait(struct mutex_t *mutex) {
/* we were woken up by scheduler. waker removed us from queue. we have the mutex now. */
}
void mutex_unlock(struct mutex_t* mutex, int yield) {
void mutex_unlock(struct mutex_t *mutex, int yield)
{
DEBUG("%s: unlocking mutex. val: %u pid: %u\n", active_thread->name, mutex->val, thread_pid);
int irqstate = disableIRQ();
@ -96,7 +104,8 @@ void mutex_unlock(struct mutex_t* mutex, int yield) {
sched_set_status(process, STATUS_PENDING);
sched_switch(active_thread->priority, process->priority, inISR());
} else {
}
else {
mutex->val = 0;
}
}

View File

@ -25,26 +25,30 @@
extern void *sbrk(int incr);
void *_malloc(size_t size) {
void *_malloc(size_t size)
{
void *ptr = sbrk(size);
DEBUG("_malloc(): allocating block of size %u at 0x%X.\n", (unsigned int) size, (unsigned int)ptr);
if(ptr != (void*) - 1) {
return ptr;
} else {
}
else {
return NULL;
}
}
void *_realloc(void *ptr, size_t size) {
void *_realloc(void *ptr, size_t size)
{
void *newptr = _malloc(size);
memcpy(newptr, ptr, size);
free(ptr);
return newptr;
}
void _free(void* ptr) {
void _free(void *ptr)
{
DEBUG("_free(): block at 0x%X lost.\n", (unsigned int)ptr);
}

View File

@ -21,39 +21,48 @@
//#define ENABLE_DEBUG 0
#include "debug.h"
void queue_remove(queue_node_t* root, queue_node_t *node) {
void queue_remove(queue_node_t *root, queue_node_t *node)
{
while(root->next != NULL) {
if(root->next == node) {
root->next = node->next;
node->next = NULL;
return;
}
root = root->next;
}
}
queue_node_t *queue_remove_head(queue_node_t* root) {
queue_node_t *queue_remove_head(queue_node_t *root)
{
queue_node_t *head = root->next;
if(head != NULL) {
root->next = head->next;
}
return head;
}
void queue_add_tail(queue_node_t* node, queue_node_t* new_obj) {
void queue_add_tail(queue_node_t *node, queue_node_t *new_obj)
{
while(node->next != NULL) {
node = node->next;
}
node->next = new_obj;
new_obj->next = NULL;
}
void queue_add_head(queue_node_t* root, queue_node_t* new_obj) {
void queue_add_head(queue_node_t *root, queue_node_t *new_obj)
{
new_obj->next = root->next;
root->next = new_obj;
}
void queue_priority_add(queue_node_t* root, queue_node_t* new_obj) {
void queue_priority_add(queue_node_t *root, queue_node_t *new_obj)
{
queue_node_t *node = root;
while(node->next != NULL) {
@ -62,6 +71,7 @@ void queue_priority_add(queue_node_t* root, queue_node_t* new_obj) {
node->next = new_obj;
return;
}
node = node->next;
}
@ -69,7 +79,8 @@ void queue_priority_add(queue_node_t* root, queue_node_t* new_obj) {
new_obj->next = NULL;
}
void queue_priority_add_generic(queue_node_t* root, queue_node_t* new_obj, int (*cmp)(queue_node_t*,queue_node_t*)) {
void queue_priority_add_generic(queue_node_t *root, queue_node_t *new_obj, int (*cmp)(queue_node_t *, queue_node_t *))
{
queue_node_t *node = root;
while(node->next != NULL) {
@ -78,6 +89,7 @@ void queue_priority_add_generic(queue_node_t* root, queue_node_t* new_obj, int (
node->next = new_obj;
return;
}
node = node->next;
}
@ -86,15 +98,18 @@ void queue_priority_add_generic(queue_node_t* root, queue_node_t* new_obj, int (
}
void queue_print(queue_node_t* node) {
void queue_print(queue_node_t *node)
{
printf("queue:\n");
while(node->next != NULL) {
node = node->next;
printf("Data: %u Priority: %u\n", node->data, node->priority);
}
}
void queue_print_node(queue_node_t *node) {
void queue_print_node(queue_node_t *node)
{
printf("Data: %u Priority: %u Next: %u\n", (unsigned int)node->data, node->priority, (unsigned int)node->next);
}

View File

@ -45,9 +45,11 @@ void sched_register_cb(void (*callback)(uint32_t, uint32_t));
schedstat pidlist[MAXTHREADS];
#endif
void sched_init() {
void sched_init()
{
printf("Scheduler...");
int i;
for(i = 0; i < MAXTHREADS; i++) {
sched_threads[i] = NULL;
#if SCHEDSTATISTICS
@ -59,6 +61,7 @@ void sched_init() {
active_thread = NULL;
thread_pid = -1;
for(i = 0; i < SCHED_PRIO_LEVELS; i++) {
runqueues[i] = NULL;
}
@ -66,7 +69,8 @@ void sched_init() {
printf("[OK]\n");
}
void sched_run() {
void sched_run()
{
sched_context_switch_request = 0;
tcb_t *my_active_thread = (tcb_t *)active_thread;
@ -77,9 +81,11 @@ void sched_run() {
}
#ifdef SCHED_TEST_STACK
if(*((unsigned int *)my_active_thread->stack_start) != (unsigned int) my_active_thread->stack_start) {
printf("scheduler(): stack overflow detected, task=%s pid=%u\n", my_active_thread->name, my_active_thread->pid);
}
#endif
}
@ -88,23 +94,28 @@ void sched_run() {
/* TODO: setup dependency from SCHEDSTATISTICS to MODULE_HWTIMER */
extern unsigned long hwtimer_now(void);
unsigned int time = hwtimer_now();
if(my_active_thread && (pidlist[my_active_thread->pid].laststart)) {
pidlist[my_active_thread->pid].runtime += time - pidlist[my_active_thread->pid].laststart;
}
#endif
DEBUG("\nscheduler: previous task: %s\n", (my_active_thread == NULL) ? "none" : my_active_thread->name);
if(num_tasks == 0) {
DEBUG("scheduler: no tasks left.\n");
while(! num_tasks) {
/* loop until a new task arrives */
;
}
DEBUG("scheduler: new task created.\n");
}
my_active_thread = NULL;
while(! my_active_thread) {
int nextrq = number_of_lowest_bit(runqueue_bitcache);
clist_node_t next = *(runqueues[nextrq]);
@ -117,20 +128,23 @@ void sched_run() {
pidlist[my_active_thread->pid].schedules ++;
#endif
#ifdef MODULE_NSS
if(active_thread && active_thread->pid != last_pid) {
last_pid = active_thread->pid;
}
#endif
}
DEBUG("scheduler: next task: %s\n", my_active_thread->name);
if(my_active_thread != active_thread) {
if (active_thread != NULL) { //TODO: necessary?
if(active_thread != NULL) { /* TODO: necessary? */
if(active_thread->status == STATUS_RUNNING) {
active_thread->status = STATUS_PENDING ;
}
}
sched_set_status((tcb_t *)my_active_thread, STATUS_RUNNING);
}
@ -140,42 +154,51 @@ void sched_run() {
}
#if SCHEDSTATISTICS
void sched_register_cb(void (*callback)(uint32_t, uint32_t)) {
void sched_register_cb(void (*callback)(uint32_t, uint32_t))
{
sched_cb = callback;
}
#endif
void sched_set_status(tcb_t *process, unsigned int status) {
void sched_set_status(tcb_t *process, unsigned int status)
{
if(status & STATUS_ON_RUNQUEUE) {
if(!(process->status & STATUS_ON_RUNQUEUE)) {
DEBUG("adding process %s to runqueue %u.\n", process->name, process->priority);
clist_add(&runqueues[process->priority], &(process->rq_entry));
runqueue_bitcache |= 1 << process->priority;
}
} else {
}
else {
if(process->status & STATUS_ON_RUNQUEUE) {
DEBUG("removing process %s from runqueue %u.\n", process->name, process->priority);
clist_remove(&runqueues[process->priority], &(process->rq_entry));
if(! runqueues[process->priority]) {
runqueue_bitcache &= ~(1 << process->priority);
}
}
}
process->status = status;
}
void sched_switch(uint16_t current_prio, uint16_t other_prio, int in_isr) {
void sched_switch(uint16_t current_prio, uint16_t other_prio, int in_isr)
{
DEBUG("%s: %i %i %i\n", active_thread->name, (int)current_prio, (int)other_prio, in_isr);
if(current_prio <= other_prio) {
if(in_isr) {
sched_context_switch_request = 1;
} else {
}
else {
thread_yield();
}
}
}
void sched_task_exit(void) {
void sched_task_exit(void)
{
DEBUG("sched_task_exit(): ending task %s...\n", active_thread->name);
dINT();

View File

@ -27,60 +27,83 @@
#include "hwtimer.h"
#include "sched.h"
inline int thread_getpid() {
inline int thread_getpid()
{
return active_thread->pid;
}
int thread_getlastpid() {
int thread_getlastpid()
{
extern int last_pid;
return last_pid;
}
unsigned int thread_getstatus(int pid) {
if (sched_threads[pid]==NULL)
unsigned int thread_getstatus(int pid)
{
if(sched_threads[pid] == NULL) {
return STATUS_NOT_FOUND;
}
return sched_threads[pid]->status;
}
void thread_sleep() {
if ( inISR()) return;
void thread_sleep()
{
if(inISR()) {
return;
}
dINT();
sched_set_status((tcb_t *)active_thread, STATUS_SLEEPING);
eINT();
thread_yield();
}
int thread_wakeup(int pid) {
int thread_wakeup(int pid)
{
DEBUG("thread_wakeup: Trying to wakeup PID %i...\n", pid);
int isr = inISR();
if(! isr) {
DEBUG("thread_wakeup: Not in interrupt.\n");
dINT();
}
int result = sched_threads[pid]->status;
if(result == STATUS_SLEEPING) {
DEBUG("thread_wakeup: Thread is sleeping.\n");
sched_set_status((tcb_t *)sched_threads[pid], STATUS_RUNNING);
if(!isr) {
eINT();
thread_yield();
} else {
}
else {
sched_context_switch_request = 1;
}
return 1;
} else {
}
else {
DEBUG("thread_wakeup: Thread is not sleeping!\n");
if (!isr) eINT();
if(!isr) {
eINT();
}
return STATUS_NOT_FOUND;
}
}
int thread_measure_stack_usage(char* stack) {
int thread_measure_stack_usage(char *stack)
{
unsigned int *stackp = (unsigned int *)stack;
/* assumption that the comparison fails before or after end of stack */
while( *stackp == (unsigned int)stackp )
while(*stackp == (unsigned int)stackp) {
stackp++;
}
int space = (unsigned int)stackp - (unsigned int)stack;
return space;
@ -94,14 +117,17 @@ int thread_create(char *stack, int stacksize, char priority, int flags, void (*f
/* align tcb address on 32bit boundary */
unsigned int tcb_address = (unsigned int) stack + stacksize;
if(tcb_address & 1) {
tcb_address--;
stacksize--;
}
if(tcb_address & 2) {
tcb_address -= 2;
stacksize -= 2;
}
tcb_t *cb = (tcb_t *) tcb_address;
if(priority >= SCHED_PRIO_LEVELS) {
@ -112,11 +138,13 @@ int thread_create(char *stack, int stacksize, char priority, int flags, void (*f
/* assign each int of the stack the value of it's address */
unsigned int *stackmax = (unsigned int *)((char *)stack + stacksize);
unsigned int *stackp = (unsigned int *)stack;
while(stackp < stackmax) {
*stackp = (unsigned int)stackp;
stackp++;
}
} else {
}
else {
/* create stack guard */
*stack = (unsigned int)stack;
}
@ -126,12 +154,14 @@ int thread_create(char *stack, int stacksize, char priority, int flags, void (*f
}
int pid = 0;
while(pid < MAXTHREADS) {
if(sched_threads[pid] == NULL) {
sched_threads[pid] = cb;
cb->pid = pid;
break;
}
pid++;
}
@ -141,6 +171,7 @@ int thread_create(char *stack, int stacksize, char priority, int flags, void (*f
if(! inISR()) {
eINT();
}
return -EOVERFLOW;
}
@ -172,13 +203,16 @@ int thread_create(char *stack, int stacksize, char priority, int flags, void (*f
if(flags & CREATE_SLEEPING) {
sched_set_status(cb, STATUS_SLEEPING);
} else {
}
else {
sched_set_status(cb, STATUS_PENDING);
if(!(flags & CREATE_WOUT_YIELD)) {
if(! inISR()) {
eINT();
thread_yield();
} else {
}
else {
sched_context_switch_request = 1;
}
}