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

📄 krnlsync.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Platform/KrnlSync.cpp,v $
 * $Date: 2003/06/07 09:05:31 $
 *
 Description:
	This file implements threads using a natively supplied facility. The
	facilities used are Win32 threads, solaris (UNIX SVR4.2 MP) and
	pthreads (POSIX 1003.1c). Although these interfaces have very different
	functions, the semantics are largely the same. So they are handled
	in the same file with lots of #if ... #endif.

\*____________________________________________________________________________*/
//$SourceTop:$

#include "OSConfig.h"		/* --- get build options --- */

/* ---
 Include this file only for builds which use some native multithreading
	technique
--- */
#if defined(CONFIG_MULTITHREADED) && !defined(CONFIG_MT_USER)

#include <assert.h>
#include <errno.h>
#include <time.h>
#include <iostream.h>

#if !defined(CONFIG_CPP_EXCEPTIONS)
#	include <setjmp.h>
#endif

#if defined(CONFIG_MT_PTHREAD)
#	include <pthread.h>
#	include <signal.h>

#elif defined(CONFIG_MT_SOLARIS)
#	include <thread.h>
#	include <synch.h> 
#	include <unistd.h>
#	include <signal.h>
#	if (CONFIG_LOCK_FLOCK)
#		include <sys/file.h>
#	endif
#	include <sys/types.h>
#	include <sys/wait.h>

#elif defined(CONFIG_MT_WIN32)
#	include <windows.h>
#	include <process.h>

#else
#	error Unsupported!
#endif

#include "PICompat.h"
#include "Allocate.h"
#include "OSEntity.h"
#include "Platform.h"
#include "PlatDefs.h"

/*____________________________________________________________________________*\
 *
 Description:
	Unix thread priorities
\*____________________________________________________________________________*/

#if defined(CONFIG_MT_PTHREAD)
#define THREAD_PRIORITY_LOWEST			1
#define THREAD_PRIORITY_BELOW_NORMAL	25
#define THREAD_PRIORITY_MED				50
#define THREAD_PRIORITY_NORMAL			THREAD_PRIORITY_MED
#define THREAD_PRIORITY_ABOVE_NORMAL	75
#define THREAD_PRIORITY_HIGHEST			99

#elif !defined(CONFIG_MT_WIN32)
#define THREAD_PRIORITY_LOWEST			1
#define THREAD_PRIORITY_BELOW_NORMAL	32	
#define THREAD_PRIORITY_MED				48	
#define THREAD_PRIORITY_NORMAL			THREAD_PRIORITY_MED
#define THREAD_PRIORITY_ABOVE_NORMAL	64
#define THREAD_PRIORITY_HIGHEST			127

#elif defined(CONFIG_MT_WIN32)
#endif

/*____________________________________________________________________________*\
 *
 Typedefs:
\*____________________________________________________________________________*/
#if defined(CONFIG_MT_PTHREAD)
	typedef pthread_t THREAD_T;
	typedef pthread_key_t THREAD_KEY_T;
#	define BAD_THREAD ((THREAD_T)-1)

#elif defined(CONFIG_MT_SOLARIS)
	typedef thread_t THREAD_T;
	typedef thread_key_t THREAD_KEY_T;
#	define BAD_THREAD ((THREAD_T)-1)

#elif defined(CONFIG_MT_WIN32)
	typedef HANDLE THREAD_T;
	typedef DWORD THREAD_KEY_T;
#	define BAD_THREAD ((THREAD_T)0)

#else
#	error Unsupported!	
#endif

/*____________________________________________________________________________*\
 *
 Declarations:
\*____________________________________________________________________________*/
static THREAD_KEY_T tTlsAllocatorKey;
static THREAD_KEY_T tThreadObjectKey;
#if !defined(CONFIG_CPP_EXCEPTIONS)
static jmp_buf ___tJmpBuf;
#endif
/*
** #define D { cerr << __FILE__ << ": " << __LINE__ << endl; }
*/
#define D

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
inline bool Internal_SetData( THREAD_KEY_T tKey, void *pData )
{
#if defined(CONFIG_MT_PTHREAD)
	int iRet = ::pthread_setspecific( tKey, pData );
	if ( iRet )
		{
		/* --- cannot use PIRETERR, because of danger of recursion --- */
		assert( 0 );
		return false;
		}
	else
		{
		return true;
		};

#elif defined(CONFIG_MT_SOLARIS)
	int iRet = ::thr_setspecific( tKey, pData );
	if ( iRet )
		{
		/* --- cannot use PIRETERR, because of danger of recursion --- */
		assert( 0 );
		return false;
		}
	else
		{
		return true;
		};

#elif defined(CONFIG_MT_WIN32)
	if ( !::TlsSetValue( tKey, pData ) )
		{ PIOSERR; return false; }
	else return true;

#else
#	error Unsupported!	
#endif
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
inline bool Internal_GetData( THREAD_KEY_T tKey, void **ppData )
{
#if defined(CONFIG_MT_PTHREAD)
	*ppData = ::pthread_getspecific( tKey );
	if ( !*ppData && errno )
		{
		assert( 0 );
		return false;
		}
	else
		{ return true; };

#elif defined(CONFIG_MT_SOLARIS)
	int iRet = ::thr_getspecific( tKey, ppData );
	if ( iRet )
		{
		/* --- cannot use PIRETERR, because danger of recursion --- */
		assert( 0 );
		return false;
		}
	else
		{ return true; };

#elif defined(CONFIG_MT_WIN32)
	*ppData = ::TlsGetValue( tKey );
	if ( !*ppData && GetLastError() ) { PIOSERR; };
	return true;

#else
#	error Unsupported!
#endif
}

#if 0
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	This function will only be called after Platform::Internal_InitThreads so
	it is safe to assume the thread key tTlsAllocator has been setup.
\*____________________________________________________________________________*/
Allocator *Platform::GetCurrentAllocator()
{
	return GetGlobalAllocator();
}
#endif

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class POSIXLocalMutex : public Semaphore
{
private:
#if defined(CONFIG_MT_PTHREAD)
	pthread_mutex_t tMutex;
#elif defined(CONFIG_MT_SOLARIS)
	mutex_t tMutex;
#elif defined(CONFIG_MT_WIN32)
	HANDLE hMutex;
#else
#	error Unsupported!	
#endif
	bool bError;

public:
	POSIXLocalMutex()
	:	bError( false )
		{
#	if defined(CONFIG_MT_PTHREAD)
#if 0
	/*
	** This mutex initialization is a little old
	*/
		pthread_mutexattr_t tAttr;
		tAttr.m_type = MUTEX_TYPE_STATIC_FAST;
		tAttr.m_flags = MUTEX_FLAGS_PRIVATE;
#endif
		int iRet = ::pthread_mutex_init( &tMutex, 0 );
		if ( iRet!= 0 )
			{ PIRETERR(iRet); bError = true; };

#	elif defined(CONFIG_MT_SOLARIS)
		int iRet = ::mutex_init( &tMutex, USYNC_THREAD, 0);
		if ( iRet != 0 )
			{ PIRETERR(iRet); bError = true; };

#	elif defined(CONFIG_MT_WIN32)
		hMutex = ::CreateMutex( 0, FALSE, 0 );
		if ( !hMutex )
			{ PIOSERR; bError = true; };
//		::InitializeCriticalSection( &tCS );

#	else
#		error Unsupported!	
#	endif
		};

	virtual ~POSIXLocalMutex()
		{
#	if defined(CONFIG_MT_PTHREAD)
		int iRet = ::pthread_mutex_destroy( &tMutex );
		assert( !iRet );
		if ( iRet ) { PIRETERR(iRet); };

#	elif defined(CONFIG_MT_SOLARIS)
		int iRet = ::mutex_destroy( &tMutex );
		assert( !iRet );
		if ( iRet ) { PIRETERR(iRet); };

#	elif defined(CONFIG_MT_WIN32)
		BOOL bRet = ::CloseHandle( hMutex );
		assert( bRet );
		if ( !bRet ) { PIOSERR; };
//		DeleteCriticalSection( &tCS );

#	else
#		error Unsupported!	
#	endif
		};

	virtual int Lock()
		{
#	if defined(CONFIG_MT_PTHREAD)
		int iRet = ::pthread_mutex_lock( &tMutex );
		if ( iRet != 0 )	
			{ PIRETERR(iRet); bError = true; };
		return iRet;

#	elif defined(CONFIG_MT_SOLARIS)
		int iRet = ::mutex_lock( &tMutex );
		if ( iRet != 0 )	
			{ PIRETERR(iRet); bError = true; };
		return iRet;

#	elif defined(CONFIG_MT_WIN32)
		if ( ::WaitForSingleObject( hMutex, INFINITE ) == WAIT_FAILED )
			{ PIOSERR; bError = true; };
		return bError ? -1 : 0;

#	else
#		error Unsupported!	
#	endif
		};	

	virtual int TryLock()
		{
#	if defined(CONFIG_MT_PTHREAD)
		int iRet = pthread_mutex_trylock( &tMutex );
		if ( !iRet ) 
			{ return 0; }
		else if ( iRet == EBUSY )
			{ return PISYNC_EBUSY; }
		else
			{ PIRETERR(iRet); bError = true; return -1; };

#	elif defined(CONFIG_MT_SOLARIS)
		int iRet = ::mutex_trylock( &tMutex );
		if ( !iRet ) 
			{ return 0; }
		else if ( iRet == EBUSY )
			{ return PISYNC_EBUSY; }
		else
			{ PIRETERR(iRet); bError = true; return -1; };

#	elif defined(CONFIG_MT_WIN32)
		DWORD dwResult = ::WaitForSingleObject( hMutex, 0 );
		if ( dwResult == WAIT_TIMEOUT )
			{ return PISYNC_EBUSY; }
		else if ( dwResult != WAIT_FAILED )
			{ return 0; }
		else
			{ PIOSERR; bError = true; return -1; };

#	else
#		error Unsupported!	
#	endif
		};	

	virtual int UnLock()
		{
#	if defined(CONFIG_MT_PTHREAD)
		int iRet = ::pthread_mutex_unlock( &tMutex );
		if ( iRet!=0 )
			{ PIRETERR(iRet); bError = true; };
		return iRet;	

#	elif defined(CONFIG_MT_SOLARIS)
		int iRet = ::mutex_unlock( &tMutex );
		if ( iRet!=0 )
			{ PIRETERR(iRet); bError = true; };
		return iRet;	

#	elif defined(CONFIG_MT_WIN32)
		bError = ::ReleaseMutex( hMutex ) != TRUE;
		if ( bError ) { PIOSERR; };
		return bError ? -1 : 0;

#	else
#		error Unsupported!	
#	endif
		};

	virtual bool IsOK() const
		{ 
		return !bError;
		};
};

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
#if !defined(CONFIG_MT_PTHREAD)
class POSIXLocalSemaphore : public Semaphore
{
private:
#if defined(CONFIG_MT_PTHREAD)
#elif defined(CONFIG_MT_SOLARIS)
	sema_t tSemaphore;
#elif defined(CONFIG_MT_WIN32)
	HANDLE hSemaphore;
#else
#	error Unsupported!	
#endif
	bool bError;

public:
	POSIXLocalSemaphore( int iInitialCount, int iMaxCount );
	virtual ~POSIXLocalSemaphore();
	virtual int Lock();
	virtual int TryLock();
	virtual int UnLock();
	virtual bool IsOK() const;
};
#endif

/*____________________________________________________________________________*\
 *
 Declarations:
\*____________________________________________________________________________*/
unsigned long PIPOSIXThreadFunction( unsigned long );

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class POSIXThread : public Thread
{
public:
	class StackUnwindException
		{
		};

private:
	THREAD_T tThread;
	ThreadFn fnThread;
	Allocator *pAllocator;
	unsigned long ulData;
	int iStackSize;
	bool InternalSetPriority( int iPriority );
	bool IsTerminated();

public:
	/* --- some public internal functions --- */
	static bool InternalWaitForJoin( THREAD_T tThread );

	POSIXThread();		/* --- special constructor for main thread --- */
	POSIXThread( Allocator *pAllocator, int iStackSize );
	virtual ~POSIXThread();
	virtual bool Begin( ThreadFn fnEntry, unsigned long ulData, 
		int iPriority = PITHREAD_PRIORITY_MED, int iFlags=0 );
	virtual bool Terminate( unsigned long ulExitCode );
	virtual bool Suspend();
	virtual bool Resume();
	virtual int GetPriority()
		{
		int iPhysicalPriority;
#if defined(CONFIG_MT_PTHREAD)

		sched_param schedParam;
		int iPolicy;

		int iRet = ::pthread_getschedparam( tThread, &iPolicy, &schedParam );

⌨️ 快捷键说明

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