www.pudn.com > MessageQueue.zip > THREAD.H
/* This module declares a thread scheduling routine and some thread management routines. Copyright (C) 2004 Liu Ge 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. This program 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 this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef Thread_H_ #define Thread_H_ #include "basedefs.h" #define THREAD_SHARED_SLOT_SPACE_LIMIT 16 #define THREAD_PRIVATE_SLOT_SPACE_LIMIT 16 typedef void (* PFN_ThreadProc)(void); // This function creates a thread associated with the thread procedure // specified by the pfnThreadProc parameter. The isSuspended parameter indicates // if the created thread automatically runs after the creation. // Return value: // if success, return the thread identifier of the created thread // if fails, return -1 int Thread_Create(PFN_ThreadProc pfn, BOOL isSuspended ); // This function destroys the thread specified by the iThreadIdentifier parameter // Return value: // if success, return TRUE. // if fails, return FALSE // Note!!!: // It need a trick to destroy the current thread. BOOL Thread_Destroy(int iThreadIdentifier); // This function resumes a suspended thread, specified by the iThreadIdentifier parameter // Return value: // if success, return TRUE. // if fails, return FALSE BOOL Thread_Resume(int iThreadIdentifier); // This function suspends a running thread, specified by the iThreadIdentifier parameter // Return value: // if success, return TRUE. // if fails, return FALSE // Note!!!: it need a trick to suspend the current thread. BOOL Thread_Suspend(int iThreadIdentifier); // This function retrieves the thread identifier of the current thread // Return value: return the thread identifier of the current thread int Thread_GetCurrentThreadId(void); // This function allocates a property slot in the thread-shared slot space. // Every thread has a private memory space, with which a thread can be // associated with many private variables. These variables can be called // thread-local variables (TLV). A TLV must be an integer-sized variable, // usually a pointer. Because of its size, a TLV can be called a thread slot. // The thread private memory space consists of two parts. The first part is // called thread-shared slot space, and the second part is called // thread-private slot space. // The programmer can call the Thread_AllocSharedSlot function to allocate // a thread slot in the thread-shared slot space. Once the variable is // allocated, the private memory spaces of all threads will contain such // a variable at the same address (the address of a slot is an index but // not a real memory address). To free a shared slot, the programmer can call // the Thread_FreeSharedSlot function. int Thread_AllocSharedSlot(void); BOOL Thread_FreeSharedSlot(int iSlot); BOOL Thread_GetSlot(int iThreadIdentifier, int iSlotIndex, int * pSlot); BOOL Thread_SetSlot(int iThreadIdentifier, int iSlotIndex, int nSlotValue); // This function allocates a property slot in the thread-private slot space. // The programmer can call the Thread_AllocPrivateSlot function to allocate // a thread slot in the thread-private slot space. To free a private slot, // the programmer can call the Thread_FreePrivateSlot function. int Thread_AllocPrivateSlot(int iThreadIdentifier); BOOL Thread_FreePrivateSlot(int iThreadIdentifier, int iSlot); // This function starts the thread system and transfer control to the root thread, // specified by the pfnRootThreadProc parameter. // The root thread is the first thread in the system. // The root thread must not terminate, otherwise the system will crash void Thread_Init(PFN_ThreadProc pfnRootThreadProc, int nMaxNumOfThreads); // This function switches control among running threads. void __TaskSwitch(void); void __SystemLock(void); void __SystemUnlock(void); #endif