⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cryptos.h

📁 提供了很多种加密算法和CA认证及相关服务如CMP、OCSP等的开发
💻 H
📖 第 1 页 / 共 2 页
字号:

#include <thread.h>

#define MUTEX					mutex_t
#define MUTEX_INIT( mutex )		mutex_init( mutex )
#define MUTEX_DESTROY			mutex_clear
#define MUTEX_LOCK				mutex_lock
#define MUTEX_TRYLOCK			mutex_try_lock
#define MUTEX_UNLOCK			mutex_unlock

#define THREAD					cthread_t
#define THREAD_SELF				cthread_self
#define THREAD_CREATE( function, arg ) \
								cthread_create( &dummy, NULL, function, arg ); 
#define THREAD_STATUS( status )	( ( status ) ? CRYPT_ERROR : CRYPT_OK )
#define THREAD_EXIT()			cthread_exit( ( void * ) 0 )

#else							/* Posix threads */

#include <pthread.h>

#define MUTEX					pthread_mutex_t
#define MUTEX_INIT( mutex )		pthread_mutex_init( mutex, NULL )
#define MUTEX_DESTROY			pthread_mutex_destroy
#define MUTEX_LOCK				pthread_mutex_lock
#define MUTEX_TRYLOCK			pthread_mutex_trylock
#define MUTEX_UNLOCK			pthread_mutex_unlock

#define THREAD					pthread_t
#define THREAD_SELF				pthread_self
#define THREAD_CREATE( function, arg ) \
								pthread_create( &dummy, NULL, function, arg ); 
#define THREAD_STATUS( status )	( ( status ) ? CRYPT_ERROR : CRYPT_OK )
#define THREAD_EXIT()			pthread_exit( ( void * ) 0 )

/* OSF1 includes some ghastly kludgery to handle binary compatibility from
   1003.4a to 1003.1c threading functions and inline asm functions with all
   sorts of name mangling and translation of function names and types.
   Unfortunately a straight vanilla compile leaves pthread_self() un-
   prototyped, which means it's implicitly prototyped as returned an int.
   This generates hundreds of warnings of int <-> pointer casting problems,
   so if pthread_self() isn't redefined into one of a dozen different
   mangled versions we prototype it ourselves here */

#if defined( __osf__ ) && !defined( pthread_self )
  extern pthread_t pthread_self( void );
#endif /* OSF1 pthread_self function prototyping bug */

/* UnixWare/SCO uses a default thread stack size so tiny that almost nothing 
   can run with it, so we have to use a custom thread-creation function which 
   sets the stack size to something reasonable */

#ifdef __SCO_VERSION__
  #undef THREAD_CREATE
  #define THREAD_CREATE( function, arg )	createThread( function, arg )

  int createThread( void *( *function )( void * ), void *arg );
#endif /* UnixWare/SCO */

#endif /* Unix variant-specific threading primitives */

/* Declare the variables required to handle the object locking for internal
   data structures */

#define DECLARE_OBJECT_LOCKING_VARS \
		MUTEX mutex; \
		BOOLEAN mutexInitialised; \
		THREAD mutexOwner;

/* Initialise and delete the locking variables */

#define initResourceLock( objectPtr ) \
		MUTEX_INIT( &( objectPtr )->mutex ); \
		( objectPtr )->mutexInitialised = TRUE; \
		( objectPtr )->mutexOwner = ( THREAD ) CRYPT_ERROR;
#define deleteResourceLock( objectPtr ) \
		if( ( objectPtr )->mutexInitialised ) \
			{ \
			MUTEX_DESTROY( &( objectPtr )->mutex ); \
			( objectPtr )->mutexInitialised = FALSE; \
			}

/* Lock and unlock a object using the locking variables */

#define lockResource( objectPtr ) \
		if( MUTEX_TRYLOCK( &( objectPtr )->mutex ) && \
			THREAD_SELF() != ( objectPtr )->mutexOwner ) \
			MUTEX_LOCK( &( objectPtr )->mutex ); \
		( objectPtr )->mutexOwner = THREAD_SELF()
#define unlockResource( objectPtr ) \
		if( ( objectPtr )->mutexOwner != ( THREAD ) CRYPT_ERROR ) \
			{ \
			( objectPtr )->mutexOwner = ( THREAD ) CRYPT_ERROR; \
			MUTEX_UNLOCK( &( objectPtr )->mutex ); \
			}

/* Some variables are protected by global object locks (for example the
   internal data structures are accessed through a global object map which
   maps handles to object information).  Before we can read or write these
   variables in a multithreaded environment we need to lock them so they
   won't be accessed or modified by other threads.  The following macros
   provide this locking capability.

   In some very unusual cases (see the initialistion handling code for
   details) it's possible that an attempt might be made to lock a mutex
   before it's been initialised (this can only happen due to a programming
   error by the caller, unfortunately it can't always be caught reliably).
   Setting the mutex to { 0 } is, in most threading implementations,
   equivalent to initialising it normally, so we do this to catch most
   occurences of the problem */

#define DECLARE_LOCKING_VARS( name ) \
		MUTEX name##Mutex = { 0 }; \
		BOOLEAN name##MutexInitialised = FALSE; \
		THREAD name##MutexOwner = ( THREAD ) CRYPT_ERROR;

#define DEFINE_LOCKING_VARS( name ) \
		extern MUTEX name##Mutex; \
		extern THREAD name##MutexOwner;

#define initGlobalResourceLock( name ) \
		if( !name##MutexInitialised ) \
			{ \
			MUTEX_INIT( &name##Mutex ); \
			name##MutexInitialised = TRUE; \
			name##MutexOwner = ( THREAD ) CRYPT_ERROR; \
			}
#define deleteGlobalResourceLock( name ) \
		if( name##MutexInitialised ) \
			{ \
			MUTEX_DESTROY( &name##Mutex ); \
			name##MutexInitialised = FALSE; \
			}
#define lockGlobalResource( name ) \
		if( MUTEX_TRYLOCK( &name##Mutex ) && \
			THREAD_SELF() != name##MutexOwner ) \
			MUTEX_LOCK( &name##Mutex ); \
		name##MutexOwner = THREAD_SELF();
#define unlockGlobalResource( name ) \
		name##MutexOwner = ( THREAD ) CRYPT_ERROR; \
		MUTEX_UNLOCK( &name##Mutex )

/* Some objects are owned by one thread and can't be accessed by any other
   threads.  The following macros provide facilities to declare the thread
   ID variables and check that the current thread is allowed to access this
   object */

#define OWNERSHIP_VAR_TYPE		THREAD
#define DECLARE_OWNERSHIP_VARS	THREAD objectOwner;

#define getCurrentIdentity()	THREAD_SELF()

/* The types used for handles to threads and system synchronisation objects */

#define THREAD_HANDLE			THREAD
#define SEMAPHORE_HANDLE		THREAD

/* Define a thread function */

#define THREADFUNC_DEFINE( name, arg ) \
									void *name( void *arg )

/* The lock for the internal data structures is used throughout the library
   so we make it globally visible */

extern MUTEX objectMutex;

#elif defined( __BEOS__ )

#include <kernel/OS.h>

#define THREAD					thread_id
#define THREAD_CREATE( function, arg ) \
								spawn_thread( function, NULL, B_NORMAL_PRIORITY, arg ); 
#define THREAD_STATUS( status )	( ( status < B_NO_ERROR ) ? CRYPT_ERROR : CRYPT_OK )
#define THREAD_EXIT()			exit_thread( 0 )

/* Declare the variables required to handle the object locking for internal
   data structures */

#define DECLARE_OBJECT_LOCKING_VARS \
		sem_id mutex; \
		BOOLEAN mutexInitialised; \
		THREAD mutexOwner;

/* Initialise and delete the locking variables */

#define initResourceLock( objectPtr ) \
		( objectPtr )->mutex = create_sem( 1, NULL ); \
		( objectPtr )->mutexInitialised = TRUE; \
		( objectPtr )->mutexOwner = ( THREAD ) CRYPT_ERROR;
#define deleteResourceLock( objectPtr ) \
		if( ( objectPtr )->mutexInitialised ) \
			{ \
			delete_sem( ( objectPtr )->mutex ); \
			( objectPtr )->mutexInitialised = FALSE; \
			}

/* Lock and unlock a object using the locking variables */

#define lockResource( objectPtr ) \
		if( acquire_sem( ( objectPtr )->mutex ); \
		( objectPtr )->mutexOwner = find_thread( NULL )
#define unlockResource( objectPtr ) \
		if( ( objectPtr )->mutexOwner != ( THREAD ) CRYPT_ERROR ) \
			{ \
			( objectPtr )->mutexOwner = ( THREAD ) CRYPT_ERROR; \
			release_sem( ( objectPtr )->mutex ); \
			}

/* Some variables are protected by global object locks (for example the
   internal data structures are accessed through a global object map which
   maps handles to object information).  Before we can read or write these
   variables in a multithreaded environment we need to lock them so they
   won't be accessed or modified by other threads.  The following macros
   provide this locking capability.

   In some very unusual cases (see the initialistion handling code for
   details) it's possible that an attempt might be made to lock a mutex
   before it's been initialised (this can only happen due to a programming
   error by the caller, unfortunately it can't always be caught reliably).
   Setting the mutex to { 0 } is, in most threading implementations,
   equivalent to initialising it normally, so we do this to catch most
   occurences of the problem */

#define DECLARE_LOCKING_VARS( name ) \
		sem_id name##Mutex = { 0 }; \
		BOOLEAN name##MutexInitialised = FALSE; \
		THREAD name##MutexOwner = ( THREAD ) CRYPT_ERROR;

#define DEFINE_LOCKING_VARS( name ) \
		extern sem_id name##Mutex; \
		extern THREAD name##MutexOwner;

#define initGlobalResourceLock( name ) \
		if( !name##MutexInitialised ) \
			{ \
			name##Mutex = create_sem( 1, NULL ); \
			name##MutexInitialised = TRUE; \
			name##MutexOwner = ( THREAD ) CRYPT_ERROR; \
			}
#define deleteGlobalResourceLock( name ) \
		if( name##MutexInitialised ) \
			{ \
			delete_sem( name##Mutex ); \
			name##MutexInitialised = FALSE; \
			}
#define lockGlobalResource( name ) \
		if( acquire_sem( name##Mutex ); \
		name##MutexOwner = find_thread( NULL );
#define unlockGlobalResource( name ) \
		name##MutexOwner = ( THREAD ) CRYPT_ERROR; \
		release_sem( name##Mutex )

/* Some resources are owned by one thread and can't be accessed by any other
   threads.  The following macros provide facilities to declare the thread
   ID variables and check that the current thread is allowed to access this
   resource */

#define OWNERSHIP_VAR_TYPE		THREAD
#define DECLARE_OWNERSHIP_VARS	THREAD objectOwner;

#define getCurrentIdentity()	find_thread( NULL )

/* The types used for handles to threads and system synchronisation objects */

#define THREAD_HANDLE			THREAD
#define SEMAPHORE_HANDLE		THREAD

/* Define a thread function */

#define THREADFUNC_DEFINE( name, arg )	int32 *name( void *arg )

/* The lock for the internal data structures is used throughout the library
   so we make it globally visible */

extern sem_id objectMutex;

#elif defined( __IBM4758__ )

#include <cpqlib.h>

/* Declare the variables required to handle the object locking for internal
   data structures */

#define DECLARE_OBJECT_LOCKING_VARS \
		long semaphore; \
		BOOLEAN semaphoreInitialised;

/* Initialise and delete the locking variables */

#define initResourceLock( objectPtr ) \
		CPCreateSerSem( NULL, 0, 0, &( objectPtr )->semaphore ); \
		( objectPtr )->semaphoreInitialised = TRUE
#define deleteResourceLock( objectPtr ) \
		if( ( objectPtr )->semaphoreInitialised ) \
			{ \
			CPDelete( ( objectPtr )->semaphore, 0 ); \
			( objectPtr )->semaphoreInitialised = FALSE; \
			}

/* Lock and unlock a object using the locking variables */

#define lockResource( objectPtr ) \
		CPSemClaim( ( objectPtr )->semaphore, SVCWAITFOREVER )
#define unlockResource( objectPtr ) \
		CPSemRelease( ( objectPtr )->semaphore )

/* Some variables are protected by global object locks (for example the
   internal data structures are accessed through a global object map which
   maps handles to object information).  Before we can read or write these
   variables in a multithreaded environment we need to lock them so they
   won't be accessed or modified by other threads.  The following macros
   provide this locking capability */

#define DECLARE_LOCKING_VARS( name ) \
		long name##Semaphore; \
		BOOLEAN name##SemaphoreInitialised = FALSE;

#define DEFINE_LOCKING_VARS( name ) \
		extern long name##Semaphore; \
		extern BOOLEAN name##SemaphoreInitialised;

#define initGlobalResourceLock( name ) \
		if( !name##SemaphoreInitialised ) \
			{ \
			CPCreateSerSem( NULL, 0, 0, &name##Semaphore ); \
			name##SemaphoreInitialised = TRUE; \
			}
#define deleteGlobalResourceLock( name ) \
		if( name##SemaphoreInitialised ) \
			{ \
			CPDelete( name##Semaphore, 0 ); \
			name##SemaphoreInitialised = FALSE; \
			}
#define lockGlobalResource( name ) \
		CPSemClaim( name##Semaphore, SVCWAITFOREVER )
#define unlockGlobalResource( name ) \
		CPSemRelease( name##Semaphore )

/* Some objects are owned by one thread (called a task in CP/Q) and can't 
   be accessed by any other threads.  The following macros provide facilities 
   to declare the thread ID variables and check that the current thread is 
   allowed to access this object.
   
   Since the 4758 access control model differs somewhat from the standard one,
   this facility isn't currently used */

#if 0
#define DECLARE_OWNERSHIP_VARS		long objectOwner;

#define getCurrentIdentity()		GetCurrentThreadId()

/* The types used for handles to threads and system synchronisation objects */

#define THREAD_HANDLE				long
#define SEMAPHORE_HANDLE			long
#endif /* 0 */

/* Define a thread function:  CP/Q tasks function in a somewhat peculiar 
   manner, this facility isn't currently used */

#endif /* OS-specific object locking and ownership handling */

/* Generic or NOP versions of functions and types declared for those OS's 
   which don't support extended functionality.  The DECLARE_xxx macros are
   expanded into dummy variable declarations to avoid problems with zero-
   size entries in cases where they're the only element in a struct or
   incomplete declarations where they're preceded by the static keyword or
   various other, similar situations.  In addition for the (global) locking
   variable names we append the actual name to prevent name space 
   collisions */

#ifndef DECLARE_OBJECT_LOCKING_VARS
  #define DECLARE_OBJECT_LOCKING_VARS			int dummy;
  #define initResourceLock( objectPtr )
  #define deleteResourceLock( objectPtr )
  #define lockResource( objectPtr )
  #define unlockResource( objectPtr )
  #define unlockResourceExit( resource, retCode )	return( retCode )

  #define DECLARE_LOCKING_VARS( name )			int dummy##name;
  #define DEFINE_LOCKING_VARS( name )
  #define initGlobalResourceLock( name )
  #define deleteGlobalResourceLock( name )
  #define lockGlobalResource( name )
  #define unlockGlobalResource( name )

  #define OWNERSHIP_VAR_TYPE					int
  #define DECLARE_OWNERSHIP_VARS
  #define getCurrentIdentity()					CRYPT_UNUSED

  /* Dummy functions which override other definitions in cryptkrn.h */
  #define checkObjectOwnership( objectPtr )		TRUE
  #define checkObjectOwned( objectPtr )			TRUE
  #define getObjectOwnership( objectPtr )		CRYPT_UNUSED
  #define setObjectOwnership( objectPtr, owner )

  #define THREAD_HANDLE							int
  #define SEMAPHORE_HANDLE						int
#endif /* Resource ownership macros */

#endif /* _CRYPTOS_DEFINED */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -