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
//
427 farthen 6
//    This file is part of emCORE.
14 theseven 7
//
427 farthen 8
//    emCORE is free software: you can redistribute it and/or
14 theseven 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
//
427 farthen 13
//    emCORE is distributed in the hope that it will be useful,
14 theseven 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
427 farthen 19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
14 theseven 20
//
21
//
22
 
23
 
24
#ifndef __THREAD_H__
25
#define __THREAD_H__
26
 
27
 
28
#include "global.h"
29
#include "contextswitch.h"
30
 
31
 
814 theseven 32
enum owner_type
33
{
34
    OWNER_THREAD = 0,
35
    OWNER_LIBRARY = 1,
36
    OWNER_KERNEL = 2,
37
    OWNER_RESERVED = 3
38
};
39
 
40
enum kernel_owner_type
41
{
42
    KERNEL_OWNER_UNKNOWN = 0,
43
    KERNEL_OWNER_USB_MONITOR = 1,
44
    KERNEL_OWNER_FILE_HANDLE = 2,
45
    KERNEL_OWNER_DIR_HANDLE = 3,
892 theseven 46
    KERNEL_OWNER_ATA_BBT = 4,
47
    KERNEL_OWNER_CUSTOM_USB = 5,
814 theseven 48
};
49
 
50
#define OWNER_TYPE(x, y) ((struct scheduler_thread*)((((int)(y)) & ~3) | x))
51
#define KERNEL_OWNER(x) ((struct scheduler_thread*)(((x) << 2) | OWNER_KERNEL))
52
 
53
 
14 theseven 54
#define TIMEOUT_NONE 0
55
#define TIMEOUT_BLOCK -1
56
 
57
#define THREAD_FOUND 1
58
#define THREAD_OK 0
59
#define THREAD_TIMEOUT -1
60
#define ALREADY_SUSPENDED -4
61
#define ALREADY_RESUMED -5
62
 
63
 
64
enum thread_state
65
{
66
    THREAD_FREE = 0,
43 theseven 67
    THREAD_SUSPENDED = 1,
14 theseven 68
    THREAD_READY,
69
    THREAD_RUNNING,
70
    THREAD_BLOCKED,
15 theseven 71
    THREAD_DEFUNCT,
72
    THREAD_DEFUNCT_ACK
14 theseven 73
};
74
 
75
enum thread_block
76
{
77
    THREAD_NOT_BLOCKED = 0,
78
    THREAD_BLOCK_SLEEP,
79
    THREAD_BLOCK_MUTEX,
15 theseven 80
    THREAD_BLOCK_WAKEUP,
50 theseven 81
    THREAD_DEFUNCT_STKOV,
82
    THREAD_DEFUNCT_PANIC
14 theseven 83
};
84
 
15 theseven 85
enum thread_type
86
{
87
    USER_THREAD = 0,
132 theseven 88
    OS_THREAD,
89
    CORE_THREAD
15 theseven 90
};
91
 
31 theseven 92
 
691 theseven 93
#define SCHEDULER_THREAD_INFO_VERSION 3
31 theseven 94
 
691 theseven 95
struct mutex;
96
 
14 theseven 97
struct scheduler_thread
98
{
99
    uint32_t regs[16];
100
    uint32_t cpsr;
43 theseven 101
    uint32_t state;
14 theseven 102
    const char* name;
43 theseven 103
    uint32_t cputime_current;
14 theseven 104
    uint64_t cputime_total;
105
    uint32_t startusec;
429 theseven 106
    struct scheduler_thread* thread_next;
14 theseven 107
    struct scheduler_thread* queue_next;
691 theseven 108
    struct mutex* owned_mutexes;
14 theseven 109
    uint32_t timeout;
110
    uint32_t blocked_since;
111
    void* blocked_by;
112
    uint32_t* stack;
71 theseven 113
    int err_no;
14 theseven 114
    enum thread_block block_type;
15 theseven 115
    enum thread_type type;
14 theseven 116
    uint8_t priority;
117
    uint8_t cpuload;
118
};
119
 
120
struct mutex
121
{
122
    struct scheduler_thread* owner;
123
    struct scheduler_thread* waiters;
691 theseven 124
    struct mutex* owned_next;
14 theseven 125
    int count;
126
};
127
 
128
struct wakeup
129
{
130
    struct scheduler_thread* waiter;
131
    bool signalled;
132
};
133
 
134
 
429 theseven 135
extern struct scheduler_thread* head_thread IBSS_ATTR;
136
extern struct scheduler_thread* current_thread IBSS_ATTR;
137
 
138
 
14 theseven 139
void scheduler_init() INITCODE_ATTR;
389 theseven 140
void scheduler_pause_accounting() ICODE_ATTR;
141
void scheduler_resume_accounting() ICODE_ATTR;
595 theseven 142
void scheduler_switch(struct scheduler_thread* thread, struct scheduler_thread* block) ICODE_ATTR;
54 theseven 143
bool scheduler_freeze(bool value);
429 theseven 144
struct scheduler_thread* thread_create(struct scheduler_thread* thread, const char* name,
835 theseven 145
                                       void (*const code)(void*, void*, void*, void*),
146
                                       void* stack, int stacksize,
147
                                       enum thread_type type, int priority, bool run,
148
                                       void* arg0, void* arg1, void* arg2, void* arg3);
429 theseven 149
int thread_suspend(struct scheduler_thread* thread);
150
int thread_resume(struct scheduler_thread* thread);
453 theseven 151
void thread_set_name(struct scheduler_thread* thread, char* name);
152
void thread_set_priority(struct scheduler_thread* thread, int priority);
429 theseven 153
int thread_terminate(struct scheduler_thread* thread);
423 theseven 154
int thread_killlevel(enum thread_type type, bool killself);
429 theseven 155
enum thread_state thread_get_state(struct scheduler_thread* thread);
14 theseven 156
void thread_exit();
157
void mutex_init(struct mutex* obj) ICODE_ATTR;
158
int mutex_lock(struct mutex* obj, int timeout) ICODE_ATTR;
159
int mutex_unlock(struct mutex* obj) ICODE_ATTR;
160
void wakeup_init(struct wakeup* obj) ICODE_ATTR;
161
int wakeup_wait(struct wakeup* obj, int timeout) ICODE_ATTR;
162
int wakeup_signal(struct wakeup* obj) ICODE_ATTR;
163
void sleep(int usecs) ICODE_ATTR;
164
 
165
#endif