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

📄 thread.h

📁 common c++提供socket
💻 H
📖 第 1 页 / 共 4 页
字号:
	 * 	 * @param th specified thread.	 */	friend inline void operator++(Thread &th)		{if (th._start) th._start->post();};	friend inline void operator--(Thread &th)		{if (th._start) th._start->wait();};#ifdef WIN32	bool isCancelled() const;	static DWORD waitThread(HANDLE hRef, timeout_t timeout);#endif	/**	 * This is used to help build wrapper functions in libraries	 * around system calls that should behave as cancellation	 * points but don't.	 *	 * @return saved cancel type.	 */	static Cancel enterCancel(void);	/**	 * This is used to restore a cancel block.		 *	 * @param cancel type that was saved.	 */	static void exitCancel(Cancel cancel);};/** * A class to automatically set the thread cancellation mode of a * member function.  When the member function returns and the automatic * variable falls out of scope, the previous thread cancellation mode * is restored. * * @author David Sugar <dyfet@gnu.org> * @short Automatic cancellation mode setting. */class __EXPORT Cancellation{private:	Thread::Cancel prior;public:	Cancellation(Thread::Cancel cancel);	~Cancellation();};#if !defined(WIN32) && !defined(__MINGW32__)typedef	int		signo_t;class PosixThread: public Thread{private:#ifndef WIN32	/** @internal */	friend class ThreadImpl;	friend class Thread;#endif#ifndef	CCXX_SIG_THREAD_ALARM	static PosixThread *_timer;	static Mutex _arm;#endif		time_t	_alarm;	static void signalThread(Thread* th,signo_t signo);protected:			/**	 * In the Posix version of Common C++, this can be used to send a	 * signal into the parent thread of the current object.	 * 	 * @param signo a posix signal id.	 */	inline void signalParent(signo_t signo)		{ signalThread(_parent,signo); };		/**	 * In the Posix version of Common C++, this can be used to send a	 * signal into the main application thread.	 * 	 * @param signo a posix signal id.	 */	inline void signalMain(signo_t signo)		{ signalThread(_main,signo);};	/**	 * A derivable method to call when a SIGALRM is being delivered	 * to a specific thread.	 */	virtual void onTimer(void);	/**	 * A derived method to handle hangup events being delivered	 * to a specific thread.	 */	virtual void onHangup(void);	/**	 * A derived method to call when a SIGABRT is being delivered	 * to a specific thread.	 */	virtual void onException(void);	/**	 * A derived method to call when a SIGPIPE is being delivered	 * to a specific thread.	 */	virtual void onDisconnect(void);	/**	 * A derived method to handle asynchronous I/O requests delivered	 * to the specified thread.	 */	virtual void onPolling(void);	/**	 * A derivable method to call for delivering a signal event to	 * a specified thread.	 *	 * @param - posix signal id.	 */	virtual void onSignal(int);		/**	 * Used to specify a timeout event that can be delivered to the	 * current thread via SIGALRM.  When the timer expires, the onTimer() 	 * method is called for the thread.  At present, only one thread	 * timer can be active at any given time.  On some operating	 * systems (including Linux) a timer can be active on each thread.	 * 	 * @param timer timeout in milliseconds.	 * @param periodic should the timer be periodic.	 * @note currently, periodic timers are only available on	 * systems with a working setitimer call.	 */	void setTimer(timeout_t timer, bool periodic = false);		/**	 * Gets the time remaining for the current threads timer before	 * it expires.	 * 	 * @return time remaining before timer expires in milliseconds.	 */	timeout_t getTimer(void) const;		/**	 * Terminates the timer before the timeout period has expired.	 * This prevents the timer from sending it's SIGALRM and makes	 * the timer available to other threads.	 */	void endTimer(void);#if defined(HAVE_SIGWAIT) || defined(HAVE_SIGWAIT2)		/**	 * Used to wait on a Posix signal from another thread.  This can be	 * used as a crude rondevious/synchronization method between threads.	 * 	 * @param signo a posix signal id.	 */	void waitSignal(signo_t signo);#endif		/**	 * Used to enable or disable a signal within the current thread.	 *	 * @param signo posix signal id.	 * @param active set to true to enable.	 */	void setSignal(int signo, bool active);	/** 	 * Access to pthread_attr structure 	 *  this allows setting/modifying pthread attributes 	 *  not covered in the platform independant Thread constructor, 	 *  e.g. contention scope or scheduling policy 	 */	pthread_attr_t *getPthreadAttrPtr(void);	/** 	 * Get pthread_t of underlying posix thread (useful for 	 * debugging/logging)  	 */	pthread_t getPthreadId(void);public:	PosixThread(int pri = 0, size_t stack = 0);		/**	 * Delivers a Posix signal to the current thread.	 * 	 * @param signo a posix signal id.	 */	inline void signalThread(int signo)		{signalThread(this, signo);};	/**	 * Install a signal handler for use by threads and	 * the OnSignal() event notification handler.	 *	 * @param signo posix signal id.	 */	static void sigInstall(int signo);};#endif/** * This class allows the creation of a thread context unique "pointer" * that can be set and retrieved and can be used to create thread specific * data areas for implementing "thread safe" library routines. *  *  Finally, Common C++ supports a * thread-safe "AtomicCounter" class.  This can often be used for reference * counting without having to protect the counter with a separate Mutex * counter.  This lends to lighter-weight code. *  *  * @author David Sugar <dyfet@ostel.com> * @short container for thread specific data storage. */class __EXPORT ThreadKey{private:#ifndef WIN32	pthread_key_t key;	typedef void (*TDestruct)(void*);	friend class ThreadImpl;	ThreadKey(TDestruct destruct);#else	DWORD	key;#endifpublic:	/**	 * Create a unique thread specific container.	 */	ThreadKey();	/**	 * Destroy a thread specific container and any contents reserved.	 */	virtual ~ThreadKey();	/**	 * Get the value of the pointer for the thread specific data	 * container.  A unique pointer can be set for each execution	 * context.	 * 	 * @return a unique void * for each execution context.	 */	void *getKey(void);	/**	 * Set the value of the pointer for the current thread specific	 * execution context.  This can be used to store thread context	 * specific data.	 * 	 * @param - ptr to thread context specific data.	 */	void setKey(void *);};/** * Timer ports are used to provide synchronized timing events when managed * under a "service thread" such as SocketService.  This is made into a * stand-alone base class since other derived libraries (such as the * serial handlers) may also use the pooled "service thread" model * and hence also require this code for managing timing. * * @author David Sugar <dyfet@ostel.com> * @short synchronized millisecond timing for service threads. */class __EXPORT TimerPort{#ifndef WIN32	struct timeval timer;#else	DWORD timer;#endif	bool active;public:	/**	 * Create a timer, mark it as inactive, and set the initial	 * "start" time to the creation time of the timer object.  This	 * allows "incTimer" to initially refer to time delays relative	 * to the original start time of the object.	 */	TimerPort();	/**	 * Set a new start time for the object based on when this call is	 * made and optionally activate the timer for a specified number	 * of milliseconds.  This can be used to set the starting time	 * of a realtime session.	 *	 * @param timeout delay in milliseconds from "now"	 */	void setTimer(timeout_t timeout = 0);	/**	 * Set a timeout based on the current time reference value either	 * from object creation or the last setTimer().  This reference	 * can be used to time synchronize realtime data over specified	 * intervals and force expiration when a new frame should be	 * released in a synchronized manner.  	 *	 * @param timeout delay in milliseconds from reference.	 */	void incTimer(timeout_t timeout);	/**	 * This is used to "disable" the service thread from expiring	 * the timer object.  It does not effect the reference time from	 * either creation or a setTimer().	 */	void endTimer(void);	/**	 * This is used by service threads to determine how much time	 * remains before the timer expires based on a timeout specified	 * in setTimer() or incTimer().  It can also be called after	 * setting a timeout with incTimer() to see if the current timeout	 * has already expired and hence that the application is already	 * delayed and should skip frame(s).	 *	 * return time remaining in milliseconds, or TIMEOUT_INF if	 * inactive.	 */	timeout_t getTimer(void) const;	/**	 * This is used to determine how much time has elapsed since a	 * timer port setTimer benchmark time was initially set.  This	 * allows one to use setTimer() to set the timer to the current	 * time and then measure elapsed time from that point forward.	 *	 * return time elapsed in milliseconds, or TIMEOUT_INF if	 * inactive.	 */	timeout_t getElapsed(void) const;};// FIXME: not in win32 implementation#if !defined(WIN32)// FIXME: private declaration ???struct	timespec *getTimeout(struct timespec *spec, timeout_t timeout);	#if !defined(__CYGWIN32__) && !defined(__MINGW32__)void	wait(signo_t signo);#endif#endif // !WIN32#ifdef USE_POLL/** * The poller class is used to help manage pollfd structs for use in the * updated serial and socket "port" code. * * @author Gianni Mariani <gianni@mariani.ws> * @short pollfd assistance class for port classes. */class Poller {private:	int nufds;	pollfd *ufds;public:	Poller();	virtual ~Poller();	/**	 * reserve a specified number of poll descriptors.  If additional	 * descriptors are needed, they are allocated.	 *	 * @return new array of descriptors.	 * @param cnt number of desctiptors to reserve	 */	pollfd *getList(int cnt);	/**	 * Retreive the current array of poll descriptors.	 *	 * @return array of descriptors.	 */	inline	pollfd *getList(void)		{return ufds;};};#endifinline Thread *getThread(void)	{return Thread::get();}/** * This class is used to access non-reentrant date and time functions in the * standard C library. * * The class has two purposes: * - 1 To be used internaly in CommonCpp's date and time classes to make them *     thread safe. * - 2 To be used by clients as thread safe replacements to the standard C *     functions, much like Thread::sleep() represents a thread safe version *     of the standard sleep() function. * * @note The class provides one function with the same name as its equivalent *       standard function and one with another, unique name. For new clients, *       the version with the unique name is recommended to make it easy to *       grep for accidental usage of the standard functions. The version with *       the standard name is provided for existing clients to sed replace their *       original version. * * @note Also note that some functions that returned pointers have been redone *       to take that pointer as an argument instead, making the caller *       responsible for memory allocation/deallocation. This is almost *       how POSIX specifies *_r functions (reentrant versions of the *       standard time functions), except the POSIX functions also return the *       given pointer while we do not. We don't use the *_r functions as they *       aren't all generally available on all platforms yet. * * @author Idar Tollefsen <idar@cognita.no> * @short Thread safe date and time functions. */class __EXPORT SysTime{private:		static Mutex timeLock;protected:		inline static void lock(void)			{timeLock.enterMutex();}		inline static void unlock(void)			{timeLock.leaveMutex();}public:        static time_t getTime(time_t *tloc = NULL);        static time_t time(time_t *tloc) 			{ return getTime(tloc); };        static int getTimeOfDay(struct timeval *tp);        static int gettimeofday(struct timeval *tp, struct timezone *)			{ return getTimeOfDay(tp); };                static struct tm *getLocalTime(const time_t *clock, struct tm *result);        static struct tm *locatime(const time_t *clock, struct tm *result)			{ return getLocalTime(clock, result); };		static struct tm *getGMTTime(const time_t *clock, struct tm *result);		static struct tm *gmtime(const time_t *clock, struct tm *result)			{ return getGMTTime(clock, result);};}; #ifndef	HAVE_LOCALTIME_Rinline struct tm *localtime_r(const time_t *t, struct tm *b)	{return SysTime::getLocalTime(t, b);};inline char *ctime_r(const time_t *t, char *buf)	{return ctime(t);};inline struct tm *gmtime_r(const time_t *t, struct tm *b) \{return SysTime::getGMTTime(t, b);};inline char *asctime_r(const struct tm *tm, char *b) \	{return asctime(tm);};#endif 	#ifdef	CCXX_NAMESPACES}#endif#endif/** EMACS ** * Local variables: * mode: c++ * c-basic-offset: 8 * End: */

⌨️ 快捷键说明

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