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,
58
    THREAD_SUSPENDED
59
};
60
 
61
enum thread_block
62
{
63
    THREAD_NOT_BLOCKED = 0,
64
    THREAD_BLOCK_SLEEP,
65
    THREAD_BLOCK_MUTEX,
66
    THREAD_BLOCK_WAKEUP
67
};
68
 
69
struct scheduler_thread
70
{
71
    uint32_t regs[16];
72
    uint32_t cpsr;
73
    const char* name;
74
    uint64_t cputime_total;
75
    uint32_t cputime_current;
76
    uint32_t startusec;
77
    struct scheduler_thread* queue_next;
78
    uint32_t timeout;
79
    uint32_t blocked_since;
80
    void* blocked_by;
81
    uint32_t* stack;
82
    enum thread_state state;
83
    enum thread_block block_type;
84
    uint8_t priority;
85
    uint8_t cpuload;
86
};
87
 
88
struct mutex
89
{
90
    struct scheduler_thread* owner;
91
    struct scheduler_thread* waiters;
92
    int count;
93
};
94
 
95
struct wakeup
96
{
97
    struct scheduler_thread* waiter;
98
    bool signalled;
99
};
100
 
101
 
102
void scheduler_init() INITCODE_ATTR;
103
void scheduler_switch(int thread) ICODE_ATTR;
104
int thread_create(const char* name, const void* code, void* stack,
105
                  int stacksize, int priority, bool run);
106
int thread_suspend(int thread);
107
int thread_resume(int thread);
108
int thread_terminate(int thread);
109
void thread_exit();
110
void mutex_init(struct mutex* obj) ICODE_ATTR;
111
int mutex_lock(struct mutex* obj, int timeout) ICODE_ATTR;
112
int mutex_unlock(struct mutex* obj) ICODE_ATTR;
113
void wakeup_init(struct wakeup* obj) ICODE_ATTR;
114
int wakeup_wait(struct wakeup* obj, int timeout) ICODE_ATTR;
115
int wakeup_signal(struct wakeup* obj) ICODE_ATTR;
116
void sleep(int usecs) ICODE_ATTR;
117
 
118
static inline void yield()
119
{
120
    context_switch();
121
}
122
 
123
#endif