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

📄 nothread.cpp

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

 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/NoThread.cpp,v $
 * $Date: 2003/05/13 18:42:13 $
 *
 Description:
	Implementation of functions when no threading is supported or
	kernel threads are used.

\*___________________________________________________________________________*/
//$SourceTop:$

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

/* ---
 Only use this file for non user context threads.
--- */
#if !defined(CONFIG_MT_USER)

#include <errno.h>

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

/*
** include some windows stuff
*/
#if defined(CONFIG_USE_WINSOCK)
extern "C"
	{
	#	include <winsock.h>					/* for timeval, fd_set etc. */
	};
#endif

#if defined(CONFIG_OS_POSIX)
#	include <unistd.h>
#	include <sys/time.h>
#	include <sys/file.h>
#	include <sys/types.h>
#	include <sys/wait.h>

#elif defined(CONFIG_OS_WIN32)
#elif defined(CONFIG_OS_WIN16)
#endif

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

/*___________________________________________________________________________*\
 *
 Typedefs:
\*___________________________________________________________________________*/
#if defined(CONFIG_OS_Stub)
	typedef thread_t THREAD_T;
	typedef thread_key_t THREAD_KEY_T;
#	define BAD_THREAD ((THREAD_T)-1)
#elif defined(CONFIG_OS_WIN32)
	typedef HANDLE THREAD_T;
	typedef DWORD THREAD_KEY_T;
#	define BAD_THREAD ((THREAD_T)0)
#endif

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

/*___________________________________________________________________________*\
 *
 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();
}

#if !defined(CONFIG_MULTITHREADED)
/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*___________________________________________________________________________*/
void Platform::Internal_EnterThreads( void (* fn)(void) )
{
	Platform::CatchExceptions( (void(*)(void*))fn, 0 );
}

/*
** Stub functions for mutexes, etc.
*/
/*___________________________________________________________________________*\
 *
 Class:
 Description:
\*___________________________________________________________________________*/
class StubLocalMutex : public Semaphore
{
private:
public:
	StubLocalMutex()										{};
	virtual ~StubLocalMutex()								{};
	virtual int Lock()										{ return 0; };
	virtual int TryLock()									{ return 0; };
	virtual int UnLock()									{ return 0; };
	virtual bool IsOK() const								{ return true; };
};

/*___________________________________________________________________________*\
 *
 Class:
 Description:
\*___________________________________________________________________________*/
class StubLocalSemaphore : public Semaphore
{
private:
public:
	StubLocalSemaphore( int iInitialCount, int iMaxCount )	{};
	virtual ~StubLocalSemaphore()							{};
	virtual int Lock()										{ return 0; };
	virtual int TryLock()									{ return 0; };
	virtual int UnLock()									{ return 0; };
	virtual bool IsOK() const								{ return true; };
};

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
Semaphore *Platform::AllocLocalMutex()
{
	return PI_NEW( StubLocalMutex() );
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
Semaphore *Platform::AllocLocalSemaphore( int iInitialCount, int iMaxCount )
{
	return PI_NEW( StubLocalSemaphore( iInitialCount, iMaxCount ) );
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
Thread *Platform::AllocThread( void *, int )
{
	PIERROR( PIAPI_NOTSUPPORTED );
	return 0;
}
#endif

#if !defined(CONFIG_MULTITHREADED)
/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
void Platform::YieldThread()
{
#if defined(CONFIG_OS_POSIX)
#	if !defined(CONFIG_NO_YIELD)
		::yield();

#	endif

#elif defined(CONFIG_OS_WIN32)
	::Sleep( 0 );	/* --- This is how Win32 gives up timeslice --- */

#elif defined(CONFIG_OS_WIN16)
	::Yield();

#else
#	error Unsupported!
#endif
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::WaitForThread( Thread * )
{
	PIERROR( PIAPI_NOTSUPPORTED );
	return -1;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
Thread *Platform::GetCurrentThread()
{
	PIERROR( PIAPI_NOTSUPPORTED );
	return 0;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
Thread *Platform::GetMainThread()
{
	PIERROR( PIAPI_NOTSUPPORTED );
	return 0;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::AllocThreadKey()
{
	PIERROR( PIAPI_NOTSUPPORTED );
	return 0;
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
bool Platform::DeleteThreadKey( int )
{
	PIERROR( PIAPI_NOTSUPPORTED );
	return 0;
}
#endif

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
void Platform::ThreadDbgDump( ostream &os )
{
#if defined(CONFIG_MULTITHREADED)
#	if defined(CONFIG_OS_POSIX)
		os << "Stub threads: no information available" << endl;
#	elif defined(CONFIG_OS_WIN32)
		os << "Win32 threads: no information available" << endl;
#	endif
#else
	os << "No threads supported" << endl;
#endif
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::PollFD( int iFD, int iFlags, int iTimeout )
{
#if defined(CONFIG_OS_POSIX)
#   if defined(CONFIG_OS_SOLARIS)
		/*
		** Solaris 2.x: man -s 3C select
		**
		** [File descriptors associated with regular files always select
		** true  for  ready  to  read, ready to write, and error conditions.]
		**
		** NOTE:
		**	  - What about pipes?
		**	  - Also change other instances of this function
		*/
		/* so just return the requested status */
		return iFlags;
#   endif
	return Platform::PollNetFD( iFD, iFlags, iTimeout );
#elif defined(CONFIG_OS_WIN32)
	/* ---
	Win32 SDK, says that WaitForSingleObject() can be used to
	check if a file handle operation will succeed without blocking
	'under certain circumstances' and is 'discouraged'. But there doesn't
	seem to be another way to do this without side effects.
	NOTE: The reason this is discouraged is because confusion can arise if
	multiple waits exists for the the same handle, therefore its
	suggested to use an event. Anyway.....
	--- */
	int iRetFlags = 0;
	switch( ::WaitForSingleObject( (HANDLE)iFD, iTimeout*1000 ) )
		{
		case WAIT_FAILED:
			PIOSERR;
			return -1;

        case WAIT_TIMEOUT:
            return 0;

        case WAIT_OBJECT_0:
			/* ---
			Don't know what made the handle signalled so set the
			return code based on what the callee requested. Files
			are unidirectional anyway so this doesn't cause confusion
			like it would with sockets
			--- */
			if ( iFlags & Platform::POLL_READ )
				{ iRetFlags = iRetFlags | Platform::POLL_READ; };
			if ( iFlags & Platform::POLL_WRITE )
				{ iRetFlags = iRetFlags | Platform::POLL_WRITE; };
			return iRetFlags;

		default:
            assert( 0 );        /* what's this ? */
			PIOSERR;
			return -1;
		};
#elif defined(CONFIG_OS_WIN16)
	/*
	** Return ready without yielding
	*/
	(void)iFD;
	(void)iTimeout;
	return iFlags;

#endif
}

/*___________________________________________________________________________*\
 *
 Function:
 Synopsis:	  static, public:
 Description:
\*___________________________________________________________________________*/
int Platform::PollPipeFD( int iFD, int iFlags, int iTimeout )
{
#if defined(CONFIG_OS_WIN32)
	int iRetFlags = 0;

	if ( iFlags & ~Platform::POLL_WRITE )
		{
		int iRet = Platform::PollFD( iFD, iFlags & ~Platform::POLL_WRITE,
			iTimeout );

		if ( iRet<0 )
			{ return iRet; };

		iRetFlags |= iRet;
		};

	/* ---
	Named pipes on Win32. Always return 'Ready to Write'.
	--- */
	if ( iFlags & Platform::POLL_WRITE )
		{ iRetFlags |= Platform::POLL_WRITE; };

	return iRetFlags;
#endif
	/*
	** Otherwise behaviour is the same as Platform::PollFD
	*/
	return Platform::PollFD( iFD, iFlags, iTimeout );	
}

/*___________________________________________________________________________*\

⌨️ 快捷键说明

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