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
#include "global.h"
25
#include "thread.h"
26
#include "timer.h"
27
#include "panic.h"
28
#include "util.h"
85 theseven 29
#ifdef HAVE_STORAGE
58 theseven 30
#include "dir.h"
31
#include "file.h"
85 theseven 32
#endif
130 theseven 33
#ifdef HAVE_BUTTON
34
#include "button.h"
35
#endif
14 theseven 36
 
37
 
15 theseven 38
struct scheduler_thread scheduler_threads[MAX_THREADS] IBSS_ATTR;
39
struct scheduler_thread* current_thread IBSS_ATTR;
40
uint32_t last_tick IBSS_ATTR;
34 theseven 41
bool scheduler_frozen IBSS_ATTR;
15 theseven 42
extern struct wakeup dbgwakeup;
14 theseven 43
 
44
 
45
void mutex_init(struct mutex* obj)
46
{
47
    memset(obj, 0, sizeof(struct mutex));
48
}
49
 
50
void mutex_add_to_queue(struct mutex* obj, struct scheduler_thread* thread)
51
{
52
    struct scheduler_thread* t;
15 theseven 53
    if (!obj->waiters || obj->waiters->priority <= thread->priority)
14 theseven 54
    {
55
        thread->queue_next = obj->waiters;
56
        obj->waiters = thread;
57
    }
58
    else
59
    {
60
        t = obj->waiters;
61
        while (t->queue_next && t->queue_next->priority > thread->priority)
62
            t = t->queue_next;
63
        thread->queue_next = t->queue_next;
64
        t->queue_next = thread;
65
    }
66
}
67
 
68
void mutex_remove_from_queue(struct mutex* obj, struct scheduler_thread* thread)
69
{
70
    struct scheduler_thread* t;
71
    if (!obj->waiters) return;
72
    if (obj->waiters == thread) obj->waiters = thread->queue_next;
73
    else
74
    {
75
        t = obj->waiters;
76
        while (t->queue_next)
77
        {
78
            if (t->queue_next == thread) t->queue_next = thread->queue_next;
79
            t = t->queue_next;
80
        }
81
    }
82
}
83
 
84
int mutex_lock(struct mutex* obj, int timeout)
85
{
86
    int ret = THREAD_OK;
87
    struct scheduler_thread* thread;
88
    uint32_t mode = enter_critical_section();
89
 
90
    if (!obj->count)
91
    {
92
        obj->count = 1;
93
        obj->owner = current_thread;
94
    }
95
    else if (obj->owner == current_thread) obj->count++;
96
    else
97
    {
98
        if (timeout)
99
        {
100
            current_thread->state = THREAD_BLOCKED;
101
            current_thread->block_type = THREAD_BLOCK_MUTEX;
102
            current_thread->blocked_by = obj;
103
            current_thread->timeout = timeout;
104
            current_thread->blocked_since = USEC_TIMER;
105
            mutex_add_to_queue(obj, current_thread);
106
            leave_critical_section(mode);
107
            context_switch();
108
            if (obj->owner != current_thread) return THREAD_TIMEOUT;
109
            return THREAD_OK;
110
        }
111
        else ret = THREAD_TIMEOUT;
112
    }
113
 
114
    leave_critical_section(mode);
115
    return ret;
116
}
117
 
118
int mutex_unlock(struct mutex* obj)
119
{
120
    int ret = THREAD_OK;
121
    uint32_t mode = enter_critical_section();
122
 
123
    if (!obj->count)
124
    {
125
        leave_critical_section(mode);
126
        panicf(PANIC_KILLTHREAD, "Trying to unlock non-owned mutex! (%08X)", obj);
127
    }
128
 
129
    if (obj->owner != current_thread)
130
    {
131
        leave_critical_section(mode);
132
        panicf(PANIC_KILLTHREAD, "Trying to unlock mutex owned by different thread! (%08X)", obj);
133
    }
134
 
135
    if (--(obj->count)) ret = obj->count;
136
    else if (obj->waiters)
137
    {
138
        obj->count = 1;
139
        obj->owner = obj->waiters;
140
        obj->waiters->state = THREAD_READY;
141
        obj->waiters->block_type = THREAD_NOT_BLOCKED;
142
        obj->waiters->blocked_by = NULL;
143
        obj->waiters->timeout = 0;
144
        obj->waiters = obj->waiters->queue_next;
145
    }
146
 
147
    leave_critical_section(mode);
148
    return ret;
149
}
150
 
151
void wakeup_init(struct wakeup* obj)
152
{
153
    memset(obj, 0, sizeof(struct wakeup));
154
}
155
 
156
int wakeup_wait(struct wakeup* obj, int timeout)
157
{
158
    int ret = THREAD_OK;
159
    uint32_t mode = enter_critical_section();
160
 
161
    if (obj->waiter)
162
    {
163
        leave_critical_section(mode);
164
        panicf(PANIC_KILLTHREAD, "Multiple threads waiting single wakeup! (%08X)", obj);
165
    }
166
 
167
    if (obj->signalled) obj->signalled = false;
168
    else
169
    {
170
        if (timeout)
171
        {
172
            current_thread->state = THREAD_BLOCKED;
173
            current_thread->block_type = THREAD_BLOCK_WAKEUP;
174
            current_thread->blocked_by = obj;
175
            current_thread->timeout = timeout;
176
            current_thread->blocked_since = USEC_TIMER;
177
            obj->waiter = current_thread;
178
            leave_critical_section(mode);
179
            context_switch();
15 theseven 180
            obj->waiter = NULL;
14 theseven 181
            if (!obj->signalled) return THREAD_TIMEOUT;
182
            obj->signalled = false;
183
            return THREAD_OK;
184
        }
185
        else ret = THREAD_TIMEOUT;
186
    }
187
 
188
    leave_critical_section(mode);
189
    return ret;
190
}
191
 
192
int wakeup_signal(struct wakeup* obj)
193
{
194
    int ret = THREAD_OK;
195
    uint32_t mode = enter_critical_section();
196
 
197
    obj->signalled = true;
198
    if (obj->waiter)
199
    {
200
        obj->waiter->state = THREAD_READY;
201
        obj->waiter->block_type = THREAD_NOT_BLOCKED;
202
        obj->waiter->blocked_by = NULL;
203
        obj->waiter->timeout = 0;
204
        ret = THREAD_FOUND;
205
    }
206
 
207
    leave_critical_section(mode);
208
    return ret;
209
}
210
 
211
void sleep(int usecs)
212
{
15 theseven 213
    if (usecs)
214
    {
215
        uint32_t mode = enter_critical_section();
216
        current_thread->state = THREAD_BLOCKED;
217
        current_thread->block_type = THREAD_BLOCK_SLEEP;
218
        current_thread->timeout = usecs;
219
        current_thread->blocked_since = USEC_TIMER;
220
        leave_critical_section(mode);
221
    }
14 theseven 222
    context_switch();
223
}
224
 
225
void scheduler_init(void)
226
{
227
    memset(scheduler_threads, 0, sizeof(scheduler_threads));
34 theseven 228
    scheduler_frozen = false;
14 theseven 229
    last_tick = USEC_TIMER;
230
    current_thread = scheduler_threads;
231
    current_thread->state = THREAD_RUNNING;
232
    current_thread->startusec = last_tick;
233
    current_thread->name = "idle thread";
234
    current_thread->stack = (uint32_t*)-1;
235
    setup_tick();
236
}
237
 
54 theseven 238
bool scheduler_freeze(bool value)
34 theseven 239
{
54 theseven 240
    bool old = scheduler_frozen;
34 theseven 241
    scheduler_frozen = value;
54 theseven 242
    return old;
34 theseven 243
}
244
 
14 theseven 245
void scheduler_switch(int thread)
246
{
247
    int i;
248
    uint32_t score, best;
249
    uint32_t usec = USEC_TIMER;
250
    if (current_thread->state == THREAD_RUNNING) current_thread->state = THREAD_READY;
251
    current_thread->cputime_total += usec - current_thread->startusec;
252
    current_thread->cputime_current += usec - current_thread->startusec;
253
    if ((int)current_thread->stack != -1 && *current_thread->stack != 0xaffebeaf)
15 theseven 254
    {
255
        for (i = 0; i < MAX_THREADS; i++)
256
            if (scheduler_threads[i].type == USER_THREAD)
257
                scheduler_threads[i].state = THREAD_SUSPENDED;
258
        current_thread->state = THREAD_DEFUNCT;
259
        current_thread->block_type = THREAD_DEFUNCT_STKOV;
260
        wakeup_signal(&dbgwakeup);
261
    }
14 theseven 262
 
263
    if (usec - last_tick > SCHEDULER_TICK)
264
    {
15 theseven 265
        last_tick = usec;
14 theseven 266
        for (i = 0; i < MAX_THREADS; i++)
267
        {
136 theseven 268
            scheduler_threads[i].cpuload = 255 * scheduler_threads[i].cputime_current / SCHEDULER_TICK;
14 theseven 269
            scheduler_threads[i].cputime_current = 0;
270
        }
271
    }
272
 
34 theseven 273
    if (scheduler_frozen) thread = 0;
14 theseven 274
    else
275
    {
276
        for (i = 0; i < MAX_THREADS; i++)
34 theseven 277
            if (scheduler_threads[i].state == THREAD_BLOCKED
278
             && scheduler_threads[i].timeout != -1
279
             && TIME_AFTER(usec, scheduler_threads[i].blocked_since
280
                               + scheduler_threads[i].timeout))
14 theseven 281
            {
34 theseven 282
                if (scheduler_threads[i].block_type == THREAD_BLOCK_MUTEX)
283
                    mutex_remove_from_queue((struct mutex*)scheduler_threads[i].blocked_by,
284
                                            &scheduler_threads[i]);
285
                scheduler_threads[i].state = THREAD_READY;
286
                scheduler_threads[i].block_type = THREAD_NOT_BLOCKED;
287
                scheduler_threads[i].blocked_by = NULL;
288
                scheduler_threads[i].timeout = 0;
289
            }
290
 
291
        if (thread >= 0 && thread < MAX_THREADS && scheduler_threads[thread].state == THREAD_READY)
292
            current_thread = &scheduler_threads[thread];
293
        else
294
        {
295
            thread = 0;
296
            best = 0xffffffff;
297
            for (i = 0; i < MAX_THREADS; i++)
298
                if (scheduler_threads[i].state == THREAD_READY && scheduler_threads[i].priority)
14 theseven 299
                {
34 theseven 300
                    score = scheduler_threads[i].cputime_current / scheduler_threads[i].priority;
301
                    if (score < best)
302
                    {
303
                        best = score;
304
                        thread = i;
305
                    }
14 theseven 306
                }
34 theseven 307
        }
14 theseven 308
    }
309
 
310
    current_thread = &scheduler_threads[thread];
311
    current_thread->state = THREAD_RUNNING;
312
    current_thread->startusec = USEC_TIMER;
313
}
314
 
315
int thread_create(const char* name, const void* code, void* stack,
15 theseven 316
                  int stacksize, enum thread_type type, int priority, bool run)
14 theseven 317
{
318
    int ret = NO_MORE_THREADS;
319
    int i;
320
 
321
    for (i = 0; i < stacksize >> 2; i ++) ((uint32_t*)stack)[i] = 0xaffebeaf;
322
 
323
    uint32_t mode = enter_critical_section();
324
 
325
    for (i = 0; i < MAX_THREADS; i++)
326
        if (scheduler_threads[i].state == THREAD_FREE)
327
        {
328
            ret = i;
329
            memset(&scheduler_threads[i], 0, sizeof(struct scheduler_thread));
330
            scheduler_threads[i].state = run ? THREAD_READY : THREAD_SUSPENDED;
15 theseven 331
            scheduler_threads[i].type = type;
14 theseven 332
            scheduler_threads[i].name = name;
333
            scheduler_threads[i].priority = priority;
43 theseven 334
            scheduler_threads[i].cpsr = 0x1f;
14 theseven 335
            scheduler_threads[i].regs[15] = (uint32_t)code;
336
            scheduler_threads[i].regs[14] = (uint32_t)thread_exit;
337
            scheduler_threads[i].regs[13] = (uint32_t)stack + stacksize;
338
            scheduler_threads[i].stack = stack;
339
            break;
340
        }
341
 
342
    leave_critical_section(mode);
343
    return ret;
344
}
345
 
346
int thread_suspend(int thread)
347
{
348
    int ret = THREAD_OK;
349
    struct scheduler_thread* t = &scheduler_threads[thread];
350
    bool needsswitch = false;
351
    uint32_t mode = enter_critical_section();
352
 
353
    if (thread == -1) t = current_thread;
354
    else if (thread < 0 || thread >= MAX_THREADS) ret = INVALID_THREAD;
355
    else if (t->state == THREAD_FREE) ret = INVALID_THREAD;
356
    else if (t->state == THREAD_SUSPENDED) ret = ALREADY_SUSPENDED;
357
    if (ret == THREAD_OK)
358
    {
359
        if (t->state == THREAD_RUNNING) needsswitch = true;
360
        else if (t->state == THREAD_BLOCKED)
361
        {
362
            if (t->block_type == THREAD_BLOCK_SLEEP)
15 theseven 363
            {
364
                if (t->timeout != -1) t->timeout -= USEC_TIMER - t->blocked_since;
365
            }
14 theseven 366
            else if (t->block_type == THREAD_BLOCK_MUTEX)
367
            {
368
                mutex_remove_from_queue((struct mutex*)t->blocked_by, t);
15 theseven 369
                if (t->timeout != -1) t->timeout -= USEC_TIMER - t->blocked_since;
14 theseven 370
            }
371
            else if (t->block_type == THREAD_BLOCK_WAKEUP)
15 theseven 372
            {
373
                if (t->timeout != -1) t->timeout -= USEC_TIMER - t->blocked_since;
374
            }
14 theseven 375
        }
376
        t->state = THREAD_SUSPENDED;
377
    }
378
 
379
    leave_critical_section(mode);
380
 
381
    if (needsswitch) context_switch();
382
 
383
    return ret;
384
}
385
 
386
int thread_resume(int thread)
387
{
388
    int ret = THREAD_OK;
389
    struct scheduler_thread* t = &scheduler_threads[thread];
390
    bool needsswitch = false;
391
    uint32_t mode = enter_critical_section();
392
 
393
    if (thread == -1) t = current_thread;
394
    else if (thread < 0 || thread >= MAX_THREADS) ret = INVALID_THREAD;
395
    else if (t->state == THREAD_FREE) ret = INVALID_THREAD;
396
    else if (t->state != THREAD_SUSPENDED) ret = ALREADY_RESUMED;
397
    if (ret == THREAD_OK)
398
    {
399
        if (t->block_type == THREAD_BLOCK_SLEEP)
400
            t->blocked_since = USEC_TIMER;
401
        else if (t->block_type == THREAD_BLOCK_MUTEX)
402
        {
403
            mutex_add_to_queue((struct mutex*)t->blocked_by, t);
404
            t->blocked_since = USEC_TIMER;
405
            t->state = THREAD_BLOCKED;
406
        }
407
        else if (t->block_type == THREAD_BLOCK_WAKEUP)
408
        {
409
            t->blocked_since = USEC_TIMER;
410
            t->state = THREAD_BLOCKED;
411
        }
412
        else t->state = THREAD_READY;
413
    }
414
 
415
    leave_critical_section(mode);
416
    return ret;
417
}
418
 
419
int thread_terminate(int thread)
420
{
421
    int ret = THREAD_OK;
422
    struct scheduler_thread* t = &scheduler_threads[thread];
423
    bool needsswitch = false;
424
    uint32_t mode = enter_critical_section();
425
 
426
    if (thread == -1) t = current_thread;
427
    else if (thread < 0 || thread >= MAX_THREADS) ret = INVALID_THREAD;
428
    else if (t->state == THREAD_FREE) ret = INVALID_THREAD;
429
    if (ret == THREAD_OK)
430
    {
431
        if (t->state == THREAD_RUNNING) needsswitch = true;
432
        else if (t->state == THREAD_BLOCKED)
433
        {
434
            if (t->block_type == THREAD_BLOCK_MUTEX)
435
                mutex_remove_from_queue((struct mutex*)t->blocked_by, t);
436
            else if (t->block_type == THREAD_BLOCK_WAKEUP)
437
                ((struct wakeup*)t->blocked_by)->waiter = NULL;
438
        }
439
        t->state = THREAD_FREE;
85 theseven 440
#ifdef HAVE_STORAGE
58 theseven 441
        close_all_of_process(t);
442
        closedir_all_of_process(t);
85 theseven 443
#endif
130 theseven 444
#ifdef HAVE_BUTTON
445
        button_unregister_all_of_thread(t);
446
#endif
14 theseven 447
    }
448
 
449
    leave_critical_section(mode);
450
 
451
    if (needsswitch) context_switch();
452
 
453
    return ret;
454
}
455
 
456
void thread_exit()
457
{
458
    thread_terminate(-1);
459
}
71 theseven 460
 
461
int* __errno()
462
{
463
    return &current_thread->err_no;
464
}