📄 thread.hxx
字号:
#ifndef CYGONCE_KERNEL_THREAD_HXX
#define CYGONCE_KERNEL_THREAD_HXX
//==========================================================================
//
// thread.hxx
//
// Thread class declarations
//
//==========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
//
// eCos 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 or (at your option) any later version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): nickg
// Contributors: nickg
// Date: 1997-09-09
// Purpose: Define Thread class interfaces
// Description: The classes defined here collectively implement the
// internal API used to create, configure and manage threads.
// Usage: #include <cyg/kernel/thread.hxx>
//
//####DESCRIPTIONEND####
//
//==========================================================================
#include <cyg/kernel/ktypes.h>
#include <cyg/infra/cyg_ass.h> // assertion macros
#include <cyg/kernel/sched.hxx>
#include <cyg/kernel/clock.hxx>
#include <cyg/kernel/except.hxx>
#include <cyg/hal/hal_arch.h>
// -------------------------------------------------------------------------
// Miscellaneous types
typedef void cyg_thread_entry(CYG_ADDRWORD data);// Thread entry point function
// -------------------------------------------------------------------------
// Hardware thread interface.
// The implementation of this class is provided by the HAL.
class Cyg_HardwareThread
{
friend class Cyg_Scheduler;
protected:
CYG_ADDRESS stack_base; // pointer to base of stack area
cyg_uint32 stack_size; // size of stack area in bytes
#ifdef CYGFUN_KERNEL_THREADS_STACK_LIMIT
CYG_ADDRESS stack_limit; // movable stack limit
#endif
CYG_ADDRESS stack_ptr; // pointer to saved state on stack
cyg_thread_entry *entry_point; // main entry point (code pointer!)
CYG_ADDRWORD entry_data; // entry point argument
#ifdef CYGDBG_KERNEL_DEBUG_GDB_THREAD_SUPPORT
HAL_SavedRegisters *saved_context; // If non-zero, this points at a more
// interesting context than stack_ptr.
#endif
Cyg_HardwareThread(
cyg_thread_entry *entry_point, // entry point function
CYG_ADDRWORD entry_data, // entry data
cyg_ucount32 stack_size = 0, // stack size, 0 = use default
CYG_ADDRESS stack_base = 0 // stack base, NULL = allocate
);
// Thread entry point. This is where all threads begin execution.
// This routine does a little housekeeping and then call the main
// entry_point specified above.
static void thread_entry(Cyg_Thread *thread);
// Initialize the context of the thread to start execution at thread_entry
void init_context( Cyg_Thread *thread );
// Save current thread's context and load that of the given next thread.
void switch_context(Cyg_HardwareThread *next);
// attach a stack to this thread
void attach_stack(CYG_ADDRESS stack, cyg_uint32 stack_size);
// detach the stack from this thread
CYG_ADDRESS detach_stack();
// Adjust the thread's saved state to call the exception
// handler when next executed.
void prepare_exception (
cyg_exception_handler *exception_handler,
CYG_ADDRWORD exception_data,
cyg_code exception_number,
CYG_ADDRWORD exception_info
);
public:
CYGDBG_DEFINE_CHECK_THIS
// Get and set entry_data.
void set_entry_data( CYG_ADDRWORD data );
CYG_ADDRWORD get_entry_data();
#ifdef CYGDBG_KERNEL_DEBUG_GDB_THREAD_SUPPORT
// Return the current saved state for this thread.
HAL_SavedRegisters *get_saved_context();
// Set the saved context pointer.
void set_saved_context(HAL_SavedRegisters *ctx);
#endif
// get the size/base of this thread's stack
CYG_ADDRESS get_stack_base();
cyg_uint32 get_stack_size();
#ifdef CYGFUN_KERNEL_THREADS_STACK_LIMIT
// Allocate some memory at the lower end of the stack
// by moving the stack limit pointer.
void *increment_stack_limit( cyg_ucount32 size);
CYG_ADDRESS get_stack_limit();
#endif
#ifdef CYGFUN_KERNEL_THREADS_STACK_CHECKING
inline void check_stack(void);
#endif
#ifdef CYGFUN_KERNEL_THREADS_STACK_MEASUREMENT
inline cyg_uint32 measure_stack_usage(void);
#endif
};
// -------------------------------------------------------------------------
// Per-thread timer support class.
// This is only included when required.
#ifdef CYGFUN_KERNEL_THREADS_TIMER
class Cyg_ThreadTimer
: public Cyg_Alarm
{
friend class Cyg_Thread;
// Pointer to current thread
Cyg_Thread *thread;
// Constructor
Cyg_ThreadTimer(
Cyg_Thread *thread
);
// Alarm function
static void alarm( Cyg_Alarm *alarm, CYG_ADDRWORD data);
CYGDBG_DEFINE_CHECK_THIS
};
#endif
// -------------------------------------------------------------------------
// Main Thread class.
// This provides the public API for controlling threads.
class Cyg_Thread
: public Cyg_HardwareThread, // provides hardware abstractions
public Cyg_SchedThread // provides scheduling abstractions
{
friend class Cyg_Scheduler;
friend void deliver_exception( CYG_WORD code, CYG_ADDRWORD data );
// The following definitions are used by all variants of the
// basic thread object.
public:
enum { // Thread state values
RUNNING = 0, // Thread is runnable or running
SLEEPING = 1, // Thread is waiting for something to happen
COUNTSLEEP = 2, // Sleep in counted manner
SUSPENDED = 4, // Suspend count is non-zero
CREATING = 8, // Thread is being created
EXITED = 16, // Thread has exited
// This is the set of bits that must be cleared by a generic
// wake() or release().
SLEEPSET = (SLEEPING | COUNTSLEEP)
};
private:
// Current thread state, a logical OR of the above values.
// Only if this word is zero can the thread execute.
cyg_uint32 state;
// Suspension counter, if > 0, the thread is suspended
cyg_ucount32 suspend_count;
// Wakeup counter, if > 0, sleep will not sleep, just decrement
cyg_ucount32 wakeup_count;
// A word of data used in syncronization object to communicate
// information between sleepers and wakers.
CYG_ADDRWORD wait_info;
// Unique thread id assigned on creation
cyg_uint16 unique_id;
#ifdef CYGPKG_KERNEL_EXCEPTIONS
// If exceptions are supported, define an exception control
// object that will be used to manage and deliver them. If
// exceptions are global there is a single static instance
// of this object, if they are per-thread then there is one
// for each thread.
private:
#ifdef CYGSEM_KERNEL_EXCEPTIONS_GLOBAL
static
#endif
Cyg_Exception_Control exception_control;
public:
static void register_exception(
cyg_code exception_number, // exception number
cyg_exception_handler handler, // handler function
CYG_ADDRWORD data, // data argument
cyg_exception_handler **old_handler, // handler function
CYG_ADDRWORD *old_data // data argument
);
static void deregister_exception(
cyg_code exception_number // exception number
);
void deliver_exception(
cyg_code exception_number, // exception being raised
CYG_ADDRWORD exception_info // exception specific info
);
#endif
public:
CYGDBG_DEFINE_CHECK_THIS
// Constructor, Initialize the thread structure. The thread is
// created in suspended state, and needs to be resumed to execute.
// It is also started at some (configurable) default priority, which
// may need to be changed before calling resume.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -