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
 
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 ALREADY_SUSPENDED -4
39
#define ALREADY_RESUMED -5
40
 
41
 
42
enum thread_state
43
{
44
    THREAD_FREE = 0,
43 theseven 45
    THREAD_SUSPENDED = 1,
14 theseven 46
    THREAD_READY,
47
    THREAD_RUNNING,
48
    THREAD_BLOCKED,
15 theseven 49
    THREAD_DEFUNCT,
50
    THREAD_DEFUNCT_ACK
14 theseven 51
};
52
 
53
enum thread_block
54
{
55
    THREAD_NOT_BLOCKED = 0,
56
    THREAD_BLOCK_SLEEP,
57
    THREAD_BLOCK_MUTEX,
15 theseven 58
    THREAD_BLOCK_WAKEUP,
50 theseven 59
    THREAD_DEFUNCT_STKOV,
60
    THREAD_DEFUNCT_PANIC
14 theseven 61
};
62
 
15 theseven 63
enum thread_type
64
{
65
    USER_THREAD = 0,
132 theseven 66
    OS_THREAD,
67
    CORE_THREAD
15 theseven 68
};
69
 
31 theseven 70
 
429 theseven 71
#define SCHEDULER_THREAD_INFO_VERSION 2
31 theseven 72
 
14 theseven 73
struct scheduler_thread
74
{
75
    uint32_t regs[16];
76
    uint32_t cpsr;
43 theseven 77
    uint32_t state;
14 theseven 78
    const char* name;
43 theseven 79
    uint32_t cputime_current;
14 theseven 80
    uint64_t cputime_total;
81
    uint32_t startusec;
429 theseven 82
    struct scheduler_thread* thread_next;
14 theseven 83
    struct scheduler_thread* queue_next;
84
    uint32_t timeout;
85
    uint32_t blocked_since;
86
    void* blocked_by;
87
    uint32_t* stack;
71 theseven 88
    int err_no;
14 theseven 89
    enum thread_block block_type;
15 theseven 90
    enum thread_type type;
14 theseven 91
    uint8_t priority;
92
    uint8_t cpuload;
93
};
94
 
95
struct mutex
96
{
97
    struct scheduler_thread* owner;
98
    struct scheduler_thread* waiters;
99
    int count;
100
};
101
 
102
struct wakeup
103
{
104
    struct scheduler_thread* waiter;
105
    bool signalled;
106
};
107
 
108
 
429 theseven 109
extern struct scheduler_thread* head_thread IBSS_ATTR;
110
extern struct scheduler_thread* current_thread IBSS_ATTR;
111
 
112
 
14 theseven 113
void scheduler_init() INITCODE_ATTR;
389 theseven 114
void scheduler_pause_accounting() ICODE_ATTR;
115
void scheduler_resume_accounting() ICODE_ATTR;
595 theseven 116
void scheduler_switch(struct scheduler_thread* thread, struct scheduler_thread* block) ICODE_ATTR;
54 theseven 117
bool scheduler_freeze(bool value);
429 theseven 118
struct scheduler_thread* thread_create(struct scheduler_thread* thread, const char* name,
119
                                       const void* code, void* stack, int stacksize,
120
                                       enum thread_type type, int priority, bool run);
121
int thread_suspend(struct scheduler_thread* thread);
122
int thread_resume(struct scheduler_thread* thread);
453 theseven 123
void thread_set_name(struct scheduler_thread* thread, char* name);
124
void thread_set_priority(struct scheduler_thread* thread, int priority);
429 theseven 125
int thread_terminate(struct scheduler_thread* thread);
423 theseven 126
int thread_killlevel(enum thread_type type, bool killself);
429 theseven 127
enum thread_state thread_get_state(struct scheduler_thread* thread);
14 theseven 128
void thread_exit();
129
void mutex_init(struct mutex* obj) ICODE_ATTR;
130
int mutex_lock(struct mutex* obj, int timeout) ICODE_ATTR;
131
int mutex_unlock(struct mutex* obj) ICODE_ATTR;
132
void wakeup_init(struct wakeup* obj) ICODE_ATTR;
133
int wakeup_wait(struct wakeup* obj, int timeout) ICODE_ATTR;
134
int wakeup_signal(struct wakeup* obj) ICODE_ATTR;
135
void sleep(int usecs) ICODE_ATTR;
136
 
137
#endif