Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emBIOS.
7
//
8
//    emBIOS is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emBIOS is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#ifndef __THREAD_H__
25
#define __THREAD_H__
26
 
27
 
28
#include "global.h"
29
#include "contextswitch.h"
30
 
31
 
32
#ifndef SCHEDULER_TICK
33
#define SCHEDULER_TICK 1048576
34
#endif
35
 
36
#ifndef SYSTEM_TICK
37
#define SYSTEM_TICK 10000
38
#endif
39
 
40
#define TIMEOUT_NONE 0
41
#define TIMEOUT_BLOCK -1
42
 
43
#define THREAD_FOUND 1
44
#define THREAD_OK 0
45
#define THREAD_TIMEOUT -1
46
#define NO_MORE_THREADS -2
47
#define INVALID_THREAD -3
48
#define ALREADY_SUSPENDED -4
49
#define ALREADY_RESUMED -5
50
 
51
 
52
enum thread_state
53
{
54
    THREAD_FREE = 0,
55
    THREAD_READY,
56
    THREAD_RUNNING,
57
    THREAD_BLOCKED,
15 theseven 58
    THREAD_SUSPENDED,
59
    THREAD_DEFUNCT,
60
    THREAD_DEFUNCT_ACK
14 theseven 61
};
62
 
63
enum thread_block
64
{
65
    THREAD_NOT_BLOCKED = 0,
66
    THREAD_BLOCK_SLEEP,
67
    THREAD_BLOCK_MUTEX,
15 theseven 68
    THREAD_BLOCK_WAKEUP,
69
    THREAD_DEFUNCT_STKOV
14 theseven 70
};
71
 
15 theseven 72
enum thread_type
73
{
74
    USER_THREAD = 0,
75
    SYSTEM_THREAD
76
};
77
 
14 theseven 78
struct scheduler_thread
79
{
80
    uint32_t regs[16];
81
    uint32_t cpsr;
82
    const char* name;
83
    uint64_t cputime_total;
84
    uint32_t cputime_current;
85
    uint32_t startusec;
86
    struct scheduler_thread* queue_next;
87
    uint32_t timeout;
88
    uint32_t blocked_since;
89
    void* blocked_by;
90
    uint32_t* stack;
91
    enum thread_state state;
92
    enum thread_block block_type;
15 theseven 93
    enum thread_type type;
14 theseven 94
    uint8_t priority;
95
    uint8_t cpuload;
96
};
97
 
98
struct mutex
99
{
100
    struct scheduler_thread* owner;
101
    struct scheduler_thread* waiters;
102
    int count;
103
};
104
 
105
struct wakeup
106
{
107
    struct scheduler_thread* waiter;
108
    bool signalled;
109
};
110
 
111
 
112
void scheduler_init() INITCODE_ATTR;
113
void scheduler_switch(int thread) ICODE_ATTR;
114
int thread_create(const char* name, const void* code, void* stack,
15 theseven 115
                  int stacksize, enum thread_type type, int priority, bool run);
14 theseven 116
int thread_suspend(int thread);
117
int thread_resume(int thread);
118
int thread_terminate(int thread);
119
void thread_exit();
120
void mutex_init(struct mutex* obj) ICODE_ATTR;
121
int mutex_lock(struct mutex* obj, int timeout) ICODE_ATTR;
122
int mutex_unlock(struct mutex* obj) ICODE_ATTR;
123
void wakeup_init(struct wakeup* obj) ICODE_ATTR;
124
int wakeup_wait(struct wakeup* obj, int timeout) ICODE_ATTR;
125
int wakeup_signal(struct wakeup* obj) ICODE_ATTR;
126
void sleep(int usecs) ICODE_ATTR;
127
 
128
static inline void yield()
129
{
130
    context_switch();
131
}
132
 
133
#endif