1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Dan Everton
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
25 #include <SDL_thread.h>
29 #include "system-sdl.h"
30 #include "thread-sdl.h"
36 /* Define this as 1 to show informational messages that are not errors. */
37 #define THREAD_SDL_DEBUGF_ENABLED 0
39 #if THREAD_SDL_DEBUGF_ENABLED
40 #define THREAD_SDL_DEBUGF(...) DEBUGF(__VA_ARGS__)
41 static char __name
[32];
42 #define THREAD_SDL_GET_NAME(thread) \
43 ({ thread_get_name(__name, ARRAYLEN(__name), thread); __name; })
45 #define THREAD_SDL_DEBUGF(...)
46 #define THREAD_SDL_GET_NAME(thread)
49 #define THREAD_PANICF(str...) \
50 ({ fprintf(stderr, str); exit(-1); })
52 /* Thread/core entries as in rockbox core */
53 static struct core_entry cores
[NUM_CORES
];
54 struct thread_entry threads
[MAXTHREADS
];
55 /* Jump buffers for graceful exit - kernel threads don't stay neatly
56 * in their start routines responding to messages so this is the only
57 * way to get them back in there so they may exit */
58 static jmp_buf thread_jmpbufs
[MAXTHREADS
];
59 /* this mutex locks out other Rockbox threads while one runs,
60 * that enables us to simulate a cooperative environment even if
61 * the host is preemptive */
63 static volatile bool threads_exit
= false;
65 extern long start_tick
;
67 void sim_thread_shutdown(void)
71 /* Tell all threads jump back to their start routines, unlock and exit
72 gracefully - we'll check each one in turn for it's status. Threads
73 _could_ terminate via remove_thread or multiple threads could exit
74 on each unlock but that is safe. */
76 /* Do this before trying to acquire lock */
82 for (i
= 0; i
< MAXTHREADS
; i
++)
84 struct thread_entry
*thread
= &threads
[i
];
85 /* exit all current threads, except the main one */
86 if (thread
->context
.t
!= NULL
)
88 /* Signal thread on delay or block */
89 SDL_Thread
*t
= thread
->context
.t
;
90 SDL_SemPost(thread
->context
.s
);
92 /* Wait for it to finish */
93 SDL_WaitThread(t
, NULL
);
94 /* Relock for next thread signal */
103 static void new_thread_id(unsigned int slot_num
,
104 struct thread_entry
*thread
)
106 unsigned int version
=
107 (thread
->id
+ (1u << THREAD_ID_VERSION_SHIFT
))
108 & THREAD_ID_VERSION_MASK
;
111 version
= 1u << THREAD_ID_VERSION_SHIFT
;
113 thread
->id
= version
| (slot_num
& THREAD_ID_SLOT_MASK
);
116 static struct thread_entry
* find_empty_thread_slot(void)
118 struct thread_entry
*thread
= NULL
;
121 for (n
= 0; n
< MAXTHREADS
; n
++)
123 int state
= threads
[n
].state
;
125 if (state
== STATE_KILLED
)
127 thread
= &threads
[n
];
136 /* Initialize SDL threading */
137 void init_threads(void)
139 struct thread_entry
*thread
;
142 memset(cores
, 0, sizeof(cores
));
143 memset(threads
, 0, sizeof(threads
));
145 m
= SDL_CreateMutex();
147 if (SDL_LockMutex(m
) == -1)
149 fprintf(stderr
, "Couldn't lock mutex\n");
153 /* Initialize all IDs */
154 for (n
= 0; n
< MAXTHREADS
; n
++)
155 threads
[n
].id
= THREAD_ID_INIT(n
);
157 /* Slot 0 is reserved for the main thread - initialize it here and
158 then create the SDL thread - it is possible to have a quick, early
159 shutdown try to access the structure. */
160 thread
= &threads
[0];
161 thread
->stack
= (uintptr_t *)" ";
162 thread
->stack_size
= 8;
163 thread
->name
= "main";
164 thread
->state
= STATE_RUNNING
;
165 thread
->context
.s
= SDL_CreateSemaphore(0);
166 thread
->context
.t
= NULL
; /* NULL for the implicit main thread */
167 cores
[CURRENT_CORE
].running
= thread
;
169 if (thread
->context
.s
== NULL
)
171 fprintf(stderr
, "Failed to create main semaphore\n");
175 THREAD_SDL_DEBUGF("Main thread: %p\n", thread
);
180 void sim_thread_exception_wait(void)
190 /* A way to yield and leave the threading system for extended periods */
191 void sim_thread_lock(void *me
)
194 cores
[CURRENT_CORE
].running
= (struct thread_entry
*)me
;
200 void * sim_thread_unlock(void)
202 struct thread_entry
*current
= cores
[CURRENT_CORE
].running
;
207 struct thread_entry
* thread_id_entry(unsigned int thread_id
)
209 return (thread_id
== THREAD_ID_CURRENT
) ?
210 cores
[CURRENT_CORE
].running
:
211 &threads
[thread_id
& THREAD_ID_SLOT_MASK
];
214 static void add_to_list_l(struct thread_entry
**list
,
215 struct thread_entry
*thread
)
219 /* Insert into unoccupied list */
220 thread
->l
.next
= thread
;
221 thread
->l
.prev
= thread
;
227 thread
->l
.next
= *list
;
228 thread
->l
.prev
= (*list
)->l
.prev
;
229 thread
->l
.prev
->l
.next
= thread
;
230 (*list
)->l
.prev
= thread
;
234 static void remove_from_list_l(struct thread_entry
**list
,
235 struct thread_entry
*thread
)
237 if (thread
== thread
->l
.next
)
246 /* List becomes next item */
247 *list
= thread
->l
.next
;
250 /* Fix links to jump over the removed entry. */
251 thread
->l
.prev
->l
.next
= thread
->l
.next
;
252 thread
->l
.next
->l
.prev
= thread
->l
.prev
;
255 unsigned int thread_get_current(void)
257 return cores
[CURRENT_CORE
].running
->id
;
260 void switch_thread(void)
262 struct thread_entry
*current
= cores
[CURRENT_CORE
].running
;
266 switch (current
->state
)
271 /* Any other thread waiting already will get it first */
274 } /* STATE_RUNNING: */
281 SDL_SemWait(current
->context
.s
);
284 oldlevel
= disable_irq_save();
285 current
->state
= STATE_RUNNING
;
286 restore_irq(oldlevel
);
288 } /* STATE_BLOCKED: */
290 case STATE_BLOCKED_W_TMO
:
292 int result
, oldlevel
;
295 result
= SDL_SemWaitTimeout(current
->context
.s
, current
->tmo_tick
);
298 oldlevel
= disable_irq_save();
300 if (current
->state
== STATE_BLOCKED_W_TMO
)
303 remove_from_list_l(current
->bqp
, current
);
305 #ifdef HAVE_WAKEUP_EXT_CB
306 if (current
->wakeup_ext_cb
!= NULL
)
307 current
->wakeup_ext_cb(current
);
309 current
->state
= STATE_RUNNING
;
312 if (result
== SDL_MUTEX_TIMEDOUT
)
314 /* Other signals from an explicit wake could have been made before
315 * arriving here if we timed out waiting for the semaphore. Make
316 * sure the count is reset. */
317 while (SDL_SemValue(current
->context
.s
) > 0)
318 SDL_SemTryWait(current
->context
.s
);
321 restore_irq(oldlevel
);
323 } /* STATE_BLOCKED_W_TMO: */
328 SDL_SemWaitTimeout(current
->context
.s
, current
->tmo_tick
);
330 current
->state
= STATE_RUNNING
;
332 } /* STATE_SLEEPING: */
335 cores
[CURRENT_CORE
].running
= current
;
341 void sleep_thread(int ticks
)
343 struct thread_entry
*current
= cores
[CURRENT_CORE
].running
;
346 current
->state
= STATE_SLEEPING
;
348 rem
= (SDL_GetTicks() - start_tick
) % (1000/HZ
);
352 current
->tmo_tick
= (1000/HZ
) * ticks
+ ((1000/HZ
)-1) - rem
;
355 void block_thread(struct thread_entry
*current
)
357 current
->state
= STATE_BLOCKED
;
358 add_to_list_l(current
->bqp
, current
);
361 void block_thread_w_tmo(struct thread_entry
*current
, int ticks
)
363 current
->state
= STATE_BLOCKED_W_TMO
;
364 current
->tmo_tick
= (1000/HZ
)*ticks
;
365 add_to_list_l(current
->bqp
, current
);
368 unsigned int wakeup_thread(struct thread_entry
**list
)
370 struct thread_entry
*thread
= *list
;
374 switch (thread
->state
)
377 case STATE_BLOCKED_W_TMO
:
378 remove_from_list_l(list
, thread
);
379 thread
->state
= STATE_RUNNING
;
380 SDL_SemPost(thread
->context
.s
);
388 unsigned int thread_queue_wake(struct thread_entry
**list
)
390 unsigned int result
= THREAD_NONE
;
394 unsigned int rc
= wakeup_thread(list
);
396 if (rc
== THREAD_NONE
)
405 void thread_thaw(unsigned int thread_id
)
407 struct thread_entry
*thread
= thread_id_entry(thread_id
);
409 if (thread
->id
== thread_id
&& thread
->state
== STATE_FROZEN
)
411 thread
->state
= STATE_RUNNING
;
412 SDL_SemPost(thread
->context
.s
);
416 int runthread(void *data
)
418 struct thread_entry
*current
;
419 jmp_buf *current_jmpbuf
;
421 /* Cannot access thread variables before locking the mutex as the
422 data structures may not be filled-in yet. */
424 cores
[CURRENT_CORE
].running
= (struct thread_entry
*)data
;
425 current
= cores
[CURRENT_CORE
].running
;
426 current_jmpbuf
= &thread_jmpbufs
[current
- threads
];
428 /* Setup jump for exit */
429 if (setjmp(*current_jmpbuf
) == 0)
431 /* Run the thread routine */
432 if (current
->state
== STATE_FROZEN
)
435 SDL_SemWait(current
->context
.s
);
437 cores
[CURRENT_CORE
].running
= current
;
442 current
->context
.start();
443 THREAD_SDL_DEBUGF("Thread Done: %d (%s)\n",
444 current
- threads
, THREAD_SDL_GET_NAME(current
));
445 /* Thread routine returned - suicide */
452 /* Unlock and exit */
459 unsigned int create_thread(void (*function
)(void),
460 void* stack
, size_t stack_size
,
461 unsigned flags
, const char *name
)
463 struct thread_entry
*thread
;
467 THREAD_SDL_DEBUGF("Creating thread: (%s)\n", name
? name
: "");
469 thread
= find_empty_thread_slot();
472 DEBUGF("Failed to find thread slot\n");
476 s
= SDL_CreateSemaphore(0);
479 DEBUGF("Failed to create semaphore\n");
483 t
= SDL_CreateThread(runthread
, thread
);
486 DEBUGF("Failed to create SDL thread\n");
487 SDL_DestroySemaphore(s
);
491 thread
->stack
= stack
;
492 thread
->stack_size
= stack_size
;
494 thread
->state
= (flags
& CREATE_THREAD_FROZEN
) ?
495 STATE_FROZEN
: STATE_RUNNING
;
496 thread
->context
.start
= function
;
497 thread
->context
.t
= t
;
498 thread
->context
.s
= s
;
500 THREAD_SDL_DEBUGF("New Thread: %d (%s)\n",
501 thread
- threads
, THREAD_SDL_GET_NAME(thread
));
506 #ifndef ALLOW_REMOVE_THREAD
507 static void remove_thread(unsigned int thread_id
)
509 void remove_thread(unsigned int thread_id
)
512 struct thread_entry
*current
= cores
[CURRENT_CORE
].running
;
513 struct thread_entry
*thread
= thread_id_entry(thread_id
);
518 if (thread_id
!= THREAD_ID_CURRENT
&& thread
->id
!= thread_id
)
521 int oldlevel
= disable_irq_save();
523 t
= thread
->context
.t
;
524 s
= thread
->context
.s
;
525 thread
->context
.t
= NULL
;
527 if (thread
!= current
)
529 switch (thread
->state
)
532 case STATE_BLOCKED_W_TMO
:
533 /* Remove thread from object it's waiting on */
534 remove_from_list_l(thread
->bqp
, thread
);
536 #ifdef HAVE_WAKEUP_EXT_CB
537 if (thread
->wakeup_ext_cb
!= NULL
)
538 thread
->wakeup_ext_cb(thread
);
546 THREAD_SDL_DEBUGF("Removing thread: %d (%s)\n",
547 thread
- threads
, THREAD_SDL_GET_NAME(thread
));
549 new_thread_id(thread
->id
, thread
);
550 thread
->state
= STATE_KILLED
;
551 thread_queue_wake(&thread
->queue
);
553 SDL_DestroySemaphore(s
);
555 if (thread
== current
)
557 /* Do a graceful exit - perform the longjmp back into the thread
558 function to return */
559 restore_irq(oldlevel
);
560 longjmp(thread_jmpbufs
[current
- threads
], 1);
564 restore_irq(oldlevel
);
567 void thread_exit(void)
569 remove_thread(THREAD_ID_CURRENT
);
572 void thread_wait(unsigned int thread_id
)
574 struct thread_entry
*current
= cores
[CURRENT_CORE
].running
;
575 struct thread_entry
*thread
= thread_id_entry(thread_id
);
577 if (thread_id
== THREAD_ID_CURRENT
||
578 (thread
->id
== thread_id
&& thread
->state
!= STATE_KILLED
))
580 current
->bqp
= &thread
->queue
;
581 block_thread(current
);
586 int thread_stack_usage(const struct thread_entry
*thread
)
592 /* Return name if one or ID if none */
593 void thread_get_name(char *buffer
, int size
,
594 struct thread_entry
*thread
)
603 /* Display thread name if one or ID if none */
604 bool named
= thread
->name
&& *thread
->name
;
605 const char *fmt
= named
? "%s" : "%08lX";
606 intptr_t name
= named
?
607 (intptr_t)thread
->name
: (intptr_t)thread
;
608 snprintf(buffer
, size
, fmt
, name
);