📄 thread.h
字号:
function -- Task function.
TPRI_SELF -- Same priority as invoking task.
16384 -- Stack size.
NULL -- Auto-allocate stack. This is given as 0 rather
than NULL since some uITRON headers define their
own NULL as 0, leading to compiler warnings.
uITRON status values are 8:8 bit pairs with the actual status in the
low 8 bits. The sub-values can be extracted with the MERCD() and SERCD()
(main- and sub-error-code) macros, however simply using the MERCD()
result isn't safe because it could be the (negative) low 8 bits of a
(positive overall) return value. When creating a task we therefore
consider a status < E_OK as being an error, without trying to pick apart
the overall value.
The handling of initialisers is a bit dodgy since TSK_NONE == TSK_SELF
(== 0) and it isn't even safe to use negative values since in some cases
these can be valid system task handles. In general however uITRON
numbers IDs from 1...n, so using 0 as a non-value is safe.
Handling of task sleep is also somewhat dodgy, time is measured in clock
ticks of an implementation-specific duration, the best that we can do is
to assume that it's close enough to ms.
In theory we don't really need to use exd_tsk() since returning from a
task ends it, but we make it explicit to be neat */
#define THREADFUNC_DEFINE( name, arg ) void name( VP_INT *arg )
#define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
{ \
static const T_CSEM pk_csem = { TA_TFIFO, 1, 64 }; \
T_CTSK pk_ctsk = { TA_HLNG | TA_ACT, ( arg ), ( function ), \
TPRI_SELF, 16384, 0 }; \
\
syncHandle = acre_sem( ( T_CSEM * ) &pk_csem ); \
threadHandle = acre_tsk( &pk_ctsk ); \
if( threadHandle < E_OK ) \
{ \
del_sem( syncHandle ); \
status = CRYPT_ERROR; \
} \
else \
status = CRYPT_OK; \
}
#define THREAD_EXIT( sync ) sig_sem( sync ); \
exd_tsk()
#define THREAD_INITIALISER TSK_NONE
#define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
#define THREAD_SELF() threadSelf()
#define THREAD_SLEEP( ms ) dly_tsk( ms )
#define THREAD_YIELD() dly_tsk( 0 )
#define THREAD_WAIT( sync, status ) \
if( wai_sem( sync ) != E_OK ) \
status = CRYPT_ERROR; \
del_sem( sync )
#define THREAD_CLOSE( sync )
/* The uITRON thread-self function returns the thread ID via a reference
parameter since uITRON IDs can be negative and there'd be no way to
differentiate a thread ID from an error code. Because of this we have
to provide a wrapper that returns it as a return value */
ID threadSelf( void );
/****************************************************************************
* *
* OS/2 *
* *
****************************************************************************/
#elif defined( __OS2__ )
#define INCL_DOSSEMAPHORES
#define INCL_DOSMISC
#define INCL_DOSFILEMGR
#define INCL_DOSMISC
#define INCL_DOSDATETIME
#define INCL_DOSPROCESS
#define INCL_WINWINDOWMGR
#define INCL_WINSYS
#include <os2.h>
ULONG DosGetThreadID( void );
/* Object handles */
#define THREAD_HANDLE TID
#define MUTEX_HANDLE HEV
/* Mutex management functions */
#define MUTEX_DECLARE_STORAGE( name ) \
HMTX name##Mutex; \
BOOLEAN name##MutexInitialised
#define MUTEX_CREATE( name, status ) \
status = CRYPT_OK; \
if( !krnlData->name##MutexInitialised ) \
{ \
if( DosCreateMutexSem( NULL, &krnlData->name##Mutex, 0L, \
FALSE ) == NO_ERROR ) \
krnlData->name##MutexInitialised = TRUE; \
else \
status = CRYPT_ERROR; \
}
#define MUTEX_DESTROY( name ) \
if( krnlData->name##MutexInitialised ) \
{ \
DosRequestMutexSem( krnlData->name##Mutex, \
( ULONG ) SEM_INDEFINITE_WAIT ); \
DosReleaseMutexSem( krnlData->name##Mutex ); \
DosCloseMutexSem( krnlData->name##Mutex ); \
krnlData->name##MutexInitialised = FALSE; \
}
#define MUTEX_LOCK( name ) \
DosRequestMutexSem( krnlData->name##Mutex, \
( ULONG ) SEM_INDEFINITE_WAIT )
#define MUTEX_UNLOCK( name ) \
DosReleaseMutexSem( krnlData->name##Mutex )
/* Thread management functions */
#define THREADFUNC_DEFINE( name, arg ) void _Optlink name( void *arg )
#define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
{ \
threadHandle = syncHandle = \
_beginthread( ( function ), NULL, 8192, ( arg ) ); \
status = ( threadHandle == -1 ) ? CRYPT_ERROR : CRYPT_OK ); \
}
#define THREAD_EXIT( sync ) _endthread()
#define THREAD_INITIALISER 0
#define THREAD_SELF() DosGetThreadID()
#define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
#define THREAD_SLEEP( ms ) DosWait( ms )
#define THREAD_YIELD() DosWait( 0 )
#define THREAD_WAIT( sync, status ) \
if( DosWaitThread( sync, INFINITE ) != NO_ERROR ) \
status = CRYPT_ERROR
#define THREAD_CLOSE( sync )
/****************************************************************************
* *
* PalmOS *
* *
****************************************************************************/
#elif defined( __PALMOS__ )
#include <CmnErrors.h>
#include <SysThread.h>
/* Object handles */
#define THREAD_HANDLE SysHandle
#define MUTEX_HANDLE SysHandle
/* Mutex management functions. These are just initialised in a slightly
odd manner, there isn't any function to explicitly initialise them but
instead they're statically initialised to a fixed value (NULL), when
the lock/unlock functions are passed this value they perform the
initialisation on-demand. This means that if the underlying hardware
supports it they can be implemented using atomic operations directly
on the critical-section value without having to allocate memory for
a struct to contain the critical-section data */
#define MUTEX_DECLARE_STORAGE( name ) \
SysCriticalSectionType name##Mutex; \
BOOLEAN name##MutexInitialised
#define MUTEX_CREATE( name, status ) \
status = CRYPT_OK; \
if( !krnlData->name##MutexInitialised ) \
{ \
krnlData->name##Mutex = sysCriticalSectionInitializer; \
krnlData->name##MutexInitialised = TRUE; \
}
#define MUTEX_DESTROY( name ) \
if( krnlData->name##MutexInitialised ) \
{ \
SysCriticalSectionEnter( &krnlData->name##Mutex ); \
SysCriticalSectionExit( &krnlData->name##Mutex ); \
krnlData->name##Mutex = sysCriticalSectionInitializer; \
krnlData->name##MutexInitialised = FALSE; \
}
#define MUTEX_LOCK( name ) \
SysCriticalSectionEnter( &krnlData->name##Mutex )
#define MUTEX_UNLOCK( name ) \
SysCriticalSectionExit( &krnlData->name##Mutex )
/* Thread management functions. PalmOS threads are created in the suspended
state, so after we create the thread we have to explicitly start it to
get it running. The default stack size (via SysThreadCreateEZ()) is a
pathetic 4K for standard threads or 8K for UI threads, to avoid this we
have to use the full SysThreadCreate() and specify our own stack size */
#define THREADFUNC_DEFINE( name, arg ) void name( void *arg )
#define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
{ \
SysSemaphoreCreateEZ( 0, ( SysHandle * ) &syncHandle ); \
if( SysThreadCreate( sysThreadNoGroup, "", \
sysThreadPriorityNormal, 32768, function, \
arg, &threadHandle ) != errNone ) \
{ \
SysSemaphoreDestroy( syncHandle ); \
status = CRYPT_ERROR; \
} \
else \
{ \
SysThreadStart( threadHandle ); \
status = CRYPT_OK; \
} \
}
#define THREAD_EXIT( sync ) SysSemaphoreSignal( sync ); \
SysThreadExit()
#define THREAD_INITIALISER 0
#define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
#define THREAD_SELF() SysCurrentThread()
#define THREAD_SLEEP( ms ) SysThreadDelay( ( ms ) * 1000000L, P_ABSOLUTE_TIMEOUT )
#define THREAD_YIELD() SysThreadDelay( 0, P_POLL )
#define THREAD_WAIT( sync, status ) \
if( SysSemaphoreWait( sync, P_WAIT_FOREVER, 0 ) != errNone ) \
status = CRYPT_ERROR; \
SysSemaphoreDestroy( sync )
#define THREAD_CLOSE( sync )
/****************************************************************************
* *
* RTEMS *
* *
****************************************************************************/
#elif defined( __RTEMS__ )
#include <rtems.h>
/* Object handles. These are actually multi-component object IDs, but they
act like standard handles */
#define THREAD_HANDLE rtems_id
#define MUTEX_HANDLE rtems_id
/* Mutex management functions. RTEMS semaphores (or at least standard
counting semaphores, which is what we're using here) are re-entrant so
we don't have to jump through the hoops that are necessary with most
other OSes.
We specify the priority ceiling as zero since it's not used for the
semaphore type that we're creating */
#define MUTEX_DECLARE_STORAGE( name ) \
rtems_id name##Mutex; \
BOOLEAN name##MutexInitialised
#define MUTEX_CREATE( name, status ) \
status = CRYPT_OK; \
if( !krnlData->name##MutexInitialised ) \
{ \
if( rtems_semaphore_create( NULL, 1, RTEMS_DEFAULT_ATTRIBUTES, 0, \
&krnlData->name##Mutex ) == RTEMS_SUCCESSFUL ) \
krnlData->name##MutexInitialised = TRUE; \
else \
status = CRYPT_ERROR; \
}
#define MUTEX_DESTROY( name ) \
if( krnlData->name##MutexInitialised ) \
{ \
rtems_semaphore_obtain( krnlData->name##Mutex, RTEMS_WAIT, 0 ); \
rtems_semaphore_release( krnlData->name##Mutex ); \
rtems_semaphore_delete( krnlData->name##Mutex ); \
krnlData->name##MutexInitialised = FALSE; \
}
#define MUTEX_LOCK( name ) \
rtems_semaphore_obtain( krnlData->name##Mutex, RTEMS_WAIT, 0 );
#define MUTEX_UNLOCK( name ) \
rtems_semaphore_release( krnlData->name##Mutex );
/* Thread management functions. RTEMS tasks are created in the suspended
state, so after we create the task we have to resume it to start it
running. The attributes for task creation are:
NULL -- Task name.
RTEMS_CURRENT_PRIORITY -- Task priority. The documentation is unclear
as to whether we can specify this directly as
the priority or have to obtain it via a call,
we use the call to be safe.
RTEMS_STACK_SIZE -- Task stack size. We use the default size for
RTEMS tasks.
RTEMS_ASR | \ -- Task mode: Enable async signal processing
RTEMS_INT_LEVEL(0) | \ (default), all interrupts enabled (default),
RTEMS_PREEMPT | \ preemptive scheduling (default), timeslicing
RTEMS_TIMESLICE for tasks of the same priority.
RTEMS_DEFAULT_ATTRIBUTES-- Task attributes: Local task, no FP regs.
Specifying the default values for the task mode is optional, but we do it
anyway to make the behaviour explicit.
We could make the synchronisation semaphore a binary semaphore, but
there's no indication that this is any more efficient than a counting
semaphore, and it saves having to create a long list of (non-default)
attributes to specify this.
Task sleep times are measured in implementation-specific ticks rather
than ms, but the default is 10ms so we divide by 10. If necessary the
absolute value can be calculated from the microseconds_per_tick field in
the RTEMS configuration table or from CONFIGURE_MICROSECONDS_PER_TICK in
confdefs.h */
#define TASK_MODE ( RTEMS_ASR | RTEMS_INTERRUPT_LEVEL(0) | \
RTEMS_PREEMPT | RTEMS_TIMESLICE )
#define THREADFUNC_DEFINE( name, arg ) rtems_task name( rtems_task_argument arg )
#define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
{ \
rtems_status_code rtemsStatus; \
rtems_task_priority rtemsPriority; \
\
rtems_task_set_priority( RTEMS_SELF, RTEMS_CURRENT_PRIORITY, \
&rtemsPriority ); \
rtems_semaphore_create( NULL, rtemsPriority, \
RTEMS_DEFAULT_ATTRIBUTES, 0, \
&syncHandle ); \
rtemsStatus = rtems_task_create( NULL, 16, RTEMS_STACK_SIZE, \
TASK_MODE, \
RTEMS_DEFAULT_ATTRIBUTES, \
&threadHandle ); \
if( rtemsStatus == RTEMS_SUCCESSFUL ) \
rtemsStatus = rtems_task_start( threadHandle, function, arg ); \
if( rtemsStatus == RTEMS_SUCCESSFUL ) \
status = CRYPT_OK; \
else \
{ \
rtems_semaphore_delete( syncHandle ); \
status = CRYPT_ERROR; \
} \
}
#define THREAD_EXIT( sync ) rtems_semaphore_release( sync ); \
rtems_task_delete( RTEMS_SELF );
#define THREAD_INITIALISER 0
#define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
#define THREAD_SELF() threadSelf()
#define THREAD_SLEEP( ms ) rtems_task_wake_after( ( ms ) / 10 )
#define THREAD_YIELD() rtems_task_wake_after( RTEMS_YIELD_PROCESSOR )
#define THREAD_WAIT( sync, status ) \
if( rtems_semaphore_obtain( sync, RTEMS_WAIT, 0 ) != RTEMS_SUCCESSFUL ) \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -