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

📄 thread.c

📁 xmlrpc,用 XML表示得远程过程调用,基于web上得远程计算
💻 C
字号:
/*******************************************************************************
**
** thread.c
**
** This file is part of the ABYSS Web server project.
**
** Copyright (C) 2000 by Moez Mahfoudh <mmoez@bigfoot.com>.
** All rights reserved.
**
** 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 BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS 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 AUTHOR OR 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.
**
*******************************************************************************/

#ifdef ABYSS_WIN32
#include <process.h>
#endif
#include "abyss.h"

/* 16K is the minimum size of stack on Win32 */
#define  THREAD_STACK_SIZE    (16*1024)

/*********************************************************************
** Thread
*********************************************************************/

abyss_bool ThreadCreate(TThread *t, TThreadProc func, void *arg )
{
#ifdef ABYSS_WIN32
	DWORD z;
   *t =(TThread)_beginthreadex( NULL, THREAD_STACK_SIZE, func, 
                                arg, CREATE_SUSPENDED, &z );
	return (*t!=NULL);
#else
#	ifdef _UNIX
#		ifdef _THREAD
 {
    pthread_attr_t attr;
    pthread_attr_init( &attr );
    pthread_attr_setstacksize( &attr, THREAD_STACK_SIZE );
 	 if( pthread_create( t,&attr,(PTHREAD_START_ROUTINE)func,arg)==0)
    {
      pthread_attr_destroy( &attr );
		return (pthread_detach(*t)==0);
    }
    pthread_attr_destroy( &attr );
	 return FALSE;
 }
#		else
	switch (fork())
	{
	case 0:
		(*func)(arg);
	 	exit(0);
	case (-1):
		return FALSE;
	};
	
	return TRUE;
#		endif	/* _THREAD */
#	else
	(*func)(arg);
	return TRUE;
#	endif	/*_UNIX */
#endif	/* ABYSS_WIN32 */
}

abyss_bool ThreadRun(TThread *t)
{
#ifdef ABYSS_WIN32
	return (ResumeThread(*t)!=0xFFFFFFFF);
#else
	return TRUE;	
#endif	/* ABYSS_WIN32 */
}
abyss_bool ThreadStop(TThread *t)
{
#ifdef ABYSS_WIN32
	return (SuspendThread(*t)!=0xFFFFFFFF);
#else
	return TRUE;
#endif	/* ABYSS_WIN32 */
}

abyss_bool ThreadKill(TThread *t)
{
#ifdef ABYSS_WIN32
	return (TerminateThread(*t,0)!=0);
#else
	/*return (pthread_kill(*t)==0);*/
	return TRUE;
#endif	/* ABYSS_WIN32 */
}

void ThreadWait(uint32 ms)
{
#ifdef ABYSS_WIN32
	Sleep(ms);
#else
	usleep(ms*1000);
#endif	/* ABYSS_WIN32 */
}

void ThreadExit( TThread *t, int ret_value )
{
#ifdef ABYSS_WIN32
   _endthreadex( ret_value );
#elif _THREAD
	pthread_exit( &ret_value );
#else
   ;
#endif	/* ABYSS_WIN32 */
}

void ThreadClose( TThread *t )
{
#ifdef ABYSS_WIN32
    CloseHandle( *t );
#endif	/* ABYSS_WIN32 */
}



/*********************************************************************
** Mutex
*********************************************************************/

abyss_bool MutexCreate(TMutex *m)
{
#if defined(ABYSS_WIN32)
	return ((*m=CreateMutex(NULL,FALSE,NULL))!=NULL);
#elif _THREAD
	return (pthread_mutex_init(m, NULL)==0);
#else
	return TRUE;
#endif	
}

abyss_bool MutexLock(TMutex *m)
{
#if defined(ABYSS_WIN32)
	return (WaitForSingleObject(*m,INFINITE)!=WAIT_TIMEOUT);
#elif _THREAD
	return (pthread_mutex_lock(m)==0);
#else
	return TRUE;
#endif
}

abyss_bool MutexUnlock(TMutex *m)
{
#if defined(ABYSS_WIN32)
	return ReleaseMutex(*m);
#elif _THREAD
	return (pthread_mutex_unlock(m)==0);
#else
	return TRUE;
#endif
}

abyss_bool MutexTryLock(TMutex *m)
{
#if defined(ABYSS_WIN32)
	return (WaitForSingleObject(*m,0)!=WAIT_TIMEOUT);
#elif _THREAD
	return (pthread_mutex_trylock(m)==0);
#else
	return TRUE;
#endif
}

void MutexFree(TMutex *m)
{
#if defined(ABYSS_WIN32)
	CloseHandle(*m);
#elif _THREAD
	pthread_mutex_destroy(m);
#else
	;
#endif
}

⌨️ 快捷键说明

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