2005-02-13 18:55:14 +00:00
|
|
|
/***************************************************************************
|
2002-04-22 12:07:34 +00:00
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Ulf Ralberg
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-04-22 12:07:34 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2004-10-15 02:13:43 +00:00
|
|
|
#include "config.h"
|
2012-01-27 01:33:28 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_SIGALTSTACK_THREADS
|
|
|
|
/*
|
|
|
|
* The sp check in glibc __longjmp_chk() will cause
|
|
|
|
* a fatal error when switching threads via longjmp().
|
|
|
|
*/
|
|
|
|
#undef _FORTIFY_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2003-02-14 09:44:34 +00:00
|
|
|
#include <stdbool.h>
|
2010-05-06 21:04:40 +00:00
|
|
|
#include <stdio.h>
|
2002-04-22 12:07:34 +00:00
|
|
|
#include "thread.h"
|
2002-07-15 22:21:18 +00:00
|
|
|
#include "panic.h"
|
2005-08-26 22:52:31 +00:00
|
|
|
#include "system.h"
|
2003-02-14 09:44:34 +00:00
|
|
|
#include "kernel.h"
|
2004-10-15 02:13:43 +00:00
|
|
|
#include "cpu.h"
|
2006-09-16 16:18:11 +00:00
|
|
|
#include "string.h"
|
2006-09-23 14:38:04 +00:00
|
|
|
#ifdef RB_PROFILE
|
|
|
|
#include <profile.h>
|
|
|
|
#endif
|
2011-12-09 15:33:59 +00:00
|
|
|
#include "gcc_extensions.h"
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* ATTENTION!! *
|
|
|
|
* See notes below on implementing processor-specific portions! *
|
|
|
|
***************************************************************************/
|
2002-04-22 12:07:34 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/* Define THREAD_EXTRA_CHECKS as 1 to enable additional state checks */
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define THREAD_EXTRA_CHECKS 1 /* Always 1 for DEBUG */
|
2007-03-26 16:55:17 +00:00
|
|
|
#else
|
2007-10-16 01:25:17 +00:00
|
|
|
#define THREAD_EXTRA_CHECKS 0
|
2007-03-26 16:55:17 +00:00
|
|
|
#endif
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/**
|
|
|
|
* General locking order to guarantee progress. Order must be observed but
|
|
|
|
* all stages are not nescessarily obligatory. Going from 1) to 3) is
|
|
|
|
* perfectly legal.
|
|
|
|
*
|
|
|
|
* 1) IRQ
|
|
|
|
* This is first because of the likelyhood of having an interrupt occur that
|
|
|
|
* also accesses one of the objects farther down the list. Any non-blocking
|
|
|
|
* synchronization done may already have a lock on something during normal
|
|
|
|
* execution and if an interrupt handler running on the same processor as
|
|
|
|
* the one that has the resource locked were to attempt to access the
|
|
|
|
* resource, the interrupt handler would wait forever waiting for an unlock
|
|
|
|
* that will never happen. There is no danger if the interrupt occurs on
|
|
|
|
* a different processor because the one that has the lock will eventually
|
|
|
|
* unlock and the other processor's handler may proceed at that time. Not
|
|
|
|
* nescessary when the resource in question is definitely not available to
|
|
|
|
* interrupt handlers.
|
|
|
|
*
|
|
|
|
* 2) Kernel Object
|
|
|
|
* 1) May be needed beforehand if the kernel object allows dual-use such as
|
|
|
|
* event queues. The kernel object must have a scheme to protect itself from
|
|
|
|
* access by another processor and is responsible for serializing the calls
|
|
|
|
* to block_thread(_w_tmo) and wakeup_thread both to themselves and to each
|
2008-03-25 02:34:12 +00:00
|
|
|
* other. Objects' queues are also protected here.
|
2007-10-16 01:25:17 +00:00
|
|
|
*
|
|
|
|
* 3) Thread Slot
|
|
|
|
* This locks access to the thread's slot such that its state cannot be
|
|
|
|
* altered by another processor when a state change is in progress such as
|
|
|
|
* when it is in the process of going on a blocked list. An attempt to wake
|
|
|
|
* a thread while it is still blocking will likely desync its state with
|
|
|
|
* the other resources used for that state.
|
|
|
|
*
|
2008-03-25 02:34:12 +00:00
|
|
|
* 4) Core Lists
|
2007-10-16 01:25:17 +00:00
|
|
|
* These lists are specific to a particular processor core and are accessible
|
2008-03-25 02:34:12 +00:00
|
|
|
* by all processor cores and interrupt handlers. The running (rtr) list is
|
|
|
|
* the prime example where a thread may be added by any means.
|
|
|
|
*/
|
2002-04-22 12:07:34 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Processor specific: core_sleep/core_wake/misc. notes
|
|
|
|
*
|
|
|
|
* ARM notes:
|
|
|
|
* FIQ is not dealt with by the scheduler code and is simply restored if it
|
|
|
|
* must by masked for some reason - because threading modifies a register
|
|
|
|
* that FIQ may also modify and there's no way to accomplish it atomically.
|
|
|
|
* s3c2440 is such a case.
|
|
|
|
*
|
|
|
|
* Audio interrupts are generally treated at a higher priority than others
|
|
|
|
* usage of scheduler code with interrupts higher than HIGHEST_IRQ_LEVEL
|
|
|
|
* are not in general safe. Special cases may be constructed on a per-
|
|
|
|
* source basis and blocking operations are not available.
|
|
|
|
*
|
|
|
|
* core_sleep procedure to implement for any CPU to ensure an asychronous
|
|
|
|
* wakup never results in requiring a wait until the next tick (up to
|
|
|
|
* 10000uS!). May require assembly and careful instruction ordering.
|
2007-10-16 01:25:17 +00:00
|
|
|
*
|
2008-03-25 02:34:12 +00:00
|
|
|
* 1) On multicore, stay awake if directed to do so by another. If so, goto
|
|
|
|
* step 4.
|
|
|
|
* 2) If processor requires, atomically reenable interrupts and perform step
|
|
|
|
* 3.
|
|
|
|
* 3) Sleep the CPU core. If wakeup itself enables interrupts (stop #0x2000
|
|
|
|
* on Coldfire) goto step 5.
|
2008-01-19 13:27:47 +00:00
|
|
|
* 4) Enable interrupts.
|
|
|
|
* 5) Exit procedure.
|
2008-03-25 02:34:12 +00:00
|
|
|
*
|
|
|
|
* core_wake and multprocessor notes for sleep/wake coordination:
|
|
|
|
* If possible, to wake up another processor, the forcing of an interrupt on
|
|
|
|
* the woken core by the waker core is the easiest way to ensure a non-
|
|
|
|
* delayed wake and immediate execution of any woken threads. If that isn't
|
|
|
|
* available then some careful non-blocking synchonization is needed (as on
|
|
|
|
* PP targets at the moment).
|
|
|
|
*---------------------------------------------------------------------------
|
2007-10-16 01:25:17 +00:00
|
|
|
*/
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Cast to the the machine pointer size, whose size could be < 4 or > 32
|
|
|
|
* (someday :). */
|
|
|
|
#define DEADBEEF ((uintptr_t)0xdeadbeefdeadbeefull)
|
2010-04-16 22:01:59 +00:00
|
|
|
static struct core_entry cores[NUM_CORES] IBSS_ATTR;
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry threads[MAXTHREADS] IBSS_ATTR;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
static const char main_thread_name[] = "main";
|
2010-08-02 20:34:47 +00:00
|
|
|
#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
|
2008-03-25 02:34:12 +00:00
|
|
|
extern uintptr_t stackbegin[];
|
|
|
|
extern uintptr_t stackend[];
|
2010-08-02 20:34:47 +00:00
|
|
|
#else
|
|
|
|
extern uintptr_t *stackbegin;
|
|
|
|
extern uintptr_t *stackend;
|
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
static inline void core_sleep(IF_COP_VOID(unsigned int core))
|
2007-10-16 01:25:17 +00:00
|
|
|
__attribute__((always_inline));
|
2006-09-16 16:18:11 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
void check_tmo_threads(void)
|
|
|
|
__attribute__((noinline));
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
static inline void block_thread_on_l(struct thread_entry *thread, unsigned state)
|
|
|
|
__attribute__((always_inline));
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
static void add_to_list_tmo(struct thread_entry *thread)
|
|
|
|
__attribute__((noinline));
|
|
|
|
|
|
|
|
static void core_schedule_wakeup(struct thread_entry *thread)
|
|
|
|
__attribute__((noinline));
|
|
|
|
|
2008-01-19 13:47:26 +00:00
|
|
|
#if NUM_CORES > 1
|
2007-10-16 01:25:17 +00:00
|
|
|
static inline void run_blocking_ops(
|
2008-01-19 13:47:26 +00:00
|
|
|
unsigned int core, struct thread_entry *thread)
|
2007-10-16 01:25:17 +00:00
|
|
|
__attribute__((always_inline));
|
2008-01-19 13:47:26 +00:00
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
static void thread_stkov(struct thread_entry *thread)
|
|
|
|
__attribute__((noinline));
|
|
|
|
|
|
|
|
static inline void store_context(void* addr)
|
|
|
|
__attribute__((always_inline));
|
2006-09-16 16:18:11 +00:00
|
|
|
|
2006-11-11 05:33:24 +00:00
|
|
|
static inline void load_context(const void* addr)
|
2007-10-16 01:25:17 +00:00
|
|
|
__attribute__((always_inline));
|
|
|
|
|
2010-06-17 20:15:58 +00:00
|
|
|
#if NUM_CORES > 1
|
2010-06-18 03:10:18 +00:00
|
|
|
static void thread_final_exit_do(struct thread_entry *current)
|
2011-12-09 16:13:03 +00:00
|
|
|
__attribute__((noinline)) NORETURN_ATTR USED_ATTR;
|
2010-06-17 20:15:58 +00:00
|
|
|
#else
|
2010-06-17 20:29:59 +00:00
|
|
|
static inline void thread_final_exit(struct thread_entry *current)
|
2011-12-09 16:13:03 +00:00
|
|
|
__attribute__((always_inline)) NORETURN_ATTR;
|
2010-06-17 20:15:58 +00:00
|
|
|
#endif
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
void switch_thread(void)
|
2007-10-16 01:25:17 +00:00
|
|
|
__attribute__((noinline));
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-02-18 22:46:01 +00:00
|
|
|
* Processor/OS-specific section - include necessary core support
|
2007-10-16 01:25:17 +00:00
|
|
|
*/
|
2011-02-18 22:46:01 +00:00
|
|
|
|
2012-01-04 17:07:21 +00:00
|
|
|
|
|
|
|
#include "asm/thread.c"
|
|
|
|
|
2007-05-12 05:20:04 +00:00
|
|
|
#if defined (CPU_PP)
|
2010-06-02 12:45:36 +00:00
|
|
|
#include "thread-pp.c"
|
|
|
|
#endif /* CPU_PP */
|
2008-07-14 15:03:10 +00:00
|
|
|
|
2010-06-02 12:45:36 +00:00
|
|
|
#ifndef IF_NO_SKIP_YIELD
|
|
|
|
#define IF_NO_SKIP_YIELD(...)
|
2008-09-14 16:26:08 +00:00
|
|
|
#endif
|
2002-04-22 12:07:34 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*
|
|
|
|
* End Processor-specific section
|
|
|
|
***************************************************************************/
|
2007-02-25 21:43:10 +00:00
|
|
|
|
2007-09-28 10:20:02 +00:00
|
|
|
#if THREAD_EXTRA_CHECKS
|
|
|
|
static void thread_panicf(const char *msg, struct thread_entry *thread)
|
2007-05-12 05:20:04 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
IF_COP( const unsigned int core = thread->core; )
|
2007-09-28 10:20:02 +00:00
|
|
|
static char name[32];
|
|
|
|
thread_get_name(name, 32, thread);
|
|
|
|
panicf ("%s %s" IF_COP(" (%d)"), msg, name IF_COP(, core));
|
2007-05-12 05:20:04 +00:00
|
|
|
}
|
2007-09-28 10:20:02 +00:00
|
|
|
static void thread_stkov(struct thread_entry *thread)
|
2007-05-12 05:20:04 +00:00
|
|
|
{
|
2007-09-28 10:20:02 +00:00
|
|
|
thread_panicf("Stkov", thread);
|
2007-05-12 05:20:04 +00:00
|
|
|
}
|
2007-09-28 10:20:02 +00:00
|
|
|
#define THREAD_PANICF(msg, thread) \
|
|
|
|
thread_panicf(msg, thread)
|
|
|
|
#define THREAD_ASSERT(exp, msg, thread) \
|
|
|
|
({ if (!({ exp; })) thread_panicf((msg), (thread)); })
|
2007-05-12 05:20:04 +00:00
|
|
|
#else
|
2007-09-28 10:20:02 +00:00
|
|
|
static void thread_stkov(struct thread_entry *thread)
|
2007-05-12 05:20:04 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
IF_COP( const unsigned int core = thread->core; )
|
2007-09-28 10:20:02 +00:00
|
|
|
static char name[32];
|
|
|
|
thread_get_name(name, 32, thread);
|
|
|
|
panicf("Stkov %s" IF_COP(" (%d)"), name IF_COP(, core));
|
2007-05-12 05:20:04 +00:00
|
|
|
}
|
2007-09-28 10:20:02 +00:00
|
|
|
#define THREAD_PANICF(msg, thread)
|
|
|
|
#define THREAD_ASSERT(exp, msg, thread)
|
2007-05-12 05:20:04 +00:00
|
|
|
#endif /* THREAD_EXTRA_CHECKS */
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/* Thread locking */
|
2008-03-25 02:34:12 +00:00
|
|
|
#if NUM_CORES > 1
|
|
|
|
#define LOCK_THREAD(thread) \
|
|
|
|
({ corelock_lock(&(thread)->slot_cl); })
|
|
|
|
#define TRY_LOCK_THREAD(thread) \
|
2010-06-17 20:15:58 +00:00
|
|
|
({ corelock_try_lock(&(thread)->slot_cl); })
|
2008-03-25 02:34:12 +00:00
|
|
|
#define UNLOCK_THREAD(thread) \
|
|
|
|
({ corelock_unlock(&(thread)->slot_cl); })
|
|
|
|
#define UNLOCK_THREAD_AT_TASK_SWITCH(thread) \
|
|
|
|
({ unsigned int _core = (thread)->core; \
|
|
|
|
cores[_core].blk_ops.flags |= TBOP_UNLOCK_CORELOCK; \
|
|
|
|
cores[_core].blk_ops.cl_p = &(thread)->slot_cl; })
|
|
|
|
#else
|
|
|
|
#define LOCK_THREAD(thread) \
|
|
|
|
({ })
|
|
|
|
#define TRY_LOCK_THREAD(thread) \
|
|
|
|
({ })
|
|
|
|
#define UNLOCK_THREAD(thread) \
|
|
|
|
({ })
|
|
|
|
#define UNLOCK_THREAD_AT_TASK_SWITCH(thread) \
|
|
|
|
({ })
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* RTR list */
|
|
|
|
#define RTR_LOCK(core) \
|
|
|
|
({ corelock_lock(&cores[core].rtr_cl); })
|
|
|
|
#define RTR_UNLOCK(core) \
|
|
|
|
({ corelock_unlock(&cores[core].rtr_cl); })
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
#define rtr_add_entry(core, priority) \
|
|
|
|
prio_add_entry(&cores[core].rtr, (priority))
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
#define rtr_subtract_entry(core, priority) \
|
|
|
|
prio_subtract_entry(&cores[core].rtr, (priority))
|
|
|
|
|
|
|
|
#define rtr_move_entry(core, from, to) \
|
|
|
|
prio_move_entry(&cores[core].rtr, (from), (to))
|
2007-10-16 01:25:17 +00:00
|
|
|
#else
|
2008-03-25 02:34:12 +00:00
|
|
|
#define rtr_add_entry(core, priority)
|
|
|
|
#define rtr_add_entry_inl(core, priority)
|
|
|
|
#define rtr_subtract_entry(core, priority)
|
|
|
|
#define rtr_subtract_entry_inl(core, priotity)
|
|
|
|
#define rtr_move_entry(core, from, to)
|
|
|
|
#define rtr_move_entry_inl(core, from, to)
|
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Thread list structure - circular:
|
|
|
|
* +------------------------------+
|
|
|
|
* | |
|
|
|
|
* +--+---+<-+---+<-+---+<-+---+<-+
|
|
|
|
* Head->| T | | T | | T | | T |
|
|
|
|
* +->+---+->+---+->+---+->+---+--+
|
|
|
|
* | |
|
|
|
|
* +------------------------------+
|
2007-10-16 01:25:17 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Adds a thread to a list of threads using "insert last". Uses the "l"
|
2007-10-16 01:25:17 +00:00
|
|
|
* links.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static void add_to_list_l(struct thread_entry **list,
|
|
|
|
struct thread_entry *thread)
|
2002-04-22 12:07:34 +00:00
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
struct thread_entry *l = *list;
|
|
|
|
|
|
|
|
if (l == NULL)
|
2006-09-16 16:18:11 +00:00
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
/* Insert into unoccupied list */
|
|
|
|
thread->l.prev = thread;
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->l.next = thread;
|
2006-09-16 16:18:11 +00:00
|
|
|
*list = thread;
|
2007-10-16 01:25:17 +00:00
|
|
|
return;
|
2006-09-16 16:18:11 +00:00
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
/* Insert last */
|
|
|
|
thread->l.prev = l->l.prev;
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->l.next = l;
|
|
|
|
l->l.prev->l.next = thread;
|
2007-10-16 01:25:17 +00:00
|
|
|
l->l.prev = thread;
|
2006-09-16 16:18:11 +00:00
|
|
|
}
|
2002-04-22 12:07:34 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Removes a thread from a list of threads. Uses the "l" links.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static void remove_from_list_l(struct thread_entry **list,
|
|
|
|
struct thread_entry *thread)
|
2006-09-16 16:18:11 +00:00
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
struct thread_entry *prev, *next;
|
|
|
|
|
|
|
|
next = thread->l.next;
|
|
|
|
|
|
|
|
if (thread == next)
|
2006-09-16 16:18:11 +00:00
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
/* The only item */
|
|
|
|
*list = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread == *list)
|
|
|
|
{
|
|
|
|
/* List becomes next item */
|
|
|
|
*list = next;
|
2006-09-16 16:18:11 +00:00
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
prev = thread->l.prev;
|
2006-09-16 16:18:11 +00:00
|
|
|
|
|
|
|
/* Fix links to jump over the removed entry. */
|
2007-10-16 01:25:17 +00:00
|
|
|
next->l.prev = prev;
|
2008-03-25 02:34:12 +00:00
|
|
|
prev->l.next = next;
|
2006-09-16 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Timeout list structure - circular reverse (to make "remove item" O(1)),
|
|
|
|
* NULL-terminated forward (to ease the far more common forward traversal):
|
|
|
|
* +------------------------------+
|
|
|
|
* | |
|
|
|
|
* +--+---+<-+---+<-+---+<-+---+<-+
|
|
|
|
* Head->| T | | T | | T | | T |
|
|
|
|
* +---+->+---+->+---+->+---+-X
|
2007-10-16 01:25:17 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2006-09-16 16:18:11 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Add a thread from the core's timout list by linking the pointers in its
|
|
|
|
* tmo structure.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static void add_to_list_tmo(struct thread_entry *thread)
|
2007-03-26 03:24:36 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *tmo = cores[IF_COP_CORE(thread->core)].timeout;
|
|
|
|
THREAD_ASSERT(thread->tmo.prev == NULL,
|
|
|
|
"add_to_list_tmo->already listed", thread);
|
2007-03-26 03:24:36 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->tmo.next = NULL;
|
2007-03-26 03:24:36 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (tmo == NULL)
|
2007-03-26 03:24:36 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Insert into unoccupied list */
|
|
|
|
thread->tmo.prev = thread;
|
|
|
|
cores[IF_COP_CORE(thread->core)].timeout = thread;
|
|
|
|
return;
|
2007-03-26 03:24:36 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Insert Last */
|
|
|
|
thread->tmo.prev = tmo->tmo.prev;
|
|
|
|
tmo->tmo.prev->tmo.next = thread;
|
|
|
|
tmo->tmo.prev = thread;
|
2007-03-26 03:24:36 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Remove a thread from the core's timout list by unlinking the pointers in
|
|
|
|
* its tmo structure. Sets thread->tmo.prev to NULL to indicate the timeout
|
|
|
|
* is cancelled.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static void remove_from_list_tmo(struct thread_entry *thread)
|
2006-09-16 16:18:11 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry **list = &cores[IF_COP_CORE(thread->core)].timeout;
|
|
|
|
struct thread_entry *prev = thread->tmo.prev;
|
2007-10-16 01:25:17 +00:00
|
|
|
struct thread_entry *next = thread->tmo.next;
|
2007-07-30 16:44:36 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
THREAD_ASSERT(prev != NULL, "remove_from_list_tmo->not listed", thread);
|
2007-03-26 03:24:36 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (next != NULL)
|
|
|
|
next->tmo.prev = prev;
|
2006-09-16 16:18:11 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (thread == *list)
|
|
|
|
{
|
|
|
|
/* List becomes next item and empty if next == NULL */
|
|
|
|
*list = next;
|
|
|
|
/* Mark as unlisted */
|
2007-10-16 01:25:17 +00:00
|
|
|
thread->tmo.prev = NULL;
|
2003-02-14 09:44:34 +00:00
|
|
|
}
|
2008-03-25 02:34:12 +00:00
|
|
|
else
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
if (next == NULL)
|
|
|
|
(*list)->tmo.prev = prev;
|
|
|
|
prev->tmo.next = next;
|
|
|
|
/* Mark as unlisted */
|
|
|
|
thread->tmo.prev = NULL;
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Priority distribution structure (one category for each possible priority):
|
|
|
|
*
|
|
|
|
* +----+----+----+ ... +-----+
|
|
|
|
* hist: | F0 | F1 | F2 | | F31 |
|
|
|
|
* +----+----+----+ ... +-----+
|
|
|
|
* mask: | b0 | b1 | b2 | | b31 |
|
|
|
|
* +----+----+----+ ... +-----+
|
|
|
|
*
|
|
|
|
* F = count of threads at priority category n (frequency)
|
|
|
|
* b = bitmask of non-zero priority categories (occupancy)
|
|
|
|
*
|
|
|
|
* / if H[n] != 0 : 1
|
|
|
|
* b[n] = |
|
|
|
|
* \ else : 0
|
|
|
|
*
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
* Basic priority inheritance priotocol (PIP):
|
|
|
|
*
|
|
|
|
* Mn = mutex n, Tn = thread n
|
|
|
|
*
|
|
|
|
* A lower priority thread inherits the priority of the highest priority
|
|
|
|
* thread blocked waiting for it to complete an action (such as release a
|
|
|
|
* mutex or respond to a message via queue_send):
|
|
|
|
*
|
|
|
|
* 1) T2->M1->T1
|
|
|
|
*
|
|
|
|
* T1 owns M1, T2 is waiting for M1 to realease M1. If T2 has a higher
|
|
|
|
* priority than T1 then T1 inherits the priority of T2.
|
|
|
|
*
|
|
|
|
* 2) T3
|
|
|
|
* \/
|
|
|
|
* T2->M1->T1
|
|
|
|
*
|
|
|
|
* Situation is like 1) but T2 and T3 are both queued waiting for M1 and so
|
|
|
|
* T1 inherits the higher of T2 and T3.
|
|
|
|
*
|
|
|
|
* 3) T3->M2->T2->M1->T1
|
|
|
|
*
|
|
|
|
* T1 owns M1, T2 owns M2. If T3 has a higher priority than both T1 and T2,
|
|
|
|
* then T1 inherits the priority of T3 through T2.
|
|
|
|
*
|
|
|
|
* Blocking chains can grow arbitrarily complex (though it's best that they
|
|
|
|
* not form at all very often :) and build-up from these units.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Increment frequency at category "priority"
|
2007-10-16 01:25:17 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-03-25 02:34:12 +00:00
|
|
|
static inline unsigned int prio_add_entry(
|
|
|
|
struct priority_distribution *pd, int priority)
|
2006-09-16 16:18:11 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
unsigned int count;
|
|
|
|
/* Enough size/instruction count difference for ARM makes it worth it to
|
|
|
|
* use different code (192 bytes for ARM). Only thing better is ASM. */
|
|
|
|
#ifdef CPU_ARM
|
|
|
|
count = pd->hist[priority];
|
|
|
|
if (++count == 1)
|
|
|
|
pd->mask |= 1 << priority;
|
|
|
|
pd->hist[priority] = count;
|
|
|
|
#else /* This one's better for Coldfire */
|
|
|
|
if ((count = ++pd->hist[priority]) == 1)
|
|
|
|
pd->mask |= 1 << priority;
|
2007-10-16 01:25:17 +00:00
|
|
|
#endif
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
return count;
|
2006-09-16 16:18:11 +00:00
|
|
|
}
|
2006-09-23 14:38:04 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Decrement frequency at category "priority"
|
2007-10-16 01:25:17 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-03-25 02:34:12 +00:00
|
|
|
static inline unsigned int prio_subtract_entry(
|
|
|
|
struct priority_distribution *pd, int priority)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
unsigned int count;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
#ifdef CPU_ARM
|
|
|
|
count = pd->hist[priority];
|
|
|
|
if (--count == 0)
|
|
|
|
pd->mask &= ~(1 << priority);
|
|
|
|
pd->hist[priority] = count;
|
|
|
|
#else
|
|
|
|
if ((count = --pd->hist[priority]) == 0)
|
|
|
|
pd->mask &= ~(1 << priority);
|
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
return count;
|
2006-09-23 14:38:04 +00:00
|
|
|
}
|
2005-06-10 23:05:15 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Remove from one category and add to another
|
2007-10-16 01:25:17 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-03-25 02:34:12 +00:00
|
|
|
static inline void prio_move_entry(
|
|
|
|
struct priority_distribution *pd, int from, int to)
|
2006-09-16 16:18:11 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
uint32_t mask = pd->mask;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
#ifdef CPU_ARM
|
|
|
|
unsigned int count;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
count = pd->hist[from];
|
|
|
|
if (--count == 0)
|
|
|
|
mask &= ~(1 << from);
|
|
|
|
pd->hist[from] = count;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
count = pd->hist[to];
|
|
|
|
if (++count == 1)
|
|
|
|
mask |= 1 << to;
|
|
|
|
pd->hist[to] = count;
|
|
|
|
#else
|
|
|
|
if (--pd->hist[from] == 0)
|
|
|
|
mask &= ~(1 << from);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (++pd->hist[to] == 1)
|
|
|
|
mask |= 1 << to;
|
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
pd->mask = mask;
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Change the priority and rtr entry for a running thread
|
2007-10-16 01:25:17 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-03-25 02:34:12 +00:00
|
|
|
static inline void set_running_thread_priority(
|
|
|
|
struct thread_entry *thread, int priority)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
const unsigned int core = IF_COP_CORE(thread->core);
|
|
|
|
RTR_LOCK(core);
|
|
|
|
rtr_move_entry(core, thread->priority, priority);
|
|
|
|
thread->priority = priority;
|
|
|
|
RTR_UNLOCK(core);
|
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Finds the highest priority thread in a list of threads. If the list is
|
|
|
|
* empty, the PRIORITY_IDLE is returned.
|
|
|
|
*
|
|
|
|
* It is possible to use the struct priority_distribution within an object
|
|
|
|
* instead of scanning the remaining threads in the list but as a compromise,
|
|
|
|
* the resulting per-object memory overhead is saved at a slight speed
|
|
|
|
* penalty under high contention.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static int find_highest_priority_in_list_l(
|
|
|
|
struct thread_entry * const thread)
|
|
|
|
{
|
2008-12-14 07:09:26 +00:00
|
|
|
if (LIKELY(thread != NULL))
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Go though list until the ending up at the initial thread */
|
|
|
|
int highest_priority = thread->priority;
|
|
|
|
struct thread_entry *curr = thread;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
int priority = curr->priority;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (priority < highest_priority)
|
|
|
|
highest_priority = priority;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
curr = curr->l.next;
|
|
|
|
}
|
|
|
|
while (curr != thread);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
return highest_priority;
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
return PRIORITY_IDLE;
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Register priority with blocking system and bubble it down the chain if
|
|
|
|
* any until we reach the end or something is already equal or higher.
|
|
|
|
*
|
|
|
|
* NOTE: A simultaneous circular wait could spin deadlock on multiprocessor
|
|
|
|
* targets but that same action also guarantees a circular block anyway and
|
|
|
|
* those are prevented, right? :-)
|
2007-10-16 01:25:17 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-03-25 02:34:12 +00:00
|
|
|
static struct thread_entry *
|
|
|
|
blocker_inherit_priority(struct thread_entry *current)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
const int priority = current->priority;
|
|
|
|
struct blocker *bl = current->blocker;
|
|
|
|
struct thread_entry * const tstart = current;
|
|
|
|
struct thread_entry *bl_t = bl->thread;
|
|
|
|
|
|
|
|
/* Blocker cannot change since the object protection is held */
|
|
|
|
LOCK_THREAD(bl_t);
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *next;
|
|
|
|
int bl_pr = bl->priority;
|
|
|
|
|
|
|
|
if (priority >= bl_pr)
|
|
|
|
break; /* Object priority already high enough */
|
|
|
|
|
|
|
|
bl->priority = priority;
|
|
|
|
|
|
|
|
/* Add this one */
|
|
|
|
prio_add_entry(&bl_t->pdist, priority);
|
|
|
|
|
|
|
|
if (bl_pr < PRIORITY_IDLE)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Not first waiter - subtract old one */
|
|
|
|
prio_subtract_entry(&bl_t->pdist, bl_pr);
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (priority >= bl_t->priority)
|
|
|
|
break; /* Thread priority high enough */
|
|
|
|
|
|
|
|
if (bl_t->state == STATE_RUNNING)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Blocking thread is a running thread therefore there are no
|
|
|
|
* further blockers. Change the "run queue" on which it
|
|
|
|
* resides. */
|
|
|
|
set_running_thread_priority(bl_t, priority);
|
|
|
|
break;
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
bl_t->priority = priority;
|
|
|
|
|
|
|
|
/* If blocking thread has a blocker, apply transitive inheritance */
|
|
|
|
bl = bl_t->blocker;
|
|
|
|
|
|
|
|
if (bl == NULL)
|
|
|
|
break; /* End of chain or object doesn't support inheritance */
|
|
|
|
|
|
|
|
next = bl->thread;
|
|
|
|
|
2008-12-14 07:09:26 +00:00
|
|
|
if (UNLIKELY(next == tstart))
|
2008-03-25 02:34:12 +00:00
|
|
|
break; /* Full-circle - deadlock! */
|
|
|
|
|
|
|
|
UNLOCK_THREAD(current);
|
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
for (;;)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
LOCK_THREAD(next);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Blocker could change - retest condition */
|
2008-12-14 07:09:26 +00:00
|
|
|
if (LIKELY(bl->thread == next))
|
2008-03-25 02:34:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
UNLOCK_THREAD(next);
|
|
|
|
next = bl->thread;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
current = bl_t;
|
|
|
|
bl_t = next;
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
UNLOCK_THREAD(bl_t);
|
|
|
|
|
|
|
|
return current;
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Readjust priorities when waking a thread blocked waiting for another
|
|
|
|
* in essence "releasing" the thread's effect on the object owner. Can be
|
|
|
|
* performed from any context.
|
2007-10-16 01:25:17 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *
|
|
|
|
wakeup_priority_protocol_release(struct thread_entry *thread)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
const int priority = thread->priority;
|
|
|
|
struct blocker *bl = thread->blocker;
|
|
|
|
struct thread_entry * const tstart = thread;
|
|
|
|
struct thread_entry *bl_t = bl->thread;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Blocker cannot change since object will be locked */
|
|
|
|
LOCK_THREAD(bl_t);
|
2006-11-11 05:33:24 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->blocker = NULL; /* Thread not blocked */
|
|
|
|
|
|
|
|
for (;;)
|
2006-11-11 05:33:24 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *next;
|
|
|
|
int bl_pr = bl->priority;
|
|
|
|
|
|
|
|
if (priority > bl_pr)
|
|
|
|
break; /* Object priority higher */
|
|
|
|
|
|
|
|
next = *thread->bqp;
|
|
|
|
|
|
|
|
if (next == NULL)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* No more threads in queue */
|
|
|
|
prio_subtract_entry(&bl_t->pdist, bl_pr);
|
|
|
|
bl->priority = PRIORITY_IDLE;
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
else
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
/* Check list for highest remaining priority */
|
|
|
|
int queue_pr = find_highest_priority_in_list_l(next);
|
|
|
|
|
|
|
|
if (queue_pr == bl_pr)
|
|
|
|
break; /* Object priority not changing */
|
|
|
|
|
|
|
|
/* Change queue priority */
|
|
|
|
prio_move_entry(&bl_t->pdist, bl_pr, queue_pr);
|
|
|
|
bl->priority = queue_pr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bl_pr > bl_t->priority)
|
|
|
|
break; /* thread priority is higher */
|
|
|
|
|
|
|
|
bl_pr = find_first_set_bit(bl_t->pdist.mask);
|
|
|
|
|
|
|
|
if (bl_pr == bl_t->priority)
|
|
|
|
break; /* Thread priority not changing */
|
|
|
|
|
|
|
|
if (bl_t->state == STATE_RUNNING)
|
|
|
|
{
|
|
|
|
/* No further blockers */
|
|
|
|
set_running_thread_priority(bl_t, bl_pr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bl_t->priority = bl_pr;
|
|
|
|
|
|
|
|
/* If blocking thread has a blocker, apply transitive inheritance */
|
|
|
|
bl = bl_t->blocker;
|
|
|
|
|
|
|
|
if (bl == NULL)
|
|
|
|
break; /* End of chain or object doesn't support inheritance */
|
|
|
|
|
|
|
|
next = bl->thread;
|
|
|
|
|
2008-12-14 07:09:26 +00:00
|
|
|
if (UNLIKELY(next == tstart))
|
2008-03-25 02:34:12 +00:00
|
|
|
break; /* Full-circle - deadlock! */
|
|
|
|
|
|
|
|
UNLOCK_THREAD(thread);
|
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
LOCK_THREAD(next);
|
|
|
|
|
|
|
|
/* Blocker could change - retest condition */
|
2008-12-14 07:09:26 +00:00
|
|
|
if (LIKELY(bl->thread == next))
|
2008-03-25 02:34:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
UNLOCK_THREAD(next);
|
|
|
|
next = bl->thread;
|
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
#endif
|
2008-03-25 02:34:12 +00:00
|
|
|
thread = bl_t;
|
|
|
|
bl_t = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK_THREAD(bl_t);
|
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
2008-12-14 07:09:26 +00:00
|
|
|
if (UNLIKELY(thread != tstart))
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
/* Relock original if it changed */
|
|
|
|
LOCK_THREAD(tstart);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return cores[CURRENT_CORE].running;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Transfer ownership to a thread waiting for an objects and transfer
|
|
|
|
* inherited priority boost from other waiters. This algorithm knows that
|
|
|
|
* blocking chains may only unblock from the very end.
|
|
|
|
*
|
|
|
|
* Only the owning thread itself may call this and so the assumption that
|
|
|
|
* it is the running thread is made.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
struct thread_entry *
|
|
|
|
wakeup_priority_protocol_transfer(struct thread_entry *thread)
|
|
|
|
{
|
|
|
|
/* Waking thread inherits priority boost from object owner */
|
|
|
|
struct blocker *bl = thread->blocker;
|
|
|
|
struct thread_entry *bl_t = bl->thread;
|
|
|
|
struct thread_entry *next;
|
|
|
|
int bl_pr;
|
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
THREAD_ASSERT(cores[CURRENT_CORE].running == bl_t,
|
|
|
|
"UPPT->wrong thread", cores[CURRENT_CORE].running);
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
LOCK_THREAD(bl_t);
|
|
|
|
|
|
|
|
bl_pr = bl->priority;
|
|
|
|
|
|
|
|
/* Remove the object's boost from the owning thread */
|
|
|
|
if (prio_subtract_entry(&bl_t->pdist, bl_pr) == 0 &&
|
|
|
|
bl_pr <= bl_t->priority)
|
|
|
|
{
|
|
|
|
/* No more threads at this priority are waiting and the old level is
|
|
|
|
* at least the thread level */
|
|
|
|
int priority = find_first_set_bit(bl_t->pdist.mask);
|
|
|
|
|
|
|
|
if (priority != bl_t->priority)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Adjust this thread's priority */
|
|
|
|
set_running_thread_priority(bl_t, priority);
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
2008-03-25 02:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
next = *thread->bqp;
|
|
|
|
|
2008-12-14 07:09:26 +00:00
|
|
|
if (LIKELY(next == NULL))
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
/* Expected shortcut - no more waiters */
|
|
|
|
bl_pr = PRIORITY_IDLE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (thread->priority <= bl_pr)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Need to scan threads remaining in queue */
|
|
|
|
bl_pr = find_highest_priority_in_list_l(next);
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
if (prio_add_entry(&thread->pdist, bl_pr) == 1 &&
|
|
|
|
bl_pr < thread->priority)
|
|
|
|
{
|
|
|
|
/* Thread priority must be raised */
|
|
|
|
thread->priority = bl_pr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bl->thread = thread; /* This thread pwns */
|
|
|
|
bl->priority = bl_pr; /* Save highest blocked priority */
|
|
|
|
thread->blocker = NULL; /* Thread not blocked */
|
|
|
|
|
|
|
|
UNLOCK_THREAD(bl_t);
|
|
|
|
|
|
|
|
return bl_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* No threads must be blocked waiting for this thread except for it to exit.
|
|
|
|
* The alternative is more elaborate cleanup and object registration code.
|
|
|
|
* Check this for risk of silent data corruption when objects with
|
|
|
|
* inheritable blocking are abandoned by the owner - not precise but may
|
|
|
|
* catch something.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2010-06-17 20:15:58 +00:00
|
|
|
static void __attribute__((noinline)) check_for_obj_waiters(
|
|
|
|
const char *function, struct thread_entry *thread)
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
/* Only one bit in the mask should be set with a frequency on 1 which
|
|
|
|
* represents the thread's own base priority */
|
|
|
|
uint32_t mask = thread->pdist.mask;
|
|
|
|
if ((mask & (mask - 1)) != 0 ||
|
|
|
|
thread->pdist.hist[find_first_set_bit(mask)] > 1)
|
|
|
|
{
|
|
|
|
unsigned char name[32];
|
|
|
|
thread_get_name(name, 32, thread);
|
|
|
|
panicf("%s->%s with obj. waiters", function, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_PRIORITY_SCHEDULING */
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Move a thread back to a running state on its core.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static void core_schedule_wakeup(struct thread_entry *thread)
|
|
|
|
{
|
|
|
|
const unsigned int core = IF_COP_CORE(thread->core);
|
|
|
|
|
|
|
|
RTR_LOCK(core);
|
|
|
|
|
|
|
|
thread->state = STATE_RUNNING;
|
|
|
|
|
|
|
|
add_to_list_l(&cores[core].running, thread);
|
|
|
|
rtr_add_entry(core, thread->priority);
|
|
|
|
|
|
|
|
RTR_UNLOCK(core);
|
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
if (core != CURRENT_CORE)
|
|
|
|
core_wake(core);
|
2006-09-16 16:18:11 +00:00
|
|
|
#endif
|
2008-03-25 02:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Check the core's timeout list when at least one thread is due to wake.
|
|
|
|
* Filtering for the condition is done before making the call. Resets the
|
|
|
|
* tick when the next check will occur.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void check_tmo_threads(void)
|
|
|
|
{
|
|
|
|
const unsigned int core = CURRENT_CORE;
|
|
|
|
const long tick = current_tick; /* snapshot the current tick */
|
|
|
|
long next_tmo_check = tick + 60*HZ; /* minimum duration: once/minute */
|
|
|
|
struct thread_entry *next = cores[core].timeout;
|
|
|
|
|
|
|
|
/* If there are no processes waiting for a timeout, just keep the check
|
|
|
|
tick from falling into the past. */
|
|
|
|
|
|
|
|
/* Break the loop once we have walked through the list of all
|
|
|
|
* sleeping processes or have removed them all. */
|
|
|
|
while (next != NULL)
|
|
|
|
{
|
|
|
|
/* Check sleeping threads. Allow interrupts between checks. */
|
2008-03-26 01:50:41 +00:00
|
|
|
enable_irq();
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
struct thread_entry *curr = next;
|
|
|
|
|
|
|
|
next = curr->tmo.next;
|
|
|
|
|
|
|
|
/* Lock thread slot against explicit wakeup */
|
2008-03-26 01:50:41 +00:00
|
|
|
disable_irq();
|
2008-03-25 02:34:12 +00:00
|
|
|
LOCK_THREAD(curr);
|
|
|
|
|
|
|
|
unsigned state = curr->state;
|
|
|
|
|
|
|
|
if (state < TIMEOUT_STATE_FIRST)
|
|
|
|
{
|
|
|
|
/* Cleanup threads no longer on a timeout but still on the
|
|
|
|
* list. */
|
|
|
|
remove_from_list_tmo(curr);
|
|
|
|
}
|
2008-12-14 07:09:26 +00:00
|
|
|
else if (LIKELY(TIME_BEFORE(tick, curr->tmo_tick)))
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Timeout still pending - this will be the usual case */
|
|
|
|
if (TIME_BEFORE(curr->tmo_tick, next_tmo_check))
|
|
|
|
{
|
|
|
|
/* Earliest timeout found so far - move the next check up
|
|
|
|
to its time */
|
|
|
|
next_tmo_check = curr->tmo_tick;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Sleep timeout has been reached so bring the thread back to
|
|
|
|
* life again. */
|
|
|
|
if (state == STATE_BLOCKED_W_TMO)
|
|
|
|
{
|
2011-02-19 00:09:08 +00:00
|
|
|
#ifdef HAVE_CORELOCK_OBJECT
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Lock the waiting thread's kernel object */
|
|
|
|
struct corelock *ocl = curr->obj_cl;
|
|
|
|
|
2008-12-14 07:09:26 +00:00
|
|
|
if (UNLIKELY(corelock_try_lock(ocl) == 0))
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
/* Need to retry in the correct order though the need is
|
|
|
|
* unlikely */
|
|
|
|
UNLOCK_THREAD(curr);
|
|
|
|
corelock_lock(ocl);
|
|
|
|
LOCK_THREAD(curr);
|
|
|
|
|
2008-12-14 07:09:26 +00:00
|
|
|
if (UNLIKELY(curr->state != STATE_BLOCKED_W_TMO))
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
/* Thread was woken or removed explicitely while slot
|
|
|
|
* was unlocked */
|
|
|
|
corelock_unlock(ocl);
|
|
|
|
remove_from_list_tmo(curr);
|
|
|
|
UNLOCK_THREAD(curr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* NUM_CORES */
|
|
|
|
|
|
|
|
remove_from_list_l(curr->bqp, curr);
|
|
|
|
|
|
|
|
#ifdef HAVE_WAKEUP_EXT_CB
|
|
|
|
if (curr->wakeup_ext_cb != NULL)
|
|
|
|
curr->wakeup_ext_cb(curr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
if (curr->blocker != NULL)
|
|
|
|
wakeup_priority_protocol_release(curr);
|
|
|
|
#endif
|
|
|
|
corelock_unlock(ocl);
|
|
|
|
}
|
|
|
|
/* else state == STATE_SLEEPING */
|
|
|
|
|
|
|
|
remove_from_list_tmo(curr);
|
|
|
|
|
|
|
|
RTR_LOCK(core);
|
|
|
|
|
|
|
|
curr->state = STATE_RUNNING;
|
|
|
|
|
|
|
|
add_to_list_l(&cores[core].running, curr);
|
|
|
|
rtr_add_entry(core, curr->priority);
|
|
|
|
|
|
|
|
RTR_UNLOCK(core);
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
UNLOCK_THREAD(curr);
|
|
|
|
}
|
|
|
|
|
|
|
|
cores[core].next_tmo_check = next_tmo_check;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Performs operations that must be done before blocking a thread but after
|
|
|
|
* the state is saved.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
static inline void run_blocking_ops(
|
|
|
|
unsigned int core, struct thread_entry *thread)
|
|
|
|
{
|
|
|
|
struct thread_blk_ops *ops = &cores[core].blk_ops;
|
|
|
|
const unsigned flags = ops->flags;
|
|
|
|
|
2008-12-14 07:09:26 +00:00
|
|
|
if (LIKELY(flags == TBOP_CLEAR))
|
2008-03-25 02:34:12 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (flags)
|
|
|
|
{
|
|
|
|
case TBOP_SWITCH_CORE:
|
|
|
|
core_switch_blk_op(core, thread);
|
|
|
|
/* Fall-through */
|
|
|
|
case TBOP_UNLOCK_CORELOCK:
|
|
|
|
corelock_unlock(ops->cl_p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ops->flags = TBOP_CLEAR;
|
|
|
|
}
|
|
|
|
#endif /* NUM_CORES > 1 */
|
|
|
|
|
|
|
|
#ifdef RB_PROFILE
|
|
|
|
void profile_thread(void)
|
|
|
|
{
|
|
|
|
profstart(cores[CURRENT_CORE].running - threads);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Prepares a thread to block on an object's list and/or for a specified
|
|
|
|
* duration - expects object and slot to be appropriately locked if needed
|
|
|
|
* and interrupts to be masked.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static inline void block_thread_on_l(struct thread_entry *thread,
|
|
|
|
unsigned state)
|
|
|
|
{
|
|
|
|
/* If inlined, unreachable branches will be pruned with no size penalty
|
|
|
|
because state is passed as a constant parameter. */
|
|
|
|
const unsigned int core = IF_COP_CORE(thread->core);
|
|
|
|
|
|
|
|
/* Remove the thread from the list of running threads. */
|
|
|
|
RTR_LOCK(core);
|
|
|
|
remove_from_list_l(&cores[core].running, thread);
|
|
|
|
rtr_subtract_entry(core, thread->priority);
|
|
|
|
RTR_UNLOCK(core);
|
|
|
|
|
|
|
|
/* Add a timeout to the block if not infinite */
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case STATE_BLOCKED:
|
|
|
|
case STATE_BLOCKED_W_TMO:
|
|
|
|
/* Put the thread into a new list of inactive threads. */
|
|
|
|
add_to_list_l(thread->bqp, thread);
|
|
|
|
|
|
|
|
if (state == STATE_BLOCKED)
|
|
|
|
break;
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/* Fall-through */
|
|
|
|
case STATE_SLEEPING:
|
|
|
|
/* If this thread times out sooner than any other thread, update
|
|
|
|
next_tmo_check to its timeout */
|
|
|
|
if (TIME_BEFORE(thread->tmo_tick, cores[core].next_tmo_check))
|
|
|
|
{
|
|
|
|
cores[core].next_tmo_check = thread->tmo_tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread->tmo.prev == NULL)
|
|
|
|
{
|
|
|
|
add_to_list_tmo(thread);
|
|
|
|
}
|
|
|
|
/* else thread was never removed from list - just keep it there */
|
|
|
|
break;
|
2006-11-11 05:33:24 +00:00
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Remember the the next thread about to block. */
|
|
|
|
cores[core].block_task = thread;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Report new state. */
|
2007-10-16 01:25:17 +00:00
|
|
|
thread->state = state;
|
2006-09-16 16:18:11 +00:00
|
|
|
}
|
2005-06-10 23:05:15 +00:00
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
2007-10-16 01:25:17 +00:00
|
|
|
* Switch thread in round robin fashion for any given priority. Any thread
|
|
|
|
* that removed itself from the running list first must specify itself in
|
|
|
|
* the paramter.
|
|
|
|
*
|
|
|
|
* INTERNAL: Intended for use by kernel and not for programs.
|
2006-09-16 16:18:11 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-03-25 02:34:12 +00:00
|
|
|
void switch_thread(void)
|
2006-09-16 16:18:11 +00:00
|
|
|
{
|
2008-07-14 15:03:10 +00:00
|
|
|
|
2007-07-30 16:44:36 +00:00
|
|
|
const unsigned int core = CURRENT_CORE;
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *block = cores[core].block_task;
|
2007-10-16 01:25:17 +00:00
|
|
|
struct thread_entry *thread = cores[core].running;
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Get context to save - next thread to run is unknown until all wakeups
|
|
|
|
* are evaluated */
|
|
|
|
if (block != NULL)
|
|
|
|
{
|
|
|
|
cores[core].block_task = NULL;
|
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
2008-12-14 07:09:26 +00:00
|
|
|
if (UNLIKELY(thread == block))
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
/* This was the last thread running and another core woke us before
|
|
|
|
* reaching here. Force next thread selection to give tmo threads or
|
|
|
|
* other threads woken before this block a first chance. */
|
|
|
|
block = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Blocking task is the old one */
|
|
|
|
thread = block;
|
|
|
|
}
|
|
|
|
}
|
2007-07-30 16:44:36 +00:00
|
|
|
|
2006-01-18 20:54:13 +00:00
|
|
|
#ifdef RB_PROFILE
|
2010-12-27 09:49:33 +00:00
|
|
|
#ifdef CPU_COLDFIRE
|
2010-12-27 10:22:39 +00:00
|
|
|
_profile_thread_stopped(thread->id & THREAD_ID_SLOT_MASK);
|
2010-12-27 09:49:33 +00:00
|
|
|
#else
|
2008-12-10 08:57:10 +00:00
|
|
|
profile_thread_stopped(thread->id & THREAD_ID_SLOT_MASK);
|
2010-12-27 09:49:33 +00:00
|
|
|
#endif
|
2006-09-16 16:18:11 +00:00
|
|
|
#endif
|
2007-03-26 03:24:36 +00:00
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
/* Begin task switching by saving our current context so that we can
|
|
|
|
* restore the state of the current thread later to the point prior
|
|
|
|
* to this call. */
|
2008-03-25 02:34:12 +00:00
|
|
|
store_context(&thread->context);
|
2006-09-16 16:18:11 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/* Check if the current thread stack is overflown */
|
2010-08-02 20:34:47 +00:00
|
|
|
if (UNLIKELY(thread->stack[0] != DEADBEEF) && thread->stack_size > 0)
|
2008-03-25 02:34:12 +00:00
|
|
|
thread_stkov(thread);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-01-19 13:27:47 +00:00
|
|
|
#if NUM_CORES > 1
|
2007-10-16 01:25:17 +00:00
|
|
|
/* Run any blocking operations requested before switching/sleeping */
|
2008-03-25 02:34:12 +00:00
|
|
|
run_blocking_ops(core, thread);
|
2008-01-19 13:27:47 +00:00
|
|
|
#endif
|
2006-12-16 18:35:12 +00:00
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
2008-06-03 04:23:09 +00:00
|
|
|
IF_NO_SKIP_YIELD( if (thread->skip_count != -1) )
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Reset the value of thread's skip count */
|
2008-06-03 04:23:09 +00:00
|
|
|
thread->skip_count = 0;
|
2008-03-25 02:34:12 +00:00
|
|
|
#endif
|
2008-02-28 17:40:18 +00:00
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* If there are threads on a timeout and the earliest wakeup is due,
|
|
|
|
* check the list and wake any threads that need to start running
|
|
|
|
* again. */
|
|
|
|
if (!TIME_BEFORE(current_tick, cores[core].next_tmo_check))
|
|
|
|
{
|
|
|
|
check_tmo_threads();
|
|
|
|
}
|
2007-03-09 08:03:18 +00:00
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
disable_irq();
|
2008-03-25 02:34:12 +00:00
|
|
|
RTR_LOCK(core);
|
|
|
|
|
|
|
|
thread = cores[core].running;
|
2007-03-09 08:03:18 +00:00
|
|
|
|
2008-12-14 07:09:26 +00:00
|
|
|
if (UNLIKELY(thread == NULL))
|
2007-03-26 16:55:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Enter sleep mode to reduce power usage - woken up on interrupt
|
|
|
|
* or wakeup request from another core - expected to enable
|
|
|
|
* interrupts. */
|
|
|
|
RTR_UNLOCK(core);
|
|
|
|
core_sleep(IF_COP(core));
|
2007-03-26 16:55:17 +00:00
|
|
|
}
|
2008-03-25 02:34:12 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
/* Select the new task based on priorities and the last time a
|
|
|
|
* process got CPU time relative to the highest priority runnable
|
|
|
|
* task. */
|
|
|
|
struct priority_distribution *pd = &cores[core].rtr;
|
|
|
|
int max = find_first_set_bit(pd->mask);
|
2006-11-11 05:33:24 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (block == NULL)
|
|
|
|
{
|
|
|
|
/* Not switching on a block, tentatively select next thread */
|
|
|
|
thread = thread->l.next;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int priority = thread->priority;
|
|
|
|
int diff;
|
|
|
|
|
|
|
|
/* This ridiculously simple method of aging seems to work
|
|
|
|
* suspiciously well. It does tend to reward CPU hogs (under
|
2010-04-18 09:43:59 +00:00
|
|
|
* yielding) but that's generally not desirable at all. On
|
|
|
|
* the plus side, it, relatively to other threads, penalizes
|
|
|
|
* excess yielding which is good if some high priority thread
|
|
|
|
* is performing no useful work such as polling for a device
|
|
|
|
* to be ready. Of course, aging is only employed when higher
|
|
|
|
* and lower priority threads are runnable. The highest
|
|
|
|
* priority runnable thread(s) are never skipped unless a
|
|
|
|
* lower-priority process has aged sufficiently. Priorities
|
|
|
|
* of REALTIME class are run strictly according to priority
|
|
|
|
* thus are not subject to switchout due to lower-priority
|
|
|
|
* processes aging; they must give up the processor by going
|
|
|
|
* off the run list. */
|
2008-12-14 07:09:26 +00:00
|
|
|
if (LIKELY(priority <= max) ||
|
2008-06-03 04:23:09 +00:00
|
|
|
IF_NO_SKIP_YIELD( thread->skip_count == -1 || )
|
2010-04-18 09:43:59 +00:00
|
|
|
(priority > PRIORITY_REALTIME &&
|
|
|
|
(diff = priority - max,
|
|
|
|
++thread->skip_count > diff*diff)))
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
cores[core].running = thread;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread = thread->l.next;
|
|
|
|
}
|
2008-02-28 17:40:18 +00:00
|
|
|
#else
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Without priority use a simple FCFS algorithm */
|
|
|
|
if (block == NULL)
|
|
|
|
{
|
|
|
|
/* Not switching on a block, select next thread */
|
|
|
|
thread = thread->l.next;
|
|
|
|
cores[core].running = thread;
|
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
#endif /* HAVE_PRIORITY_SCHEDULING */
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
RTR_UNLOCK(core);
|
2008-03-26 01:50:41 +00:00
|
|
|
enable_irq();
|
2008-03-25 02:34:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
/* And finally give control to the next thread. */
|
2007-10-16 01:25:17 +00:00
|
|
|
load_context(&thread->context);
|
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
#ifdef RB_PROFILE
|
2008-12-10 08:57:10 +00:00
|
|
|
profile_thread_started(thread->id & THREAD_ID_SLOT_MASK);
|
2006-01-18 20:54:13 +00:00
|
|
|
#endif
|
2008-07-14 15:03:10 +00:00
|
|
|
|
2002-04-22 12:07:34 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Sleeps a thread for at least a specified number of ticks with zero being
|
|
|
|
* a wait until the next tick.
|
2007-10-16 01:25:17 +00:00
|
|
|
*
|
|
|
|
* INTERNAL: Intended for use by kernel and not for programs.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void sleep_thread(int ticks)
|
2006-11-11 05:33:24 +00:00
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
struct thread_entry *current = cores[CURRENT_CORE].running;
|
2006-11-11 05:33:24 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
LOCK_THREAD(current);
|
2006-11-11 05:33:24 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Set our timeout, remove from run list and join timeout list. */
|
2007-10-16 01:25:17 +00:00
|
|
|
current->tmo_tick = current_tick + ticks + 1;
|
2008-03-25 02:34:12 +00:00
|
|
|
block_thread_on_l(current, STATE_SLEEPING);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
UNLOCK_THREAD(current);
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Indefinitely block a thread on a blocking queue for explicit wakeup.
|
|
|
|
*
|
|
|
|
* INTERNAL: Intended for use by kernel objects and not for programs.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-03-25 02:34:12 +00:00
|
|
|
void block_thread(struct thread_entry *current)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Set the state to blocked and take us off of the run queue until we
|
|
|
|
* are explicitly woken */
|
|
|
|
LOCK_THREAD(current);
|
2006-12-16 18:35:12 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Set the list for explicit wakeup */
|
|
|
|
block_thread_on_l(current, STATE_BLOCKED);
|
2006-11-11 05:33:24 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
if (current->blocker != NULL)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Object supports PIP */
|
|
|
|
current = blocker_inherit_priority(current);
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
2006-11-11 05:33:24 +00:00
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
UNLOCK_THREAD(current);
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Block a thread on a blocking queue for a specified time interval or until
|
|
|
|
* explicitly woken - whichever happens first.
|
|
|
|
*
|
|
|
|
* INTERNAL: Intended for use by kernel objects and not for programs.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-03-25 02:34:12 +00:00
|
|
|
void block_thread_w_tmo(struct thread_entry *current, int timeout)
|
2003-02-14 09:44:34 +00:00
|
|
|
{
|
2006-09-16 16:18:11 +00:00
|
|
|
/* Get the entry for the current running thread. */
|
2008-03-25 02:34:12 +00:00
|
|
|
LOCK_THREAD(current);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2006-11-11 05:33:24 +00:00
|
|
|
/* Set the state to blocked with the specified timeout */
|
2007-10-16 01:25:17 +00:00
|
|
|
current->tmo_tick = current_tick + timeout;
|
2008-03-25 02:34:12 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/* Set the list for explicit wakeup */
|
2008-03-25 02:34:12 +00:00
|
|
|
block_thread_on_l(current, STATE_BLOCKED_W_TMO);
|
2006-11-11 05:33:24 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
if (current->blocker != NULL)
|
|
|
|
{
|
|
|
|
/* Object supports PIP */
|
|
|
|
current = blocker_inherit_priority(current);
|
|
|
|
}
|
|
|
|
#endif
|
2006-11-11 05:33:24 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
UNLOCK_THREAD(current);
|
2003-02-14 09:44:34 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Explicitly wakeup a thread on a blocking queue. Only effects threads of
|
|
|
|
* STATE_BLOCKED and STATE_BLOCKED_W_TMO.
|
|
|
|
*
|
|
|
|
* This code should be considered a critical section by the caller meaning
|
|
|
|
* that the object's corelock should be held.
|
2007-10-16 01:25:17 +00:00
|
|
|
*
|
|
|
|
* INTERNAL: Intended for use by kernel objects and not for programs.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-03-25 02:34:12 +00:00
|
|
|
unsigned int wakeup_thread(struct thread_entry **list)
|
2006-12-16 18:35:12 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *thread = *list;
|
|
|
|
unsigned int result = THREAD_NONE;
|
2006-12-16 18:35:12 +00:00
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
/* Check if there is a blocked thread at all. */
|
2008-03-25 02:34:12 +00:00
|
|
|
if (thread == NULL)
|
|
|
|
return result;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
LOCK_THREAD(thread);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
/* Determine thread's current state. */
|
2008-03-25 02:34:12 +00:00
|
|
|
switch (thread->state)
|
2006-09-16 16:18:11 +00:00
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
case STATE_BLOCKED:
|
|
|
|
case STATE_BLOCKED_W_TMO:
|
2008-03-25 02:34:12 +00:00
|
|
|
remove_from_list_l(list, thread);
|
|
|
|
|
|
|
|
result = THREAD_OK;
|
|
|
|
|
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
struct thread_entry *current;
|
|
|
|
struct blocker *bl = thread->blocker;
|
|
|
|
|
|
|
|
if (bl == NULL)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* No inheritance - just boost the thread by aging */
|
2008-06-03 04:23:09 +00:00
|
|
|
IF_NO_SKIP_YIELD( if (thread->skip_count != -1) )
|
|
|
|
thread->skip_count = thread->priority;
|
2008-03-25 02:34:12 +00:00
|
|
|
current = cores[CURRENT_CORE].running;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Call the specified unblocking PIP */
|
|
|
|
current = bl->wakeup_protocol(thread);
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
2010-04-18 09:43:59 +00:00
|
|
|
if (current != NULL &&
|
2010-04-18 10:03:25 +00:00
|
|
|
find_first_set_bit(cores[IF_COP_CORE(current->core)].rtr.mask)
|
|
|
|
< current->priority)
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
2010-04-18 10:03:25 +00:00
|
|
|
/* There is a thread ready to run of higher or same priority on
|
|
|
|
* the same core as the current one; recommend a task switch.
|
|
|
|
* Knowing if this is an interrupt call would be helpful here. */
|
2008-03-25 02:34:12 +00:00
|
|
|
result |= THREAD_SWITCH;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_PRIORITY_SCHEDULING */
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
core_schedule_wakeup(thread);
|
2008-03-25 02:34:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Nothing to do. State is not blocked. */
|
2007-10-16 01:25:17 +00:00
|
|
|
#if THREAD_EXTRA_CHECKS
|
2008-03-25 02:34:12 +00:00
|
|
|
default:
|
2007-10-16 01:25:17 +00:00
|
|
|
THREAD_PANICF("wakeup_thread->block invalid", thread);
|
|
|
|
case STATE_RUNNING:
|
|
|
|
case STATE_KILLED:
|
2008-03-25 02:34:12 +00:00
|
|
|
break;
|
2007-10-16 01:25:17 +00:00
|
|
|
#endif
|
2006-09-16 16:18:11 +00:00
|
|
|
}
|
2006-08-21 17:35:35 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
UNLOCK_THREAD(thread);
|
|
|
|
return result;
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Wakeup an entire queue of threads - returns bitwise-or of return bitmask
|
|
|
|
* from each operation or THREAD_NONE of nothing was awakened. Object owning
|
|
|
|
* the queue must be locked first.
|
|
|
|
*
|
|
|
|
* INTERNAL: Intended for use by kernel objects and not for programs.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
unsigned int thread_queue_wake(struct thread_entry **list)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
unsigned result = THREAD_NONE;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
unsigned int rc = wakeup_thread(list);
|
|
|
|
|
|
|
|
if (rc == THREAD_NONE)
|
|
|
|
break; /* No more threads */
|
|
|
|
|
|
|
|
result |= rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Assign the thread slot a new ID. Version is 1-255.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static void new_thread_id(unsigned int slot_num,
|
|
|
|
struct thread_entry *thread)
|
|
|
|
{
|
|
|
|
unsigned int version =
|
|
|
|
(thread->id + (1u << THREAD_ID_VERSION_SHIFT))
|
|
|
|
& THREAD_ID_VERSION_MASK;
|
|
|
|
|
|
|
|
/* If wrapped to 0, make it 1 */
|
|
|
|
if (version == 0)
|
|
|
|
version = 1u << THREAD_ID_VERSION_SHIFT;
|
|
|
|
|
|
|
|
thread->id = version | (slot_num & THREAD_ID_SLOT_MASK);
|
|
|
|
}
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Find an empty thread slot or MAXTHREADS if none found. The slot returned
|
|
|
|
* will be locked on multicore.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-03-25 02:34:12 +00:00
|
|
|
static struct thread_entry * find_empty_thread_slot(void)
|
2007-03-26 16:55:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Any slot could be on an interrupt-accessible list */
|
2008-03-26 01:50:41 +00:00
|
|
|
IF_COP( int oldlevel = disable_irq_save(); )
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *thread = NULL;
|
2007-03-26 16:55:17 +00:00
|
|
|
int n;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2007-03-26 16:55:17 +00:00
|
|
|
for (n = 0; n < MAXTHREADS; n++)
|
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
/* Obtain current slot state - lock it on multicore */
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *t = &threads[n];
|
|
|
|
LOCK_THREAD(t);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (t->state == STATE_KILLED IF_COP( && t->name != THREAD_DESTRUCT ))
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
|
|
|
/* Slot is empty - leave it locked and caller will unlock */
|
2008-03-25 02:34:12 +00:00
|
|
|
thread = t;
|
2007-10-16 01:25:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finished examining slot - no longer busy - unlock on multicore */
|
2008-03-25 02:34:12 +00:00
|
|
|
UNLOCK_THREAD(t);
|
2007-03-26 16:55:17 +00:00
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
IF_COP( restore_irq(oldlevel); ) /* Reenable interrups - this slot is
|
2008-03-25 02:34:12 +00:00
|
|
|
not accesible to them yet */
|
|
|
|
return thread;
|
2007-03-26 16:55:17 +00:00
|
|
|
}
|
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Return the thread_entry pointer for a thread_id. Return the current
|
2010-12-28 14:24:13 +00:00
|
|
|
* thread if the ID is (unsigned int)-1 (alias for current).
|
2008-12-10 08:57:10 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
struct thread_entry * thread_id_entry(unsigned int thread_id)
|
|
|
|
{
|
2011-03-05 17:48:06 +00:00
|
|
|
return &threads[thread_id & THREAD_ID_SLOT_MASK];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Return the thread id of the calling thread
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
unsigned int thread_self(void)
|
|
|
|
{
|
|
|
|
return cores[CURRENT_CORE].running->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Return the thread entry of the calling thread.
|
|
|
|
*
|
|
|
|
* INTERNAL: Intended for use by kernel and not for programs.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
struct thread_entry* thread_self_entry(void)
|
|
|
|
{
|
|
|
|
return cores[CURRENT_CORE].running;
|
2008-12-10 08:57:10 +00:00
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Place the current core in idle mode - woken up on interrupt or wake
|
|
|
|
* request from another core.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void core_idle(void)
|
2007-03-26 03:24:36 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
IF_COP( const unsigned int core = CURRENT_CORE; )
|
2008-03-26 01:50:41 +00:00
|
|
|
disable_irq();
|
2008-01-19 13:27:47 +00:00
|
|
|
core_sleep(IF_COP(core));
|
2007-03-26 03:24:36 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 17:35:35 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Create a thread. If using a dual core architecture, specify which core to
|
|
|
|
* start the thread on.
|
|
|
|
*
|
2007-03-04 20:06:41 +00:00
|
|
|
* Return ID if context area could be allocated, else NULL.
|
2002-04-22 12:07:34 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-12-10 08:57:10 +00:00
|
|
|
unsigned int create_thread(void (*function)(void),
|
|
|
|
void* stack, size_t stack_size,
|
|
|
|
unsigned flags, const char *name
|
|
|
|
IF_PRIO(, int priority)
|
|
|
|
IF_COP(, unsigned int core))
|
2002-04-22 12:07:34 +00:00
|
|
|
{
|
2005-06-10 23:05:15 +00:00
|
|
|
unsigned int i;
|
2008-03-25 02:34:12 +00:00
|
|
|
unsigned int stack_words;
|
|
|
|
uintptr_t stackptr, stackend;
|
2006-09-02 07:56:52 +00:00
|
|
|
struct thread_entry *thread;
|
2007-10-16 01:25:17 +00:00
|
|
|
unsigned state;
|
2008-03-25 02:34:12 +00:00
|
|
|
int oldlevel;
|
2007-03-04 20:06:41 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
thread = find_empty_thread_slot();
|
|
|
|
if (thread == NULL)
|
2007-03-04 20:06:41 +00:00
|
|
|
{
|
2008-12-10 08:57:10 +00:00
|
|
|
return 0;
|
2007-03-04 20:06:41 +00:00
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
oldlevel = disable_irq_save();
|
2008-03-25 02:34:12 +00:00
|
|
|
|
2005-06-10 23:05:15 +00:00
|
|
|
/* Munge the stack to make it easy to spot stack overflows */
|
2008-03-25 02:34:12 +00:00
|
|
|
stackptr = ALIGN_UP((uintptr_t)stack, sizeof (uintptr_t));
|
|
|
|
stackend = ALIGN_DOWN((uintptr_t)stack + stack_size, sizeof (uintptr_t));
|
|
|
|
stack_size = stackend - stackptr;
|
|
|
|
stack_words = stack_size / sizeof (uintptr_t);
|
|
|
|
|
|
|
|
for (i = 0; i < stack_words; i++)
|
2005-06-10 23:05:15 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
((uintptr_t *)stackptr)[i] = DEADBEEF;
|
2005-06-10 23:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Store interesting information */
|
2006-09-02 07:56:52 +00:00
|
|
|
thread->name = name;
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->stack = (uintptr_t *)stackptr;
|
2006-09-02 07:56:52 +00:00
|
|
|
thread->stack_size = stack_size;
|
2007-10-16 01:25:17 +00:00
|
|
|
thread->queue = NULL;
|
2008-03-25 02:34:12 +00:00
|
|
|
#ifdef HAVE_WAKEUP_EXT_CB
|
|
|
|
thread->wakeup_ext_cb = NULL;
|
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
#ifdef HAVE_SCHEDULER_BOOSTCTRL
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->cpu_boost = 0;
|
2007-10-16 01:25:17 +00:00
|
|
|
#endif
|
2006-09-16 16:18:11 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
2008-03-25 02:34:12 +00:00
|
|
|
memset(&thread->pdist, 0, sizeof(thread->pdist));
|
|
|
|
thread->blocker = NULL;
|
|
|
|
thread->base_priority = priority;
|
2006-09-16 16:18:11 +00:00
|
|
|
thread->priority = priority;
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->skip_count = priority;
|
|
|
|
prio_add_entry(&thread->pdist, priority);
|
2007-03-26 16:55:17 +00:00
|
|
|
#endif
|
|
|
|
|
2010-04-03 22:02:09 +00:00
|
|
|
#ifdef HAVE_IO_PRIORITY
|
|
|
|
/* Default to high (foreground) priority */
|
|
|
|
thread->io_priority = IO_PRIORITY_IMMEDIATE;
|
|
|
|
#endif
|
|
|
|
|
2007-03-26 16:55:17 +00:00
|
|
|
#if NUM_CORES > 1
|
|
|
|
thread->core = core;
|
2007-08-05 12:32:34 +00:00
|
|
|
|
|
|
|
/* Writeback stack munging or anything else before starting */
|
|
|
|
if (core != CURRENT_CORE)
|
2007-09-28 10:54:27 +00:00
|
|
|
{
|
2012-02-05 03:27:42 +00:00
|
|
|
commit_dcache();
|
2007-09-28 10:54:27 +00:00
|
|
|
}
|
2006-09-16 16:18:11 +00:00
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
/* Thread is not on any timeout list but be a bit paranoid */
|
|
|
|
thread->tmo.prev = NULL;
|
|
|
|
|
|
|
|
state = (flags & CREATE_THREAD_FROZEN) ?
|
|
|
|
STATE_FROZEN : STATE_RUNNING;
|
2006-09-16 16:18:11 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->context.sp = (typeof (thread->context.sp))stackend;
|
2007-09-28 10:20:02 +00:00
|
|
|
|
|
|
|
/* Load the thread's context structure with needed startup information */
|
|
|
|
THREAD_STARTUP_INIT(core, thread, function);
|
2004-06-22 11:32:36 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->state = state;
|
2008-12-10 08:57:10 +00:00
|
|
|
i = thread->id; /* Snapshot while locked */
|
2008-03-25 02:34:12 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
if (state == STATE_RUNNING)
|
2008-03-25 02:34:12 +00:00
|
|
|
core_schedule_wakeup(thread);
|
|
|
|
|
|
|
|
UNLOCK_THREAD(thread);
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(oldlevel);
|
2007-02-25 21:43:10 +00:00
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
return i;
|
2004-04-30 20:23:04 +00:00
|
|
|
}
|
|
|
|
|
2006-10-15 11:57:52 +00:00
|
|
|
#ifdef HAVE_SCHEDULER_BOOSTCTRL
|
2008-03-25 02:34:12 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Change the boost state of a thread boosting or unboosting the CPU
|
|
|
|
* as required.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static inline void boost_thread(struct thread_entry *thread, bool boost)
|
|
|
|
{
|
|
|
|
if ((thread->cpu_boost != 0) != boost)
|
|
|
|
{
|
|
|
|
thread->cpu_boost = boost;
|
|
|
|
cpu_boost(boost);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-15 11:57:52 +00:00
|
|
|
void trigger_cpu_boost(void)
|
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
struct thread_entry *current = cores[CURRENT_CORE].running;
|
2008-03-25 02:34:12 +00:00
|
|
|
boost_thread(current, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cancel_cpu_boost(void)
|
|
|
|
{
|
|
|
|
struct thread_entry *current = cores[CURRENT_CORE].running;
|
|
|
|
boost_thread(current, false);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_SCHEDULER_BOOSTCTRL */
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Block the current thread until another thread terminates. A thread may
|
|
|
|
* wait on itself to terminate which prevents it from running again and it
|
|
|
|
* will need to be killed externally.
|
|
|
|
* Parameter is the ID as returned from create_thread().
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-12-10 08:57:10 +00:00
|
|
|
void thread_wait(unsigned int thread_id)
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
struct thread_entry *current = cores[CURRENT_CORE].running;
|
2008-12-10 08:57:10 +00:00
|
|
|
struct thread_entry *thread = thread_id_entry(thread_id);
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
/* Lock thread-as-waitable-object lock */
|
|
|
|
corelock_lock(&thread->waiter_cl);
|
|
|
|
|
|
|
|
/* Be sure it hasn't been killed yet */
|
2011-03-05 17:48:06 +00:00
|
|
|
if (thread->id == thread_id && thread->state != STATE_KILLED)
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
IF_COP( current->obj_cl = &thread->waiter_cl; )
|
|
|
|
current->bqp = &thread->queue;
|
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
disable_irq();
|
2008-03-25 02:34:12 +00:00
|
|
|
block_thread(current);
|
|
|
|
|
|
|
|
corelock_unlock(&thread->waiter_cl);
|
|
|
|
|
|
|
|
switch_thread();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
corelock_unlock(&thread->waiter_cl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Exit the current thread. The Right Way to Do Things (TM).
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2010-06-17 20:15:58 +00:00
|
|
|
/* This is done to foil optimizations that may require the current stack,
|
|
|
|
* such as optimizing subexpressions that put variables on the stack that
|
|
|
|
* get used after switching stacks. */
|
|
|
|
#if NUM_CORES > 1
|
2010-06-18 03:10:18 +00:00
|
|
|
/* Called by ASM stub */
|
|
|
|
static void thread_final_exit_do(struct thread_entry *current)
|
|
|
|
#else
|
|
|
|
/* No special procedure is required before calling */
|
|
|
|
static inline void thread_final_exit(struct thread_entry *current)
|
|
|
|
#endif
|
|
|
|
{
|
2010-06-17 20:15:58 +00:00
|
|
|
/* At this point, this thread isn't using resources allocated for
|
|
|
|
* execution except the slot itself. */
|
|
|
|
|
|
|
|
/* Signal this thread */
|
|
|
|
thread_queue_wake(¤t->queue);
|
|
|
|
corelock_unlock(¤t->waiter_cl);
|
|
|
|
switch_thread();
|
|
|
|
/* This should never and must never be reached - if it is, the
|
|
|
|
* state is corrupted */
|
|
|
|
THREAD_PANICF("thread_exit->K:*R", current);
|
|
|
|
while (1);
|
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
void thread_exit(void)
|
|
|
|
{
|
2010-06-17 20:15:58 +00:00
|
|
|
register struct thread_entry * current = cores[CURRENT_CORE].running;
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
/* Cancel CPU boost if any */
|
|
|
|
cancel_cpu_boost();
|
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
disable_irq();
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
corelock_lock(¤t->waiter_cl);
|
|
|
|
LOCK_THREAD(current);
|
|
|
|
|
|
|
|
#if defined (ALLOW_REMOVE_THREAD) && NUM_CORES > 1
|
|
|
|
if (current->name == THREAD_DESTRUCT)
|
|
|
|
{
|
|
|
|
/* Thread being killed - become a waiter */
|
2008-12-10 08:57:10 +00:00
|
|
|
unsigned int id = current->id;
|
2008-03-25 02:34:12 +00:00
|
|
|
UNLOCK_THREAD(current);
|
|
|
|
corelock_unlock(¤t->waiter_cl);
|
2008-12-10 08:57:10 +00:00
|
|
|
thread_wait(id);
|
2008-03-25 02:34:12 +00:00
|
|
|
THREAD_PANICF("thread_exit->WK:*R", current);
|
|
|
|
}
|
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
check_for_obj_waiters("thread_exit", current);
|
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (current->tmo.prev != NULL)
|
|
|
|
{
|
|
|
|
/* Cancel pending timeout list removal */
|
|
|
|
remove_from_list_tmo(current);
|
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Switch tasks and never return */
|
|
|
|
block_thread_on_l(current, STATE_KILLED);
|
2007-11-20 03:44:25 +00:00
|
|
|
|
2010-06-17 20:15:58 +00:00
|
|
|
/* Slot must be unusable until thread is really gone */
|
|
|
|
UNLOCK_THREAD_AT_TASK_SWITCH(current);
|
2008-12-10 08:57:10 +00:00
|
|
|
|
|
|
|
/* Update ID for this slot */
|
|
|
|
new_thread_id(current->id, current);
|
2008-03-25 02:34:12 +00:00
|
|
|
current->name = NULL;
|
2007-11-20 03:44:25 +00:00
|
|
|
|
2010-06-18 03:10:18 +00:00
|
|
|
/* Do final cleanup and remove the thread */
|
2010-06-17 20:15:58 +00:00
|
|
|
thread_final_exit(current);
|
2006-10-15 11:57:52 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
#ifdef ALLOW_REMOVE_THREAD
|
2006-08-21 17:35:35 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Remove a thread from the scheduler. Not The Right Way to Do Things in
|
|
|
|
* normal programs.
|
|
|
|
*
|
2004-04-30 20:23:04 +00:00
|
|
|
* Parameter is the ID as returned from create_thread().
|
2007-10-16 01:25:17 +00:00
|
|
|
*
|
|
|
|
* Use with care on threads that are not under careful control as this may
|
2008-03-25 02:34:12 +00:00
|
|
|
* leave various objects in an undefined state.
|
2004-04-30 20:23:04 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-12-10 08:57:10 +00:00
|
|
|
void remove_thread(unsigned int thread_id)
|
2006-08-21 17:35:35 +00:00
|
|
|
{
|
2011-02-19 00:09:08 +00:00
|
|
|
#ifdef HAVE_CORELOCK_OBJECT
|
2007-10-16 01:25:17 +00:00
|
|
|
/* core is not constant here because of core switching */
|
|
|
|
unsigned int core = CURRENT_CORE;
|
|
|
|
unsigned int old_core = NUM_CORES;
|
2008-03-25 02:34:12 +00:00
|
|
|
struct corelock *ocl = NULL;
|
2007-10-16 01:25:17 +00:00
|
|
|
#else
|
2007-09-28 10:20:02 +00:00
|
|
|
const unsigned int core = CURRENT_CORE;
|
2007-10-16 01:25:17 +00:00
|
|
|
#endif
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *current = cores[core].running;
|
2008-12-10 08:57:10 +00:00
|
|
|
struct thread_entry *thread = thread_id_entry(thread_id);
|
2008-03-25 02:34:12 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
unsigned state;
|
|
|
|
int oldlevel;
|
2007-07-30 16:44:36 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (thread == current)
|
|
|
|
thread_exit(); /* Current thread - do normal exit */
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
oldlevel = disable_irq_save();
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
corelock_lock(&thread->waiter_cl);
|
|
|
|
LOCK_THREAD(thread);
|
|
|
|
|
|
|
|
state = thread->state;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
if (thread->id != thread_id || state == STATE_KILLED)
|
2007-10-16 01:25:17 +00:00
|
|
|
goto thread_killed;
|
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
2008-03-25 02:34:12 +00:00
|
|
|
if (thread->name == THREAD_DESTRUCT)
|
|
|
|
{
|
|
|
|
/* Thread being killed - become a waiter */
|
|
|
|
UNLOCK_THREAD(thread);
|
|
|
|
corelock_unlock(&thread->waiter_cl);
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(oldlevel);
|
2008-12-10 08:57:10 +00:00
|
|
|
thread_wait(thread_id);
|
2008-03-25 02:34:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->name = THREAD_DESTRUCT; /* Slot can't be used for now */
|
|
|
|
|
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
check_for_obj_waiters("remove_thread", thread);
|
|
|
|
#endif
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
if (thread->core != core)
|
|
|
|
{
|
|
|
|
/* Switch cores and safely extract the thread there */
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Slot HAS to be unlocked or a deadlock could occur which means other
|
|
|
|
* threads have to be guided into becoming thread waiters if they
|
|
|
|
* attempt to remove it. */
|
2007-10-16 01:25:17 +00:00
|
|
|
unsigned int new_core = thread->core;
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
corelock_unlock(&thread->waiter_cl);
|
|
|
|
|
|
|
|
UNLOCK_THREAD(thread);
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(oldlevel);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
old_core = switch_core(new_core);
|
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
oldlevel = disable_irq_save();
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
corelock_lock(&thread->waiter_cl);
|
|
|
|
LOCK_THREAD(thread);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
state = thread->state;
|
|
|
|
core = new_core;
|
2007-10-16 01:25:17 +00:00
|
|
|
/* Perform the extraction and switch ourselves back to the original
|
|
|
|
processor */
|
|
|
|
}
|
|
|
|
#endif /* NUM_CORES > 1 */
|
|
|
|
|
|
|
|
if (thread->tmo.prev != NULL)
|
|
|
|
{
|
|
|
|
/* Clean thread off the timeout list if a timeout check hasn't
|
|
|
|
* run yet */
|
|
|
|
remove_from_list_tmo(thread);
|
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
#ifdef HAVE_SCHEDULER_BOOSTCTRL
|
|
|
|
/* Cancel CPU boost if any */
|
2007-11-20 03:44:25 +00:00
|
|
|
boost_thread(thread, false);
|
2007-09-28 10:20:02 +00:00
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
IF_COP( retry_state: )
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case STATE_RUNNING:
|
2008-03-25 02:34:12 +00:00
|
|
|
RTR_LOCK(core);
|
2007-10-16 01:25:17 +00:00
|
|
|
/* Remove thread from ready to run tasks */
|
|
|
|
remove_from_list_l(&cores[core].running, thread);
|
2008-03-25 02:34:12 +00:00
|
|
|
rtr_subtract_entry(core, thread->priority);
|
|
|
|
RTR_UNLOCK(core);
|
2007-10-16 01:25:17 +00:00
|
|
|
break;
|
|
|
|
case STATE_BLOCKED:
|
|
|
|
case STATE_BLOCKED_W_TMO:
|
|
|
|
/* Remove thread from the queue it's blocked on - including its
|
|
|
|
* own if waiting there */
|
2008-03-25 02:34:12 +00:00
|
|
|
#if NUM_CORES > 1
|
|
|
|
if (&thread->waiter_cl != thread->obj_cl)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
ocl = thread->obj_cl;
|
|
|
|
|
2008-12-14 07:09:26 +00:00
|
|
|
if (UNLIKELY(corelock_try_lock(ocl) == 0))
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
UNLOCK_THREAD(thread);
|
|
|
|
corelock_lock(ocl);
|
|
|
|
LOCK_THREAD(thread);
|
|
|
|
|
2008-12-14 07:09:26 +00:00
|
|
|
if (UNLIKELY(thread->state != state))
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
/* Something woke the thread */
|
|
|
|
state = thread->state;
|
|
|
|
corelock_unlock(ocl);
|
|
|
|
goto retry_state;
|
|
|
|
}
|
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
2008-03-25 02:34:12 +00:00
|
|
|
#endif
|
|
|
|
remove_from_list_l(thread->bqp, thread);
|
|
|
|
|
|
|
|
#ifdef HAVE_WAKEUP_EXT_CB
|
|
|
|
if (thread->wakeup_ext_cb != NULL)
|
|
|
|
thread->wakeup_ext_cb(thread);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
if (thread->blocker != NULL)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Remove thread's priority influence from its chain */
|
|
|
|
wakeup_priority_protocol_release(thread);
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
2008-03-25 02:34:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
if (ocl != NULL)
|
|
|
|
corelock_unlock(ocl);
|
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
break;
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Otherwise thread is frozen and hasn't run yet */
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
new_thread_id(thread_id, thread);
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->state = STATE_KILLED;
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/* If thread was waiting on itself, it will have been removed above.
|
|
|
|
* The wrong order would result in waking the thread first and deadlocking
|
|
|
|
* since the slot is already locked. */
|
2008-03-25 02:34:12 +00:00
|
|
|
thread_queue_wake(&thread->queue);
|
|
|
|
|
|
|
|
thread->name = NULL;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
thread_killed: /* Thread was already killed */
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Removal complete - safe to unlock and reenable interrupts */
|
|
|
|
corelock_unlock(&thread->waiter_cl);
|
|
|
|
UNLOCK_THREAD(thread);
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(oldlevel);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
if (old_core < NUM_CORES)
|
|
|
|
{
|
|
|
|
/* Did a removal on another processor's thread - switch back to
|
|
|
|
native core */
|
|
|
|
switch_core(old_core);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2008-03-25 02:34:12 +00:00
|
|
|
#endif /* ALLOW_REMOVE_THREAD */
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Sets the thread's relative base priority for the core it runs on. Any
|
|
|
|
* needed inheritance changes also may happen.
|
2007-10-16 01:25:17 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-12-10 08:57:10 +00:00
|
|
|
int thread_set_priority(unsigned int thread_id, int priority)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
int old_base_priority = -1;
|
2008-12-10 08:57:10 +00:00
|
|
|
struct thread_entry *thread = thread_id_entry(thread_id);
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
/* A little safety measure */
|
|
|
|
if (priority < HIGHEST_PRIORITY || priority > LOWEST_PRIORITY)
|
|
|
|
return -1;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Thread could be on any list and therefore on an interrupt accessible
|
|
|
|
one - disable interrupts */
|
2008-03-26 01:50:41 +00:00
|
|
|
int oldlevel = disable_irq_save();
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
LOCK_THREAD(thread);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Make sure it's not killed */
|
2011-03-05 17:48:06 +00:00
|
|
|
if (thread->id == thread_id && thread->state != STATE_KILLED)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
int old_priority = thread->priority;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
old_base_priority = thread->base_priority;
|
|
|
|
thread->base_priority = priority;
|
|
|
|
|
|
|
|
prio_move_entry(&thread->pdist, old_base_priority, priority);
|
|
|
|
priority = find_first_set_bit(thread->pdist.mask);
|
|
|
|
|
|
|
|
if (old_priority == priority)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* No priority change - do nothing */
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
2008-03-25 02:34:12 +00:00
|
|
|
else if (thread->state == STATE_RUNNING)
|
|
|
|
{
|
|
|
|
/* This thread is running - change location on the run
|
|
|
|
* queue. No transitive inheritance needed. */
|
|
|
|
set_running_thread_priority(thread, priority);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
thread->priority = priority;
|
|
|
|
|
|
|
|
if (thread->blocker != NULL)
|
|
|
|
{
|
|
|
|
/* Bubble new priority down the chain */
|
|
|
|
struct blocker *bl = thread->blocker; /* Blocker struct */
|
|
|
|
struct thread_entry *bl_t = bl->thread; /* Blocking thread */
|
|
|
|
struct thread_entry * const tstart = thread; /* Initial thread */
|
|
|
|
const int highest = MIN(priority, old_priority); /* Higher of new or old */
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
struct thread_entry *next; /* Next thread to check */
|
|
|
|
int bl_pr; /* Highest blocked thread */
|
|
|
|
int queue_pr; /* New highest blocked thread */
|
2007-10-16 01:25:17 +00:00
|
|
|
#if NUM_CORES > 1
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Owner can change but thread cannot be dislodged - thread
|
|
|
|
* may not be the first in the queue which allows other
|
|
|
|
* threads ahead in the list to be given ownership during the
|
|
|
|
* operation. If thread is next then the waker will have to
|
|
|
|
* wait for us and the owner of the object will remain fixed.
|
|
|
|
* If we successfully grab the owner -- which at some point
|
|
|
|
* is guaranteed -- then the queue remains fixed until we
|
|
|
|
* pass by. */
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
LOCK_THREAD(bl_t);
|
2006-08-21 17:35:35 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Double-check the owner - retry if it changed */
|
2008-12-14 07:09:26 +00:00
|
|
|
if (LIKELY(bl->thread == bl_t))
|
2008-03-25 02:34:12 +00:00
|
|
|
break;
|
2007-03-09 08:03:18 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
UNLOCK_THREAD(bl_t);
|
|
|
|
bl_t = bl->thread;
|
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
#endif
|
2008-03-25 02:34:12 +00:00
|
|
|
bl_pr = bl->priority;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (highest > bl_pr)
|
|
|
|
break; /* Object priority won't change */
|
|
|
|
|
|
|
|
/* This will include the thread being set */
|
|
|
|
queue_pr = find_highest_priority_in_list_l(*thread->bqp);
|
|
|
|
|
|
|
|
if (queue_pr == bl_pr)
|
|
|
|
break; /* Object priority not changing */
|
|
|
|
|
|
|
|
/* Update thread boost for this object */
|
|
|
|
bl->priority = queue_pr;
|
|
|
|
prio_move_entry(&bl_t->pdist, bl_pr, queue_pr);
|
|
|
|
bl_pr = find_first_set_bit(bl_t->pdist.mask);
|
|
|
|
|
|
|
|
if (bl_t->priority == bl_pr)
|
|
|
|
break; /* Blocking thread priority not changing */
|
|
|
|
|
|
|
|
if (bl_t->state == STATE_RUNNING)
|
|
|
|
{
|
|
|
|
/* Thread not blocked - we're done */
|
|
|
|
set_running_thread_priority(bl_t, bl_pr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bl_t->priority = bl_pr;
|
|
|
|
bl = bl_t->blocker; /* Blocking thread has a blocker? */
|
|
|
|
|
|
|
|
if (bl == NULL)
|
|
|
|
break; /* End of chain */
|
|
|
|
|
|
|
|
next = bl->thread;
|
|
|
|
|
2008-12-14 07:09:26 +00:00
|
|
|
if (UNLIKELY(next == tstart))
|
2008-03-25 02:34:12 +00:00
|
|
|
break; /* Full-circle */
|
|
|
|
|
|
|
|
UNLOCK_THREAD(thread);
|
|
|
|
|
|
|
|
thread = bl_t;
|
|
|
|
bl_t = next;
|
|
|
|
} /* for (;;) */
|
|
|
|
|
|
|
|
UNLOCK_THREAD(bl_t);
|
|
|
|
}
|
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
UNLOCK_THREAD(thread);
|
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(oldlevel);
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
return old_base_priority;
|
2002-04-22 12:07:34 +00:00
|
|
|
}
|
2006-11-06 18:07:30 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Returns the current base priority for a thread.
|
2007-10-16 01:25:17 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-12-10 08:57:10 +00:00
|
|
|
int thread_get_priority(unsigned int thread_id)
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
2008-12-10 08:57:10 +00:00
|
|
|
struct thread_entry *thread = thread_id_entry(thread_id);
|
|
|
|
int base_priority = thread->base_priority;
|
2006-11-06 18:07:30 +00:00
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
/* Simply check without locking slot. It may or may not be valid by the
|
|
|
|
* time the function returns anyway. If all tests pass, it is the
|
|
|
|
* correct value for when it was valid. */
|
2011-03-05 17:48:06 +00:00
|
|
|
if (thread->id != thread_id || thread->state == STATE_KILLED)
|
2008-12-10 08:57:10 +00:00
|
|
|
base_priority = -1;
|
|
|
|
|
|
|
|
return base_priority;
|
2006-11-06 18:07:30 +00:00
|
|
|
}
|
2008-03-25 02:34:12 +00:00
|
|
|
#endif /* HAVE_PRIORITY_SCHEDULING */
|
2007-03-09 08:03:18 +00:00
|
|
|
|
2010-04-03 22:02:09 +00:00
|
|
|
#ifdef HAVE_IO_PRIORITY
|
|
|
|
int thread_get_io_priority(unsigned int thread_id)
|
|
|
|
{
|
|
|
|
struct thread_entry *thread = thread_id_entry(thread_id);
|
|
|
|
return thread->io_priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_set_io_priority(unsigned int thread_id,int io_priority)
|
|
|
|
{
|
|
|
|
struct thread_entry *thread = thread_id_entry(thread_id);
|
|
|
|
thread->io_priority = io_priority;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
2008-03-25 02:34:12 +00:00
|
|
|
* Starts a frozen thread - similar semantics to wakeup_thread except that
|
|
|
|
* the thread is on no scheduler or wakeup queue at all. It exists simply by
|
|
|
|
* virtue of the slot having a state of STATE_FROZEN.
|
2007-10-16 01:25:17 +00:00
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-12-10 08:57:10 +00:00
|
|
|
void thread_thaw(unsigned int thread_id)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
2008-12-10 08:57:10 +00:00
|
|
|
struct thread_entry *thread = thread_id_entry(thread_id);
|
2008-03-26 01:50:41 +00:00
|
|
|
int oldlevel = disable_irq_save();
|
2008-12-10 08:57:10 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
LOCK_THREAD(thread);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
/* If thread is the current one, it cannot be frozen, therefore
|
|
|
|
* there is no need to check that. */
|
|
|
|
if (thread->id == thread_id && thread->state == STATE_FROZEN)
|
2008-03-25 02:34:12 +00:00
|
|
|
core_schedule_wakeup(thread);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
UNLOCK_THREAD(thread);
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(oldlevel);
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Switch the processor that the currently executing thread runs on.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
unsigned int switch_core(unsigned int new_core)
|
|
|
|
{
|
|
|
|
const unsigned int core = CURRENT_CORE;
|
|
|
|
struct thread_entry *current = cores[core].running;
|
|
|
|
|
|
|
|
if (core == new_core)
|
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
/* No change - just return same core */
|
2007-10-16 01:25:17 +00:00
|
|
|
return core;
|
|
|
|
}
|
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
int oldlevel = disable_irq_save();
|
2008-03-25 02:34:12 +00:00
|
|
|
LOCK_THREAD(current);
|
|
|
|
|
|
|
|
if (current->name == THREAD_DESTRUCT)
|
|
|
|
{
|
|
|
|
/* Thread being killed - deactivate and let process complete */
|
2008-12-10 08:57:10 +00:00
|
|
|
unsigned int id = current->id;
|
2008-03-25 02:34:12 +00:00
|
|
|
UNLOCK_THREAD(current);
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(oldlevel);
|
2008-12-10 08:57:10 +00:00
|
|
|
thread_wait(id);
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Should never be reached */
|
|
|
|
THREAD_PANICF("switch_core->D:*R", current);
|
|
|
|
}
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
/* Get us off the running list for the current core */
|
2008-03-25 02:34:12 +00:00
|
|
|
RTR_LOCK(core);
|
2007-10-16 01:25:17 +00:00
|
|
|
remove_from_list_l(&cores[core].running, current);
|
2008-03-25 02:34:12 +00:00
|
|
|
rtr_subtract_entry(core, current->priority);
|
|
|
|
RTR_UNLOCK(core);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
/* Stash return value (old core) in a safe place */
|
|
|
|
current->retval = core;
|
|
|
|
|
|
|
|
/* If a timeout hadn't yet been cleaned-up it must be removed now or
|
|
|
|
* the other core will likely attempt a removal from the wrong list! */
|
|
|
|
if (current->tmo.prev != NULL)
|
|
|
|
{
|
|
|
|
remove_from_list_tmo(current);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change the core number for this thread slot */
|
|
|
|
current->core = new_core;
|
|
|
|
|
|
|
|
/* Do not use core_schedule_wakeup here since this will result in
|
|
|
|
* the thread starting to run on the other core before being finished on
|
2008-03-25 02:34:12 +00:00
|
|
|
* this one. Delay the list unlock to keep the other core stuck
|
2007-10-16 01:25:17 +00:00
|
|
|
* until this thread is ready. */
|
2008-03-25 02:34:12 +00:00
|
|
|
RTR_LOCK(new_core);
|
|
|
|
|
|
|
|
rtr_add_entry(new_core, current->priority);
|
|
|
|
add_to_list_l(&cores[new_core].running, current);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
/* Make a callback into device-specific code, unlock the wakeup list so
|
|
|
|
* that execution may resume on the new core, unlock our slot and finally
|
|
|
|
* restore the interrupt level */
|
2008-03-25 02:34:12 +00:00
|
|
|
cores[core].blk_ops.flags = TBOP_SWITCH_CORE;
|
|
|
|
cores[core].blk_ops.cl_p = &cores[new_core].rtr_cl;
|
|
|
|
cores[core].block_task = current;
|
|
|
|
|
|
|
|
UNLOCK_THREAD(current);
|
|
|
|
|
|
|
|
/* Alert other core to activity */
|
|
|
|
core_wake(new_core);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
/* Do the stack switching, cache_maintenence and switch_thread call -
|
|
|
|
requires native code */
|
|
|
|
switch_thread_core(core, current);
|
|
|
|
|
|
|
|
/* Finally return the old core to caller */
|
|
|
|
return current->retval;
|
|
|
|
}
|
|
|
|
#endif /* NUM_CORES > 1 */
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Initialize threading API. This assumes interrupts are not yet enabled. On
|
|
|
|
* multicore setups, no core is allowed to proceed until create_thread calls
|
|
|
|
* are safe to perform.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2002-06-07 14:56:10 +00:00
|
|
|
void init_threads(void)
|
|
|
|
{
|
2007-07-30 16:44:36 +00:00
|
|
|
const unsigned int core = CURRENT_CORE;
|
2007-10-19 11:51:45 +00:00
|
|
|
struct thread_entry *thread;
|
2006-08-21 17:35:35 +00:00
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
if (core == CPU)
|
|
|
|
{
|
|
|
|
/* Initialize core locks and IDs in all slots */
|
|
|
|
int n;
|
|
|
|
for (n = 0; n < MAXTHREADS; n++)
|
|
|
|
{
|
|
|
|
thread = &threads[n];
|
|
|
|
corelock_init(&thread->waiter_cl);
|
|
|
|
corelock_init(&thread->slot_cl);
|
|
|
|
thread->id = THREAD_ID_INIT(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-28 10:20:02 +00:00
|
|
|
/* CPU will initialize first and then sleep */
|
2008-03-25 02:34:12 +00:00
|
|
|
thread = find_empty_thread_slot();
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (thread == NULL)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
|
|
|
/* WTF? There really must be a slot available at this stage.
|
|
|
|
* This can fail if, for example, .bss isn't zero'ed out by the loader
|
|
|
|
* or threads is in the wrong section. */
|
|
|
|
THREAD_PANICF("init_threads->no slot", NULL);
|
2007-10-01 04:13:17 +00:00
|
|
|
}
|
2007-10-19 11:51:45 +00:00
|
|
|
|
|
|
|
/* Initialize initially non-zero members of core */
|
2007-10-16 01:25:17 +00:00
|
|
|
cores[core].next_tmo_check = current_tick; /* Something not in the past */
|
2007-10-19 11:51:45 +00:00
|
|
|
|
|
|
|
/* Initialize initially non-zero members of slot */
|
2008-03-25 02:34:12 +00:00
|
|
|
UNLOCK_THREAD(thread); /* No sync worries yet */
|
2007-10-19 11:51:45 +00:00
|
|
|
thread->name = main_thread_name;
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->state = STATE_RUNNING;
|
|
|
|
IF_COP( thread->core = core; )
|
2006-09-16 16:18:11 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
2008-03-25 02:34:12 +00:00
|
|
|
corelock_init(&cores[core].rtr_cl);
|
|
|
|
thread->base_priority = PRIORITY_USER_INTERFACE;
|
|
|
|
prio_add_entry(&thread->pdist, PRIORITY_USER_INTERFACE);
|
2007-10-19 11:51:45 +00:00
|
|
|
thread->priority = PRIORITY_USER_INTERFACE;
|
2008-03-25 02:34:12 +00:00
|
|
|
rtr_add_entry(core, PRIORITY_USER_INTERFACE);
|
2006-09-16 16:18:11 +00:00
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2007-10-19 11:51:45 +00:00
|
|
|
add_to_list_l(&cores[core].running, thread);
|
2007-10-16 01:25:17 +00:00
|
|
|
|
2006-09-02 07:56:52 +00:00
|
|
|
if (core == CPU)
|
2006-08-21 17:35:35 +00:00
|
|
|
{
|
2007-10-19 11:51:45 +00:00
|
|
|
thread->stack = stackbegin;
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->stack_size = (uintptr_t)stackend - (uintptr_t)stackbegin;
|
2006-08-21 17:35:35 +00:00
|
|
|
#if NUM_CORES > 1 /* This code path will not be run on single core targets */
|
2008-04-06 22:08:36 +00:00
|
|
|
/* Wait for other processors to finish their inits since create_thread
|
|
|
|
* isn't safe to call until the kernel inits are done. The first
|
2008-12-10 21:10:34 +00:00
|
|
|
* threads created in the system must of course be created by CPU.
|
|
|
|
* Another possible approach is to initialize all cores and slots
|
|
|
|
* for each core by CPU, let the remainder proceed in parallel and
|
|
|
|
* signal CPU when all are finished. */
|
2008-04-06 22:08:36 +00:00
|
|
|
core_thread_init(CPU);
|
2007-09-28 10:20:02 +00:00
|
|
|
}
|
2007-10-16 01:25:17 +00:00
|
|
|
else
|
2007-03-26 16:55:17 +00:00
|
|
|
{
|
2008-04-06 22:08:36 +00:00
|
|
|
/* Initial stack is the idle stack */
|
|
|
|
thread->stack = idle_stacks[core];
|
2007-10-19 11:51:45 +00:00
|
|
|
thread->stack_size = IDLE_STACK_SIZE;
|
2008-04-06 22:08:36 +00:00
|
|
|
/* After last processor completes, it should signal all others to
|
|
|
|
* proceed or may signal the next and call thread_exit(). The last one
|
|
|
|
* to finish will signal CPU. */
|
|
|
|
core_thread_init(core);
|
|
|
|
/* Other cores do not have a main thread - go idle inside switch_thread
|
|
|
|
* until a thread can run on the core. */
|
2008-03-25 02:34:12 +00:00
|
|
|
thread_exit();
|
2007-09-28 10:54:27 +00:00
|
|
|
#endif /* NUM_CORES */
|
2007-09-28 10:20:02 +00:00
|
|
|
}
|
2011-02-18 22:46:01 +00:00
|
|
|
#ifdef INIT_MAIN_THREAD
|
|
|
|
init_main_thread(&thread->context);
|
|
|
|
#endif
|
2002-07-15 22:21:18 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/* Shared stack scan helper for thread_stack_usage and idle_stack_usage */
|
|
|
|
#if NUM_CORES == 1
|
|
|
|
static inline int stack_usage(uintptr_t *stackptr, size_t stack_size)
|
|
|
|
#else
|
|
|
|
static int stack_usage(uintptr_t *stackptr, size_t stack_size)
|
|
|
|
#endif
|
2002-07-15 22:21:18 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
unsigned int stack_words = stack_size / sizeof (uintptr_t);
|
|
|
|
unsigned int i;
|
|
|
|
int usage = 0;
|
2005-06-10 23:05:15 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
for (i = 0; i < stack_words; i++)
|
2005-06-10 23:05:15 +00:00
|
|
|
{
|
2006-09-02 07:56:52 +00:00
|
|
|
if (stackptr[i] != DEADBEEF)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
|
|
|
usage = ((stack_words - i) * 100) / stack_words;
|
2005-06-10 23:05:15 +00:00
|
|
|
break;
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
2005-06-10 23:05:15 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
return usage;
|
2006-09-16 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Returns the maximum percentage of stack a thread ever used while running.
|
|
|
|
* NOTE: Some large buffer allocations that don't use enough the buffer to
|
|
|
|
* overwrite stackptr[0] will not be seen.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
int thread_stack_usage(const struct thread_entry *thread)
|
|
|
|
{
|
2010-08-02 20:34:47 +00:00
|
|
|
if (LIKELY(thread->stack_size > 0))
|
|
|
|
return stack_usage(thread->stack, thread->stack_size);
|
|
|
|
return 0;
|
2008-03-25 02:34:12 +00:00
|
|
|
}
|
|
|
|
|
2007-09-29 06:17:33 +00:00
|
|
|
#if NUM_CORES > 1
|
2007-09-28 10:20:02 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Returns the maximum percentage of the core's idle stack ever used during
|
|
|
|
* runtime.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
int idle_stack_usage(unsigned int core)
|
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
return stack_usage(idle_stacks[core], IDLE_STACK_SIZE);
|
2007-09-28 10:20:02 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Fills in the buffer with the specified thread's name. If the name is NULL,
|
|
|
|
* empty, or the thread is in destruct state a formatted ID is written
|
|
|
|
* instead.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void thread_get_name(char *buffer, int size,
|
|
|
|
struct thread_entry *thread)
|
|
|
|
{
|
|
|
|
if (size <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*buffer = '\0';
|
|
|
|
|
|
|
|
if (thread)
|
|
|
|
{
|
|
|
|
/* Display thread name if one or ID if none */
|
|
|
|
const char *name = thread->name;
|
|
|
|
const char *fmt = "%s";
|
2007-10-16 01:25:17 +00:00
|
|
|
if (name == NULL IF_COP(|| name == THREAD_DESTRUCT) || *name == '\0')
|
2007-09-28 10:20:02 +00:00
|
|
|
{
|
2011-02-18 23:17:07 +00:00
|
|
|
name = (const char *)(uintptr_t)thread->id;
|
2010-12-29 20:03:47 +00:00
|
|
|
fmt = "%04lX";
|
2007-09-28 10:20:02 +00:00
|
|
|
}
|
|
|
|
snprintf(buffer, size, fmt, name);
|
|
|
|
}
|
|
|
|
}
|