mkmpioboot: restructure model / firmware file information.
[kugel-rb.git] / firmware / thread.c
blobb71bc1749c3dfdf1f67d470fc4e87b6c876e78d5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Ulf Ralberg
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 ****************************************************************************/
21 #include "config.h"
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include "thread.h"
25 #include "panic.h"
26 #include "system.h"
27 #include "kernel.h"
28 #include "cpu.h"
29 #include "string.h"
30 #include "buffer.h"
31 #ifdef RB_PROFILE
32 #include <profile.h>
33 #endif
34 /****************************************************************************
35 * ATTENTION!! *
36 * See notes below on implementing processor-specific portions! *
37 ***************************************************************************/
39 /* Define THREAD_EXTRA_CHECKS as 1 to enable additional state checks */
40 #ifdef DEBUG
41 #define THREAD_EXTRA_CHECKS 1 /* Always 1 for DEBUG */
42 #else
43 #define THREAD_EXTRA_CHECKS 0
44 #endif
46 /**
47 * General locking order to guarantee progress. Order must be observed but
48 * all stages are not nescessarily obligatory. Going from 1) to 3) is
49 * perfectly legal.
51 * 1) IRQ
52 * This is first because of the likelyhood of having an interrupt occur that
53 * also accesses one of the objects farther down the list. Any non-blocking
54 * synchronization done may already have a lock on something during normal
55 * execution and if an interrupt handler running on the same processor as
56 * the one that has the resource locked were to attempt to access the
57 * resource, the interrupt handler would wait forever waiting for an unlock
58 * that will never happen. There is no danger if the interrupt occurs on
59 * a different processor because the one that has the lock will eventually
60 * unlock and the other processor's handler may proceed at that time. Not
61 * nescessary when the resource in question is definitely not available to
62 * interrupt handlers.
64 * 2) Kernel Object
65 * 1) May be needed beforehand if the kernel object allows dual-use such as
66 * event queues. The kernel object must have a scheme to protect itself from
67 * access by another processor and is responsible for serializing the calls
68 * to block_thread(_w_tmo) and wakeup_thread both to themselves and to each
69 * other. Objects' queues are also protected here.
71 * 3) Thread Slot
72 * This locks access to the thread's slot such that its state cannot be
73 * altered by another processor when a state change is in progress such as
74 * when it is in the process of going on a blocked list. An attempt to wake
75 * a thread while it is still blocking will likely desync its state with
76 * the other resources used for that state.
78 * 4) Core Lists
79 * These lists are specific to a particular processor core and are accessible
80 * by all processor cores and interrupt handlers. The running (rtr) list is
81 * the prime example where a thread may be added by any means.
84 /*---------------------------------------------------------------------------
85 * Processor specific: core_sleep/core_wake/misc. notes
87 * ARM notes:
88 * FIQ is not dealt with by the scheduler code and is simply restored if it
89 * must by masked for some reason - because threading modifies a register
90 * that FIQ may also modify and there's no way to accomplish it atomically.
91 * s3c2440 is such a case.
93 * Audio interrupts are generally treated at a higher priority than others
94 * usage of scheduler code with interrupts higher than HIGHEST_IRQ_LEVEL
95 * are not in general safe. Special cases may be constructed on a per-
96 * source basis and blocking operations are not available.
98 * core_sleep procedure to implement for any CPU to ensure an asychronous
99 * wakup never results in requiring a wait until the next tick (up to
100 * 10000uS!). May require assembly and careful instruction ordering.
102 * 1) On multicore, stay awake if directed to do so by another. If so, goto
103 * step 4.
104 * 2) If processor requires, atomically reenable interrupts and perform step
105 * 3.
106 * 3) Sleep the CPU core. If wakeup itself enables interrupts (stop #0x2000
107 * on Coldfire) goto step 5.
108 * 4) Enable interrupts.
109 * 5) Exit procedure.
111 * core_wake and multprocessor notes for sleep/wake coordination:
112 * If possible, to wake up another processor, the forcing of an interrupt on
113 * the woken core by the waker core is the easiest way to ensure a non-
114 * delayed wake and immediate execution of any woken threads. If that isn't
115 * available then some careful non-blocking synchonization is needed (as on
116 * PP targets at the moment).
117 *---------------------------------------------------------------------------
120 /* Cast to the the machine pointer size, whose size could be < 4 or > 32
121 * (someday :). */
122 #define DEADBEEF ((uintptr_t)0xdeadbeefdeadbeefull)
123 static struct core_entry cores[NUM_CORES] IBSS_ATTR;
124 struct thread_entry threads[MAXTHREADS] IBSS_ATTR;
126 static const char main_thread_name[] = "main";
127 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
128 extern uintptr_t stackbegin[];
129 extern uintptr_t stackend[];
130 #else
131 extern uintptr_t *stackbegin;
132 extern uintptr_t *stackend;
133 #endif
135 static inline void core_sleep(IF_COP_VOID(unsigned int core))
136 __attribute__((always_inline));
138 void check_tmo_threads(void)
139 __attribute__((noinline));
141 static inline void block_thread_on_l(struct thread_entry *thread, unsigned state)
142 __attribute__((always_inline));
144 static void add_to_list_tmo(struct thread_entry *thread)
145 __attribute__((noinline));
147 static void core_schedule_wakeup(struct thread_entry *thread)
148 __attribute__((noinline));
150 #if NUM_CORES > 1
151 static inline void run_blocking_ops(
152 unsigned int core, struct thread_entry *thread)
153 __attribute__((always_inline));
154 #endif
156 static void thread_stkov(struct thread_entry *thread)
157 __attribute__((noinline));
159 static inline void store_context(void* addr)
160 __attribute__((always_inline));
162 static inline void load_context(const void* addr)
163 __attribute__((always_inline));
165 #if NUM_CORES > 1
166 static void thread_final_exit_do(struct thread_entry *current)
167 __attribute__((noinline, noreturn, used));
168 #else
169 static inline void thread_final_exit(struct thread_entry *current)
170 __attribute__((always_inline, noreturn));
171 #endif
173 void switch_thread(void)
174 __attribute__((noinline));
176 /****************************************************************************
177 * Processor-specific section - include necessary core support
179 #if defined(ANDROID)
180 #include "thread-android-arm.c"
181 #elif defined(CPU_ARM)
182 #include "thread-arm.c"
183 #if defined (CPU_PP)
184 #include "thread-pp.c"
185 #endif /* CPU_PP */
186 #elif defined(CPU_COLDFIRE)
187 #include "thread-coldfire.c"
188 #elif CONFIG_CPU == SH7034
189 #include "thread-sh.c"
190 #elif defined(CPU_MIPS) && CPU_MIPS == 32
191 #include "thread-mips32.c"
192 #else
193 /* Wouldn't compile anyway */
194 #error Processor not implemented.
195 #endif /* CONFIG_CPU == */
197 #ifndef IF_NO_SKIP_YIELD
198 #define IF_NO_SKIP_YIELD(...)
199 #endif
202 * End Processor-specific section
203 ***************************************************************************/
205 #if THREAD_EXTRA_CHECKS
206 static void thread_panicf(const char *msg, struct thread_entry *thread)
208 IF_COP( const unsigned int core = thread->core; )
209 static char name[32];
210 thread_get_name(name, 32, thread);
211 panicf ("%s %s" IF_COP(" (%d)"), msg, name IF_COP(, core));
213 static void thread_stkov(struct thread_entry *thread)
215 thread_panicf("Stkov", thread);
217 #define THREAD_PANICF(msg, thread) \
218 thread_panicf(msg, thread)
219 #define THREAD_ASSERT(exp, msg, thread) \
220 ({ if (!({ exp; })) thread_panicf((msg), (thread)); })
221 #else
222 static void thread_stkov(struct thread_entry *thread)
224 IF_COP( const unsigned int core = thread->core; )
225 static char name[32];
226 thread_get_name(name, 32, thread);
227 panicf("Stkov %s" IF_COP(" (%d)"), name IF_COP(, core));
229 #define THREAD_PANICF(msg, thread)
230 #define THREAD_ASSERT(exp, msg, thread)
231 #endif /* THREAD_EXTRA_CHECKS */
233 /* Thread locking */
234 #if NUM_CORES > 1
235 #define LOCK_THREAD(thread) \
236 ({ corelock_lock(&(thread)->slot_cl); })
237 #define TRY_LOCK_THREAD(thread) \
238 ({ corelock_try_lock(&(thread)->slot_cl); })
239 #define UNLOCK_THREAD(thread) \
240 ({ corelock_unlock(&(thread)->slot_cl); })
241 #define UNLOCK_THREAD_AT_TASK_SWITCH(thread) \
242 ({ unsigned int _core = (thread)->core; \
243 cores[_core].blk_ops.flags |= TBOP_UNLOCK_CORELOCK; \
244 cores[_core].blk_ops.cl_p = &(thread)->slot_cl; })
245 #else
246 #define LOCK_THREAD(thread) \
247 ({ })
248 #define TRY_LOCK_THREAD(thread) \
249 ({ })
250 #define UNLOCK_THREAD(thread) \
251 ({ })
252 #define UNLOCK_THREAD_AT_TASK_SWITCH(thread) \
253 ({ })
254 #endif
256 /* RTR list */
257 #define RTR_LOCK(core) \
258 ({ corelock_lock(&cores[core].rtr_cl); })
259 #define RTR_UNLOCK(core) \
260 ({ corelock_unlock(&cores[core].rtr_cl); })
262 #ifdef HAVE_PRIORITY_SCHEDULING
263 #define rtr_add_entry(core, priority) \
264 prio_add_entry(&cores[core].rtr, (priority))
266 #define rtr_subtract_entry(core, priority) \
267 prio_subtract_entry(&cores[core].rtr, (priority))
269 #define rtr_move_entry(core, from, to) \
270 prio_move_entry(&cores[core].rtr, (from), (to))
271 #else
272 #define rtr_add_entry(core, priority)
273 #define rtr_add_entry_inl(core, priority)
274 #define rtr_subtract_entry(core, priority)
275 #define rtr_subtract_entry_inl(core, priotity)
276 #define rtr_move_entry(core, from, to)
277 #define rtr_move_entry_inl(core, from, to)
278 #endif
280 /*---------------------------------------------------------------------------
281 * Thread list structure - circular:
282 * +------------------------------+
283 * | |
284 * +--+---+<-+---+<-+---+<-+---+<-+
285 * Head->| T | | T | | T | | T |
286 * +->+---+->+---+->+---+->+---+--+
287 * | |
288 * +------------------------------+
289 *---------------------------------------------------------------------------
292 /*---------------------------------------------------------------------------
293 * Adds a thread to a list of threads using "insert last". Uses the "l"
294 * links.
295 *---------------------------------------------------------------------------
297 static void add_to_list_l(struct thread_entry **list,
298 struct thread_entry *thread)
300 struct thread_entry *l = *list;
302 if (l == NULL)
304 /* Insert into unoccupied list */
305 thread->l.prev = thread;
306 thread->l.next = thread;
307 *list = thread;
308 return;
311 /* Insert last */
312 thread->l.prev = l->l.prev;
313 thread->l.next = l;
314 l->l.prev->l.next = thread;
315 l->l.prev = thread;
318 /*---------------------------------------------------------------------------
319 * Removes a thread from a list of threads. Uses the "l" links.
320 *---------------------------------------------------------------------------
322 static void remove_from_list_l(struct thread_entry **list,
323 struct thread_entry *thread)
325 struct thread_entry *prev, *next;
327 next = thread->l.next;
329 if (thread == next)
331 /* The only item */
332 *list = NULL;
333 return;
336 if (thread == *list)
338 /* List becomes next item */
339 *list = next;
342 prev = thread->l.prev;
344 /* Fix links to jump over the removed entry. */
345 next->l.prev = prev;
346 prev->l.next = next;
349 /*---------------------------------------------------------------------------
350 * Timeout list structure - circular reverse (to make "remove item" O(1)),
351 * NULL-terminated forward (to ease the far more common forward traversal):
352 * +------------------------------+
353 * | |
354 * +--+---+<-+---+<-+---+<-+---+<-+
355 * Head->| T | | T | | T | | T |
356 * +---+->+---+->+---+->+---+-X
357 *---------------------------------------------------------------------------
360 /*---------------------------------------------------------------------------
361 * Add a thread from the core's timout list by linking the pointers in its
362 * tmo structure.
363 *---------------------------------------------------------------------------
365 static void add_to_list_tmo(struct thread_entry *thread)
367 struct thread_entry *tmo = cores[IF_COP_CORE(thread->core)].timeout;
368 THREAD_ASSERT(thread->tmo.prev == NULL,
369 "add_to_list_tmo->already listed", thread);
371 thread->tmo.next = NULL;
373 if (tmo == NULL)
375 /* Insert into unoccupied list */
376 thread->tmo.prev = thread;
377 cores[IF_COP_CORE(thread->core)].timeout = thread;
378 return;
381 /* Insert Last */
382 thread->tmo.prev = tmo->tmo.prev;
383 tmo->tmo.prev->tmo.next = thread;
384 tmo->tmo.prev = thread;
387 /*---------------------------------------------------------------------------
388 * Remove a thread from the core's timout list by unlinking the pointers in
389 * its tmo structure. Sets thread->tmo.prev to NULL to indicate the timeout
390 * is cancelled.
391 *---------------------------------------------------------------------------
393 static void remove_from_list_tmo(struct thread_entry *thread)
395 struct thread_entry **list = &cores[IF_COP_CORE(thread->core)].timeout;
396 struct thread_entry *prev = thread->tmo.prev;
397 struct thread_entry *next = thread->tmo.next;
399 THREAD_ASSERT(prev != NULL, "remove_from_list_tmo->not listed", thread);
401 if (next != NULL)
402 next->tmo.prev = prev;
404 if (thread == *list)
406 /* List becomes next item and empty if next == NULL */
407 *list = next;
408 /* Mark as unlisted */
409 thread->tmo.prev = NULL;
411 else
413 if (next == NULL)
414 (*list)->tmo.prev = prev;
415 prev->tmo.next = next;
416 /* Mark as unlisted */
417 thread->tmo.prev = NULL;
422 #ifdef HAVE_PRIORITY_SCHEDULING
423 /*---------------------------------------------------------------------------
424 * Priority distribution structure (one category for each possible priority):
426 * +----+----+----+ ... +-----+
427 * hist: | F0 | F1 | F2 | | F31 |
428 * +----+----+----+ ... +-----+
429 * mask: | b0 | b1 | b2 | | b31 |
430 * +----+----+----+ ... +-----+
432 * F = count of threads at priority category n (frequency)
433 * b = bitmask of non-zero priority categories (occupancy)
435 * / if H[n] != 0 : 1
436 * b[n] = |
437 * \ else : 0
439 *---------------------------------------------------------------------------
440 * Basic priority inheritance priotocol (PIP):
442 * Mn = mutex n, Tn = thread n
444 * A lower priority thread inherits the priority of the highest priority
445 * thread blocked waiting for it to complete an action (such as release a
446 * mutex or respond to a message via queue_send):
448 * 1) T2->M1->T1
450 * T1 owns M1, T2 is waiting for M1 to realease M1. If T2 has a higher
451 * priority than T1 then T1 inherits the priority of T2.
453 * 2) T3
454 * \/
455 * T2->M1->T1
457 * Situation is like 1) but T2 and T3 are both queued waiting for M1 and so
458 * T1 inherits the higher of T2 and T3.
460 * 3) T3->M2->T2->M1->T1
462 * T1 owns M1, T2 owns M2. If T3 has a higher priority than both T1 and T2,
463 * then T1 inherits the priority of T3 through T2.
465 * Blocking chains can grow arbitrarily complex (though it's best that they
466 * not form at all very often :) and build-up from these units.
467 *---------------------------------------------------------------------------
470 /*---------------------------------------------------------------------------
471 * Increment frequency at category "priority"
472 *---------------------------------------------------------------------------
474 static inline unsigned int prio_add_entry(
475 struct priority_distribution *pd, int priority)
477 unsigned int count;
478 /* Enough size/instruction count difference for ARM makes it worth it to
479 * use different code (192 bytes for ARM). Only thing better is ASM. */
480 #ifdef CPU_ARM
481 count = pd->hist[priority];
482 if (++count == 1)
483 pd->mask |= 1 << priority;
484 pd->hist[priority] = count;
485 #else /* This one's better for Coldfire */
486 if ((count = ++pd->hist[priority]) == 1)
487 pd->mask |= 1 << priority;
488 #endif
490 return count;
493 /*---------------------------------------------------------------------------
494 * Decrement frequency at category "priority"
495 *---------------------------------------------------------------------------
497 static inline unsigned int prio_subtract_entry(
498 struct priority_distribution *pd, int priority)
500 unsigned int count;
502 #ifdef CPU_ARM
503 count = pd->hist[priority];
504 if (--count == 0)
505 pd->mask &= ~(1 << priority);
506 pd->hist[priority] = count;
507 #else
508 if ((count = --pd->hist[priority]) == 0)
509 pd->mask &= ~(1 << priority);
510 #endif
512 return count;
515 /*---------------------------------------------------------------------------
516 * Remove from one category and add to another
517 *---------------------------------------------------------------------------
519 static inline void prio_move_entry(
520 struct priority_distribution *pd, int from, int to)
522 uint32_t mask = pd->mask;
524 #ifdef CPU_ARM
525 unsigned int count;
527 count = pd->hist[from];
528 if (--count == 0)
529 mask &= ~(1 << from);
530 pd->hist[from] = count;
532 count = pd->hist[to];
533 if (++count == 1)
534 mask |= 1 << to;
535 pd->hist[to] = count;
536 #else
537 if (--pd->hist[from] == 0)
538 mask &= ~(1 << from);
540 if (++pd->hist[to] == 1)
541 mask |= 1 << to;
542 #endif
544 pd->mask = mask;
547 /*---------------------------------------------------------------------------
548 * Change the priority and rtr entry for a running thread
549 *---------------------------------------------------------------------------
551 static inline void set_running_thread_priority(
552 struct thread_entry *thread, int priority)
554 const unsigned int core = IF_COP_CORE(thread->core);
555 RTR_LOCK(core);
556 rtr_move_entry(core, thread->priority, priority);
557 thread->priority = priority;
558 RTR_UNLOCK(core);
561 /*---------------------------------------------------------------------------
562 * Finds the highest priority thread in a list of threads. If the list is
563 * empty, the PRIORITY_IDLE is returned.
565 * It is possible to use the struct priority_distribution within an object
566 * instead of scanning the remaining threads in the list but as a compromise,
567 * the resulting per-object memory overhead is saved at a slight speed
568 * penalty under high contention.
569 *---------------------------------------------------------------------------
571 static int find_highest_priority_in_list_l(
572 struct thread_entry * const thread)
574 if (LIKELY(thread != NULL))
576 /* Go though list until the ending up at the initial thread */
577 int highest_priority = thread->priority;
578 struct thread_entry *curr = thread;
582 int priority = curr->priority;
584 if (priority < highest_priority)
585 highest_priority = priority;
587 curr = curr->l.next;
589 while (curr != thread);
591 return highest_priority;
594 return PRIORITY_IDLE;
597 /*---------------------------------------------------------------------------
598 * Register priority with blocking system and bubble it down the chain if
599 * any until we reach the end or something is already equal or higher.
601 * NOTE: A simultaneous circular wait could spin deadlock on multiprocessor
602 * targets but that same action also guarantees a circular block anyway and
603 * those are prevented, right? :-)
604 *---------------------------------------------------------------------------
606 static struct thread_entry *
607 blocker_inherit_priority(struct thread_entry *current)
609 const int priority = current->priority;
610 struct blocker *bl = current->blocker;
611 struct thread_entry * const tstart = current;
612 struct thread_entry *bl_t = bl->thread;
614 /* Blocker cannot change since the object protection is held */
615 LOCK_THREAD(bl_t);
617 for (;;)
619 struct thread_entry *next;
620 int bl_pr = bl->priority;
622 if (priority >= bl_pr)
623 break; /* Object priority already high enough */
625 bl->priority = priority;
627 /* Add this one */
628 prio_add_entry(&bl_t->pdist, priority);
630 if (bl_pr < PRIORITY_IDLE)
632 /* Not first waiter - subtract old one */
633 prio_subtract_entry(&bl_t->pdist, bl_pr);
636 if (priority >= bl_t->priority)
637 break; /* Thread priority high enough */
639 if (bl_t->state == STATE_RUNNING)
641 /* Blocking thread is a running thread therefore there are no
642 * further blockers. Change the "run queue" on which it
643 * resides. */
644 set_running_thread_priority(bl_t, priority);
645 break;
648 bl_t->priority = priority;
650 /* If blocking thread has a blocker, apply transitive inheritance */
651 bl = bl_t->blocker;
653 if (bl == NULL)
654 break; /* End of chain or object doesn't support inheritance */
656 next = bl->thread;
658 if (UNLIKELY(next == tstart))
659 break; /* Full-circle - deadlock! */
661 UNLOCK_THREAD(current);
663 #if NUM_CORES > 1
664 for (;;)
666 LOCK_THREAD(next);
668 /* Blocker could change - retest condition */
669 if (LIKELY(bl->thread == next))
670 break;
672 UNLOCK_THREAD(next);
673 next = bl->thread;
675 #endif
676 current = bl_t;
677 bl_t = next;
680 UNLOCK_THREAD(bl_t);
682 return current;
685 /*---------------------------------------------------------------------------
686 * Readjust priorities when waking a thread blocked waiting for another
687 * in essence "releasing" the thread's effect on the object owner. Can be
688 * performed from any context.
689 *---------------------------------------------------------------------------
691 struct thread_entry *
692 wakeup_priority_protocol_release(struct thread_entry *thread)
694 const int priority = thread->priority;
695 struct blocker *bl = thread->blocker;
696 struct thread_entry * const tstart = thread;
697 struct thread_entry *bl_t = bl->thread;
699 /* Blocker cannot change since object will be locked */
700 LOCK_THREAD(bl_t);
702 thread->blocker = NULL; /* Thread not blocked */
704 for (;;)
706 struct thread_entry *next;
707 int bl_pr = bl->priority;
709 if (priority > bl_pr)
710 break; /* Object priority higher */
712 next = *thread->bqp;
714 if (next == NULL)
716 /* No more threads in queue */
717 prio_subtract_entry(&bl_t->pdist, bl_pr);
718 bl->priority = PRIORITY_IDLE;
720 else
722 /* Check list for highest remaining priority */
723 int queue_pr = find_highest_priority_in_list_l(next);
725 if (queue_pr == bl_pr)
726 break; /* Object priority not changing */
728 /* Change queue priority */
729 prio_move_entry(&bl_t->pdist, bl_pr, queue_pr);
730 bl->priority = queue_pr;
733 if (bl_pr > bl_t->priority)
734 break; /* thread priority is higher */
736 bl_pr = find_first_set_bit(bl_t->pdist.mask);
738 if (bl_pr == bl_t->priority)
739 break; /* Thread priority not changing */
741 if (bl_t->state == STATE_RUNNING)
743 /* No further blockers */
744 set_running_thread_priority(bl_t, bl_pr);
745 break;
748 bl_t->priority = bl_pr;
750 /* If blocking thread has a blocker, apply transitive inheritance */
751 bl = bl_t->blocker;
753 if (bl == NULL)
754 break; /* End of chain or object doesn't support inheritance */
756 next = bl->thread;
758 if (UNLIKELY(next == tstart))
759 break; /* Full-circle - deadlock! */
761 UNLOCK_THREAD(thread);
763 #if NUM_CORES > 1
764 for (;;)
766 LOCK_THREAD(next);
768 /* Blocker could change - retest condition */
769 if (LIKELY(bl->thread == next))
770 break;
772 UNLOCK_THREAD(next);
773 next = bl->thread;
775 #endif
776 thread = bl_t;
777 bl_t = next;
780 UNLOCK_THREAD(bl_t);
782 #if NUM_CORES > 1
783 if (UNLIKELY(thread != tstart))
785 /* Relock original if it changed */
786 LOCK_THREAD(tstart);
788 #endif
790 return cores[CURRENT_CORE].running;
793 /*---------------------------------------------------------------------------
794 * Transfer ownership to a thread waiting for an objects and transfer
795 * inherited priority boost from other waiters. This algorithm knows that
796 * blocking chains may only unblock from the very end.
798 * Only the owning thread itself may call this and so the assumption that
799 * it is the running thread is made.
800 *---------------------------------------------------------------------------
802 struct thread_entry *
803 wakeup_priority_protocol_transfer(struct thread_entry *thread)
805 /* Waking thread inherits priority boost from object owner */
806 struct blocker *bl = thread->blocker;
807 struct thread_entry *bl_t = bl->thread;
808 struct thread_entry *next;
809 int bl_pr;
811 THREAD_ASSERT(cores[CURRENT_CORE].running == bl_t,
812 "UPPT->wrong thread", cores[CURRENT_CORE].running);
814 LOCK_THREAD(bl_t);
816 bl_pr = bl->priority;
818 /* Remove the object's boost from the owning thread */
819 if (prio_subtract_entry(&bl_t->pdist, bl_pr) == 0 &&
820 bl_pr <= bl_t->priority)
822 /* No more threads at this priority are waiting and the old level is
823 * at least the thread level */
824 int priority = find_first_set_bit(bl_t->pdist.mask);
826 if (priority != bl_t->priority)
828 /* Adjust this thread's priority */
829 set_running_thread_priority(bl_t, priority);
833 next = *thread->bqp;
835 if (LIKELY(next == NULL))
837 /* Expected shortcut - no more waiters */
838 bl_pr = PRIORITY_IDLE;
840 else
842 if (thread->priority <= bl_pr)
844 /* Need to scan threads remaining in queue */
845 bl_pr = find_highest_priority_in_list_l(next);
848 if (prio_add_entry(&thread->pdist, bl_pr) == 1 &&
849 bl_pr < thread->priority)
851 /* Thread priority must be raised */
852 thread->priority = bl_pr;
856 bl->thread = thread; /* This thread pwns */
857 bl->priority = bl_pr; /* Save highest blocked priority */
858 thread->blocker = NULL; /* Thread not blocked */
860 UNLOCK_THREAD(bl_t);
862 return bl_t;
865 /*---------------------------------------------------------------------------
866 * No threads must be blocked waiting for this thread except for it to exit.
867 * The alternative is more elaborate cleanup and object registration code.
868 * Check this for risk of silent data corruption when objects with
869 * inheritable blocking are abandoned by the owner - not precise but may
870 * catch something.
871 *---------------------------------------------------------------------------
873 static void __attribute__((noinline)) check_for_obj_waiters(
874 const char *function, struct thread_entry *thread)
876 /* Only one bit in the mask should be set with a frequency on 1 which
877 * represents the thread's own base priority */
878 uint32_t mask = thread->pdist.mask;
879 if ((mask & (mask - 1)) != 0 ||
880 thread->pdist.hist[find_first_set_bit(mask)] > 1)
882 unsigned char name[32];
883 thread_get_name(name, 32, thread);
884 panicf("%s->%s with obj. waiters", function, name);
887 #endif /* HAVE_PRIORITY_SCHEDULING */
889 /*---------------------------------------------------------------------------
890 * Move a thread back to a running state on its core.
891 *---------------------------------------------------------------------------
893 static void core_schedule_wakeup(struct thread_entry *thread)
895 const unsigned int core = IF_COP_CORE(thread->core);
897 RTR_LOCK(core);
899 thread->state = STATE_RUNNING;
901 add_to_list_l(&cores[core].running, thread);
902 rtr_add_entry(core, thread->priority);
904 RTR_UNLOCK(core);
906 #if NUM_CORES > 1
907 if (core != CURRENT_CORE)
908 core_wake(core);
909 #endif
912 /*---------------------------------------------------------------------------
913 * Check the core's timeout list when at least one thread is due to wake.
914 * Filtering for the condition is done before making the call. Resets the
915 * tick when the next check will occur.
916 *---------------------------------------------------------------------------
918 void check_tmo_threads(void)
920 const unsigned int core = CURRENT_CORE;
921 const long tick = current_tick; /* snapshot the current tick */
922 long next_tmo_check = tick + 60*HZ; /* minimum duration: once/minute */
923 struct thread_entry *next = cores[core].timeout;
925 /* If there are no processes waiting for a timeout, just keep the check
926 tick from falling into the past. */
928 /* Break the loop once we have walked through the list of all
929 * sleeping processes or have removed them all. */
930 while (next != NULL)
932 /* Check sleeping threads. Allow interrupts between checks. */
933 enable_irq();
935 struct thread_entry *curr = next;
937 next = curr->tmo.next;
939 /* Lock thread slot against explicit wakeup */
940 disable_irq();
941 LOCK_THREAD(curr);
943 unsigned state = curr->state;
945 if (state < TIMEOUT_STATE_FIRST)
947 /* Cleanup threads no longer on a timeout but still on the
948 * list. */
949 remove_from_list_tmo(curr);
951 else if (LIKELY(TIME_BEFORE(tick, curr->tmo_tick)))
953 /* Timeout still pending - this will be the usual case */
954 if (TIME_BEFORE(curr->tmo_tick, next_tmo_check))
956 /* Earliest timeout found so far - move the next check up
957 to its time */
958 next_tmo_check = curr->tmo_tick;
961 else
963 /* Sleep timeout has been reached so bring the thread back to
964 * life again. */
965 if (state == STATE_BLOCKED_W_TMO)
967 #if NUM_CORES > 1
968 /* Lock the waiting thread's kernel object */
969 struct corelock *ocl = curr->obj_cl;
971 if (UNLIKELY(corelock_try_lock(ocl) == 0))
973 /* Need to retry in the correct order though the need is
974 * unlikely */
975 UNLOCK_THREAD(curr);
976 corelock_lock(ocl);
977 LOCK_THREAD(curr);
979 if (UNLIKELY(curr->state != STATE_BLOCKED_W_TMO))
981 /* Thread was woken or removed explicitely while slot
982 * was unlocked */
983 corelock_unlock(ocl);
984 remove_from_list_tmo(curr);
985 UNLOCK_THREAD(curr);
986 continue;
989 #endif /* NUM_CORES */
991 remove_from_list_l(curr->bqp, curr);
993 #ifdef HAVE_WAKEUP_EXT_CB
994 if (curr->wakeup_ext_cb != NULL)
995 curr->wakeup_ext_cb(curr);
996 #endif
998 #ifdef HAVE_PRIORITY_SCHEDULING
999 if (curr->blocker != NULL)
1000 wakeup_priority_protocol_release(curr);
1001 #endif
1002 corelock_unlock(ocl);
1004 /* else state == STATE_SLEEPING */
1006 remove_from_list_tmo(curr);
1008 RTR_LOCK(core);
1010 curr->state = STATE_RUNNING;
1012 add_to_list_l(&cores[core].running, curr);
1013 rtr_add_entry(core, curr->priority);
1015 RTR_UNLOCK(core);
1018 UNLOCK_THREAD(curr);
1021 cores[core].next_tmo_check = next_tmo_check;
1024 /*---------------------------------------------------------------------------
1025 * Performs operations that must be done before blocking a thread but after
1026 * the state is saved.
1027 *---------------------------------------------------------------------------
1029 #if NUM_CORES > 1
1030 static inline void run_blocking_ops(
1031 unsigned int core, struct thread_entry *thread)
1033 struct thread_blk_ops *ops = &cores[core].blk_ops;
1034 const unsigned flags = ops->flags;
1036 if (LIKELY(flags == TBOP_CLEAR))
1037 return;
1039 switch (flags)
1041 case TBOP_SWITCH_CORE:
1042 core_switch_blk_op(core, thread);
1043 /* Fall-through */
1044 case TBOP_UNLOCK_CORELOCK:
1045 corelock_unlock(ops->cl_p);
1046 break;
1049 ops->flags = TBOP_CLEAR;
1051 #endif /* NUM_CORES > 1 */
1053 #ifdef RB_PROFILE
1054 void profile_thread(void)
1056 profstart(cores[CURRENT_CORE].running - threads);
1058 #endif
1060 /*---------------------------------------------------------------------------
1061 * Prepares a thread to block on an object's list and/or for a specified
1062 * duration - expects object and slot to be appropriately locked if needed
1063 * and interrupts to be masked.
1064 *---------------------------------------------------------------------------
1066 static inline void block_thread_on_l(struct thread_entry *thread,
1067 unsigned state)
1069 /* If inlined, unreachable branches will be pruned with no size penalty
1070 because state is passed as a constant parameter. */
1071 const unsigned int core = IF_COP_CORE(thread->core);
1073 /* Remove the thread from the list of running threads. */
1074 RTR_LOCK(core);
1075 remove_from_list_l(&cores[core].running, thread);
1076 rtr_subtract_entry(core, thread->priority);
1077 RTR_UNLOCK(core);
1079 /* Add a timeout to the block if not infinite */
1080 switch (state)
1082 case STATE_BLOCKED:
1083 case STATE_BLOCKED_W_TMO:
1084 /* Put the thread into a new list of inactive threads. */
1085 add_to_list_l(thread->bqp, thread);
1087 if (state == STATE_BLOCKED)
1088 break;
1090 /* Fall-through */
1091 case STATE_SLEEPING:
1092 /* If this thread times out sooner than any other thread, update
1093 next_tmo_check to its timeout */
1094 if (TIME_BEFORE(thread->tmo_tick, cores[core].next_tmo_check))
1096 cores[core].next_tmo_check = thread->tmo_tick;
1099 if (thread->tmo.prev == NULL)
1101 add_to_list_tmo(thread);
1103 /* else thread was never removed from list - just keep it there */
1104 break;
1107 /* Remember the the next thread about to block. */
1108 cores[core].block_task = thread;
1110 /* Report new state. */
1111 thread->state = state;
1114 /*---------------------------------------------------------------------------
1115 * Switch thread in round robin fashion for any given priority. Any thread
1116 * that removed itself from the running list first must specify itself in
1117 * the paramter.
1119 * INTERNAL: Intended for use by kernel and not for programs.
1120 *---------------------------------------------------------------------------
1122 void switch_thread(void)
1125 const unsigned int core = CURRENT_CORE;
1126 struct thread_entry *block = cores[core].block_task;
1127 struct thread_entry *thread = cores[core].running;
1129 /* Get context to save - next thread to run is unknown until all wakeups
1130 * are evaluated */
1131 if (block != NULL)
1133 cores[core].block_task = NULL;
1135 #if NUM_CORES > 1
1136 if (UNLIKELY(thread == block))
1138 /* This was the last thread running and another core woke us before
1139 * reaching here. Force next thread selection to give tmo threads or
1140 * other threads woken before this block a first chance. */
1141 block = NULL;
1143 else
1144 #endif
1146 /* Blocking task is the old one */
1147 thread = block;
1151 #ifdef RB_PROFILE
1152 #ifdef CPU_COLDFIRE
1153 _profile_thread_stopped(thread->id & THREAD_ID_SLOT_MASK);
1154 #else
1155 profile_thread_stopped(thread->id & THREAD_ID_SLOT_MASK);
1156 #endif
1157 #endif
1159 /* Begin task switching by saving our current context so that we can
1160 * restore the state of the current thread later to the point prior
1161 * to this call. */
1162 store_context(&thread->context);
1164 /* Check if the current thread stack is overflown */
1165 if (UNLIKELY(thread->stack[0] != DEADBEEF) && thread->stack_size > 0)
1166 thread_stkov(thread);
1168 #ifdef BUFFER_ALLOC_DEBUG
1169 /* Check if the current thread just did bad things with buffer_alloc()ed
1170 * memory */
1172 static char name[32];
1173 thread_get_name(name, 32, thread);
1174 buffer_alloc_check(name);
1176 #endif
1178 #if NUM_CORES > 1
1179 /* Run any blocking operations requested before switching/sleeping */
1180 run_blocking_ops(core, thread);
1181 #endif
1183 #ifdef HAVE_PRIORITY_SCHEDULING
1184 IF_NO_SKIP_YIELD( if (thread->skip_count != -1) )
1185 /* Reset the value of thread's skip count */
1186 thread->skip_count = 0;
1187 #endif
1189 for (;;)
1191 /* If there are threads on a timeout and the earliest wakeup is due,
1192 * check the list and wake any threads that need to start running
1193 * again. */
1194 if (!TIME_BEFORE(current_tick, cores[core].next_tmo_check))
1196 check_tmo_threads();
1199 disable_irq();
1200 RTR_LOCK(core);
1202 thread = cores[core].running;
1204 if (UNLIKELY(thread == NULL))
1206 /* Enter sleep mode to reduce power usage - woken up on interrupt
1207 * or wakeup request from another core - expected to enable
1208 * interrupts. */
1209 RTR_UNLOCK(core);
1210 core_sleep(IF_COP(core));
1212 else
1214 #ifdef HAVE_PRIORITY_SCHEDULING
1215 /* Select the new task based on priorities and the last time a
1216 * process got CPU time relative to the highest priority runnable
1217 * task. */
1218 struct priority_distribution *pd = &cores[core].rtr;
1219 int max = find_first_set_bit(pd->mask);
1221 if (block == NULL)
1223 /* Not switching on a block, tentatively select next thread */
1224 thread = thread->l.next;
1227 for (;;)
1229 int priority = thread->priority;
1230 int diff;
1232 /* This ridiculously simple method of aging seems to work
1233 * suspiciously well. It does tend to reward CPU hogs (under
1234 * yielding) but that's generally not desirable at all. On
1235 * the plus side, it, relatively to other threads, penalizes
1236 * excess yielding which is good if some high priority thread
1237 * is performing no useful work such as polling for a device
1238 * to be ready. Of course, aging is only employed when higher
1239 * and lower priority threads are runnable. The highest
1240 * priority runnable thread(s) are never skipped unless a
1241 * lower-priority process has aged sufficiently. Priorities
1242 * of REALTIME class are run strictly according to priority
1243 * thus are not subject to switchout due to lower-priority
1244 * processes aging; they must give up the processor by going
1245 * off the run list. */
1246 if (LIKELY(priority <= max) ||
1247 IF_NO_SKIP_YIELD( thread->skip_count == -1 || )
1248 (priority > PRIORITY_REALTIME &&
1249 (diff = priority - max,
1250 ++thread->skip_count > diff*diff)))
1252 cores[core].running = thread;
1253 break;
1256 thread = thread->l.next;
1258 #else
1259 /* Without priority use a simple FCFS algorithm */
1260 if (block == NULL)
1262 /* Not switching on a block, select next thread */
1263 thread = thread->l.next;
1264 cores[core].running = thread;
1266 #endif /* HAVE_PRIORITY_SCHEDULING */
1268 RTR_UNLOCK(core);
1269 enable_irq();
1270 break;
1274 /* And finally give control to the next thread. */
1275 load_context(&thread->context);
1277 #ifdef RB_PROFILE
1278 profile_thread_started(thread->id & THREAD_ID_SLOT_MASK);
1279 #endif
1283 /*---------------------------------------------------------------------------
1284 * Sleeps a thread for at least a specified number of ticks with zero being
1285 * a wait until the next tick.
1287 * INTERNAL: Intended for use by kernel and not for programs.
1288 *---------------------------------------------------------------------------
1290 void sleep_thread(int ticks)
1292 struct thread_entry *current = cores[CURRENT_CORE].running;
1294 LOCK_THREAD(current);
1296 /* Set our timeout, remove from run list and join timeout list. */
1297 current->tmo_tick = current_tick + ticks + 1;
1298 block_thread_on_l(current, STATE_SLEEPING);
1300 UNLOCK_THREAD(current);
1303 /*---------------------------------------------------------------------------
1304 * Indefinitely block a thread on a blocking queue for explicit wakeup.
1306 * INTERNAL: Intended for use by kernel objects and not for programs.
1307 *---------------------------------------------------------------------------
1309 void block_thread(struct thread_entry *current)
1311 /* Set the state to blocked and take us off of the run queue until we
1312 * are explicitly woken */
1313 LOCK_THREAD(current);
1315 /* Set the list for explicit wakeup */
1316 block_thread_on_l(current, STATE_BLOCKED);
1318 #ifdef HAVE_PRIORITY_SCHEDULING
1319 if (current->blocker != NULL)
1321 /* Object supports PIP */
1322 current = blocker_inherit_priority(current);
1324 #endif
1326 UNLOCK_THREAD(current);
1329 /*---------------------------------------------------------------------------
1330 * Block a thread on a blocking queue for a specified time interval or until
1331 * explicitly woken - whichever happens first.
1333 * INTERNAL: Intended for use by kernel objects and not for programs.
1334 *---------------------------------------------------------------------------
1336 void block_thread_w_tmo(struct thread_entry *current, int timeout)
1338 /* Get the entry for the current running thread. */
1339 LOCK_THREAD(current);
1341 /* Set the state to blocked with the specified timeout */
1342 current->tmo_tick = current_tick + timeout;
1344 /* Set the list for explicit wakeup */
1345 block_thread_on_l(current, STATE_BLOCKED_W_TMO);
1347 #ifdef HAVE_PRIORITY_SCHEDULING
1348 if (current->blocker != NULL)
1350 /* Object supports PIP */
1351 current = blocker_inherit_priority(current);
1353 #endif
1355 UNLOCK_THREAD(current);
1358 /*---------------------------------------------------------------------------
1359 * Explicitly wakeup a thread on a blocking queue. Only effects threads of
1360 * STATE_BLOCKED and STATE_BLOCKED_W_TMO.
1362 * This code should be considered a critical section by the caller meaning
1363 * that the object's corelock should be held.
1365 * INTERNAL: Intended for use by kernel objects and not for programs.
1366 *---------------------------------------------------------------------------
1368 unsigned int wakeup_thread(struct thread_entry **list)
1370 struct thread_entry *thread = *list;
1371 unsigned int result = THREAD_NONE;
1373 /* Check if there is a blocked thread at all. */
1374 if (thread == NULL)
1375 return result;
1377 LOCK_THREAD(thread);
1379 /* Determine thread's current state. */
1380 switch (thread->state)
1382 case STATE_BLOCKED:
1383 case STATE_BLOCKED_W_TMO:
1384 remove_from_list_l(list, thread);
1386 result = THREAD_OK;
1388 #ifdef HAVE_PRIORITY_SCHEDULING
1389 struct thread_entry *current;
1390 struct blocker *bl = thread->blocker;
1392 if (bl == NULL)
1394 /* No inheritance - just boost the thread by aging */
1395 IF_NO_SKIP_YIELD( if (thread->skip_count != -1) )
1396 thread->skip_count = thread->priority;
1397 current = cores[CURRENT_CORE].running;
1399 else
1401 /* Call the specified unblocking PIP */
1402 current = bl->wakeup_protocol(thread);
1405 if (current != NULL &&
1406 find_first_set_bit(cores[IF_COP_CORE(current->core)].rtr.mask)
1407 < current->priority)
1409 /* There is a thread ready to run of higher or same priority on
1410 * the same core as the current one; recommend a task switch.
1411 * Knowing if this is an interrupt call would be helpful here. */
1412 result |= THREAD_SWITCH;
1414 #endif /* HAVE_PRIORITY_SCHEDULING */
1416 core_schedule_wakeup(thread);
1417 break;
1419 /* Nothing to do. State is not blocked. */
1420 #if THREAD_EXTRA_CHECKS
1421 default:
1422 THREAD_PANICF("wakeup_thread->block invalid", thread);
1423 case STATE_RUNNING:
1424 case STATE_KILLED:
1425 break;
1426 #endif
1429 UNLOCK_THREAD(thread);
1430 return result;
1433 /*---------------------------------------------------------------------------
1434 * Wakeup an entire queue of threads - returns bitwise-or of return bitmask
1435 * from each operation or THREAD_NONE of nothing was awakened. Object owning
1436 * the queue must be locked first.
1438 * INTERNAL: Intended for use by kernel objects and not for programs.
1439 *---------------------------------------------------------------------------
1441 unsigned int thread_queue_wake(struct thread_entry **list)
1443 unsigned result = THREAD_NONE;
1445 for (;;)
1447 unsigned int rc = wakeup_thread(list);
1449 if (rc == THREAD_NONE)
1450 break; /* No more threads */
1452 result |= rc;
1455 return result;
1458 /*---------------------------------------------------------------------------
1459 * Assign the thread slot a new ID. Version is 1-255.
1460 *---------------------------------------------------------------------------
1462 static void new_thread_id(unsigned int slot_num,
1463 struct thread_entry *thread)
1465 unsigned int version =
1466 (thread->id + (1u << THREAD_ID_VERSION_SHIFT))
1467 & THREAD_ID_VERSION_MASK;
1469 /* If wrapped to 0, make it 1 */
1470 if (version == 0)
1471 version = 1u << THREAD_ID_VERSION_SHIFT;
1473 thread->id = version | (slot_num & THREAD_ID_SLOT_MASK);
1476 /*---------------------------------------------------------------------------
1477 * Find an empty thread slot or MAXTHREADS if none found. The slot returned
1478 * will be locked on multicore.
1479 *---------------------------------------------------------------------------
1481 static struct thread_entry * find_empty_thread_slot(void)
1483 /* Any slot could be on an interrupt-accessible list */
1484 IF_COP( int oldlevel = disable_irq_save(); )
1485 struct thread_entry *thread = NULL;
1486 int n;
1488 for (n = 0; n < MAXTHREADS; n++)
1490 /* Obtain current slot state - lock it on multicore */
1491 struct thread_entry *t = &threads[n];
1492 LOCK_THREAD(t);
1494 if (t->state == STATE_KILLED IF_COP( && t->name != THREAD_DESTRUCT ))
1496 /* Slot is empty - leave it locked and caller will unlock */
1497 thread = t;
1498 break;
1501 /* Finished examining slot - no longer busy - unlock on multicore */
1502 UNLOCK_THREAD(t);
1505 IF_COP( restore_irq(oldlevel); ) /* Reenable interrups - this slot is
1506 not accesible to them yet */
1507 return thread;
1510 /*---------------------------------------------------------------------------
1511 * Return the thread_entry pointer for a thread_id. Return the current
1512 * thread if the ID is (unsigned int)-1 (alias for current).
1513 *---------------------------------------------------------------------------
1515 struct thread_entry * thread_id_entry(unsigned int thread_id)
1517 return (thread_id == THREAD_ID_CURRENT) ?
1518 cores[CURRENT_CORE].running :
1519 &threads[thread_id & THREAD_ID_SLOT_MASK];
1522 /*---------------------------------------------------------------------------
1523 * Place the current core in idle mode - woken up on interrupt or wake
1524 * request from another core.
1525 *---------------------------------------------------------------------------
1527 void core_idle(void)
1529 IF_COP( const unsigned int core = CURRENT_CORE; )
1530 disable_irq();
1531 core_sleep(IF_COP(core));
1534 /*---------------------------------------------------------------------------
1535 * Create a thread. If using a dual core architecture, specify which core to
1536 * start the thread on.
1538 * Return ID if context area could be allocated, else NULL.
1539 *---------------------------------------------------------------------------
1541 unsigned int create_thread(void (*function)(void),
1542 void* stack, size_t stack_size,
1543 unsigned flags, const char *name
1544 IF_PRIO(, int priority)
1545 IF_COP(, unsigned int core))
1547 unsigned int i;
1548 unsigned int stack_words;
1549 uintptr_t stackptr, stackend;
1550 struct thread_entry *thread;
1551 unsigned state;
1552 int oldlevel;
1554 thread = find_empty_thread_slot();
1555 if (thread == NULL)
1557 return 0;
1560 oldlevel = disable_irq_save();
1562 /* Munge the stack to make it easy to spot stack overflows */
1563 stackptr = ALIGN_UP((uintptr_t)stack, sizeof (uintptr_t));
1564 stackend = ALIGN_DOWN((uintptr_t)stack + stack_size, sizeof (uintptr_t));
1565 stack_size = stackend - stackptr;
1566 stack_words = stack_size / sizeof (uintptr_t);
1568 for (i = 0; i < stack_words; i++)
1570 ((uintptr_t *)stackptr)[i] = DEADBEEF;
1573 /* Store interesting information */
1574 thread->name = name;
1575 thread->stack = (uintptr_t *)stackptr;
1576 thread->stack_size = stack_size;
1577 thread->queue = NULL;
1578 #ifdef HAVE_WAKEUP_EXT_CB
1579 thread->wakeup_ext_cb = NULL;
1580 #endif
1581 #ifdef HAVE_SCHEDULER_BOOSTCTRL
1582 thread->cpu_boost = 0;
1583 #endif
1584 #ifdef HAVE_PRIORITY_SCHEDULING
1585 memset(&thread->pdist, 0, sizeof(thread->pdist));
1586 thread->blocker = NULL;
1587 thread->base_priority = priority;
1588 thread->priority = priority;
1589 thread->skip_count = priority;
1590 prio_add_entry(&thread->pdist, priority);
1591 #endif
1593 #ifdef HAVE_IO_PRIORITY
1594 /* Default to high (foreground) priority */
1595 thread->io_priority = IO_PRIORITY_IMMEDIATE;
1596 #endif
1598 #if NUM_CORES > 1
1599 thread->core = core;
1601 /* Writeback stack munging or anything else before starting */
1602 if (core != CURRENT_CORE)
1604 cpucache_flush();
1606 #endif
1608 /* Thread is not on any timeout list but be a bit paranoid */
1609 thread->tmo.prev = NULL;
1611 state = (flags & CREATE_THREAD_FROZEN) ?
1612 STATE_FROZEN : STATE_RUNNING;
1614 thread->context.sp = (typeof (thread->context.sp))stackend;
1616 /* Load the thread's context structure with needed startup information */
1617 THREAD_STARTUP_INIT(core, thread, function);
1619 thread->state = state;
1620 i = thread->id; /* Snapshot while locked */
1622 if (state == STATE_RUNNING)
1623 core_schedule_wakeup(thread);
1625 UNLOCK_THREAD(thread);
1626 restore_irq(oldlevel);
1628 return i;
1631 #ifdef HAVE_SCHEDULER_BOOSTCTRL
1632 /*---------------------------------------------------------------------------
1633 * Change the boost state of a thread boosting or unboosting the CPU
1634 * as required.
1635 *---------------------------------------------------------------------------
1637 static inline void boost_thread(struct thread_entry *thread, bool boost)
1639 if ((thread->cpu_boost != 0) != boost)
1641 thread->cpu_boost = boost;
1642 cpu_boost(boost);
1646 void trigger_cpu_boost(void)
1648 struct thread_entry *current = cores[CURRENT_CORE].running;
1649 boost_thread(current, true);
1652 void cancel_cpu_boost(void)
1654 struct thread_entry *current = cores[CURRENT_CORE].running;
1655 boost_thread(current, false);
1657 #endif /* HAVE_SCHEDULER_BOOSTCTRL */
1659 /*---------------------------------------------------------------------------
1660 * Block the current thread until another thread terminates. A thread may
1661 * wait on itself to terminate which prevents it from running again and it
1662 * will need to be killed externally.
1663 * Parameter is the ID as returned from create_thread().
1664 *---------------------------------------------------------------------------
1666 void thread_wait(unsigned int thread_id)
1668 struct thread_entry *current = cores[CURRENT_CORE].running;
1669 struct thread_entry *thread = thread_id_entry(thread_id);
1671 /* Lock thread-as-waitable-object lock */
1672 corelock_lock(&thread->waiter_cl);
1674 /* Be sure it hasn't been killed yet */
1675 if (thread_id == THREAD_ID_CURRENT ||
1676 (thread->id == thread_id && thread->state != STATE_KILLED))
1678 IF_COP( current->obj_cl = &thread->waiter_cl; )
1679 current->bqp = &thread->queue;
1681 disable_irq();
1682 block_thread(current);
1684 corelock_unlock(&thread->waiter_cl);
1686 switch_thread();
1687 return;
1690 corelock_unlock(&thread->waiter_cl);
1693 /*---------------------------------------------------------------------------
1694 * Exit the current thread. The Right Way to Do Things (TM).
1695 *---------------------------------------------------------------------------
1697 /* This is done to foil optimizations that may require the current stack,
1698 * such as optimizing subexpressions that put variables on the stack that
1699 * get used after switching stacks. */
1700 #if NUM_CORES > 1
1701 /* Called by ASM stub */
1702 static void thread_final_exit_do(struct thread_entry *current)
1703 #else
1704 /* No special procedure is required before calling */
1705 static inline void thread_final_exit(struct thread_entry *current)
1706 #endif
1708 /* At this point, this thread isn't using resources allocated for
1709 * execution except the slot itself. */
1711 /* Signal this thread */
1712 thread_queue_wake(&current->queue);
1713 corelock_unlock(&current->waiter_cl);
1714 switch_thread();
1715 /* This should never and must never be reached - if it is, the
1716 * state is corrupted */
1717 THREAD_PANICF("thread_exit->K:*R", current);
1718 while (1);
1721 void thread_exit(void)
1723 register struct thread_entry * current = cores[CURRENT_CORE].running;
1725 /* Cancel CPU boost if any */
1726 cancel_cpu_boost();
1728 disable_irq();
1730 corelock_lock(&current->waiter_cl);
1731 LOCK_THREAD(current);
1733 #if defined (ALLOW_REMOVE_THREAD) && NUM_CORES > 1
1734 if (current->name == THREAD_DESTRUCT)
1736 /* Thread being killed - become a waiter */
1737 unsigned int id = current->id;
1738 UNLOCK_THREAD(current);
1739 corelock_unlock(&current->waiter_cl);
1740 thread_wait(id);
1741 THREAD_PANICF("thread_exit->WK:*R", current);
1743 #endif
1745 #ifdef HAVE_PRIORITY_SCHEDULING
1746 check_for_obj_waiters("thread_exit", current);
1747 #endif
1749 if (current->tmo.prev != NULL)
1751 /* Cancel pending timeout list removal */
1752 remove_from_list_tmo(current);
1755 /* Switch tasks and never return */
1756 block_thread_on_l(current, STATE_KILLED);
1758 /* Slot must be unusable until thread is really gone */
1759 UNLOCK_THREAD_AT_TASK_SWITCH(current);
1761 /* Update ID for this slot */
1762 new_thread_id(current->id, current);
1763 current->name = NULL;
1765 /* Do final cleanup and remove the thread */
1766 thread_final_exit(current);
1769 #ifdef ALLOW_REMOVE_THREAD
1770 /*---------------------------------------------------------------------------
1771 * Remove a thread from the scheduler. Not The Right Way to Do Things in
1772 * normal programs.
1774 * Parameter is the ID as returned from create_thread().
1776 * Use with care on threads that are not under careful control as this may
1777 * leave various objects in an undefined state.
1778 *---------------------------------------------------------------------------
1780 void remove_thread(unsigned int thread_id)
1782 #if NUM_CORES > 1
1783 /* core is not constant here because of core switching */
1784 unsigned int core = CURRENT_CORE;
1785 unsigned int old_core = NUM_CORES;
1786 struct corelock *ocl = NULL;
1787 #else
1788 const unsigned int core = CURRENT_CORE;
1789 #endif
1790 struct thread_entry *current = cores[core].running;
1791 struct thread_entry *thread = thread_id_entry(thread_id);
1793 unsigned state;
1794 int oldlevel;
1796 if (thread == current)
1797 thread_exit(); /* Current thread - do normal exit */
1799 oldlevel = disable_irq_save();
1801 corelock_lock(&thread->waiter_cl);
1802 LOCK_THREAD(thread);
1804 state = thread->state;
1806 if (thread->id != thread_id || state == STATE_KILLED)
1807 goto thread_killed;
1809 #if NUM_CORES > 1
1810 if (thread->name == THREAD_DESTRUCT)
1812 /* Thread being killed - become a waiter */
1813 UNLOCK_THREAD(thread);
1814 corelock_unlock(&thread->waiter_cl);
1815 restore_irq(oldlevel);
1816 thread_wait(thread_id);
1817 return;
1820 thread->name = THREAD_DESTRUCT; /* Slot can't be used for now */
1822 #ifdef HAVE_PRIORITY_SCHEDULING
1823 check_for_obj_waiters("remove_thread", thread);
1824 #endif
1826 if (thread->core != core)
1828 /* Switch cores and safely extract the thread there */
1829 /* Slot HAS to be unlocked or a deadlock could occur which means other
1830 * threads have to be guided into becoming thread waiters if they
1831 * attempt to remove it. */
1832 unsigned int new_core = thread->core;
1834 corelock_unlock(&thread->waiter_cl);
1836 UNLOCK_THREAD(thread);
1837 restore_irq(oldlevel);
1839 old_core = switch_core(new_core);
1841 oldlevel = disable_irq_save();
1843 corelock_lock(&thread->waiter_cl);
1844 LOCK_THREAD(thread);
1846 state = thread->state;
1847 core = new_core;
1848 /* Perform the extraction and switch ourselves back to the original
1849 processor */
1851 #endif /* NUM_CORES > 1 */
1853 if (thread->tmo.prev != NULL)
1855 /* Clean thread off the timeout list if a timeout check hasn't
1856 * run yet */
1857 remove_from_list_tmo(thread);
1860 #ifdef HAVE_SCHEDULER_BOOSTCTRL
1861 /* Cancel CPU boost if any */
1862 boost_thread(thread, false);
1863 #endif
1865 IF_COP( retry_state: )
1867 switch (state)
1869 case STATE_RUNNING:
1870 RTR_LOCK(core);
1871 /* Remove thread from ready to run tasks */
1872 remove_from_list_l(&cores[core].running, thread);
1873 rtr_subtract_entry(core, thread->priority);
1874 RTR_UNLOCK(core);
1875 break;
1876 case STATE_BLOCKED:
1877 case STATE_BLOCKED_W_TMO:
1878 /* Remove thread from the queue it's blocked on - including its
1879 * own if waiting there */
1880 #if NUM_CORES > 1
1881 if (&thread->waiter_cl != thread->obj_cl)
1883 ocl = thread->obj_cl;
1885 if (UNLIKELY(corelock_try_lock(ocl) == 0))
1887 UNLOCK_THREAD(thread);
1888 corelock_lock(ocl);
1889 LOCK_THREAD(thread);
1891 if (UNLIKELY(thread->state != state))
1893 /* Something woke the thread */
1894 state = thread->state;
1895 corelock_unlock(ocl);
1896 goto retry_state;
1900 #endif
1901 remove_from_list_l(thread->bqp, thread);
1903 #ifdef HAVE_WAKEUP_EXT_CB
1904 if (thread->wakeup_ext_cb != NULL)
1905 thread->wakeup_ext_cb(thread);
1906 #endif
1908 #ifdef HAVE_PRIORITY_SCHEDULING
1909 if (thread->blocker != NULL)
1911 /* Remove thread's priority influence from its chain */
1912 wakeup_priority_protocol_release(thread);
1914 #endif
1916 #if NUM_CORES > 1
1917 if (ocl != NULL)
1918 corelock_unlock(ocl);
1919 #endif
1920 break;
1921 /* Otherwise thread is frozen and hasn't run yet */
1924 new_thread_id(thread_id, thread);
1925 thread->state = STATE_KILLED;
1927 /* If thread was waiting on itself, it will have been removed above.
1928 * The wrong order would result in waking the thread first and deadlocking
1929 * since the slot is already locked. */
1930 thread_queue_wake(&thread->queue);
1932 thread->name = NULL;
1934 thread_killed: /* Thread was already killed */
1935 /* Removal complete - safe to unlock and reenable interrupts */
1936 corelock_unlock(&thread->waiter_cl);
1937 UNLOCK_THREAD(thread);
1938 restore_irq(oldlevel);
1940 #if NUM_CORES > 1
1941 if (old_core < NUM_CORES)
1943 /* Did a removal on another processor's thread - switch back to
1944 native core */
1945 switch_core(old_core);
1947 #endif
1949 #endif /* ALLOW_REMOVE_THREAD */
1951 #ifdef HAVE_PRIORITY_SCHEDULING
1952 /*---------------------------------------------------------------------------
1953 * Sets the thread's relative base priority for the core it runs on. Any
1954 * needed inheritance changes also may happen.
1955 *---------------------------------------------------------------------------
1957 int thread_set_priority(unsigned int thread_id, int priority)
1959 int old_base_priority = -1;
1960 struct thread_entry *thread = thread_id_entry(thread_id);
1962 /* A little safety measure */
1963 if (priority < HIGHEST_PRIORITY || priority > LOWEST_PRIORITY)
1964 return -1;
1966 /* Thread could be on any list and therefore on an interrupt accessible
1967 one - disable interrupts */
1968 int oldlevel = disable_irq_save();
1970 LOCK_THREAD(thread);
1972 /* Make sure it's not killed */
1973 if (thread_id == THREAD_ID_CURRENT ||
1974 (thread->id == thread_id && thread->state != STATE_KILLED))
1976 int old_priority = thread->priority;
1978 old_base_priority = thread->base_priority;
1979 thread->base_priority = priority;
1981 prio_move_entry(&thread->pdist, old_base_priority, priority);
1982 priority = find_first_set_bit(thread->pdist.mask);
1984 if (old_priority == priority)
1986 /* No priority change - do nothing */
1988 else if (thread->state == STATE_RUNNING)
1990 /* This thread is running - change location on the run
1991 * queue. No transitive inheritance needed. */
1992 set_running_thread_priority(thread, priority);
1994 else
1996 thread->priority = priority;
1998 if (thread->blocker != NULL)
2000 /* Bubble new priority down the chain */
2001 struct blocker *bl = thread->blocker; /* Blocker struct */
2002 struct thread_entry *bl_t = bl->thread; /* Blocking thread */
2003 struct thread_entry * const tstart = thread; /* Initial thread */
2004 const int highest = MIN(priority, old_priority); /* Higher of new or old */
2006 for (;;)
2008 struct thread_entry *next; /* Next thread to check */
2009 int bl_pr; /* Highest blocked thread */
2010 int queue_pr; /* New highest blocked thread */
2011 #if NUM_CORES > 1
2012 /* Owner can change but thread cannot be dislodged - thread
2013 * may not be the first in the queue which allows other
2014 * threads ahead in the list to be given ownership during the
2015 * operation. If thread is next then the waker will have to
2016 * wait for us and the owner of the object will remain fixed.
2017 * If we successfully grab the owner -- which at some point
2018 * is guaranteed -- then the queue remains fixed until we
2019 * pass by. */
2020 for (;;)
2022 LOCK_THREAD(bl_t);
2024 /* Double-check the owner - retry if it changed */
2025 if (LIKELY(bl->thread == bl_t))
2026 break;
2028 UNLOCK_THREAD(bl_t);
2029 bl_t = bl->thread;
2031 #endif
2032 bl_pr = bl->priority;
2034 if (highest > bl_pr)
2035 break; /* Object priority won't change */
2037 /* This will include the thread being set */
2038 queue_pr = find_highest_priority_in_list_l(*thread->bqp);
2040 if (queue_pr == bl_pr)
2041 break; /* Object priority not changing */
2043 /* Update thread boost for this object */
2044 bl->priority = queue_pr;
2045 prio_move_entry(&bl_t->pdist, bl_pr, queue_pr);
2046 bl_pr = find_first_set_bit(bl_t->pdist.mask);
2048 if (bl_t->priority == bl_pr)
2049 break; /* Blocking thread priority not changing */
2051 if (bl_t->state == STATE_RUNNING)
2053 /* Thread not blocked - we're done */
2054 set_running_thread_priority(bl_t, bl_pr);
2055 break;
2058 bl_t->priority = bl_pr;
2059 bl = bl_t->blocker; /* Blocking thread has a blocker? */
2061 if (bl == NULL)
2062 break; /* End of chain */
2064 next = bl->thread;
2066 if (UNLIKELY(next == tstart))
2067 break; /* Full-circle */
2069 UNLOCK_THREAD(thread);
2071 thread = bl_t;
2072 bl_t = next;
2073 } /* for (;;) */
2075 UNLOCK_THREAD(bl_t);
2080 UNLOCK_THREAD(thread);
2082 restore_irq(oldlevel);
2084 return old_base_priority;
2087 /*---------------------------------------------------------------------------
2088 * Returns the current base priority for a thread.
2089 *---------------------------------------------------------------------------
2091 int thread_get_priority(unsigned int thread_id)
2093 struct thread_entry *thread = thread_id_entry(thread_id);
2094 int base_priority = thread->base_priority;
2096 /* Simply check without locking slot. It may or may not be valid by the
2097 * time the function returns anyway. If all tests pass, it is the
2098 * correct value for when it was valid. */
2099 if (thread_id != THREAD_ID_CURRENT &&
2100 (thread->id != thread_id || thread->state == STATE_KILLED))
2101 base_priority = -1;
2103 return base_priority;
2105 #endif /* HAVE_PRIORITY_SCHEDULING */
2107 #ifdef HAVE_IO_PRIORITY
2108 int thread_get_io_priority(unsigned int thread_id)
2110 struct thread_entry *thread = thread_id_entry(thread_id);
2111 return thread->io_priority;
2114 void thread_set_io_priority(unsigned int thread_id,int io_priority)
2116 struct thread_entry *thread = thread_id_entry(thread_id);
2117 thread->io_priority = io_priority;
2119 #endif
2121 /*---------------------------------------------------------------------------
2122 * Starts a frozen thread - similar semantics to wakeup_thread except that
2123 * the thread is on no scheduler or wakeup queue at all. It exists simply by
2124 * virtue of the slot having a state of STATE_FROZEN.
2125 *---------------------------------------------------------------------------
2127 void thread_thaw(unsigned int thread_id)
2129 struct thread_entry *thread = thread_id_entry(thread_id);
2130 int oldlevel = disable_irq_save();
2132 LOCK_THREAD(thread);
2134 /* If thread is the current one, it cannot be frozen, therefore
2135 * there is no need to check that. */
2136 if (thread->id == thread_id && thread->state == STATE_FROZEN)
2137 core_schedule_wakeup(thread);
2139 UNLOCK_THREAD(thread);
2140 restore_irq(oldlevel);
2143 /*---------------------------------------------------------------------------
2144 * Return the ID of the currently executing thread.
2145 *---------------------------------------------------------------------------
2147 unsigned int thread_get_current(void)
2149 return cores[CURRENT_CORE].running->id;
2152 #if NUM_CORES > 1
2153 /*---------------------------------------------------------------------------
2154 * Switch the processor that the currently executing thread runs on.
2155 *---------------------------------------------------------------------------
2157 unsigned int switch_core(unsigned int new_core)
2159 const unsigned int core = CURRENT_CORE;
2160 struct thread_entry *current = cores[core].running;
2162 if (core == new_core)
2164 /* No change - just return same core */
2165 return core;
2168 int oldlevel = disable_irq_save();
2169 LOCK_THREAD(current);
2171 if (current->name == THREAD_DESTRUCT)
2173 /* Thread being killed - deactivate and let process complete */
2174 unsigned int id = current->id;
2175 UNLOCK_THREAD(current);
2176 restore_irq(oldlevel);
2177 thread_wait(id);
2178 /* Should never be reached */
2179 THREAD_PANICF("switch_core->D:*R", current);
2182 /* Get us off the running list for the current core */
2183 RTR_LOCK(core);
2184 remove_from_list_l(&cores[core].running, current);
2185 rtr_subtract_entry(core, current->priority);
2186 RTR_UNLOCK(core);
2188 /* Stash return value (old core) in a safe place */
2189 current->retval = core;
2191 /* If a timeout hadn't yet been cleaned-up it must be removed now or
2192 * the other core will likely attempt a removal from the wrong list! */
2193 if (current->tmo.prev != NULL)
2195 remove_from_list_tmo(current);
2198 /* Change the core number for this thread slot */
2199 current->core = new_core;
2201 /* Do not use core_schedule_wakeup here since this will result in
2202 * the thread starting to run on the other core before being finished on
2203 * this one. Delay the list unlock to keep the other core stuck
2204 * until this thread is ready. */
2205 RTR_LOCK(new_core);
2207 rtr_add_entry(new_core, current->priority);
2208 add_to_list_l(&cores[new_core].running, current);
2210 /* Make a callback into device-specific code, unlock the wakeup list so
2211 * that execution may resume on the new core, unlock our slot and finally
2212 * restore the interrupt level */
2213 cores[core].blk_ops.flags = TBOP_SWITCH_CORE;
2214 cores[core].blk_ops.cl_p = &cores[new_core].rtr_cl;
2215 cores[core].block_task = current;
2217 UNLOCK_THREAD(current);
2219 /* Alert other core to activity */
2220 core_wake(new_core);
2222 /* Do the stack switching, cache_maintenence and switch_thread call -
2223 requires native code */
2224 switch_thread_core(core, current);
2226 /* Finally return the old core to caller */
2227 return current->retval;
2229 #endif /* NUM_CORES > 1 */
2231 /*---------------------------------------------------------------------------
2232 * Initialize threading API. This assumes interrupts are not yet enabled. On
2233 * multicore setups, no core is allowed to proceed until create_thread calls
2234 * are safe to perform.
2235 *---------------------------------------------------------------------------
2237 void init_threads(void)
2239 const unsigned int core = CURRENT_CORE;
2240 struct thread_entry *thread;
2242 if (core == CPU)
2244 /* Initialize core locks and IDs in all slots */
2245 int n;
2246 for (n = 0; n < MAXTHREADS; n++)
2248 thread = &threads[n];
2249 corelock_init(&thread->waiter_cl);
2250 corelock_init(&thread->slot_cl);
2251 thread->id = THREAD_ID_INIT(n);
2255 /* CPU will initialize first and then sleep */
2256 thread = find_empty_thread_slot();
2258 if (thread == NULL)
2260 /* WTF? There really must be a slot available at this stage.
2261 * This can fail if, for example, .bss isn't zero'ed out by the loader
2262 * or threads is in the wrong section. */
2263 THREAD_PANICF("init_threads->no slot", NULL);
2266 /* Initialize initially non-zero members of core */
2267 cores[core].next_tmo_check = current_tick; /* Something not in the past */
2269 /* Initialize initially non-zero members of slot */
2270 UNLOCK_THREAD(thread); /* No sync worries yet */
2271 thread->name = main_thread_name;
2272 thread->state = STATE_RUNNING;
2273 IF_COP( thread->core = core; )
2274 #ifdef HAVE_PRIORITY_SCHEDULING
2275 corelock_init(&cores[core].rtr_cl);
2276 thread->base_priority = PRIORITY_USER_INTERFACE;
2277 prio_add_entry(&thread->pdist, PRIORITY_USER_INTERFACE);
2278 thread->priority = PRIORITY_USER_INTERFACE;
2279 rtr_add_entry(core, PRIORITY_USER_INTERFACE);
2280 #endif
2282 add_to_list_l(&cores[core].running, thread);
2284 if (core == CPU)
2286 thread->stack = stackbegin;
2287 thread->stack_size = (uintptr_t)stackend - (uintptr_t)stackbegin;
2288 #if NUM_CORES > 1 /* This code path will not be run on single core targets */
2289 /* Wait for other processors to finish their inits since create_thread
2290 * isn't safe to call until the kernel inits are done. The first
2291 * threads created in the system must of course be created by CPU.
2292 * Another possible approach is to initialize all cores and slots
2293 * for each core by CPU, let the remainder proceed in parallel and
2294 * signal CPU when all are finished. */
2295 core_thread_init(CPU);
2297 else
2299 /* Initial stack is the idle stack */
2300 thread->stack = idle_stacks[core];
2301 thread->stack_size = IDLE_STACK_SIZE;
2302 /* After last processor completes, it should signal all others to
2303 * proceed or may signal the next and call thread_exit(). The last one
2304 * to finish will signal CPU. */
2305 core_thread_init(core);
2306 /* Other cores do not have a main thread - go idle inside switch_thread
2307 * until a thread can run on the core. */
2308 thread_exit();
2309 #endif /* NUM_CORES */
2313 /* Shared stack scan helper for thread_stack_usage and idle_stack_usage */
2314 #if NUM_CORES == 1
2315 static inline int stack_usage(uintptr_t *stackptr, size_t stack_size)
2316 #else
2317 static int stack_usage(uintptr_t *stackptr, size_t stack_size)
2318 #endif
2320 unsigned int stack_words = stack_size / sizeof (uintptr_t);
2321 unsigned int i;
2322 int usage = 0;
2324 for (i = 0; i < stack_words; i++)
2326 if (stackptr[i] != DEADBEEF)
2328 usage = ((stack_words - i) * 100) / stack_words;
2329 break;
2333 return usage;
2336 /*---------------------------------------------------------------------------
2337 * Returns the maximum percentage of stack a thread ever used while running.
2338 * NOTE: Some large buffer allocations that don't use enough the buffer to
2339 * overwrite stackptr[0] will not be seen.
2340 *---------------------------------------------------------------------------
2342 int thread_stack_usage(const struct thread_entry *thread)
2344 if (LIKELY(thread->stack_size > 0))
2345 return stack_usage(thread->stack, thread->stack_size);
2346 return 0;
2349 #if NUM_CORES > 1
2350 /*---------------------------------------------------------------------------
2351 * Returns the maximum percentage of the core's idle stack ever used during
2352 * runtime.
2353 *---------------------------------------------------------------------------
2355 int idle_stack_usage(unsigned int core)
2357 return stack_usage(idle_stacks[core], IDLE_STACK_SIZE);
2359 #endif
2361 /*---------------------------------------------------------------------------
2362 * Fills in the buffer with the specified thread's name. If the name is NULL,
2363 * empty, or the thread is in destruct state a formatted ID is written
2364 * instead.
2365 *---------------------------------------------------------------------------
2367 void thread_get_name(char *buffer, int size,
2368 struct thread_entry *thread)
2370 if (size <= 0)
2371 return;
2373 *buffer = '\0';
2375 if (thread)
2377 /* Display thread name if one or ID if none */
2378 const char *name = thread->name;
2379 const char *fmt = "%s";
2380 if (name == NULL IF_COP(|| name == THREAD_DESTRUCT) || *name == '\0')
2382 name = (const char *)(unsigned int)thread->id;
2383 fmt = "%04lX";
2385 snprintf(buffer, size, fmt, name);