Go to the documentation of this file.
8 #ifndef _THREAD_DEFINED
10 #define _THREAD_DEFINED
193 #if defined( __AMX__ )
204 #define THREAD_HANDLE CJ_ID
205 #define MUTEX_HANDLE CJ_ID
211 #define MUTEX_DECLARE_STORAGE( name ) \
213 BOOLEAN name##MutexInitialised
214 #define MUTEX_CREATE( name, status ) \
216 if( !krnlData->name##MutexInitialised ) \
218 if( cjrmcreate( &krnlData->name##Mutex, NULL ) == CJ_EROK ) \
219 krnlData->name##MutexInitialised = TRUE; \
221 status = CRYPT_ERROR; \
223 #define MUTEX_DESTROY( name ) \
224 if( krnlData->name##MutexInitialised ) \
226 cjrmrsv( krnlData->name##Mutex, threadPriority(), 0 ); \
227 cjrmrls( krnlData->name##Mutex ); \
228 cjrmdelete( krnlData->name##Mutex ); \
229 krnlData->name##MutexInitialised = FALSE; \
231 #define MUTEX_LOCK( name ) \
232 cjrmrsv( krnlData->name##Mutex, x, 0 )
233 #define MUTEX_UNLOCK( name ) \
234 cjrmrls( krnlData->name##Mutex )
256 #define THREADFUNC_DEFINE( name, arg ) void name( cyg_addrword_t arg )
257 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
259 BYTE *threadData = malloc( 4096 ); \
261 cjsmcreate( &syncHandle, NULL, CJ_SMBINARY ); \
262 if( cjtkcreate( &threadHandle, NULL, function, threadData, \
263 4096, 0, threadPriority(), 0 ) != CJ_EROK ) \
265 free( threadData ); \
266 status = CRYPT_ERROR; \
270 cjtktrigger( threadHandle ); \
274 #define THREAD_EXIT( sync ) cjsmsignal( sync ); \
276 #define THREAD_INITIALISER CJ_IDNULL
277 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
278 #define THREAD_SELF() cjtkid()
279 #define THREAD_SLEEP( ms ) cjtkdelay( cjtmconvert( ms ) )
280 #define THREAD_YIELD() cjtkdelay( 1 )
281 #define THREAD_WAIT( sync, status ) \
282 if( cjsmwait( sync, threadPriority(), 0 ) != CJ_EROK ) \
283 status = CRYPT_ERROR; \
285 #define THREAD_CLOSE( sync )
294 #ifndef AMX_THREAD_WRAPPERS
298 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
300 #define THREAD_EXIT( sync )
301 #define THREAD_CLOSE( sync )
308 int threadPriority(
void );
316 #elif defined( __BEOS__ )
318 #include <kernel/OS.h>
322 #define THREAD_HANDLE thread_id
323 #define MUTEX_HANDLE thread_id
327 #define MUTEX_DECLARE_STORAGE( name ) \
328 sem_id name##Mutex; \
329 BOOLEAN name##MutexInitialised; \
330 thread_id name##MutexOwner; \
331 int name##MutexLockcount
332 #define MUTEX_CREATE( name, status ) \
334 if( !krnlData->name##MutexInitialised ) \
336 if( ( krnlData->name##Mutex = create_sem( 1, NULL ) ) < B_NO_ERROR ) \
337 status = CRYPT_ERROR; \
339 krnlData->name##MutexInitialised = TRUE; \
341 #define MUTEX_DESTROY( name ) \
342 if( krnlData->name##MutexInitialised ) \
344 acquire_sem( krnlData->name##Mutex ); \
345 release_sem( krnlData->name##Mutex ); \
346 delete_sem( krnlData->name##Mutex ); \
347 krnlData->name##MutexInitialised = FALSE; \
349 #define MUTEX_LOCK( name ) \
350 if( acquire_sem_etc( krnlData->name##Mutex, 1, \
351 B_RELATIVE_TIMEOUT, 0 ) == B_WOULD_BLOCK ) \
353 if( !THREAD_SAME( krnlData->name##MutexOwner, THREAD_SELF() ) ) \
354 acquire_sem( krnlData->name##Mutex ); \
356 krnlData->name##MutexLockcount++; \
358 krnlData->name##MutexOwner = THREAD_SELF();
359 #define MUTEX_UNLOCK( name ) \
360 if( krnlData->name##MutexLockcount > 0 ) \
361 krnlData->name##MutexLockcount--; \
364 krnlData->name##MutexOwner = THREAD_INITIALISER; \
365 release_sem( krnlData->name##Mutex ); \
372 #define THREADFUNC_DEFINE( name, arg ) thread_id name( void *arg )
373 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
375 threadHandle = syncHandle = \
376 spawn_thread( ( function ), NULL, B_NORMAL_PRIORITY, \
378 if( threadHandle < B_NO_ERROR ) \
379 status = CRYPT_ERROR; \
381 resume_thread( threadHandle ); \
383 #define THREAD_EXIT( sync ) exit_thread( 0 )
384 #define THREAD_INITIALISER 0
385 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
386 #define THREAD_SELF() find_thread( NULL )
387 #define THREAD_SLEEP( ms ) snooze( ms )
388 #define THREAD_YIELD() snooze( estimate_max_scheduling_latency( -1 ) + 1 )
389 #define THREAD_WAIT( sync, status ) \
393 if( wait_for_thread( sync, &dummy ) != B_NO_ERROR.) \
394 status = CRYPT_ERROR; \
396 #define THREAD_CLOSE( sync )
404 #elif defined( __CHORUS__ )
412 #include <exec/chExec.h>
416 #define THREAD_HANDLE KnThreadLid
417 #define MUTEX_HANDLE KnMutex
423 #define MUTEX_DECLARE_STORAGE( name ) \
424 KnMutex name##Mutex; \
425 BOOLEAN name##MutexInitialised; \
426 KnThreadLid name##MutexOwner; \
427 int name##MutexLockcount
428 #define MUTEX_CREATE( name, status ) \
430 if( !krnlData->name##MutexInitialised ) \
432 if( mutexInit( &krnlData->name##Mutex ) == K_OK ) \
433 krnlData->name##MutexInitialised = TRUE; \
435 status = CRYPT_ERROR; \
437 #define MUTEX_DESTROY( name ) \
438 if( krnlData->name##MutexInitialised ) \
440 mutexGet( &krnlData->name##Mutex ); \
441 mutexRel( &krnlData->name##Mutex ); \
442 krnlData->name##MutexInitialised = FALSE; \
444 #define MUTEX_LOCK( name ) \
445 if( mutexTry( &krnlData->name##Mutex ) == 0 ) \
447 if( !THREAD_SAME( krnlData->name##MutexOwner, THREAD_SELF() ) ) \
448 mutexGet( &krnlData->name##Mutex ); \
450 krnlData->name##MutexLockcount++; \
452 krnlData->name##MutexOwner = THREAD_SELF();
453 #define MUTEX_UNLOCK( name ) \
454 if( krnlData->name##MutexLockcount > 0 ) \
455 krnlData->name##MutexLockcount--; \
458 krnlData->name##MutexOwner = THREAD_INITIALISER; \
459 mutexRel( &krnlData->name##Mutex ); \
479 #define THREADFUNC_DEFINE( name, arg ) void name( void )
480 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
482 BYTE *threadStack = malloc( 4096 ); \
483 KnDefaultStartInfo startInfo = { \
484 K_START_INFO | K_START_INFO_SOFTREG, K_DEFAULT_STACK_SIZE, \
485 function, threadStack, K_USERTHREAD, arg }; \
487 semInit( &syncHandle, 1 ); \
488 if( threadCreate( K_MYACTOR, &threadHandle, K_ACTIVE, NULL, \
489 &startInfo ) != K_OK ) \
491 free( threadStack ); \
492 status = CRYPT_ERROR; \
497 #define THREAD_EXIT( sync ) semV( sync ); \
498 threadDelete( K_MYACTOR, K_MYSELF )
499 #define THREAD_INITIALISER NULL
500 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
501 #define THREAD_SELF() threadSelf()
502 #define THREAD_SLEEP( ms ) { \
505 K_MILLI_TO_TIMEVAL( &timeVal, ms ); \
506 threadDelay( &timeVal ); \
508 #define THREAD_YIELD() threadDelay( K_NOBLOCK )
509 #define THREAD_WAIT( sync, status ) \
510 if( semP( sync, K_NOTIMEOUT ) < 0 ) \
512 #define THREAD_CLOSE( sync )
521 #ifndef CHORUS_THREAD_WRAPPERS
525 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
527 #define THREAD_EXIT( sync )
528 #define THREAD_CLOSE( sync )
537 #elif defined( __ECOS__ )
544 #include <cyg/hal/hal_arch.h>
545 #include <cyg/kernel/kapi.h>
549 #define THREAD_HANDLE cyg_handle_t
550 #define MUTEX_HANDLE cyg_sem_t
555 #define NONSCALAR_THREADS
556 #define DUMMY_INIT_MUTEX { 0 }
560 #define MUTEX_DECLARE_STORAGE( name ) \
561 cyg_mutex_t name##Mutex; \
562 BOOLEAN name##MutexInitialised; \
563 cyg_handle_t name##MutexOwner; \
564 int name##MutexLockcount
565 #define MUTEX_CREATE( name, status ) \
567 if( !krnlData->name##MutexInitialised ) \
569 cyg_mutex_init( &krnlData->name##Mutex ); \
570 krnlData->name##MutexInitialised = TRUE; \
572 #define MUTEX_DESTROY( name ) \
573 if( krnlData->name##MutexInitialised ) \
575 cyg_mutex_lock( &krnlData->name##Mutex ); \
576 cyg_mutex_unlock( &krnlData->name##Mutex ); \
577 cyg_mutex_destroy( &krnlData->name##Mutex ); \
578 krnlData->name##MutexInitialised = FALSE; \
580 #define MUTEX_LOCK( name ) \
581 if( !cyg_mutex_trylock( &krnlData->name##Mutex ) ) \
583 if( !THREAD_SAME( krnlData->name##MutexOwner, THREAD_SELF() ) ) \
584 cyg_mutex_lock( &krnlData->name##Mutex ); \
586 krnlData->name##MutexLockcount++; \
588 krnlData->name##MutexOwner = THREAD_SELF();
589 #define MUTEX_UNLOCK( name ) \
590 if( krnlData->name##MutexLockcount > 0 ) \
591 krnlData->name##MutexLockcount--; \
594 krnlData->name##MutexOwner = THREAD_INITIALISER; \
595 cyg_mutex_unlock( &krnlData->name##Mutex ); \
625 #define THREADFUNC_DEFINE( name, arg ) void name( cyg_addrword_t arg )
626 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
628 BYTE *threadData = malloc( sizeof( cyg_thread ) + \
629 CYGNUM_HAL_STACK_SIZE_TYPICAL ); \
631 cyg_semaphore_init( &syncHandle, 0 ); \
632 cyg_thread_create( cyg_thread_get_priority( cyg_thread_self() ), \
633 function, ( cyg_addrword_t ) arg, NULL, \
634 threadData + sizeof( cyg_thread ), \
635 CYGNUM_HAL_STACK_SIZE_TYPICAL, \
636 &threadHandle, ( cyg_thread * ) threadData ); \
637 if( threadHandle <= 0 ) \
639 free( threadData ); \
640 status = CRYPT_ERROR; \
644 cyg_thread_resume( threadHandle ); \
648 #define THREAD_EXIT( sync ) cyg_semaphore_post( &sync ); \
650 #define THREAD_INITIALISER 0
651 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
652 #define THREAD_SELF() cyg_thread_self()
653 #define THREAD_SLEEP( ms ) cyg_thread_delay( ( ms ) / 10 )
654 #define THREAD_YIELD() cyg_thread_yield()
655 #define THREAD_WAIT( sync, status ) \
656 if( !cyg_semaphore_wait( &sync ) ) \
657 status = CRYPT_ERROR; \
658 cyg_semaphore_destroy( &sync )
659 #define THREAD_CLOSE( sync ) cyg_thread_delete( &sync )
668 #ifndef ECOS_THREAD_WRAPPERS
672 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
674 #define THREAD_EXIT( sync )
675 #define THREAD_CLOSE( sync )
684 #elif defined( __EmbOS__ )
695 #define THREAD_HANDLE OS_TASK *
696 #define MUTEX_HANDLE OS_RSEMA
701 #define NONSCALAR_THREADS
702 #define DUMMY_INIT_MUTEX { 0 }
707 #define MUTEX_DECLARE_STORAGE( name ) \
708 OS_RSEMA name##Mutex; \
709 BOOLEAN name##MutexInitialised
710 #define MUTEX_CREATE( name, status ) \
712 if( !krnlData->name##MutexInitialised ) \
714 OS_CREATERSEMA( &krnlData->name##Mutex ); \
715 krnlData->name##MutexInitialised = TRUE; \
717 #define MUTEX_DESTROY( name ) \
718 if( krnlData->name##MutexInitialised ) \
720 OS_Use( &krnlData->name##Mutex ); \
721 OS_Unuse( &krnlData->name##Mutex ); \
722 OS_DeleteRSema( &krnlData->name##Mutex ); \
723 krnlData->name##MutexInitialised = FALSE; \
725 #define MUTEX_LOCK( name ) \
726 OS_Use( &krnlData->name##Mutex )
727 #define MUTEX_UNLOCK( name ) \
728 OS_Unuse( &krnlData->name##Mutex )
755 #define THREADFUNC_DEFINE( name, arg ) void name( void *arg )
756 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
758 BYTE *threadData = malloc( 4096 ); \
760 if( threadData == NULL ) \
761 status = CRYPT_ERROR_MEMORY; \
764 OS_CREATERSEMA( &syncHandle ); \
765 OS_CREATETASK_EX( threadHandle, NULL, function, \
766 OS_GetPriority( NULL ), threadData, arg ); \
770 #define THREAD_EXIT( sync ) OS_Unuse( ( OS_RSEMA * ) &sync ); \
771 OS_Terminate( OS_GetTaskID() )
772 #define THREAD_INITIALISER { 0 }
773 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
774 #define THREAD_SELF() OS_GetTaskID()
775 #define THREAD_SLEEP( ms ) OS_Delay( ms )
776 #define THREAD_YIELD() OS_Yield()
777 #define THREAD_WAIT( sync, status ) \
779 OS_DeleteRSema( &sync ); \
781 #define THREAD_CLOSE( sync )
790 #ifndef EMBOS_THREAD_WRAPPERS
794 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
796 #define THREAD_EXIT( sync )
797 #define THREAD_CLOSE( sync )
806 #elif defined( __FreeRTOS__ )
808 #include <FreeRTOS.h>
814 #define THREAD_HANDLE xTaskHandle
815 #define MUTEX_HANDLE xSemaphoreHandle
825 #define MUTEX_WAIT_TIME ( 5000 / portTICK_RATE_MS )
827 #define MUTEX_DECLARE_STORAGE( name ) \
828 xSemaphoreHandle name##Mutex; \
829 BOOLEAN name##MutexInitialised
830 #define MUTEX_CREATE( name, status ) \
832 if( !krnlData->name##MutexInitialised ) \
834 vSemaphoreCreateBinary( krnlData->name##Mutex ); \
835 if( krnlData->name##Mutex == NULL ) \
836 status = CRYPT_ERROR; \
839 krnlData->name##MutexInitialised = TRUE; \
843 #define MUTEX_DESTROY( name ) \
844 if( krnlData->name##MutexInitialised ) \
846 xSemaphoreTakeRecursive( krnlData->name##Mutex, \
848 xSemaphoreGiveRecursive( krnlData->name##Mutex ); \
849 krnlData->name##MutexInitialised = FALSE; \
851 #define MUTEX_LOCK( name ) \
852 xSemaphoreTakeRecursive( krnlData->name##Mutex, \
854 #define MUTEX_UNLOCK( name ) \
855 xSemaphoreGiveRecursive( krnlData->name##Mutex )
863 #define STACK_DEPTH 8192
865 #define THREADFUNC_DEFINE( name, arg ) void name( void *arg )
866 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
868 portBASE_TYPE result; \
870 syncHandle = xSemaphoreCreateMutex(); \
871 xSemaphoreTake( syncHandle, MUTEX_WAIT_TIME ); \
872 result = xTaskCreate( function, "clibTask", STACK_DEPTH, \
873 arg, tskIDLE_PRIORITY, &threadHandle ); \
874 if( result != pdPASS ) \
875 status = CRYPT_ERROR; \
879 #define THREAD_EXIT( sync ) xSemaphoreGive( &sync )
880 #define THREAD_INITIALISER 0
881 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
882 #define THREAD_SELF() xTaskGetCurrentTaskHandle()
883 #define THREAD_SLEEP( ms ) vTaskDelay( ( ms ) / portTICK_RATE_MS )
884 #define THREAD_YIELD() taskYIELD()
885 #define THREAD_WAIT( sync, status ) \
886 if( xSemaphoreTake( sync, MUTEX_WAIT_TIME ) != pdTRUE ) \
888 #define THREAD_CLOSE( sync ) vTaskDelete( &sync )
896 #elif defined( __IBM4758__ )
902 #define THREAD_HANDLE long
903 #define MUTEX_HANDLE long
907 #define MUTEX_DECLARE_STORAGE( name ) \
908 long name##Semaphore; \
909 BOOLEAN name##SemaphoreInitialised
910 #define MUTEX_CREATE( name, status ) \
912 if( !krnlData->name##SemaphoreInitialised ) \
914 if( CPCreateSerSem( NULL, 0, 0, \
915 &krnlData->name##Semaphore ) == 0 ) \
916 krnlData->name##SemaphoreInitialised = TRUE; \
918 status = CRYPT_ERROR; \
920 #define MUTEX_DESTROY( name ) \
921 if( krnlData->name##SemaphoreInitialised ) \
923 CPSemClaim( krnlData->name##Semaphore, SVCWAITFOREVER ); \
924 CPSemRelease( krnlData->name##Semaphore ); \
925 CPDelete( krnlData->name##Semaphore, 0 ); \
926 krnlData->name##SemaphoreInitialised = FALSE; \
928 #define MUTEX_LOCK( name ) \
929 CPSemClaim( krnlData->name##Semaphore, SVCWAITFOREVER )
930 #define MUTEX_UNLOCK( name ) \
931 CPSemRelease( krnlData->name##Semaphore )
943 #elif defined( __ITRON__ )
953 #define THREAD_HANDLE ID
954 #define MUTEX_HANDLE ID
961 #define MUTEX_DECLARE_STORAGE( name ) \
963 BOOLEAN name##MutexInitialised; \
964 ID name##MutexOwner; \
965 int name##MutexLockcount
966 #define MUTEX_CREATE( name, status ) \
968 if( !krnlData->name##MutexInitialised ) \
970 static const T_CMTX pk_cmtx = { TA_TFIFO, 0, 0 }; \
972 if( ( krnlData->name##Mutex = \
973 acre_mtx( ( T_CMTX * ) &pk_cmtx ) ) < E_OK ) \
974 status = CRYPT_ERROR; \
976 krnlData->name##MutexInitialised = TRUE; \
978 #define MUTEX_DESTROY( name ) \
979 if( krnlData->name##MutexInitialised ) \
981 loc_mtx( krnlData->name##Mutex ); \
982 unl_mtx( krnlData->name##Mutex ); \
983 del_mtx( krnlData->name##Mutex ); \
984 krnlData->name##MutexInitialised = FALSE; \
986 #define MUTEX_LOCK( name ) \
987 if( ploc_mtx( krnlData->name##Mutex ) == E_ILUSE ) \
989 if( !THREAD_SAME( krnlData->name##MutexOwner, THREAD_SELF() ) ) \
990 loc_mtx( krnlData->name##Mutex ); \
992 krnlData->name##MutexLockcount++; \
994 krnlData->name##MutexOwner = threadSelf();
995 #define MUTEX_UNLOCK( name ) \
996 if( krnlData->name##MutexLockcount > 0 ) \
997 krnlData->name##MutexLockcount--; \
1000 krnlData->name##MutexOwner = THREAD_INITIALISER; \
1001 unl_mtx( krnlData->name##Mutex ); \
1037 #define THREADFUNC_DEFINE( name, arg ) void name( VP_INT *arg )
1038 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
1040 static const T_CSEM pk_csem = { TA_TFIFO, 1, 64 }; \
1041 T_CTSK pk_ctsk = { TA_HLNG | TA_ACT, ( arg ), ( function ), \
1042 TPRI_SELF, 16384, 0 }; \
1044 syncHandle = acre_sem( ( T_CSEM * ) &pk_csem ); \
1045 threadHandle = acre_tsk( &pk_ctsk ); \
1046 if( threadHandle < E_OK ) \
1048 del_sem( syncHandle ); \
1049 status = CRYPT_ERROR; \
1052 status = CRYPT_OK; \
1054 #define THREAD_EXIT( sync ) sig_sem( sync ); \
1056 #define THREAD_INITIALISER TSK_NONE
1057 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
1058 #define THREAD_SELF() threadSelf()
1059 #define THREAD_SLEEP( ms ) dly_tsk( ms )
1060 #define THREAD_YIELD() dly_tsk( 0 )
1061 #define THREAD_WAIT( sync, status ) \
1062 if( wai_sem( sync ) != E_OK ) \
1063 status = CRYPT_ERROR; \
1065 #define THREAD_CLOSE( sync )
1072 ID threadSelf(
void );
1080 #elif defined( __MQX__ )
1086 #define THREAD_HANDLE _task_id
1087 #define MUTEX_HANDLE MUTEX_STRUCT
1094 #define MUTEX_DECLARE_STORAGE( name ) \
1095 MUTEX_HANDLE name##Mutex; \
1096 BOOLEAN name##MutexInitialised
1097 #define MUTEX_CREATE( name, status ) \
1098 status = CRYPT_OK; \
1099 if( !krnlData->name##MutexInitialised ) \
1101 if( _mutex_init( &krnlData->name##Mutex, NULL ) == MQX_OK ) \
1102 krnlData->name##MutexInitialised = TRUE; \
1104 status = CRYPT_ERROR; \
1106 #define MUTEX_DESTROY( name ) \
1107 if( krnlData->name##MutexInitialised ) \
1109 _mutex_lock( &krnlData->name##Mutex ); \
1110 _mutex_unlock( &krnlData->name##Mutex ); \
1111 _mutex_destroy( &krnlData->name##Mutex ); \
1112 krnlData->name##MutexInitialised = FALSE; \
1114 #define MUTEX_LOCK( name ) \
1115 _mutex_lock( &krnlData->name##Mutex )
1116 #define MUTEX_UNLOCK( name ) \
1117 _mutex_unlock( &krnlData->name##Mutex )
1144 #define THREADFUNC_DEFINE( name, arg ) void name( uint32 arg )
1145 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
1147 _lwsem_create( &syncHandle, 1 ); \
1148 threadHandle = _task_create( 0, 0x100, ( uint32 ) arg ); \
1149 if( threadHandle == MQX_NULL_TASK_ID ) \
1150 status = CRYPT_ERROR; \
1152 status = CRYPT_OK; \
1154 #define THREAD_EXIT( sync ) _lwsem_post( &sync ); \
1155 _task_destroy( MQX_NULL_TASK_ID )
1156 #define THREAD_INITIALISER 0
1157 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
1158 #define THREAD_SELF() _task_get_id()
1159 #define THREAD_SLEEP( ms ) _time_delay( ms )
1160 #define THREAD_YIELD() _sched_yield()
1161 #define THREAD_WAIT( sync, status ) \
1162 if( _lwsem_wait( &sync ) != MQX_OK ) \
1163 status = CRYPT_ERROR; \
1164 _lwsem_destroy( &sync )
1165 #define THREAD_CLOSE( sync )
1173 #elif defined( __OS2__ )
1175 #define INCL_DOSSEMAPHORES
1176 #define INCL_DOSMISC
1177 #define INCL_DOSFILEMGR
1178 #define INCL_DOSMISC
1179 #define INCL_DOSDATETIME
1180 #define INCL_DOSPROCESS
1181 #define INCL_WINWINDOWMGR
1188 #define THREAD_HANDLE TID
1189 #define MUTEX_HANDLE HEV
1193 #define MUTEX_DECLARE_STORAGE( name ) \
1195 BOOLEAN name##MutexInitialised
1196 #define MUTEX_CREATE( name, status ) \
1197 status = CRYPT_OK; \
1198 if( !krnlData->name##MutexInitialised ) \
1200 if( DosCreateMutexSem( NULL, &krnlData->name##Mutex, 0L, \
1201 FALSE ) == NO_ERROR ) \
1202 krnlData->name##MutexInitialised = TRUE; \
1204 status = CRYPT_ERROR; \
1206 #define MUTEX_DESTROY( name ) \
1207 if( krnlData->name##MutexInitialised ) \
1209 DosRequestMutexSem( krnlData->name##Mutex, \
1210 ( ULONG ) SEM_INDEFINITE_WAIT ); \
1211 DosReleaseMutexSem( krnlData->name##Mutex ); \
1212 DosCloseMutexSem( krnlData->name##Mutex ); \
1213 krnlData->name##MutexInitialised = FALSE; \
1215 #define MUTEX_LOCK( name ) \
1216 DosRequestMutexSem( krnlData->name##Mutex, \
1217 ( ULONG ) SEM_INDEFINITE_WAIT )
1218 #define MUTEX_UNLOCK( name ) \
1219 DosReleaseMutexSem( krnlData->name##Mutex )
1223 #define THREADFUNC_DEFINE( name, arg ) void _Optlink name( void *arg )
1224 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
1226 threadHandle = syncHandle = \
1227 _beginthread( ( function ), NULL, 8192, ( arg ) ); \
1228 status = ( threadHandle == -1 ) ? CRYPT_ERROR : CRYPT_OK ); \
1230 #define THREAD_EXIT( sync ) _endthread()
1231 #define THREAD_INITIALISER 0
1232 #define THREAD_SELF() DosGetThreadID()
1233 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
1234 #define THREAD_SLEEP( ms ) DosWait( ms )
1235 #define THREAD_YIELD() DosWait( 0 )
1236 #define THREAD_WAIT( sync, status ) \
1237 if( DosWaitThread( sync, INFINITE ) != NO_ERROR ) \
1238 status = CRYPT_ERROR
1239 #define THREAD_CLOSE( sync )
1247 #elif defined( __PALMOS__ )
1249 #include <CmnErrors.h>
1250 #include <SysThread.h>
1254 #define THREAD_HANDLE SysHandle
1255 #define MUTEX_HANDLE SysHandle
1266 #define MUTEX_DECLARE_STORAGE( name ) \
1267 SysCriticalSectionType name##Mutex; \
1268 BOOLEAN name##MutexInitialised
1269 #define MUTEX_CREATE( name, status ) \
1270 status = CRYPT_OK; \
1271 if( !krnlData->name##MutexInitialised ) \
1273 krnlData->name##Mutex = sysCriticalSectionInitializer; \
1274 krnlData->name##MutexInitialised = TRUE; \
1276 #define MUTEX_DESTROY( name ) \
1277 if( krnlData->name##MutexInitialised ) \
1279 SysCriticalSectionEnter( &krnlData->name##Mutex ); \
1280 SysCriticalSectionExit( &krnlData->name##Mutex ); \
1281 krnlData->name##Mutex = sysCriticalSectionInitializer; \
1282 krnlData->name##MutexInitialised = FALSE; \
1284 #define MUTEX_LOCK( name ) \
1285 SysCriticalSectionEnter( &krnlData->name##Mutex )
1286 #define MUTEX_UNLOCK( name ) \
1287 SysCriticalSectionExit( &krnlData->name##Mutex )
1295 #define THREADFUNC_DEFINE( name, arg ) void name( void *arg )
1296 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
1298 SysSemaphoreCreateEZ( 0, ( SysHandle * ) &syncHandle ); \
1299 if( SysThreadCreate( sysThreadNoGroup, "", \
1300 sysThreadPriorityNormal, 32768, function, \
1301 arg, &threadHandle ) != errNone ) \
1303 SysSemaphoreDestroy( syncHandle ); \
1304 status = CRYPT_ERROR; \
1308 SysThreadStart( threadHandle ); \
1309 status = CRYPT_OK; \
1312 #define THREAD_EXIT( sync ) SysSemaphoreSignal( sync ); \
1314 #define THREAD_INITIALISER 0
1315 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
1316 #define THREAD_SELF() SysCurrentThread()
1317 #define THREAD_SLEEP( ms ) SysThreadDelay( ( ms ) * 1000000L, P_ABSOLUTE_TIMEOUT )
1318 #define THREAD_YIELD() SysThreadDelay( 0, P_POLL )
1319 #define THREAD_WAIT( sync, status ) \
1320 if( SysSemaphoreWait( sync, P_WAIT_FOREVER, 0 ) != errNone ) \
1321 status = CRYPT_ERROR; \
1322 SysSemaphoreDestroy( sync )
1323 #define THREAD_CLOSE( sync )
1331 #elif defined( __RTEMS__ )
1338 #define THREAD_HANDLE rtems_id
1339 #define MUTEX_HANDLE rtems_id
1357 #define MUTEX_DECLARE_STORAGE( name ) \
1358 rtems_id name##Mutex; \
1359 BOOLEAN name##MutexInitialised
1360 #define MUTEX_CREATE( name, status ) \
1361 status = CRYPT_OK; \
1362 if( !krnlData->name##MutexInitialised ) \
1364 if( rtems_semaphore_create( NO_NAME, 1, RTEMS_DEFAULT_ATTRIBUTES, 0, \
1365 &krnlData->name##Mutex ) == RTEMS_SUCCESSFUL ) \
1366 krnlData->name##MutexInitialised = TRUE; \
1368 status = CRYPT_ERROR; \
1370 #define MUTEX_DESTROY( name ) \
1371 if( krnlData->name##MutexInitialised ) \
1373 rtems_semaphore_obtain( krnlData->name##Mutex, RTEMS_WAIT, 0 ); \
1374 rtems_semaphore_release( krnlData->name##Mutex ); \
1375 rtems_semaphore_delete( krnlData->name##Mutex ); \
1376 krnlData->name##MutexInitialised = FALSE; \
1378 #define MUTEX_LOCK( name ) \
1379 rtems_semaphore_obtain( krnlData->name##Mutex, RTEMS_WAIT, 0 );
1380 #define MUTEX_UNLOCK( name ) \
1381 rtems_semaphore_release( krnlData->name##Mutex );
1414 #define TASK_MODE ( RTEMS_ASR | RTEMS_INTERRUPT_LEVEL(0) | \
1415 RTEMS_PREEMPT | RTEMS_TIMESLICE )
1417 #define THREADFUNC_DEFINE( name, arg ) rtems_task name( rtems_task_argument arg )
1418 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
1420 rtems_status_code rtemsStatus; \
1421 rtems_task_priority rtemsPriority; \
1423 rtems_task_set_priority( RTEMS_SELF, RTEMS_CURRENT_PRIORITY, \
1425 rtems_semaphore_create( NO_NAME, 1, RTEMS_DEFAULT_ATTRIBUTES, 0, \
1427 rtemsStatus = rtems_task_create( NO_NAME, rtemsPriority, \
1428 RTEMS_STACK_SIZE, TASK_MODE, \
1429 RTEMS_DEFAULT_ATTRIBUTES, \
1431 if( rtemsStatus == RTEMS_SUCCESSFUL ) \
1432 rtemsStatus = rtems_task_start( threadHandle, function, \
1433 ( rtems_task_argument ) arg ); \
1434 if( rtemsStatus == RTEMS_SUCCESSFUL ) \
1435 status = CRYPT_OK; \
1438 rtems_semaphore_delete( syncHandle ); \
1439 status = CRYPT_ERROR; \
1442 #define THREAD_EXIT( sync ) rtems_semaphore_release( sync ); \
1443 rtems_task_delete( RTEMS_SELF );
1444 #define THREAD_INITIALISER 0
1445 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
1446 #define THREAD_SELF() threadSelf()
1447 #define THREAD_SLEEP( ms ) rtems_task_wake_after( ( ms ) / 10 )
1448 #define THREAD_YIELD() rtems_task_wake_after( RTEMS_YIELD_PROCESSOR )
1449 #define THREAD_WAIT( sync, status ) \
1450 if( rtems_semaphore_obtain( sync, RTEMS_WAIT, 0 ) != RTEMS_SUCCESSFUL ) \
1451 status = CRYPT_ERROR; \
1453 rtems_semaphore_release( sync ); \
1454 rtems_semaphore_delete( sync )
1455 #define THREAD_CLOSE( sync )
1461 rtems_id threadSelf(
void );
1469 #elif defined( __ThreadX__ )
1477 #pragma message( "ThreadX header path faked" )
1478 #include <threadx_tx_api.h>
1482 #define THREAD_HANDLE TX_THREAD *
1483 #define MUTEX_HANDLE TX_MUTEX
1488 #define NONSCALAR_THREADS
1489 #define DUMMY_INIT_MUTEX { 0 }
1499 #define MUTEX_DECLARE_STORAGE( name ) \
1500 TX_MUTEX name##Mutex; \
1501 BOOLEAN name##MutexInitialised
1502 #define MUTEX_CREATE( name, status ) \
1503 status = CRYPT_OK; \
1504 if( !krnlData->name##MutexInitialised ) \
1506 if( tx_mutex_create( &krnlData->name##Mutex, "name", \
1507 TX_NO_INHERIT ) == TX_SUCCESS ) \
1508 krnlData->name##MutexInitialised = TRUE; \
1510 status = CRYPT_ERROR; \
1512 #define MUTEX_DESTROY( name ) \
1513 if( krnlData->name##MutexInitialised ) \
1515 tx_mutex_get( &krnlData->name##Mutex, TX_WAIT_FOREVER ); \
1516 tx_mutex_put( &krnlData->name##Mutex ); \
1517 tx_mutex_delete( &krnlData->name##Mutex ); \
1518 krnlData->name##MutexInitialised = FALSE; \
1520 #define MUTEX_LOCK( name ) \
1521 tx_mutex_get( &krnlData->name##Mutex, TX_WAIT_FOREVER )
1522 #define MUTEX_UNLOCK( name ) \
1523 tx_mutex_put( &krnlData->name##Mutex )
1542 #define THREADFUNC_DEFINE( name, arg ) VOID name( void * arg )
1543 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
1545 BYTE *threadData = malloc( 16384 ); \
1547 tx_mutex_create( &syncHandle, "name", TX_NO_INHERIT ); \
1548 if( tx_thread_create( &threadHandle, "name", function, \
1549 ( ULONG ) arg, threadData, 16384, \
1550 15, 50, 15, TX_AUTO_START ) != TX_SUCCESS ) \
1552 free( threadData ); \
1553 status = CRYPT_ERROR; \
1556 status = CRYPT_OK; \
1558 #define THREAD_EXIT( sync ) tx_mutex_put( &sync ); \
1559 tx_thread_terminate( tx_thread_identify() )
1560 #define THREAD_INITIALISER 0
1561 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
1562 #define THREAD_SELF() tx_thread_identify()
1563 #define THREAD_SLEEP( ms ) tx_thread_sleep( ( ms ) / 10 )
1564 #define THREAD_YIELD() tx_thread_relinquish()
1565 #define THREAD_WAIT( sync, status ) \
1566 if( !tx_mutex_get( &sync, TX_WAIT_FOREVER ) ) \
1567 status = CRYPT_ERROR; \
1568 tx_mutex_delete( &sync )
1569 #define THREAD_CLOSE( sync ) tx_thread_delete( &sync )
1578 #ifndef THREADX_THREAD_WRAPPERS
1579 #undef THREAD_CREATE
1582 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
1583 status = CRYPT_ERROR
1584 #define THREAD_EXIT( sync )
1585 #define THREAD_CLOSE( sync )
1594 #elif defined( __TKernel__ )
1596 #include <tkernel.h>
1600 #define THREAD_HANDLE ID
1601 #define MUTEX_HANDLE ID
1608 #define MUTEX_DECLARE_STORAGE( name ) \
1610 BOOLEAN name##MutexInitialised; \
1611 ID name##MutexOwner; \
1612 int name##MutexLockcount
1613 #define MUTEX_CREATE( name, status ) \
1614 status = CRYPT_OK; \
1615 if( !krnlData->name##MutexInitialised ) \
1617 static const T_CMTX pk_cmtx = { NULL, TA_TFIFO, 0 }; \
1619 if( ( krnlData->name##Mutex = \
1620 tk_cre_mtx( &pk_cmtx ) ) < E_OK ) \
1621 status = CRYPT_ERROR; \
1623 krnlData->name##MutexInitialised = TRUE; \
1625 #define MUTEX_DESTROY( name ) \
1626 if( krnlData->name##MutexInitialised ) \
1628 tk_loc_mtx( krnlData->name##Mutex, TMO_FEVR ); \
1629 tk_unl_mtx( krnlData->name##Mutex ); \
1630 tk_del_mtx( krnlData->name##Mutex ); \
1631 krnlData->name##MutexInitialised = FALSE; \
1633 #define MUTEX_LOCK( name ) \
1634 if( tk_loc_mtx( krnlData->name##Mutex, TMO_POL ) == E_TMOUT ) \
1636 if( !THREAD_SAME( krnlData->name##MutexOwner, THREAD_SELF() ) ) \
1637 tk_loc_mtx( krnlData->name##Mutex, TMO_FEVR ); \
1639 krnlData->name##MutexLockcount++; \
1641 krnlData->name##MutexOwner = THREAD_SELF();
1642 #define MUTEX_UNLOCK( name ) \
1643 if( krnlData->name##MutexLockcount > 0 ) \
1644 krnlData->name##MutexLockcount--; \
1647 krnlData->name##MutexOwner = THREAD_INITIALISER; \
1648 tk_unl_mtx( krnlData->name##Mutex ); \
1682 #define THREADFUNC_DEFINE( name, arg ) void name( INT stacd, VP arg )
1683 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
1685 static const T_CSEM pk_csem = { NULL, TA_TFIFO, 1, 64 }; \
1686 T_CTSK pk_ctsk = { ( arg ), TA_HLNG, ( function ), \
1687 TPRI_INI, 16384, 0, 0, 0, 0, 0 }; \
1689 syncHandle = tk_cre_sem( &pk_csem ); \
1690 threadHandle = tk_cre_tsk( &pk_ctsk ); \
1691 if( threadHandle < E_OK ) \
1693 tk_del_sem( syncHandle ); \
1694 status = CRYPT_ERROR; \
1698 tk_sta_tsk( threadHandle, 0 ); \
1699 status = CRYPT_OK; \
1702 #define THREAD_EXIT( sync ) tk_sig_sem( sync, 1 ); \
1704 #define THREAD_INITIALISER 0
1705 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
1706 #define THREAD_SELF() tk_get_tid()
1707 #define THREAD_SLEEP( ms ) tk_slp_tsk( ms )
1708 #define THREAD_YIELD() tk_slp_tsk( 0 )
1709 #define THREAD_WAIT( sync, status ) \
1710 if( tk_wai_sem( sync, 1, TMO_FEVR ) != E_OK ) \
1711 status = CRYPT_ERROR; \
1713 #define THREAD_CLOSE( sync )
1721 #elif defined( __UCOS__ )
1754 #define UCOS_TASKID 20
1761 #include <ucos_ucos_ii.h>
1772 #define THREAD_HANDLE INT8U
1773 #define MUTEX_HANDLE OS_EVENT *
1780 #define MUTEX_DECLARE_STORAGE( name ) \
1781 OS_EVENT *name##Mutex; \
1782 BOOLEAN name##MutexInitialised; \
1783 INT8U name##MutexOwner; \
1784 int name##MutexLockcount
1785 #define MUTEX_CREATE( name, status ) \
1786 status = CRYPT_OK; \
1787 if( !krnlData->name##MutexInitialised ) \
1791 krnlData->name##Mutex = OSMutexCreate( UCOS_PIP, &err ); \
1792 if( err == OS_ERR_NONE ) \
1793 krnlData->name##MutexInitialised = TRUE; \
1795 status = CRYPT_ERROR; \
1797 #define MUTEX_DESTROY( name ) \
1798 if( krnlData->name##MutexInitialised ) \
1802 OSMutexPend( krnlData->name##Mutex, 0, &err ); \
1803 OSMutexPost( krnlData->name##Mutex ); \
1804 OSMutexDel( krnlData->name##Mutex, OS_DEL_ALWAYS, &err ); \
1805 krnlData->name##MutexInitialised = FALSE; \
1807 #define MUTEX_LOCK( name ) \
1811 if( OSMutexAccept( krnlData->name##Mutex, &err ) == 0 ) \
1813 if( !THREAD_SAME( krnlData->name##MutexOwner, THREAD_SELF() ) ) \
1814 OSMutexPend( krnlData->name##Mutex, 0, &err ); \
1816 krnlData->name##MutexLockcount++; \
1818 krnlData->name##MutexOwner = THREAD_SELF(); \
1820 #define MUTEX_UNLOCK( name ) \
1821 if( krnlData->name##MutexLockcount > 0 ) \
1822 krnlData->name##MutexLockcount--; \
1825 krnlData->name##MutexOwner = THREAD_INITIALISER; \
1826 OSMutexPost( krnlData->name##Mutex ); \
1837 #define THREADFUNC_DEFINE( name, arg ) void name( void *arg )
1838 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
1840 OS_STK *threadData = malloc( 4096 ); \
1842 syncHandle = OSSemCreate( 0 ); \
1843 if( OSTaskCreate( function, arg, ( BYTE * ) threadData + 4095, \
1844 UCOS_TASKID ) != OS_ERR_NONE ) \
1846 free( threadData ); \
1847 status = CRYPT_ERROR; \
1850 status = CRYPT_OK; \
1852 #define THREAD_EXIT( sync ) OSSemPost( sync ); \
1853 OSTaskDel( OS_PRIO_SELF )
1854 #define THREAD_INITIALISER 0
1855 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
1856 #define THREAD_SELF() threadSelf()
1857 #if OS_TICKS_PER_SEC >= 1000
1858 #define THREAD_SLEEP( ms ) OSTimeDly( ( OS_TICKS_PER_SEC / 1000 ) * ms )
1860 #define THREAD_SLEEP( ms ) OSTimeDly( max( ( ms * OS_TICKS_PER_SEC ) / 1000, 1 ) )
1862 #define THREAD_YIELD() THREAD_SLEEP( 1 )
1863 #define THREAD_WAIT( sync, status ) \
1867 OSSemPend( sync, 0, &err ); \
1868 if( err != OS_ERR_NONE ) \
1869 status = CRYPT_ERROR; \
1870 OSSemDel( sync, OS_DEL_ALWAYS, &err ); \
1872 #define THREAD_CLOSE( sync )
1878 INT8U threadSelf(
void );
1885 #ifndef UCOS_USE_TASKS
1886 #undef THREAD_CREATE
1889 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
1890 status = CRYPT_ERROR
1891 #define THREAD_EXIT( sync )
1892 #define THREAD_CLOSE( sync )
1901 #elif ( defined( __UNIX__ ) || defined( __XMK__ ) ) && defined( USE_THREADS )
1915 #if defined( __osf__ ) || defined( __alpha__ )
1942 #include <pthread.h>
1943 #include <sys/time.h>
1945 #include <sys/process.h>
1946 #include <sys/timer.h>
1951 #define THREAD_HANDLE pthread_t
1952 #define MUTEX_HANDLE pthread_t
1974 #define MUTEX_DECLARE_STORAGE( name ) \
1975 pthread_mutex_t name##Mutex; \
1976 BOOLEAN name##MutexInitialised; \
1977 pthread_t name##MutexOwner; \
1978 int name##MutexLockcount
1979 #define MUTEX_CREATE( name, status ) \
1980 status = CRYPT_OK; \
1981 if( !krnlData->name##MutexInitialised ) \
1983 if( pthread_mutex_init( &krnlData->name##Mutex, \
1985 krnlData->name##MutexInitialised = TRUE; \
1987 status = CRYPT_ERROR; \
1989 #define MUTEX_DESTROY( name ) \
1990 if( krnlData->name##MutexInitialised ) \
1992 pthread_mutex_destroy( &krnlData->name##Mutex ); \
1993 krnlData->name##MutexInitialised = FALSE; \
1995 #define MUTEX_LOCK( name ) \
1996 if( pthread_mutex_trylock( &krnlData->name##Mutex ) ) \
1998 if( !THREAD_SAME( krnlData->name##MutexOwner, THREAD_SELF() ) ) \
1999 pthread_mutex_lock( &krnlData->name##Mutex ); \
2001 krnlData->name##MutexLockcount++; \
2003 krnlData->name##MutexOwner = THREAD_SELF();
2004 #define MUTEX_UNLOCK( name ) \
2005 if( krnlData->name##MutexLockcount > 0 ) \
2006 krnlData->name##MutexLockcount--; \
2009 krnlData->name##MutexOwner = THREAD_INITIALISER; \
2010 pthread_mutex_unlock( &krnlData->name##Mutex ); \
2031 #define MUTEX_CREATE( name, status ) \
2032 if( !krnlData->name##MutexInitialised ) \
2034 pthread_mutexattr_t mutexAttr;\
2036 pthread_mutexattr_init( &mutexAttr );\
2037 pthread_mutexattr_settype( &mutexAttr, \
2038 PTHREAD_MUTEX_RECURSIVE ); \
2039 if( pthread_mutex_init( &krnlData->name##Mutex, \
2040 &mutexAttr ) == 0 ) \
2042 krnlData->name##MutexInitialised = TRUE; \
2043 status = CRYPT_OK; \
2046 status = CRYPT_ERROR; \
2047 pthread_mutexattr_destroy ( &mutexAttr );\
2049 #define MUTEX_LOCK( name ) pthread_mutex_lock( &krnlData->name##Mutex )
2050 #define MUTEX_UNLOCK( name ) pthread_mutex_unlock( &krnlData->name##Mutex )
2090 #define THREADFUNC_DEFINE( name, arg ) void *name( void *arg )
2091 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
2093 status = pthread_create( &threadHandle, NULL, function, arg ) ? \
2094 CRYPT_ERROR : CRYPT_OK; \
2095 syncHandle = threadHandle; \
2097 #define THREAD_EXIT( sync ) pthread_exit( ( void * ) 0 )
2098 #define THREAD_INITIALISER 0
2099 #define THREAD_SELF() pthread_self()
2100 #define THREAD_SAME( thread1, thread2 ) pthread_equal( ( thread1 ), ( thread2 ) )
2101 #if defined( __osf__ ) || defined( __alpha__ ) || defined( __APPLE__ )
2102 #define THREAD_YIELD() pthread_yield_np()
2103 #elif defined( __MVS__ )
2104 #define THREAD_YIELD() pthread_yield( NULL )
2105 #elif defined( sun )
2110 #define THREAD_YIELD() { if( sched_yield() ) thr_yield(); }
2112 #define THREAD_YIELD() sched_yield()
2114 #elif defined( _AIX ) || defined( __CYGWIN__ ) || \
2115 ( defined( __hpux ) && ( OSVERSION >= 11 ) ) || \
2116 defined( __NetBSD__ ) || defined( __QNX__ ) || defined( __UCLIBC__ )
2117 #define THREAD_YIELD() sched_yield()
2118 #elif defined( __XMK__ )
2122 #define THREAD_YIELD() yield()
2124 #if defined( __linux__ ) && !defined( __USE_GNU )
2125 void pthread_yield(
void );
2127 #define THREAD_YIELD() pthread_yield()
2129 #define THREAD_SLEEP( ms ) { \
2130 struct timeval tv = { 0 }; \
2132 tv.tv_usec = ( ms ) * 1000; \
2133 select( 1, NULL, NULL, NULL, &tv ); \
2135 #define THREAD_WAIT( sync, status ) \
2136 if( pthread_join( sync, NULL ) < 0 ) \
2137 status = CRYPT_ERROR
2138 #define THREAD_CLOSE( sync )
2149 #if ( defined( __osf__ ) || defined( __alpha__ ) ) && \
2150 !defined( pthread_self )
2151 #ifdef _PTHREAD_USE_MANGLED_NAMES_
2152 #define pthread_self __pthread_self
2154 extern pthread_t pthread_self(
void );
2163 #define MUTEX_CREATE( name, status ) \
2164 status = CRYPT_OK; \
2165 if( !krnlData->name##MutexInitialised ) \
2167 if( pthread_mutex_init( &krnlData->name##Mutex, \
2168 pthread_mutexattr_default ) == 0 ) \
2169 krnlData->name##MutexInitialised = TRUE; \
2171 status = CRYPT_ERROR; \
2174 #undef THREAD_CREATE
2175 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
2177 status = pthread_create( &threadHandle, pthread_attr_default, \
2179 CRYPT_ERROR : CRYPT_OK ); \
2180 syncHandle = ( long ) threadHandle; \
2187 #if defined( __MVS__ ) || defined( _MPRAS )
2188 #define NONSCALAR_THREADS
2189 #undef THREAD_INITIALISER
2190 #define THREAD_INITIALISER { 0 }
2201 #define THREAD_SLEEP( ms ) sleep( ms )
2209 #ifdef __SCO_VERSION__
2210 #undef THREAD_CREATE
2211 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
2213 pthread_attr_t attr; \
2215 pthread_attr_init( &attr ); \
2216 pthread_attr_setstacksize( &attr, 32768 ); \
2217 status = pthread_create( &threadHandle, &attr, function, arg ); \
2218 pthread_attr_destroy( &attr ); \
2220 status = CRYPT_ERROR; \
2223 status = CRYPT_OK; \
2224 syncHandle = ( long ) threadHandle; \
2235 #elif defined( __VDK__ )
2257 #define THREAD_HANDLE VDK_ThreadID
2258 #define MUTEX_HANDLE VDK_SemaphoreID
2269 #define MUTEX_DECLARE_STORAGE( name ) \
2270 VDK_SemaphoreID name##Mutex; \
2271 BOOLEAN name##MutexInitialised
2272 #define MUTEX_CREATE( name, status ) \
2273 status = CRYPT_OK; \
2274 if( !krnlData->name##MutexInitialised ) \
2276 if( ( krnlData->name##Mutex = VDK_CreateSemaphore( 1, 1, 1, 0 ) ) != UINT_MAX ) \
2277 krnlData->name##MutexInitialised = TRUE; \
2279 status = CRYPT_ERROR; \
2281 #define MUTEX_DESTROY( name ) \
2282 if( krnlData->name##MutexInitialised ) \
2284 VDK_PendSemaphore( krnlData->name##Mutex, 1000000 ); \
2285 VDK_PostSemaphore( krnlData->name##Mutex ); \
2286 VDK_DestroySemaphore( krnlData->name##Mutex ); \
2287 krnlData->name##MutexInitialised = FALSE; \
2289 #define MUTEX_LOCK( name ) \
2290 VDK_PendSemaphore( krnlData->name##Mutex, 1000000 ); \
2291 #define MUTEX_UNLOCK( name ) \
2292 VDK_PostSemaphore( krnlData->name##Mutex );
2319 #define THREADFUNC_DEFINE( name, arg ) thread_id name( void *arg )
2320 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
2322 VDK_ThreadCreationBlock threadInfo; \
2324 memset( &threadInfo, 0, sizeof( VDK_ThreadCreationBlock ) ); \
2325 threadInfo.template_id = kDynamicThread; \
2326 threadInfo.user_data_ptr = arg; \
2327 threadInfo.pTemplate = ; \
2329 syncHandle = VDK_CreateSemaphore( 1, 1, 1, 0 ); \
2330 threadHandle = VDK_CreateThreadEx( &threadInfo ); \
2331 if( threadHandle == UINT_MAX ) \
2333 VDK_DestroySemaphore( syncHandle ); \
2334 status = CRYPT_ERROR; \
2337 status = CRYPT_OK; \
2339 #define THREAD_EXIT( sync ) VDK_PostSemaphore( sync ); \
2341 #define THREAD_INITIALISER 0
2342 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
2343 #define THREAD_SELF() VDK_GetThreadID()
2344 #define THREAD_SLEEP( ms ) VDK_Sleep( VDK_GetTickPeriod() * ms )
2345 #define THREAD_YIELD() VDK_Yield()
2346 #define THREAD_WAIT( sync, status ) \
2347 if( !VDK_PendSemaphore( sync, 1000000 ) ) \
2348 status = CRYPT_ERROR; \
2350 VDK_PostSemaphore( sync ); \
2351 VDK_DestroySemaphore( sync )
2352 #define THREAD_CLOSE( sync )
2358 #ifndef VDK_USE_THREADS
2359 #undef THREAD_CREATE
2362 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
2363 status = CRYPT_ERROR
2364 #define THREAD_EXIT( sync )
2365 #define THREAD_CLOSE( sync )
2374 #elif defined( __VXWORKS__ )
2376 #include <vxWorks.h>
2378 #include <taskLib.h>
2382 #define THREAD_HANDLE int
2383 #define MUTEX_HANDLE SEM_ID
2389 #define MUTEX_DECLARE_STORAGE( name ) \
2390 SEM_ID name##Mutex; \
2391 BOOLEAN name##MutexInitialised
2392 #define MUTEX_CREATE( name, status ) \
2393 status = CRYPT_OK; \
2394 if( !krnlData->name##MutexInitialised ) \
2396 if( ( krnlData->name##Mutex = semMCreate( SEM_Q_FIFO ) ) != NULL ) \
2397 krnlData->name##MutexInitialised = TRUE; \
2399 status = CRYPT_ERROR; \
2401 #define MUTEX_DESTROY( name ) \
2402 if( krnlData->name##MutexInitialised ) \
2404 semTake( krnlData->name##Mutex, WAIT_FOREVER ); \
2405 semGive( krnlData->name##Mutex ); \
2406 semDelete( krnlData->name##Mutex ); \
2407 krnlData->name##MutexInitialised = FALSE; \
2409 #define MUTEX_LOCK( name ) \
2410 semTake( krnlData->name##Mutex, WAIT_FOREVER ); \
2411 #define MUTEX_UNLOCK( name ) \
2412 semGive( krnlData->name##Mutex );
2420 #define TASK_ATTRIBUTES VX_FP_TASK
2422 #define TASK_ATTRIBUTES 0
2435 #define THREADFUNC_DEFINE( name, arg ) thread_id name( void *arg )
2436 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
2438 syncHandle = semBCreate( SEM_Q_FIFO, SEM_EMPTY ); \
2439 threadHandle = taskSpawn( NULL, T_PRIORITY, TASK_ATTRIBUTES, 16384, \
2440 function, ( int ) arg, 0, 0, 0, 0, \
2442 if( threadHandle == ERROR ) \
2444 semDelete( syncHandle ); \
2445 status = CRYPT_ERROR; \
2448 status = CRYPT_OK; \
2450 #define THREAD_EXIT( sync ) semGive( sync ); \
2452 #define THREAD_INITIALISER 0
2453 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
2454 #define THREAD_SELF() taskIdSelf()
2455 #define THREAD_SLEEP( ms ) taskDelay( ms )
2456 #define THREAD_YIELD() taskDelay( NO_WAIT )
2457 #define THREAD_WAIT( sync, status ) \
2458 if( semTake( sync, WAIT_FOREVER ) != OK ) \
2459 status = CRYPT_ERROR; \
2463 #define THREAD_CLOSE( sync )
2471 #elif ( defined( __WIN32__ ) && !defined( NT_DRIVER ) ) || \
2472 defined( __WINCE__ )
2475 #include <process.h>
2480 #define THREAD_HANDLE DWORD
2481 #define MUTEX_HANDLE HANDLE
2553 #define MUTEX_DECLARE_STORAGE( name ) \
2554 CRITICAL_SECTION name##CriticalSection; \
2555 BOOLEAN name##CriticalSectionInitialised
2556 #define MUTEX_CREATE( name, status ) \
2557 status = CRYPT_OK; \
2558 if( !krnlData->name##CriticalSectionInitialised ) \
2560 InitializeCriticalSection( &krnlData->name##CriticalSection ); \
2561 krnlData->name##CriticalSectionInitialised = TRUE; \
2563 #define MUTEX_DESTROY( name ) \
2564 if( krnlData->name##CriticalSectionInitialised ) \
2566 EnterCriticalSection( &krnlData->name##CriticalSection ); \
2567 LeaveCriticalSection( &krnlData->name##CriticalSection ); \
2568 DeleteCriticalSection( &krnlData->name##CriticalSection ); \
2569 krnlData->name##CriticalSectionInitialised = FALSE; \
2571 #define MUTEX_LOCK( name ) \
2572 EnterCriticalSection( &krnlData->name##CriticalSection )
2573 #define MUTEX_UNLOCK( name ) \
2574 LeaveCriticalSection( &krnlData->name##CriticalSection )
2588 #define MUTEX_QUEUE_SIZE 16
2594 #undef MUTEX_DECLARE_STORAGE
2595 #define MUTEX_DECLARE_STORAGE( name ) \
2596 CRITICAL_SECTION name##CriticalSection; \
2597 BOOLEAN name##CriticalSectionInitialised; \
2598 MUTEX_QUEUE_INFO name##EntryQueue[ MUTEX_QUEUE_SIZE ]; \
2599 MUTEX_QUEUE_INFO name##ExitQueue[ MUTEX_QUEUE_SIZE ]; \
2600 int name##LockCount; \
2601 int name##SequenceNo
2603 #define MUTEX_CREATE( name, status ) \
2604 status = CRYPT_OK; \
2605 if( !krnlData->name##CriticalSectionInitialised ) \
2607 InitializeCriticalSection( &krnlData->name##CriticalSection ); \
2608 memset( &krnlData->name##EntryQueue, 0, \
2609 sizeof( MUTEX_QUEUE_INFO ) * MUTEX_QUEUE_SIZE ); \
2610 memset( &krnlData->name##ExitQueue, 0, \
2611 sizeof( MUTEX_QUEUE_INFO ) * MUTEX_QUEUE_SIZE ); \
2612 krnlData->name##LockCount = krnlData->name##SequenceNo = 0; \
2613 krnlData->name##CriticalSectionInitialised = TRUE; \
2617 #define MUTEX_LOCK( name ) \
2621 EnterCriticalSection( &krnlData->name##CriticalSection ); \
2622 for( i = 1; i < MUTEX_QUEUE_SIZE; i++ ) \
2623 krnlData->name##EntryQueue[ i - 1 ] = \
2624 krnlData->name##EntryQueue[ i ]; \
2625 krnlData->name##EntryQueue[ MUTEX_QUEUE_SIZE - 1 ].owner = THREAD_SELF(); \
2626 krnlData->name##EntryQueue[ MUTEX_QUEUE_SIZE - 1 ].lockCount = krnlData->name##LockCount++; \
2627 krnlData->name##EntryQueue[ MUTEX_QUEUE_SIZE - 1 ].sequenceNo = krnlData->name##SequenceNo++; \
2629 #define MUTEX_UNLOCK( name ) \
2633 for( i = 1; i < MUTEX_QUEUE_SIZE; i++ ) \
2634 krnlData->name##ExitQueue[ i - 1 ] = \
2635 krnlData->name##ExitQueue[ i ]; \
2636 krnlData->name##ExitQueue[ MUTEX_QUEUE_SIZE - 1 ].owner = THREAD_SELF(); \
2637 krnlData->name##ExitQueue[ MUTEX_QUEUE_SIZE - 1 ].lockCount = krnlData->name##LockCount--; \
2638 krnlData->name##ExitQueue[ MUTEX_QUEUE_SIZE - 1 ].sequenceNo = krnlData->name##SequenceNo++; \
2639 LeaveCriticalSection( &krnlData->name##CriticalSection ); \
2661 #if defined( __WIN32__ )
2662 #define THREADFUNC_DEFINE( name, arg ) \
2663 unsigned __stdcall name( void *arg )
2664 #if defined( _MSC_VER ) && VC_GE_2005( _MSC_VER )
2665 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
2667 uintptr_t hThread; \
2669 hThread = _beginthreadex( NULL, 0, ( function ), ( arg ), 0, \
2671 syncHandle = ( MUTEX_HANDLE ) hThread; \
2672 status = ( hThread == 0 ) ? CRYPT_ERROR : CRYPT_OK; \
2675 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
2677 unsigned long hThread; \
2679 hThread = _beginthreadex( NULL, 0, ( function ), ( arg ), 0, \
2681 syncHandle = ( MUTEX_HANDLE ) hThread; \
2682 status = ( hThread == 0 ) ? CRYPT_ERROR : CRYPT_OK; \
2685 #define THREAD_EXIT( sync ) _endthreadex( 0 ); return( 0 )
2686 #elif defined( __WINCE__ )
2687 #define THREADFUNC_DEFINE( name, arg ) \
2688 DWORD WINAPI name( void *arg )
2689 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
2693 hThread = CreateThread( NULL, 0, ( function ), ( arg ), 0, \
2695 syncHandle = hThread; \
2696 status = ( hThread == NULL ) ? CRYPT_ERROR : CRYPT_OK; \
2698 #define THREAD_EXIT( sync ) ExitThread( 0 ); return( 0 )
2700 #define THREAD_INITIALISER 0
2701 #define THREAD_SELF() GetCurrentThreadId()
2702 #define THREAD_SAME( thread1, thread2 ) ( ( thread1 ) == ( thread2 ) )
2703 #define THREAD_SLEEP( ms ) Sleep( ms )
2704 #if defined( __WIN32__ )
2705 #define THREAD_YIELD() threadYield()
2707 #define THREAD_YIELD() Sleep( 0 )
2709 #define THREAD_WAIT( sync, status ) \
2710 if( WaitForSingleObject( sync, INFINITE ) != WAIT_OBJECT_0 ) \
2711 status = CRYPT_ERROR
2712 #define THREAD_CLOSE( sync ) CloseHandle( sync )
2717 void threadYield(
void );
2719 #elif defined( __WIN32__ ) && defined( NT_DRIVER )
2723 #define THREAD_HANDLE HANDLE
2724 #define MUTEX_HANDLE HANDLE
2728 #define MUTEX_DECLARE_STORAGE( name ) \
2729 KMUTEX name##CriticalSection; \
2730 BOOLEAN name##CriticalSectionInitialised
2731 #define MUTEX_CREATE( name, status ) \
2732 status = CRYPT_OK; \
2733 if( !krnlData->name##CriticalSectionInitialised ) \
2735 KeInitializeMutex( &krnlData->name##CriticalSection, 1 ); \
2736 krnlData->name##CriticalSectionInitialised = TRUE; \
2738 #define MUTEX_DESTROY( name )
2739 #define MUTEX_LOCK( name ) \
2740 KeWaitForMutexObject( &krnlData->name##CriticalSection, Executive, \
2741 KernelMode, FALSE, NULL )
2742 #define MUTEX_UNLOCK( name ) \
2743 KeReleaseMutex( &krnlData->name##CriticalSection, FALSE )
2756 #define THREAD_HANDLE int
2757 #define MUTEX_HANDLE int
2759 #define MUTEX_DECLARE_STORAGE( name )
2760 #define MUTEX_CREATE( name, status ) status = CRYPT_OK
2761 #define MUTEX_DESTROY( name )
2762 #define MUTEX_LOCK( name )
2763 #define MUTEX_UNLOCK( name )
2765 #define THREAD_CREATE( function, arg, threadHandle, syncHandle, status ) \
2766 status = CRYPT_ERROR
2767 #define THREAD_EXIT( sync )
2768 #define THREAD_INITIALISER 0
2769 #define THREAD_SAME( thread1, thread2 ) TRUE
2770 #define THREAD_SELF() 0
2771 #define THREAD_SLEEP( ms )
2772 #define THREAD_YIELD()
2773 #define THREAD_WAIT( sync, status )
2774 #define THREAD_CLOSE( sync )
2788 #ifndef DUMMY_INIT_MUTEX
2789 #define DUMMY_INIT_MUTEX ( MUTEX_HANDLE ) DUMMY_INIT