📄 kapi.cxx
字号:
//==========================================================================//// common/kapi.cxx//// C API Implementation////==========================================================================//####COPYRIGHTBEGIN####// // ------------------------------------------- // The contents of this file are subject to the Red Hat eCos Public License // Version 1.1 (the "License"); you may not use this file except in // compliance with the License. You may obtain a copy of the License at // http://www.redhat.com/ // // Software distributed under the License is distributed on an "AS IS" // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the // License for the specific language governing rights and limitations under // the License. // // The Original Code is eCos - Embedded Configurable Operating System, // released September 30, 1998. // // The Initial Developer of the Original Code is Red Hat. // Portions created by Red Hat are // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. // All Rights Reserved. // ------------------------------------------- // //####COPYRIGHTEND####//==========================================================================//#####DESCRIPTIONBEGIN####//// Author(s): nickg, dsm// Contributors: nickg// Date: 1998-03-02// Purpose: C API Implementation// Description: C++ implementation of the C API// ////####DESCRIPTIONEND####////==========================================================================#include <pkgconf/kernel.h>#ifdef CYGFUN_KERNEL_API_C#include <cyg/kernel/ktypes.h> // base kernel types#include <cyg/infra/cyg_trac.h> // tracing macros#include <cyg/infra/cyg_ass.h> // assertion macros#include <cyg/kernel/instrmnt.h> // instrumentation#include <cyg/kernel/diag.h>#include <cyg/kernel/thread.hxx>#include <cyg/kernel/thread.inl> // thread inlines#include <cyg/kernel/sched.hxx>#include <cyg/kernel/intr.hxx>#include <cyg/kernel/clock.hxx>#include <cyg/kernel/memfixed.hxx>#include <cyg/kernel/memvar.hxx>#include <cyg/kernel/sema.hxx>#include <cyg/kernel/flag.hxx>#include <cyg/kernel/mutex.hxx>#include <cyg/kernel/mbox.hxx>#include <cyg/kernel/sched.inl> // scheduler inlines#include <cyg/kernel/clock.inl> // clock inlines#include <cyg/kernel/kapi.h> // C API// -------------------------------------------------------------------------// Magic new functioninline void *operator new(size_t size, void *ptr){ CYG_CHECK_DATA_PTR( ptr, "Bad pointer" ); return ptr;}// -------------------------------------------------------------------------#ifdef CYGDBG_USE_ASSERTS#define CYG_ASSERT_SIZES(cstruct, cxxstruct) \CYG_MACRO_START \ char *msg = "Size of C struct " #cstruct \ " != size of C++ struct " #cxxstruct ; \ CYG_ASSERT( sizeof(cstruct) == sizeof(cxxstruct) , msg ); \CYG_MACRO_END#else#define CYG_ASSERT_SIZES(cstruct, cxxstruct)#endif/*---------------------------------------------------------------------------*//* Scheduler operations *//* Starts scheduler with created threads. Never returns. */externC void cyg_scheduler_start(void){ Cyg_Scheduler::start();}/* Lock the scheduler. */externC void cyg_scheduler_lock(void){ Cyg_Scheduler::lock(); // get_sched_lock() is unsigned, see below "cyg_ucount32 lock" CYG_ASSERT( (0xff000000 & (Cyg_Scheduler::get_sched_lock())) == 0, "Scheduler overlocked" );}/* Unlock the scheduler. */externC void cyg_scheduler_unlock(void){ cyg_ucount32 slock = Cyg_Scheduler::get_sched_lock(); CYG_ASSERT( 0 < slock, "Scheduler not locked" ); // And program defensively too: if ( 0 < slock ) Cyg_Scheduler::unlock();}/*---------------------------------------------------------------------------*//* Thread operations */externC void cyg_thread_create( cyg_addrword_t sched_info, /* scheduling info (eg pri) */ cyg_thread_entry_t *entry, /* entry point function */ cyg_addrword_t entry_data, /* entry data */ char *name, /* optional thread name */ void *stack_base, /* stack base, NULL = alloc */ cyg_ucount32 stack_size, /* stack size, 0 = default */ cyg_handle_t *handle, /* returned thread handle */ cyg_thread *thread /* put thread here */){ CYG_ASSERT_SIZES( cyg_thread, Cyg_Thread ); Cyg_Thread *t = new((void *)thread) Cyg_Thread ( (CYG_ADDRWORD) sched_info, (cyg_thread_entry *)entry, (CYG_ADDRWORD) entry_data, name, (CYG_ADDRWORD) stack_base, stack_size ); t=t; CYG_CHECK_DATA_PTR( handle, "Bad handle pointer" ); *handle = (cyg_handle_t)thread;}externC void cyg_thread_exit(){ Cyg_Thread::exit();}externC cyg_bool_t cyg_thread_delete( cyg_handle_t thread ){ Cyg_Thread *th = (Cyg_Thread *)thread; if( th->get_state() != Cyg_Thread::EXITED ) th->kill(); // encourage it to terminate if( th->get_state() != Cyg_Thread::EXITED ) return false; // it didn't run yet, leave it up to the app to fix th->~Cyg_Thread(); return true;}externC void cyg_thread_suspend(cyg_handle_t thread){ ((Cyg_Thread *)thread)->suspend();}externC void cyg_thread_resume(cyg_handle_t thread){ Cyg_Thread *th = (Cyg_Thread *)thread; // If we are resuming an exited thread then // reinitialize it. if( th->get_state() == Cyg_Thread::EXITED ) th->reinitialize(); th->resume();}externC void cyg_thread_kill( cyg_handle_t thread){ ((Cyg_Thread *)thread)->kill();}externC void cyg_thread_release( cyg_handle_t thread){ ((Cyg_Thread *)thread)->release(); }externC void cyg_thread_yield(){ Cyg_Thread::yield();}externC cyg_handle_t cyg_thread_self(){ return (cyg_handle_t)Cyg_Thread::self();}// idle thread is not really a plain CygThread; danger.externC cyg_handle_t cyg_thread_idle_thread(){ extern Cyg_Thread idle_thread; return (cyg_handle_t)&idle_thread;}/* Priority manipulation */externC void cyg_thread_set_priority( cyg_handle_t thread, cyg_priority_t priority ){#ifdef CYGIMP_THREAD_PRIORITY ((Cyg_Thread *)thread)->set_priority(priority);#endif}externC cyg_priority_t cyg_thread_get_priority(cyg_handle_t thread){#ifdef CYGIMP_THREAD_PRIORITY return ((Cyg_Thread *)thread)->get_priority();#else return 0;#endif}/* Deadline scheduling control (optional) */externC void cyg_thread_deadline_wait( cyg_tick_count_t start_time, /* abs earliest start time */ cyg_tick_count_t run_time, /* worst case execution time */ cyg_tick_count_t deadline /* absolute deadline */){ CYG_ASSERT(0,"Not implemented");} externC void cyg_thread_delay(cyg_tick_count_t delay){ Cyg_Thread::self()->delay(delay);}/* Stack information */externC cyg_addrword_t cyg_thread_get_stack_base(cyg_handle_t thread){ return ((Cyg_Thread *)thread)->get_stack_base();}externC cyg_uint32 cyg_thread_get_stack_size(cyg_handle_t thread){ return ((Cyg_Thread *)thread)->get_stack_size();}/*---------------------------------------------------------------------------*//* Per-thread data */#ifdef CYGVAR_KERNEL_THREADS_DATAexternC cyg_ucount32 cyg_thread_new_data_index(){ return Cyg_Thread::new_data_index();}externC void cyg_thread_free_data_index(cyg_ucount32 index){ Cyg_Thread::free_data_index(index);}externC CYG_ADDRWORD cyg_thread_get_data(cyg_ucount32 index){ return Cyg_Thread::get_data(index);}externC CYG_ADDRWORD *cyg_thread_get_data_ptr(cyg_ucount32 index){ return Cyg_Thread::get_data_ptr(index);}externC void cyg_thread_set_data(cyg_ucount32 index, CYG_ADDRWORD data){ Cyg_Thread::self()->set_data(index, data);}#endif/*---------------------------------------------------------------------------*//* Exception handling. */#ifdef CYGPKG_KERNEL_EXCEPTIONS externC void cyg_exception_set_handler( cyg_code_t exception_number, cyg_exception_handler_t *new_handler, cyg_addrword_t new_data, cyg_exception_handler_t **old_handler, cyg_addrword_t *old_data){ Cyg_Thread::register_exception( exception_number, (cyg_exception_handler *)new_handler, (CYG_ADDRWORD)new_data, (cyg_exception_handler **)old_handler, (CYG_ADDRWORD *)old_data );}/* Clear exception handler to default */externC void cyg_exception_clear_handler( cyg_code_t exception_number){ Cyg_Thread::deregister_exception( exception_number );}/* Invoke exception handler */externC void cyg_exception_call_handler( cyg_handle_t thread, cyg_code_t exception_number, cyg_addrword_t error_code){ Cyg_Thread *t = (Cyg_Thread *)thread; t->deliver_exception( exception_number, error_code );}#endif /*---------------------------------------------------------------------------*//* Interrupt handling */externC void cyg_interrupt_create( cyg_vector_t vector, /* Vector to attach to */ cyg_priority_t priority, /* Queue priority */ cyg_addrword_t data, /* Data pointer */ cyg_ISR_t *isr, /* Interrupt Service Routine */ cyg_DSR_t *dsr, /* Deferred Service Routine */ cyg_handle_t *handle, /* returned handle */ cyg_interrupt *intr /* put interrupt here */){ CYG_ASSERT_SIZES( cyg_interrupt, Cyg_Interrupt ); Cyg_Interrupt *t = new((void *)intr) Cyg_Interrupt ( (cyg_vector)vector, (cyg_priority)priority, (CYG_ADDRWORD)data, (cyg_ISR *)isr, (cyg_DSR *)dsr ); t=t; CYG_CHECK_DATA_PTR( handle, "Bad handle pointer" ); *handle = (cyg_handle_t)intr;}externC void cyg_interrupt_delete( cyg_handle_t interrupt){ ((Cyg_Interrupt *)interrupt)->~Cyg_Interrupt();}void cyg_interrupt_attach( cyg_handle_t interrupt ){ ((Cyg_Interrupt *)interrupt)->attach();}void cyg_interrupt_detach( cyg_handle_t interrupt ){ ((Cyg_Interrupt *)interrupt)->detach();}/* VSR manipulation */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -