📄 pthread.h
字号:
{
int done; /* indicates if user function executed */
long started; /* First thread to increment this value */
/* to zero executes the user function */
};
#define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) -1)
#define PTHREAD_COND_INITIALIZER ((pthread_cond_t) -1)
#define PTHREAD_RWLOCK_INITIALIZER ((pthread_rwlock_t) -1)
/*
* ====================
* ====================
* Scheduling
* ====================
* ====================
*/
/* Thread scheduling policies */
#define SCHED_OTHER 0
#define SCHED_FIFO 1
#define SCHED_RR 2
#define SCHED_MIN SCHED_OTHER
#define SCHED_MAX SCHED_RR
struct sched_param {
int sched_priority;
};
/* 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.
* For example, although the library itself can't be
* built (yet) in C, an application written in C can
* be linked and run against a library built using
* either WIN32 SEH or C++ EH.
*
* 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.
*/
typedef struct ptw32_cleanup_t ptw32_cleanup_t;
struct ptw32_cleanup_t
{
void (*routine) (void *);
void *arg;
#if !defined(_MSC_VER) && !defined(__cplusplus)
ptw32_leanup_t *prev;
#endif /* !_MSC_VER && ! __cplusplus */
};
#if defined(_MSC_VER) && !defined(__cplusplus)
/*
* WIN32 SEH version of cancel cleanup.
*/
#define pthread_cleanup_push( _rout, _arg ) \
{ \
ptw32_cleanup_t _cleanup; \
\
_cleanup.routine = (_rout); \
_cleanup.arg = (_arg); \
__try \
{ \
#define pthread_cleanup_pop( _execute ) \
} \
__finally \
{ \
if( _execute || AbnormalTermination()) \
{ \
(*(_cleanup.routine))( _cleanup.arg ); \
} \
} \
}
#else /* _MSC_VER && ! __cplusplus */
#ifndef __cplusplus
/*
* C implementation of PThreads cancel cleanup
*/
#define pthread_cleanup_push( _rout, _arg ) \
{ \
ptw32_cleanup_t _cleanup; \
\
pthread_push_cleanup( &_cleanup, (_rout), (_arg) ); \
#define pthread_cleanup_pop( _execute ) \
(void) pthread_pop_cleanup( _execute ); \
}
#else /* !__cplusplus */
/*
* 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)
*/
void (PT_STDCALL *cleanUpRout)( void * );
void * obj;
int executeIt;
public:
PThreadCleanup() :
cleanUpRout( NULL ),
obj( NULL ),
executeIt( 0 )
/*
* No cleanup performed
*/
{
}
PThreadCleanup(
void (PT_STDCALL *routine)( void * ),
void * arg ) :
cleanUpRout( routine ),
obj( arg ),
executeIt( 1 )
/*
* Registers a cleanup routine for 'arg'
*/
{
}
~PThreadCleanup()
{
if ( executeIt && ((void *) cleanUpRout != NULL) )
{
(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((void (PT_STDCALL *)(void *))(_rout), \
(void *) (_arg) );
#define pthread_cleanup_pop( _execute ) \
cleanup.execute( _execute ); \
}
#endif /* !__cplusplus) */
#endif /* _MSC_VER && ! __cplusplus */
/*
* ===============
* ===============
* Methods
* ===============
* ===============
*/
/*
* Useful if an application wants to statically link
* the lib rather than load the DLL at run-time.
*/
int pthread_win32_initialize_np(void);
/*
* PThread Attribute Functions
*/
int pthread_attr_init (pthread_attr_t * attr);
int pthread_attr_destroy (pthread_attr_t * attr);
int pthread_attr_getdetachstate (const pthread_attr_t * attr,
int *detachstate);
int pthread_attr_getstackaddr (const pthread_attr_t * attr,
void **stackaddr);
int pthread_attr_getstacksize (const pthread_attr_t * attr,
size_t * stacksize);
int pthread_attr_setdetachstate (pthread_attr_t * attr,
int detachstate);
int pthread_attr_setstackaddr (pthread_attr_t * attr,
void *stackaddr);
int pthread_attr_setstacksize (pthread_attr_t * attr,
size_t stacksize);
/*
* PThread Functions
*/
int pthread_create (pthread_t * tid,
const pthread_attr_t * attr,
void *(*start) (void *),
void *arg);
int pthread_detach (pthread_t tid);
int pthread_equal (pthread_t t1,
pthread_t t2);
void pthread_exit (void *value_ptr);
int pthread_join (pthread_t thread,
void **value_ptr);
pthread_t pthread_self (void);
int pthread_cancel (pthread_t thread);
int pthread_setcancelstate (int state,
int *oldstate);
int pthread_setcanceltype (int type,
int *oldtype);
void pthread_testcancel (void);
int pthread_once (pthread_once_t * once_control,
void (*init_routine) (void));
ptw32_cleanup_t *pthread_pop_cleanup (int execute);
void pthread_push_cleanup (ptw32_cleanup_t * cleanup,
void (*routine) (void *),
void *arg);
/*
* Thread Specific Data Functions
*/
int pthread_key_create (pthread_key_t * key,
void (*destructor) (void *));
int pthread_key_delete (pthread_key_t key);
int pthread_setspecific (pthread_key_t key,
const void *value);
void *pthread_getspecific (pthread_key_t key);
/*
* Mutex Attribute Functions
*/
int pthread_mutexattr_init (pthread_mutexattr_t * attr);
int pthread_mutexattr_destroy (pthread_mutexattr_t * attr);
int pthread_mutexattr_getpshared (const pthread_mutexattr_t
* attr,
int *pshared);
int pthread_mutexattr_setpshared (pthread_mutexattr_t * attr,
int pshared);
/*
* Mutex Functions
*/
int pthread_mutex_init (pthread_mutex_t * mutex,
const pthread_mutexattr_t * attr);
int pthread_mutex_destroy (pthread_mutex_t * mutex);
int pthread_mutex_lock (pthread_mutex_t * mutex);
int pthread_mutex_trylock (pthread_mutex_t * mutex);
int pthread_mutex_unlock (pthread_mutex_t * mutex);
/*
* Condition Variable Attribute Functions
*/
int pthread_condattr_init (pthread_condattr_t * attr);
int pthread_condattr_destroy (pthread_condattr_t * attr);
int pthread_condattr_getpshared (const pthread_condattr_t * attr,
int *pshared);
int pthread_condattr_setpshared (pthread_condattr_t * attr,
int pshared);
/*
* Condition Variable Functions
*/
int pthread_cond_init (pthread_cond_t * cond,
const pthread_condattr_t * attr);
int pthread_cond_destroy (pthread_cond_t * cond);
int pthread_cond_wait (pthread_cond_t * cond,
pthread_mutex_t * mutex);
int pthread_cond_timedwait (pthread_cond_t * cond,
pthread_mutex_t * mutex,
const struct timespec *abstime);
int pthread_cond_signal (pthread_cond_t * cond);
int pthread_cond_broadcast (pthread_cond_t * cond);
/*
* Scheduling
*/
int pthread_setschedparam (pthread_t thread,
int policy,
const struct sched_param *param);
int pthread_getschedparam (pthread_t thread,
int *policy,
struct sched_param *param);
int pthread_attr_getschedparam (const pthread_attr_t *attr,
struct sched_param *param);
int pthread_attr_setschedparam (pthread_attr_t *attr,
const struct sched_param *param);
/*
* Read-Write Lock Functions
*/
int pthread_rwlock_init(pthread_rwlock_t *lock,
const pthread_rwlockattr_t *attr);
int pthread_rwlock_destroy(pthread_rwlock_t *lock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *);
int pthread_rwlock_trywrlock(pthread_rwlock_t *);
int pthread_rwlock_rdlock(pthread_rwlock_t *lock);
int pthread_rwlock_wrlock(pthread_rwlock_t *lock);
int pthread_rwlock_unlock(pthread_rwlock_t *lock);
/*
* Non-portable functions
*/
int pthread_mutexattr_setforcecs_np(pthread_mutexattr_t *attr,
int forcecs);
HANDLE pthread_getw32threadhandle_np(pthread_t thread);
/*
* Protected Methods
*
* This function blocks until the given WIN32 handle
* is signaled or pthread_cancel had been called.
* This function allows the caller to hook into the
* PThreads cancel mechanism. It is implemented using
*
* WaitForMultipleObjects
*
* on 'waitHandle' and a manually reset WIN32 Event
* used to implement pthread_cancel. The 'timeout'
* argument to TimedWait is simply passed to
* WaitForMultipleObjects.
*/
int pthreadCancelableWait (HANDLE waitHandle);
int pthreadCancelableTimedWait (HANDLE waitHandle, DWORD timeout);
/*
* Thread-Safe C Runtime Library Mappings.
*/
#if (! defined(NEED_ERRNO)) || (! defined( _REENTRANT ) && (! defined( _MT ) || ! defined( _MD )))
_CRTIMP extern int * __cdecl _errno(void);
//int * _errno( void );
#endif
/*
* WIN32 C runtime library had been made thread-safe
* without affecting the user interface. Provide
* mappings from the UNIX thread-safe versions to
* the standard C runtime library calls.
* Only provide function mappings for functions that
* actually exist on WIN32.
*/
#if !defined(__MINGW32__)
#define strtok_r( _s, _sep, _lasts ) \
( *(_lasts) = strtok( (_s), (_sep) ) )
#endif /* !__MINGW32__ */
#define asctime_r( _tm, _buf ) \
( strcpy( (_buf), asctime( (_tm) ) ), \
(_buf) )
#define ctime_r( _clock, _buf ) \
( strcpy( (_buf), ctime( (_tm) ) ), \
(_buf) )
#define gmtime_r( _clock, _result ) \
( *(_result) = *gmtime( (_clock) ), \
(_result) )
#define localtime_r( _clock, _result ) \
( *(_result) = *localtime( (_clock) ), \
(_result) )
#define rand_r( _seed ) \
rand()
#ifdef __cplusplus
/*
* Internal exceptions
*/
class ptw32_exception {};
class ptw32_exception_cancel : public ptw32_exception {};
class ptw32_exception_exit : public ptw32_exception {};
#endif
/* FIXME: This is only required if the library was built using SEH */
/*
* Get internal SEH tag
*/
DWORD ptw32_get_exception_services_code(void);
#ifndef PTW32_BUILD
#if defined(_MSC_VER) && !defined(__cplusplus)
/*
* Redefine the SEH __except keyword to ensure that applications
* propagate our internal exceptions up to the library's internal handlers.
*/
#define __except( E ) \
__except( ( GetExceptionCode() == ptw32_get_exception_services_code() ) \
? EXCEPTION_CONTINUE_SEARCH : ( E ) )
#endif /* _MSC_VER && ! __cplusplus */
#ifdef __cplusplus
/*
* Redefine the C++ catch keyword to ensure that applications
* propagate our internal exceptions up to the library's internal handlers.
*/
#ifdef _MSC_VER
/*
* WARNING: Replace any 'catch( ... )' with 'PtW32CatchAll'
* if you want Pthread-Win32 cancelation and pthread_exit to work.
*/
#ifndef PtW32NoCatchWarn
#pragma message("When compiling applications with MSVC++ and C++ exception handling:")
#pragma message(" Replace any 'catch( ... )' with 'PtW32CatchAll' in POSIX threads")
#pragma message(" if you want POSIX thread cancelation and pthread_exit to work.")
#endif
#define PtW32CatchAll \
catch( ptw32_exception & ) { throw; } \
catch( ... )
#else /* _MSC_VER */
#define catch( E ) \
catch( ptw32_exception & ) { throw; } \
catch( E )
#endif /* _MSC_VER */
#endif /* __cplusplus */
#endif /* ! PTW32_BUILD */
#ifdef __cplusplus
} /* End of extern "C" */
#endif /* __cplusplus */
#endif /* PTHREAD_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -