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

📄 thrdfunc.c

📁 sybase数据库ct library的开发,使用了所以有函数
💻 C
字号:

/*
** Sybase Open Client Version 10.0
** Confidential property of Sybase, Inc.
** (c) Copyright Sybase, Inc. 1992, 1995
** All rights reserved.
*/
 
/*
**
**  thrdfunc.c
**
**  Thread and synchronization routines for NT threads. 
**
**
*/
 

#include 	<windows.h>
#include        <stdio.h>
#include 	<stdlib.h>
#include        <string.h>
#include        <errno.h>
#include        <process.h>
#include        <ctpublic.h>
 
#include        <thrdfunc.h>
 
#if USE_SCCSID
static char Sccsid[] = {"%Z% %M% %I% %G%"};
#endif /* USE_SCCSID */

/* 
** global mutex 
*/
CS_STATIC CS_VOID *Ex_global_mutex;

/*
** ex_create_mutex()
**
** Type of function:
**	USER API.
**
** Purpose:
**	Creates a mutex . 
**
** Returns:
**	CS_SUCCEED	If the mutex was created
**	CS_FAIL	The mutex failed.
**
** Side effects:
**
** History:
*/

/*ARGSUSED*/
CS_RETCODE 
ex_create_mutex(mutex)
CS_VOID **mutex;
{
	HANDLE *handle;

	if((handle = (HANDLE *)malloc(CS_SIZEOF(HANDLE))) == (HANDLE)NULL)
		return CS_FAIL;

	if((*handle = CreateMutex(NULL, FALSE, NULL)) == (HANDLE)NULL)
	{
		free(handle);
		return CS_FAIL;
	}

	*(HANDLE **)mutex = handle;

	return CS_SUCCEED;
}

/*
** ex_take_mutex()
**
** Type of function:
**	USER API.
**
** Purpose:
**	Waits for mutex control.  This function blocks until the thread
**	currently holding the mutex gives it up.  
**
** Returns:
**	CS_SUCCEED	If the thread has control of the mutex
**	CS_FAIL	If the control cannot be obtained, or an error.
**
** Side effects:
**
** History:
*/

/*ARGSUSED*/
CS_RETCODE 
ex_take_mutex(mutex)
CS_VOID *mutex;
{
	DWORD retcode;
	
	if((retcode =
		WaitForSingleObject(*(HANDLE *)mutex, INFINITE))
			!= WAIT_OBJECT_0)
	{
		return CS_FAIL ;
	}

	return CS_SUCCEED;
}

/*
** ex_release_mutex()
**
** Type of function:
**	USER API.
**
** Purpose:
**	Releases control of the mutex, so other threads can grab it.
**
** Returns:
**	CS_SUCCEED	If the thread gave up control
**	CS_FAIL	If the control cannot be released, or an error.
**
** Side effects:
**
** History:
*/

/*ARGSUSED*/
CS_RETCODE 
ex_release_mutex(mutex)
CS_VOID *mutex;
{
	if(ReleaseMutex(*(HANDLE *)mutex) != TRUE)
		return CS_FAIL;

	return CS_SUCCEED;
}

/*
** ex_delete_mutex
**
** Type of function:
**	USER API.
**
** Purpose:
**	This function deletes the mutex from the system.
**	Any threads waiting on this object will return failure after this.
**
** Returns:
**	CS_SUCCEED	If the mutex was successfully deleted
**	CS_FAIL		If the semaphore was successfully deleted.
**
** Side effects:
**
** History:
*/

/*ARGSUSED*/
CS_RETCODE 
ex_delete_mutex(mutex)
CS_VOID *mutex;
{
	if(CloseHandle(*(HANDLE *)mutex) != TRUE)
		return CS_FAIL;

	free(mutex);

	return CS_SUCCEED;
}

/*
** ex_create_sem()
**
** Type of function:
**      Internal USER API.
**
** Purpose:
**	Create a semaphore.
**
** Returns:
**      CS_SUCCEED     	If semaphore created successfully 
**      CS_FAIL         If there were errors. 
**
** Side effects:
**
** History:
*/
CS_RETCODE 	
ex_create_sem(sempp, count)
CS_VOID			**sempp;
CS_UINT			count;
{
	HANDLE		*handle;

	/* Initialize */
	*sempp = (CS_VOID *)NULL;

	if ((handle =  (HANDLE *)malloc(CS_SIZEOF(HANDLE))) == (HANDLE *)NULL)
	{
		return(CS_FAIL);
	}

	if ((*handle = CreateSemaphore(NULL, 0, count, NULL)) == (HANDLE)NULL)
	{
		free(handle);
		return(CS_FAIL);
	}

	*sempp = handle;
	return CS_SUCCEED;
}

/*
** ex_delete_sem()
**
** Type of function:
**      Internal USER API.
**
** Purpose:
**      Delete a semaphore.
**
** Returns:
**      CS_SUCCEED      Always.  
**
** Side effects:
**
** History:
*/
CS_RETCODE  
ex_delete_sem(semp)
CS_VOID	*semp;
{
	HANDLE handle;

	/* Initialize */
	handle = *((HANDLE *)semp);

	/* Get the real free function  and free the handle */
	free(semp);
	CloseHandle(handle);
	return CS_SUCCEED;
}

/*
** ex_release_sem()
**
** Type of function:
**      Internal USER API.
**
** Purpose:
**      Release the semaphore. Increments semaphore count internally and
**	wakes up if a process is waiting on this semaphore.
**
** Returns:
**      CS_SUCCEED      If semaphore released successfully
**      CS_FAIL         If there were errors.
**
** Side effects:
**
** History:
*/
CS_RETCODE 
ex_release_sem(semp)
CS_VOID	*semp;
{
	HANDLE handle;

	/* Initialize */
	handle = *((HANDLE *)semp);
	if (ReleaseSemaphore(handle, 1, NULL) != TRUE)
	{
		return CS_FAIL;
	}
	return CS_SUCCEED;
}

/*
** ex_waitfor_sem()
**
** Type of function:
**      Internal USER API.
**
** Purpose:
**     	Wait for the semaphore. 
**
** Returns:
**      CS_SUCCEED      If semaphore was obtained successfully. 
**      CS_FAIL         If there were errors.
**
** Side effects:
**
** History:
*/
CS_RETCODE 
ex_waitfor_sem(semp)
CS_VOID	*semp;
{
	HANDLE handle;
	DWORD retcode;

	/* Initialize */
	handle = *((HANDLE *)semp);

	if ((retcode = WaitForSingleObject(handle, INFINITE)) == WAIT_TIMEOUT)
	{
		return CS_TIMED_OUT;
	}
	else if (retcode == WAIT_OBJECT_0)
	{
		return CS_SUCCEED;
	}
	
	return CS_FAIL;
}



/*
** ex_detach_thread()
**
** Type of function:
**      Internal USER API.
**
** Purpose:
**	Detach thread internal resources. Thread object is not accessible
**	any more after this call is completed.
**
** Returns:
**      CS_SUCCEED      If thread resources are released successfully 
**      CS_FAIL         If there were errors.
**
** Side effects:
**
** History:
*/
CS_RETCODE  
ex_detach_thread(threadp)
CS_VOID	*threadp;
{
	HANDLE handle;

	/* Initialize */
	handle = *((HANDLE *)threadp);

	/* Get the real free function  and free the handle */
	free(threadp);

	return CS_SUCCEED;
}

/*
** ex_create_thread()
**
** Type of function:
**      Internal USER API.
**
** Purpose:
**	Create a thread with default stack size.
**
** Returns:
**      CS_SUCCEED      If thread was created successfully
**      CS_FAIL         If there were errors.
**
** Side effects:
**
** History:
*/
CS_RETCODE 
ex_create_thread(threadpp, startroutine, args)
CS_VOID         **threadpp;
CS_VOID         *startroutine;
CS_VOID         *args;
{
	HANDLE		*handle;
 
        /* Initialize */
        *threadpp = (CS_VOID *)NULL;
 
        if ((handle =  (HANDLE *)malloc(CS_SIZEOF(HANDLE))) == (HANDLE *)NULL)
        {
                return(CS_FAIL);
        }
 
	/*
	** start thread with default stack - On NT, this is same size as that 
	** of primary thread and grows as necessary
	*/ 
        if ((*handle = (HANDLE)_beginthread(startroutine, 0, (LPVOID)args)) 
				== (HANDLE)NULL)
        {
                free(handle);
                return(CS_FAIL);
        }
 
        *threadpp = handle;
        return CS_SUCCEED;
}

/*
** ex_yield()
**
** Type of function:
**      Internal USER API.
**
** Purpose:
**	Relinquish rest of cpu time slice to other threads.
**
** Returns:
**	None.
**
** Side effects:
**
** History:
*/
CS_VOID  
ex_yield()
{
	Sleep(0);
	return ;
}

/*
** ex_take_global_mutex()
**
**      Take global mutex
**
** Returns:
**      CS_SUCCEED 
**	CS_FAIL
**
** Side Effects:
**      None
*/
CS_RETCODE
ex_take_global_mutex()
{
	return(ex_take_mutex(Ex_global_mutex));
}
 
/*
** ex_release_global_mutex()
**
**      Release global mutex
**
** Returns:
**      CS_SUCCEED 
**      CS_FAIL
**
** Side Effects:
**      None
*/
CS_RETCODE
ex_release_global_mutex()
{
	return(ex_release_mutex(Ex_global_mutex));
}
 
/*
** ex_create_global_mutex()
**
**      Setup global mutex mechanism
**
** Returns:
**      CS_SUCCEED 
**      CS_FAIL
**
** Side Effects:
**      None
*/
CS_RETCODE
ex_create_global_mutex()
{
	if (Ex_global_mutex == (CS_VOID *)NULL)
	{
		return(ex_create_mutex(&Ex_global_mutex));
	}
	else
	{
		return CS_SUCCEED;
	}
}

/*
** ex_delete_global_mutex()
**
**      Delete global mutex
**
** Returns:
**      CS_SUCCEED 
**      CS_FAIL
**
** Side Effects:
**      None
*/
CS_RETCODE
ex_delete_global_mutex()
{
	CS_RETCODE ret;
	ret = ex_delete_mutex(Ex_global_mutex);
	Ex_global_mutex = (CS_VOID *)NULL;
	return ret;
}

⌨️ 快捷键说明

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