summaryrefslogtreecommitdiff
path: root/firmware
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2011-03-05 17:48:06 +0000
committerThomas Martitz <kugel@rockbox.org>2011-03-05 17:48:06 +0000
commitcc889e9d608e6b07b78541849b7e63b6fb3f6058 (patch)
treeea01a47602b68561d294f705e8ab7669fb00ae9a /firmware
parent0b0f99b18ebe6305c9cab12bf8b36d154fc9c87f (diff)
downloadrockbox-cc889e9d608e6b07b78541849b7e63b6fb3f6058.tar.gz
rockbox-cc889e9d608e6b07b78541849b7e63b6fb3f6058.zip
Change the thread api a bit.
* Remove THREAD_ID_CURRENT macro in favor of a thread_self() function, this allows thread functions to be simpler. * thread_self_entry() shortcut for kernel.c. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29521 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware')
-rw-r--r--firmware/drivers/ata.c3
-rw-r--r--firmware/export/thread.h11
-rw-r--r--firmware/kernel.c18
-rw-r--r--firmware/storage.c4
-rw-r--r--firmware/target/arm/imx31/gigabeat-s/powermgmt-gigabeat-s.c2
-rw-r--r--firmware/target/hosted/sdl/thread-sdl.c22
-rw-r--r--firmware/thread.c42
-rw-r--r--firmware/usb.c4
8 files changed, 58 insertions, 48 deletions
diff --git a/firmware/drivers/ata.c b/firmware/drivers/ata.c
index a16a6cc15e..baef6691d4 100644
--- a/firmware/drivers/ata.c
+++ b/firmware/drivers/ata.c
@@ -99,8 +99,7 @@ static void ata_lock_init(struct ata_lock *l)
99 99
100static void ata_lock_lock(struct ata_lock *l) 100static void ata_lock_lock(struct ata_lock *l)
101{ 101{
102 struct thread_entry * const current = 102 struct thread_entry * const current = thread_self_entry();
103 thread_id_entry(THREAD_ID_CURRENT);
104 103
105 if (current == l->thread) 104 if (current == l->thread)
106 { 105 {
diff --git a/firmware/export/thread.h b/firmware/export/thread.h
index 0db849ab0a..d0f61f9d85 100644
--- a/firmware/export/thread.h
+++ b/firmware/export/thread.h
@@ -323,9 +323,6 @@ struct thread_entry
323#define THREAD_ID_SLOT_MASK 0x00ff 323#define THREAD_ID_SLOT_MASK 0x00ff
324#define THREAD_ID_INIT(n) ((1u << THREAD_ID_VERSION_SHIFT) | (n)) 324#define THREAD_ID_INIT(n) ((1u << THREAD_ID_VERSION_SHIFT) | (n))
325 325
326/* Specify current thread in a function taking an ID. */
327#define THREAD_ID_CURRENT ((unsigned int)-1)
328
329#ifdef HAVE_CORELOCK_OBJECT 326#ifdef HAVE_CORELOCK_OBJECT
330/* Operations to be performed just before stopping a thread and starting 327/* Operations to be performed just before stopping a thread and starting
331 a new one if specified before calling switch_thread */ 328 a new one if specified before calling switch_thread */
@@ -445,7 +442,13 @@ int thread_get_io_priority(unsigned int thread_id);
445#if NUM_CORES > 1 442#if NUM_CORES > 1
446unsigned int switch_core(unsigned int new_core); 443unsigned int switch_core(unsigned int new_core);
447#endif 444#endif
448unsigned int thread_get_current(void); 445
446/* Return the id of the calling thread. */
447unsigned int thread_self(void);
448
449/* Return the thread_entry for the calling thread.
450 * INTERNAL: Intended for use by kernel and not for programs. */
451struct thread_entry* thread_self_entry(void);
449 452
450/* Debugging info - only! */ 453/* Debugging info - only! */
451int thread_stack_usage(const struct thread_entry *thread); 454int thread_stack_usage(const struct thread_entry *thread);
diff --git a/firmware/kernel.c b/firmware/kernel.c
index a8718ebf34..e0879a3c59 100644
--- a/firmware/kernel.c
+++ b/firmware/kernel.c
@@ -509,7 +509,7 @@ void queue_wait(struct event_queue *q, struct queue_event *ev)
509 509
510#ifdef HAVE_PRIORITY_SCHEDULING 510#ifdef HAVE_PRIORITY_SCHEDULING
511 KERNEL_ASSERT(QUEUE_GET_THREAD(q) == NULL || 511 KERNEL_ASSERT(QUEUE_GET_THREAD(q) == NULL ||
512 QUEUE_GET_THREAD(q) == thread_id_entry(THREAD_ID_CURRENT), 512 QUEUE_GET_THREAD(q) == thread_self_entry(),
513 "queue_wait->wrong thread\n"); 513 "queue_wait->wrong thread\n");
514#endif 514#endif
515 515
@@ -527,7 +527,7 @@ void queue_wait(struct event_queue *q, struct queue_event *ev)
527 if (rd != q->write) /* A waking message could disappear */ 527 if (rd != q->write) /* A waking message could disappear */
528 break; 528 break;
529 529
530 current = thread_id_entry(THREAD_ID_CURRENT); 530 current = thread_self_entry();
531 531
532 IF_COP( current->obj_cl = &q->cl; ) 532 IF_COP( current->obj_cl = &q->cl; )
533 current->bqp = &q->queue; 533 current->bqp = &q->queue;
@@ -559,7 +559,7 @@ void queue_wait_w_tmo(struct event_queue *q, struct queue_event *ev, int ticks)
559 559
560#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME 560#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
561 KERNEL_ASSERT(QUEUE_GET_THREAD(q) == NULL || 561 KERNEL_ASSERT(QUEUE_GET_THREAD(q) == NULL ||
562 QUEUE_GET_THREAD(q) == thread_id_entry(THREAD_ID_CURRENT), 562 QUEUE_GET_THREAD(q) == thread_self_entry(),
563 "queue_wait_w_tmo->wrong thread\n"); 563 "queue_wait_w_tmo->wrong thread\n");
564#endif 564#endif
565 565
@@ -573,7 +573,7 @@ void queue_wait_w_tmo(struct event_queue *q, struct queue_event *ev, int ticks)
573 wr = q->write; 573 wr = q->write;
574 if (rd == wr && ticks > 0) 574 if (rd == wr && ticks > 0)
575 { 575 {
576 struct thread_entry *current = thread_id_entry(THREAD_ID_CURRENT); 576 struct thread_entry *current = thread_self_entry();
577 577
578 IF_COP( current->obj_cl = &q->cl; ) 578 IF_COP( current->obj_cl = &q->cl; )
579 current->bqp = &q->queue; 579 current->bqp = &q->queue;
@@ -658,7 +658,7 @@ intptr_t queue_send(struct event_queue *q, long id, intptr_t data)
658 { 658 {
659 struct queue_sender_list *send = q->send; 659 struct queue_sender_list *send = q->send;
660 struct thread_entry **spp = &send->senders[wr]; 660 struct thread_entry **spp = &send->senders[wr];
661 struct thread_entry *current = thread_id_entry(THREAD_ID_CURRENT); 661 struct thread_entry *current = thread_self_entry();
662 662
663 if(UNLIKELY(*spp)) 663 if(UNLIKELY(*spp))
664 { 664 {
@@ -893,7 +893,7 @@ void mutex_init(struct mutex *m)
893/* Gain ownership of a mutex object or block until it becomes free */ 893/* Gain ownership of a mutex object or block until it becomes free */
894void mutex_lock(struct mutex *m) 894void mutex_lock(struct mutex *m)
895{ 895{
896 struct thread_entry *current = thread_id_entry(THREAD_ID_CURRENT); 896 struct thread_entry *current = thread_self_entry();
897 897
898 if(current == mutex_get_thread(m)) 898 if(current == mutex_get_thread(m))
899 { 899 {
@@ -932,10 +932,10 @@ void mutex_lock(struct mutex *m)
932void mutex_unlock(struct mutex *m) 932void mutex_unlock(struct mutex *m)
933{ 933{
934 /* unlocker not being the owner is an unlocking violation */ 934 /* unlocker not being the owner is an unlocking violation */
935 KERNEL_ASSERT(mutex_get_thread(m) == thread_id_entry(THREAD_ID_CURRENT), 935 KERNEL_ASSERT(mutex_get_thread(m) == thread_self_entry(),
936 "mutex_unlock->wrong thread (%s != %s)\n", 936 "mutex_unlock->wrong thread (%s != %s)\n",
937 mutex_get_thread(m)->name, 937 mutex_get_thread(m)->name,
938 thread_id_entry(THREAD_ID_CURRENT)->name); 938 thread_self_entry()->name);
939 939
940 if(m->recursion > 0) 940 if(m->recursion > 0)
941 { 941 {
@@ -1019,7 +1019,7 @@ int semaphore_wait(struct semaphore *s, int timeout)
1019 else 1019 else
1020 { 1020 {
1021 /* too many waits - block until count is upped... */ 1021 /* too many waits - block until count is upped... */
1022 struct thread_entry * current = thread_id_entry(THREAD_ID_CURRENT); 1022 struct thread_entry * current = thread_self_entry();
1023 IF_COP( current->obj_cl = &s->cl; ) 1023 IF_COP( current->obj_cl = &s->cl; )
1024 current->bqp = &s->queue; 1024 current->bqp = &s->queue;
1025 /* return value will be OBJ_WAIT_SUCCEEDED after wait if wake was 1025 /* return value will be OBJ_WAIT_SUCCEEDED after wait if wake was
diff --git a/firmware/storage.c b/firmware/storage.c
index 867c68cf6b..69f28d94a8 100644
--- a/firmware/storage.c
+++ b/firmware/storage.c
@@ -71,14 +71,14 @@ static void storage_wait_turn(IF_MD_NONVOID(int drive))
71#ifndef HAVE_MULTIDRIVE 71#ifndef HAVE_MULTIDRIVE
72 int drive=0; 72 int drive=0;
73#endif 73#endif
74 int my_prio = thread_get_io_priority(THREAD_ID_CURRENT); 74 int my_prio = thread_get_io_priority(thread_self());
75 int loops=my_prio; 75 int loops=my_prio;
76 while(storage_should_wait(drive, my_prio) && (loops--)>=0) 76 while(storage_should_wait(drive, my_prio) && (loops--)>=0)
77 { 77 {
78 sleep(STORAGE_DELAY_UNIT); 78 sleep(STORAGE_DELAY_UNIT);
79 } 79 }
80 80
81 storage_last_thread[drive] = thread_get_current(); 81 storage_last_thread[drive] = thread_self();
82 storage_last_activity[drive] = current_tick; 82 storage_last_activity[drive] = current_tick;
83} 83}
84#endif 84#endif
diff --git a/firmware/target/arm/imx31/gigabeat-s/powermgmt-gigabeat-s.c b/firmware/target/arm/imx31/gigabeat-s/powermgmt-gigabeat-s.c
index 283c1a2c73..2df461015e 100644
--- a/firmware/target/arm/imx31/gigabeat-s/powermgmt-gigabeat-s.c
+++ b/firmware/target/arm/imx31/gigabeat-s/powermgmt-gigabeat-s.c
@@ -197,7 +197,7 @@ static enum
197static inline void charging_set_thread_priority(bool charging) 197static inline void charging_set_thread_priority(bool charging)
198{ 198{
199#ifdef HAVE_PRIORITY_SCHEDULING 199#ifdef HAVE_PRIORITY_SCHEDULING
200 thread_set_priority(THREAD_ID_CURRENT, 200 thread_set_priority(thread_self(),
201 charging ? PRIORITY_REALTIME : PRIORITY_SYSTEM); 201 charging ? PRIORITY_REALTIME : PRIORITY_SYSTEM);
202#endif 202#endif
203 (void)charging; 203 (void)charging;
diff --git a/firmware/target/hosted/sdl/thread-sdl.c b/firmware/target/hosted/sdl/thread-sdl.c
index 08f20367cc..eaffa86aee 100644
--- a/firmware/target/hosted/sdl/thread-sdl.c
+++ b/firmware/target/hosted/sdl/thread-sdl.c
@@ -253,9 +253,7 @@ void * sim_thread_unlock(void)
253 253
254struct thread_entry * thread_id_entry(unsigned int thread_id) 254struct thread_entry * thread_id_entry(unsigned int thread_id)
255{ 255{
256 return (thread_id == THREAD_ID_CURRENT) ? 256 return &threads[thread_id & THREAD_ID_SLOT_MASK];
257 cores[CURRENT_CORE].running :
258 &threads[thread_id & THREAD_ID_SLOT_MASK];
259} 257}
260 258
261static void add_to_list_l(struct thread_entry **list, 259static void add_to_list_l(struct thread_entry **list,
@@ -299,11 +297,16 @@ static void remove_from_list_l(struct thread_entry **list,
299 thread->l.next->l.prev = thread->l.prev; 297 thread->l.next->l.prev = thread->l.prev;
300} 298}
301 299
302unsigned int thread_get_current(void) 300unsigned int thread_self(void)
303{ 301{
304 return cores[CURRENT_CORE].running->id; 302 return cores[CURRENT_CORE].running->id;
305} 303}
306 304
305struct thread_entry* thread_self_entry(void)
306{
307 return cores[CURRENT_CORE].running;
308}
309
307void switch_thread(void) 310void switch_thread(void)
308{ 311{
309 struct thread_entry *current = cores[CURRENT_CORE].running; 312 struct thread_entry *current = cores[CURRENT_CORE].running;
@@ -562,7 +565,7 @@ void remove_thread(unsigned int thread_id)
562 SDL_Thread *t; 565 SDL_Thread *t;
563 SDL_sem *s; 566 SDL_sem *s;
564 567
565 if (thread_id != THREAD_ID_CURRENT && thread->id != thread_id) 568 if (thread->id != thread_id)
566 return; 569 return;
567 570
568 int oldlevel = disable_irq_save(); 571 int oldlevel = disable_irq_save();
@@ -629,11 +632,11 @@ void remove_thread(unsigned int thread_id)
629 632
630void thread_exit(void) 633void thread_exit(void)
631{ 634{
632 remove_thread(THREAD_ID_CURRENT); 635 unsigned int id = thread_self();
636 remove_thread(id);
633 /* This should never and must never be reached - if it is, the 637 /* This should never and must never be reached - if it is, the
634 * state is corrupted */ 638 * state is corrupted */
635 THREAD_PANICF("thread_exit->K:*R (ID: %d)", 639 THREAD_PANICF("thread_exit->K:*R (ID: %d)", id);
636 thread_id_entry(THREAD_ID_CURRENT)->id);
637 while (1); 640 while (1);
638} 641}
639 642
@@ -642,8 +645,7 @@ void thread_wait(unsigned int thread_id)
642 struct thread_entry *current = cores[CURRENT_CORE].running; 645 struct thread_entry *current = cores[CURRENT_CORE].running;
643 struct thread_entry *thread = thread_id_entry(thread_id); 646 struct thread_entry *thread = thread_id_entry(thread_id);
644 647
645 if (thread_id == THREAD_ID_CURRENT || 648 if (thread->id == thread_id && thread->state != STATE_KILLED)
646 (thread->id == thread_id && thread->state != STATE_KILLED))
647 { 649 {
648 current->bqp = &thread->queue; 650 current->bqp = &thread->queue;
649 block_thread(current); 651 block_thread(current);
diff --git a/firmware/thread.c b/firmware/thread.c
index ea73150853..cfedbbedba 100644
--- a/firmware/thread.c
+++ b/firmware/thread.c
@@ -1517,9 +1517,27 @@ static struct thread_entry * find_empty_thread_slot(void)
1517 */ 1517 */
1518struct thread_entry * thread_id_entry(unsigned int thread_id) 1518struct thread_entry * thread_id_entry(unsigned int thread_id)
1519{ 1519{
1520 return (thread_id == THREAD_ID_CURRENT) ? 1520 return &threads[thread_id & THREAD_ID_SLOT_MASK];
1521 cores[CURRENT_CORE].running : 1521}
1522 &threads[thread_id & THREAD_ID_SLOT_MASK]; 1522
1523/*---------------------------------------------------------------------------
1524 * Return the thread id of the calling thread
1525 * --------------------------------------------------------------------------
1526 */
1527unsigned int thread_self(void)
1528{
1529 return cores[CURRENT_CORE].running->id;
1530}
1531
1532/*---------------------------------------------------------------------------
1533 * Return the thread entry of the calling thread.
1534 *
1535 * INTERNAL: Intended for use by kernel and not for programs.
1536 *---------------------------------------------------------------------------
1537 */
1538struct thread_entry* thread_self_entry(void)
1539{
1540 return cores[CURRENT_CORE].running;
1523} 1541}
1524 1542
1525/*--------------------------------------------------------------------------- 1543/*---------------------------------------------------------------------------
@@ -1675,8 +1693,7 @@ void thread_wait(unsigned int thread_id)
1675 corelock_lock(&thread->waiter_cl); 1693 corelock_lock(&thread->waiter_cl);
1676 1694
1677 /* Be sure it hasn't been killed yet */ 1695 /* Be sure it hasn't been killed yet */
1678 if (thread_id == THREAD_ID_CURRENT || 1696 if (thread->id == thread_id && thread->state != STATE_KILLED)
1679 (thread->id == thread_id && thread->state != STATE_KILLED))
1680 { 1697 {
1681 IF_COP( current->obj_cl = &thread->waiter_cl; ) 1698 IF_COP( current->obj_cl = &thread->waiter_cl; )
1682 current->bqp = &thread->queue; 1699 current->bqp = &thread->queue;
@@ -1973,8 +1990,7 @@ int thread_set_priority(unsigned int thread_id, int priority)
1973 LOCK_THREAD(thread); 1990 LOCK_THREAD(thread);
1974 1991
1975 /* Make sure it's not killed */ 1992 /* Make sure it's not killed */
1976 if (thread_id == THREAD_ID_CURRENT || 1993 if (thread->id == thread_id && thread->state != STATE_KILLED)
1977 (thread->id == thread_id && thread->state != STATE_KILLED))
1978 { 1994 {
1979 int old_priority = thread->priority; 1995 int old_priority = thread->priority;
1980 1996
@@ -2099,8 +2115,7 @@ int thread_get_priority(unsigned int thread_id)
2099 /* Simply check without locking slot. It may or may not be valid by the 2115 /* Simply check without locking slot. It may or may not be valid by the
2100 * time the function returns anyway. If all tests pass, it is the 2116 * time the function returns anyway. If all tests pass, it is the
2101 * correct value for when it was valid. */ 2117 * correct value for when it was valid. */
2102 if (thread_id != THREAD_ID_CURRENT && 2118 if (thread->id != thread_id || thread->state == STATE_KILLED)
2103 (thread->id != thread_id || thread->state == STATE_KILLED))
2104 base_priority = -1; 2119 base_priority = -1;
2105 2120
2106 return base_priority; 2121 return base_priority;
@@ -2143,15 +2158,6 @@ void thread_thaw(unsigned int thread_id)
2143 restore_irq(oldlevel); 2158 restore_irq(oldlevel);
2144} 2159}
2145 2160
2146/*---------------------------------------------------------------------------
2147 * Return the ID of the currently executing thread.
2148 *---------------------------------------------------------------------------
2149 */
2150unsigned int thread_get_current(void)
2151{
2152 return cores[CURRENT_CORE].running->id;
2153}
2154
2155#if NUM_CORES > 1 2161#if NUM_CORES > 1
2156/*--------------------------------------------------------------------------- 2162/*---------------------------------------------------------------------------
2157 * Switch the processor that the currently executing thread runs on. 2163 * Switch the processor that the currently executing thread runs on.
diff --git a/firmware/usb.c b/firmware/usb.c
index a794e2ac8a..aada27dfd9 100644
--- a/firmware/usb.c
+++ b/firmware/usb.c
@@ -220,7 +220,7 @@ static inline void usb_slave_mode(bool on)
220 { 220 {
221 trigger_cpu_boost(); 221 trigger_cpu_boost();
222#ifdef HAVE_PRIORITY_SCHEDULING 222#ifdef HAVE_PRIORITY_SCHEDULING
223 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_REALTIME); 223 thread_set_priority(thread_self(), PRIORITY_REALTIME);
224#endif 224#endif
225 disk_unmount_all(); 225 disk_unmount_all();
226 usb_attach(); 226 usb_attach();
@@ -229,7 +229,7 @@ static inline void usb_slave_mode(bool on)
229 { 229 {
230 usb_enable(false); 230 usb_enable(false);
231#ifdef HAVE_PRIORITY_SCHEDULING 231#ifdef HAVE_PRIORITY_SCHEDULING
232 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_SYSTEM); 232 thread_set_priority(thread_self(), PRIORITY_SYSTEM);
233#endif 233#endif
234 /* Entered exclusive mode */ 234 /* Entered exclusive mode */
235 rc = disk_mount_all(); 235 rc = disk_mount_all();