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

📄 pthread.h

📁 “网络安全技术实践与代码详解”实例代码
💻 H
📖 第 1 页 / 共 3 页
字号:
#define _POSIX_THREAD_PRIO_INHERIT
#define _POSIX_THREAD_PRIO_PROTECT
#define _POSIX_THREAD_PROCESS_SHARED

#endif				/* KLUDGE */

/*
 * POSIX Limits
 *
 *	PTHREAD_DESTRUCTOR_ITERATIONS
 *		Standard states this must be at least
 *		4.
 *
 *	PTHREAD_KEYS_MAX
 *		WIN32 permits only 64 TLS keys per process.
 *		This limitation could be worked around by
 *		simply simulating keys.
 *
 *	PTHREADS_STACK_MIN
 *		POSIX specifies 0 which is also the value WIN32
 *		interprets as allowing the system to
 *		set the size to that of the main thread. The
 *		maximum stack size in Win32 is 1Meg. WIN32
 *		allocates more stack as required up to the 1Meg
 *		limit.
 *
 *	PTHREAD_THREADS_MAX
 *		Not documented by WIN32. Wrote a test program
 *		that kept creating threads until it failed
 *		revealed this approximate number (Windows NT).
 *		This number is somewhat less for Windows 9x
 *		and is effectively less than 64. Perhaps this
 *		constant should be set at DLL load time.
 *
 */
#define PTHREAD_DESTRUCTOR_ITERATIONS			       4
#define PTHREAD_KEYS_MAX			64
#define PTHREAD_STACK_MIN			 0
#define PTHREAD_THREADS_MAX		      2019
#ifndef _POSIX_SEM_NSEMS_MAX
/* Not used and only an arbitrary value. */
#  define _POSIX_SEM_NSEMS_MAX		      1024
#endif
#ifndef _POSIX_SEM_VALUE_MAX
#  define _POSIX_SEM_VALUE_MAX	       (INT_MAX/2)
#endif

#if __GNUC__ && ! defined (__declspec)
# error Please upgrade your GNU compiler to one that supports __declspec.
#endif

/*
 * When building the DLL code, you should define PTW32_BUILD so that
 * the variables/functions are exported correctly. When using the DLL,
 * do NOT define PTW32_BUILD, and then the variables/functions will
 * be imported correctly.
 */
#ifdef _DLL
#  ifdef PTW32_BUILD
#    define PTW32_DLLPORT __declspec (dllexport)
#  else
#    define PTW32_DLLPORT __declspec (dllimport)
#  endif
#endif

#if defined(_UWIN) && PTW32_LEVEL >= PTW32_LEVEL_MAX
#   include	<sys/types.h>
#else
typedef struct pthread_t_ *pthread_t;
typedef struct pthread_attr_t_ *pthread_attr_t;
typedef struct pthread_once_t_ pthread_once_t;
typedef struct pthread_key_t_ *pthread_key_t;
typedef struct pthread_mutex_t_ *pthread_mutex_t;
typedef struct pthread_mutexattr_t_ *pthread_mutexattr_t;
typedef struct pthread_cond_t_ *pthread_cond_t;
typedef struct pthread_condattr_t_ *pthread_condattr_t;
#endif
typedef struct pthread_rwlock_t_ *pthread_rwlock_t;
typedef struct pthread_rwlockattr_t_ *pthread_rwlockattr_t;
typedef struct pthread_spinlock_t_ *pthread_spinlock_t;
typedef struct pthread_barrier_t_ *pthread_barrier_t;
typedef struct pthread_barrierattr_t_ *pthread_barrierattr_t;

/*
 * ====================
 * ====================
 * POSIX Threads
 * ====================
 * ====================
 */

enum {
/*
 * pthread_attr_{get,set}detachstate
 */
  PTHREAD_CREATE_JOINABLE	= 0,  /* Default */
  PTHREAD_CREATE_DETACHED	= 1,

/*
 * pthread_attr_{get,set}inheritsched
 */
  PTHREAD_INHERIT_SCHED 	= 0,
  PTHREAD_EXPLICIT_SCHED	= 1,  /* Default */

/*
 * pthread_{get,set}scope
 */
  PTHREAD_SCOPE_PROCESS 	= 0,
  PTHREAD_SCOPE_SYSTEM		= 1,  /* Default */

/*
 * pthread_setcancelstate paramters
 */
  PTHREAD_CANCEL_ENABLE 	= 0,  /* Default */
  PTHREAD_CANCEL_DISABLE	= 1,

/*
 * pthread_setcanceltype parameters
 */
  PTHREAD_CANCEL_ASYNCHRONOUS	= 0,
  PTHREAD_CANCEL_DEFERRED	= 1,  /* Default */

/*
 * pthread_mutexattr_{get,set}pshared
 * pthread_condattr_{get,set}pshared
 */
  PTHREAD_PROCESS_PRIVATE	= 0,
  PTHREAD_PROCESS_SHARED	= 1,

/*
 * pthread_barrier_wait
 */
  PTHREAD_BARRIER_SERIAL_THREAD = -1
};

/*
 * ====================
 * ====================
 * Cancelation
 * ====================
 * ====================
 */
#define PTHREAD_CANCELED       ((void *) -1)


/*
 * ====================
 * ====================
 * Once Key
 * ====================
 * ====================
 */
#define PTHREAD_ONCE_INIT	{ PTW32_FALSE, -1 }

struct pthread_once_t_
{
  int done;		    /* indicates if user function executed  */
  long started; 	    /* First thread to increment this value */
			    /* to zero executes the user function   */
};


/*
 * ====================
 * ====================
 * Object initialisers
 * ====================
 * ====================
 */
#define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) -1)

#define PTHREAD_COND_INITIALIZER ((pthread_cond_t) -1)

#define PTHREAD_RWLOCK_INITIALIZER ((pthread_rwlock_t) -1)

#define PTHREAD_SPINLOCK_INITIALIZER ((pthread_spinlock_t) -1)


/*
 * Mutex types.
 */
enum
{
  /* Compatibility with LinuxThreads */
  PTHREAD_MUTEX_FAST_NP,
  PTHREAD_MUTEX_RECURSIVE_NP,
  PTHREAD_MUTEX_ERRORCHECK_NP,
  PTHREAD_MUTEX_TIMED_NP = PTHREAD_MUTEX_FAST_NP,
  PTHREAD_MUTEX_ADAPTIVE_NP = PTHREAD_MUTEX_FAST_NP,
  /* For compatibility with POSIX */
  PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_FAST_NP,
  PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
  PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
  PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
};


/* There are three implementations of cancel cleanup.
 * Note that pthread.h is included in both application
 * compilation units and also internally for the library.
 * The code here and within the library aims to work
 * for all reasonable combinations of environments.
 *
 * The three implementations are:
 *
 *   WIN32 SEH
 *   C
 *   C++
 *
 * Please note that exiting a push/pop block via
 * "return", "exit", "break", or "continue" will
 * lead to different behaviour amongst applications
 * depending upon whether the library was built
 * using SEH, C++, or C. For example, a library built
 * with SEH will call the cleanup routine, while both
 * C++ and C built versions will not.
 */

/*
 * Define defaults for cleanup code.
 * Note: Unless the build explicitly defines one of the following, then
 * we default to standard C style cleanup. This style uses setjmp/longjmp
 * in the cancelation and thread exit implementations and therefore won't
 * do stack unwinding if linked to applications that have it (e.g.
 * C++ apps). This is currently consistent with most/all commercial Unix
 * POSIX threads implementations.
 */
#if !defined( __CLEANUP_SEH ) && !defined( __CLEANUP_CXX ) && !defined( __CLEANUP_C )
# define __CLEANUP_C
#endif

#if defined( __CLEANUP_SEH ) && defined(__GNUC__)
#error ERROR [__FILE__, line __LINE__]: GNUC does not support SEH.
#endif

typedef struct ptw32_cleanup_t ptw32_cleanup_t;
typedef void (__cdecl *ptw32_cleanup_callback_t)(void *);

struct ptw32_cleanup_t
{
  ptw32_cleanup_callback_t routine;
  void *arg;
  struct ptw32_cleanup_t *prev;
};

#ifdef __CLEANUP_SEH
	/*
	 * WIN32 SEH version of cancel cleanup.
	 */

#define pthread_cleanup_push( _rout, _arg ) \
	{ \
	    ptw32_cleanup_t	_cleanup; \
	    \
	_cleanup.routine	= (ptw32_cleanup_callback_t)(_rout); \
	    _cleanup.arg	= (_arg); \
	    __try \
	      { \

#define pthread_cleanup_pop( _execute ) \
	      } \
	    __finally \
		{ \
		    if( _execute || AbnormalTermination()) \
		      { \
			  (*(_cleanup.routine))( _cleanup.arg ); \
		      } \
		} \
	}

#else /* __CLEANUP_SEH */

#ifdef __CLEANUP_C

	/*
	 * C implementation of PThreads cancel cleanup
	 */

#define pthread_cleanup_push( _rout, _arg ) \
	{ \
	    ptw32_cleanup_t	_cleanup; \
	    \
	    ptw32_push_cleanup( &_cleanup, (ptw32_cleanup_callback_t) (_rout), (_arg) ); \

#define pthread_cleanup_pop( _execute ) \
	    (void) ptw32_pop_cleanup( _execute ); \
	}

#else /* __CLEANUP_C */

#ifdef __CLEANUP_CXX

	/*
	 * C++ version of cancel cleanup.
	 * - John E. Bossom.
	 */

	class PThreadCleanup {
	  /*
	   * PThreadCleanup
	   *
	   * Purpose
	   *	  This class is a C++ helper class that is
	   *	  used to implement pthread_cleanup_push/
	   *	  pthread_cleanup_pop.
	   *	  The destructor of this class automatically
	   *	  pops the pushed cleanup routine regardless
	   *	  of how the code exits the scope
	   *	  (i.e. such as by an exception)
	   */
      ptw32_cleanup_callback_t cleanUpRout;
	  void	  *	  obj;
	  int		  executeIt;

	public:
	  PThreadCleanup() :
	    cleanUpRout( 0 ),
	    obj( 0 ),
	    executeIt( 0 )
	    /*
	     * No cleanup performed
	     */
	    {
	    }

	  PThreadCleanup(
	     ptw32_cleanup_callback_t routine,
			 void	 *	 arg ) :
	    cleanUpRout( routine ),
	    obj( arg ),
	    executeIt( 1 )
	    /*
	     * Registers a cleanup routine for 'arg'
	     */
	    {
	    }

	  ~PThreadCleanup()
	    {
	      if ( executeIt && ((void *) cleanUpRout != (void *) 0) )
		{
		  (void) (*cleanUpRout)( obj );
		}
	    }

	  void execute( int exec )
	    {
	      executeIt = exec;
	    }
	};

	/*
	 * C++ implementation of PThreads cancel cleanup;
	 * This implementation takes advantage of a helper
	 * class who's destructor automatically calls the
	 * cleanup routine if we exit our scope weirdly
	 */
#define pthread_cleanup_push( _rout, _arg ) \
	{ \
	    PThreadCleanup  cleanup((ptw32_cleanup_callback_t)(_rout), \
				    (void *) (_arg) );

#define pthread_cleanup_pop( _execute ) \
	    cleanup.execute( _execute ); \
	}

#else

#error ERROR [__FILE__, line __LINE__]: Cleanup type undefined.

#endif /* __CLEANUP_CXX */

#endif /* __CLEANUP_C */

#endif /* __CLEANUP_SEH */

/*
 * ===============
 * ===============
 * Methods
 * ===============
 * ===============
 */

/*
 * PThread Attribute Functions
 */
PTW32_DLLPORT int pthread_attr_init (pthread_attr_t * attr);

PTW32_DLLPORT int pthread_attr_destroy (pthread_attr_t * attr);

PTW32_DLLPORT int pthread_attr_getdetachstate (const pthread_attr_t * attr,
					 int *detachstate);

PTW32_DLLPORT int pthread_attr_getstackaddr (const pthread_attr_t * attr,
				       void **stackaddr);

PTW32_DLLPORT int pthread_attr_getstacksize (const pthread_attr_t * attr,
				       size_t * stacksize);

PTW32_DLLPORT int pthread_attr_setdetachstate (pthread_attr_t * attr,
					 int detachstate);

PTW32_DLLPORT int pthread_attr_setstackaddr (pthread_attr_t * attr,
				       void *stackaddr);

PTW32_DLLPORT int pthread_attr_setstacksize (pthread_attr_t * attr,
				       size_t stacksize);

PTW32_DLLPORT int pthread_attr_getschedparam (const pthread_attr_t *attr,
					struct sched_param *param);

PTW32_DLLPORT int pthread_attr_setschedparam (pthread_attr_t *attr,
					const struct sched_param *param);

PTW32_DLLPORT int pthread_attr_setschedpolicy (pthread_attr_t *,
					 int);

PTW32_DLLPORT int pthread_attr_getschedpolicy (pthread_attr_t *,
					 int *);

PTW32_DLLPORT int pthread_attr_setinheritsched(pthread_attr_t * attr,
					 int inheritsched);

PTW32_DLLPORT int pthread_attr_getinheritsched(pthread_attr_t * attr,
					 int * inheritsched);

PTW32_DLLPORT int pthread_attr_setscope (pthread_attr_t *,
				   int);

PTW32_DLLPORT int pthread_attr_getscope (const pthread_attr_t *,
				   int *);

/*

⌨️ 快捷键说明

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