Fix some warnings in lwip bertos implementation
[bertos.git] / bertos / net / lwip / src / arch / sys_arch.c
index 1d314cc3d7e15ec481406ac06f6c56f14db3d2cd..adbb8fbc6a0d0c5af373e906bf0aa6c360565b72 100644 (file)
@@ -1,4 +1,4 @@
-#include <cfg/cfg_lwip.h>
+#include "cfg/cfg_lwip.h"
 
 #define LOG_LEVEL  3
 #define LOG_FORMAT 0
@@ -317,15 +317,6 @@ static struct sys_timeouts lwip_system_timeouts; // Default timeouts list for lw
 
 struct sys_timeouts *sys_arch_timeouts(void)
 {
-       ThreadNode *thread_node;
-       struct Process *curr_pid = proc_current();
-
-       FOREACH_NODE(thread_node, &used_thread)
-       {
-               if (thread_node->pid == curr_pid)
-                       return &(thread_node->timeout);
-       }
-
        return &lwip_system_timeouts;
 }
 
@@ -336,10 +327,23 @@ static void thread_trampoline(void)
        thread_node->entry(thread_node->arg);
 }
 
-sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg),
+#if !CONFIG_KERN_HEAP
+/*
+ * NOTE: threads are never destroyed, consequently these stacks are never
+ * deallocated. So, the stack allocator can be implemented as a simple index
+ * that is atomically incremented at each allocation.
+ */
+static cpu_stack_t thread_stack[MAX_THREAD_CNT]
+                       [DEFAULT_THREAD_STACKSIZE / sizeof(cpu_stack_t)]
+                               ALIGNED(sizeof(cpu_stack_t));
+static int last_stack;
+#endif
+
+sys_thread_t sys_thread_new(const char *name, void (* thread)(void *arg),
                                void *arg, int stacksize, int prio)
 {
        ThreadNode *thread_node;
+       cpu_stack_t *stackbase;
 
        proc_forbid();
        thread_node = (ThreadNode *)list_remHead(&free_thread);
@@ -354,13 +358,22 @@ sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg),
        thread_node->entry = thread;
        thread_node->arg = arg;
 
+       #if !CONFIG_KERN_HEAP
+               ASSERT(stacksize <= DEFAULT_THREAD_STACKSIZE);
+               PROC_ATOMIC(stackbase = thread_stack[last_stack++]);
+       #else
+               stackbase = NULL;
+       #endif
        thread_node->pid = proc_new_with_name(name, thread_trampoline,
-                               (void *)thread_node, stacksize, NULL);
+                               (void *)thread_node, stacksize, stackbase);
        if (thread_node->pid == NULL)
                return NULL;
 
        #if CONFIG_KERN_PRI
                proc_setPri(thread_node->pid, prio);
+       #else
+               /* Avoid warnings when priorities are disabled */
+               (void) prio;
        #endif
 
        return thread_node->pid;