aboutsummaryrefslogtreecommitdiff
path: root/core/include/thread.h
blob: 8ec4a26749144d19d2a37ea5ffc6da4fb852afcc (plain)
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
#ifndef _THREAD_H
#define _THREAD_H

#include <stddef.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <timer.h>
#include <sys/cpu.h>

/* The idle thread runs at this priority */
#define IDLE_THREAD_PRIORITY	INT_MAX

/* This priority should normally be used for hardware-polling threads */
#define POLL_THREAD_PRIORITY	(INT_MAX-1)

struct semaphore;

struct thread_list {
    struct thread_list *next, *prev;
};

/*
 * Stack frame used by __switch_to, see thread_asm.S
 */
struct thread_stack {
    int errno;
    uint16_t rmsp, rmss;
    uint32_t edi, esi, ebp, ebx;
    void (*eip)(void);
};

struct thread_block {
    struct thread_list list;
    struct thread *thread;
    struct semaphore *semaphore;
    mstime_t block_time;
    mstime_t timeout;
    bool timed_out;
};

#define THREAD_MAGIC 0x3568eb7d

struct thread {
    struct thread_stack *esp;	/* Must be first; stack pointer */
    unsigned int thread_magic;
    const char *name;		/* Name (for debugging) */
    struct thread_list  list;
    struct thread_block *blocked;
    void *stack, *rmstack;	/* Stacks, iff allocated by malloc/lmalloc */
    void *pvt; 			/* For the benefit of lwIP */
    int prio;
};

extern void (*sched_hook_func)(void);

void __thread_process_timeouts(void);
void __schedule(void);
void __switch_to(struct thread *);
void thread_yield(void);

extern struct thread *__current;
static inline struct thread *current(void)
{
    return __current;
}

struct semaphore {
    int count;
    struct thread_list list;
};

#define DECLARE_INIT_SEMAPHORE(sem, cnt)	\
    struct semaphore sem = {			\
	.count = (cnt),				\
	.list =	{				\
            .next = &sem.list,			\
            .prev = &sem.list                   \
        }					\
    }

mstime_t sem_down(struct semaphore *, mstime_t);
void sem_up(struct semaphore *);
void sem_init(struct semaphore *, int);

/*
 * This marks a semaphore object as unusable; it will remain unusable
 * until sem_init() is called on it again.  This DOES NOT clear the
 * list of blocked processes on this semaphore!
 *
 * It is also possible to mark the semaphore invalid by zeroing its
 * memory structure.
 */
static inline void sem_set_invalid(struct semaphore *sem)
{
    if (!!sem)
	sem->list.next = NULL;
}

/*
 * Ask if a semaphore object has been initialized.
 */
static inline bool sem_is_valid(struct semaphore *sem)
{
    return ((!!sem) && (!!sem->list.next));
}

struct thread *start_thread(const char *name, size_t stack_size, int prio,
			    void (*start_func)(void *), void *func_arg);
void __exit_thread(void);
void kill_thread(struct thread *);

void start_idle_thread(void);
void test_thread(void);

#endif /* _THREAD_H */