Subversion Repositories freemyipod

Rev

Go to most recent revision | 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
#define TIMEOUT_NONE 0
33
#define TIMEOUT_BLOCK -1
34
 
35
#define THREAD_FOUND 1
36
#define THREAD_OK 0
37
#define THREAD_TIMEOUT -1
38
#define NO_MORE_THREADS -2
39
#define INVALID_THREAD -3
40
#define ALREADY_SUSPENDED -4
41
#define ALREADY_RESUMED -5
42
 
43
 
44
enum thread_state
45
{
46
    THREAD_FREE = 0,
47
    THREAD_READY,
48
    THREAD_RUNNING,
49
    THREAD_BLOCKED,
15 theseven 50
    THREAD_SUSPENDED,
51
    THREAD_DEFUNCT,
52
    THREAD_DEFUNCT_ACK
14 theseven 53
};
54
 
55
enum thread_block
56
{
57
    THREAD_NOT_BLOCKED = 0,
58
    THREAD_BLOCK_SLEEP,
59
    THREAD_BLOCK_MUTEX,
15 theseven 60
    THREAD_BLOCK_WAKEUP,
61
    THREAD_DEFUNCT_STKOV
14 theseven 62
};
63
 
15 theseven 64
enum thread_type
65
{
66
    USER_THREAD = 0,
67
    SYSTEM_THREAD
68
};
69
 
31 theseven 70
 
71
#define SCHEDULER_THREAD_INFO_VERSION 1
72
 
14 theseven 73
struct scheduler_thread
74
{
75
    uint32_t regs[16];
76
    uint32_t cpsr;
77
    const char* name;
78
    uint64_t cputime_total;
79
    uint32_t cputime_current;
80
    uint32_t startusec;
81
    struct scheduler_thread* queue_next;
82
    uint32_t timeout;
83
    uint32_t blocked_since;
84
    void* blocked_by;
85
    uint32_t* stack;
86
    enum thread_state state;
87
    enum thread_block block_type;
15 theseven 88
    enum thread_type type;
14 theseven 89
    uint8_t priority;
90
    uint8_t cpuload;
91
};
92
 
93
struct mutex
94
{
95
    struct scheduler_thread* owner;
96
    struct scheduler_thread* waiters;
97
    int count;
98
};
99
 
100
struct wakeup
101
{
102
    struct scheduler_thread* waiter;
103
    bool signalled;
104
};
105
 
106
 
107
void scheduler_init() INITCODE_ATTR;
108
void scheduler_switch(int thread) ICODE_ATTR;
34 theseven 109
void scheduler_freeze(bool value);
14 theseven 110
int thread_create(const char* name, const void* code, void* stack,
15 theseven 111
                  int stacksize, enum thread_type type, int priority, bool run);
14 theseven 112
int thread_suspend(int thread);
113
int thread_resume(int thread);
114
int thread_terminate(int thread);
115
void thread_exit();
116
void mutex_init(struct mutex* obj) ICODE_ATTR;
117
int mutex_lock(struct mutex* obj, int timeout) ICODE_ATTR;
118
int mutex_unlock(struct mutex* obj) ICODE_ATTR;
119
void wakeup_init(struct wakeup* obj) ICODE_ATTR;
120
int wakeup_wait(struct wakeup* obj, int timeout) ICODE_ATTR;
121
int wakeup_signal(struct wakeup* obj) ICODE_ATTR;
122
void sleep(int usecs) ICODE_ATTR;
123
 
124
static inline void yield()
125
{
126
    context_switch();
127
}
128
 
129
#endif