event.c

来自「用c/c++实现的一个CMPP API」· C语言 代码 · 共 262 行

C
262
字号
/************************************************************************ * Program ID: event.c                                                  * * Description:                                                         * *          1、                                                         * * Functions:                                                           * *          int     nInitEvent(recEvent *rEvent)                        * *          int     nWaitEvent(recEvent *rEvent)                        * *          int     nSetEvent(recEvent *rEvent)                         * *          int     nDestroyEvent(recEvent *rEvent)                     * ************************************************************************ * Modification Log:                                                    * *  Date                Author      Description                         * *  2002/03/02          tangwn                                          * ************************************************************************/#include "event.h"#include "log.h"/************************************************************************ * Function ID: nInitEvent                                              * * Description:                                                         * *          1、初始化mutex句柄                                          * *          2、初始化cond句柄                                               * * Input Param:                                                         * *          recEvent    *rEvent                                         * * Output Param:                                                        * *          无                                                          * * Return:                                                              * *          0           - 成功                                          * *          -1          - 失败                                          * ************************************************************************/#ifdef WIN32int nInitEvent(recEvent *rEvent){    if (rEvent == NULL)        return EVENT_FAIL;	rEvent->cond_h = CreateEvent(NULL,TRUE,FALSE,NULL);	if(rEvent->cond_h == NULL)        return EVENT_FAIL;    return EVENT_OK;}#endif#if defined(_HPUX_SOURCE) || defined(_LINUX_SOURCE)int nInitEvent(recEvent *rEvent){    if (rEvent == NULL)        return EVENT_FAIL;    if (pthread_mutex_init(&rEvent->mutex_h, NULL))        return EVENT_FAIL;    if (pthread_cond_init(&rEvent->cond_h, NULL))        return EVENT_FAIL;    rEvent->pbSended  = malloc( sizeof( int  ) );    *(rEvent->pbSended) = FALSE;    return EVENT_OK;}#endif/************************************************************************ * Function ID: nWaitEvent                                              * * Description:                                                         * *          1、                                                         * * Input Param:                                                         * *          recEvent    *rEvent                                         * * Output Param:                                                        * *          无                                                          * * Return:                                                              * *          0           - 成功                                          * *          -1          - 失败                                          * ************************************************************************/#ifdef WIN32int nWaitEvent(recEvent *rEvent){    if (rEvent == NULL)        return EVENT_FAIL;    switch(WaitForSingleObject(rEvent->cond_h, INFINITE))	{		case WAIT_FAILED:			return -1;		case WAIT_TIMEOUT:			return 1;	}	ResetEvent(rEvent->cond_h);        return EVENT_OK;}#endif#if defined(_HPUX_SOURCE) || defined(_LINUX_SOURCE)int nWaitEvent(recEvent *rEvent){    if (rEvent == NULL)        return EVENT_FAIL;        pthread_mutex_lock(&rEvent->mutex_h);    if( *(rEvent->pbSended) == TRUE )    //发送线程发出事件    {        *(rEvent->pbSended ) = FALSE;        goto OUTEXIT;         }     pthread_cond_wait(&rEvent->cond_h, &rEvent->mutex_h);            OUTEXIT:        pthread_mutex_unlock(&rEvent->mutex_h);            return EVENT_OK;}#endif/*********************************************************************** * Function ID: nTimedWaitEvent            * Description:                *   1、                 * Input Param:                *   recEvent *rEvent            * Output Param:               *   无                * Return:                 *      0   - 成功            *          1           - 超时 *      -1   - 失败            **********************************************************************/#ifdef WIN32int nTimedWaitEvent(recEvent *rEvent, int nWaitTime){    int ret = 0;            switch(WaitForSingleObject(rEvent->cond_h, nWaitTime*1000))	{		case WAIT_FAILED:			return -1;		case WAIT_TIMEOUT:			return 1;	}	ResetEvent(rEvent->cond_h);    return 0;}#endif#if defined(_HPUX_SOURCE) || defined(_LINUX_SOURCE)int nTimedWaitEvent(recEvent *rEvent, int nWaitTime){    int ret = 0;    struct timespec abstime;        abstime.tv_sec = time((time_t*)0)+nWaitTime;   /* seconds */    abstime.tv_nsec = 0;                /* and nanoseconds */            if (rEvent == NULL)    return EVENT_FAIL;        pthread_mutex_lock(&rEvent->mutex_h);    if( *(rEvent->pbSended) == TRUE)    //发送线程发出事件    {        //Trace("YYYYYYY   %d\n",*rEvent->pbSended);        *(rEvent->pbSended) = FALSE;        goto OUTEXIT;         }     ret = pthread_cond_timedwait(&rEvent->cond_h, &rEvent->mutex_h, &abstime);    if (ret == ETIMEDOUT) {        ret = EVENT_TIMEOUT;    }OUTEXIT:        pthread_mutex_unlock(&rEvent->mutex_h);    return ret;}#endif /************************************************************************ * Function ID: nSetEvent                                               * * Description:                                                         * *          1、                                                         * * Input Param:                                                         * *          recEvent    *rEvent                                         * * Output Param:                                                        * *          无                                                          * * Return:                                                              * *          0           - 成功                                          * *          -1          - 失败                                          * ************************************************************************/#ifdef WIN32int nSetEvent(recEvent *rEvent){    if (rEvent == NULL)        return EVENT_FAIL;    if(!SetEvent(rEvent->cond_h))		return EVENT_FAIL;    return EVENT_OK;}#endif#if defined(_HPUX_SOURCE) || defined(_LINUX_SOURCE)int nSetEvent(recEvent *rEvent){    int ret;    if (rEvent == NULL)        return EVENT_FAIL;    pthread_mutex_lock(&rEvent->mutex_h);    if( rEvent->pbSended != NULL )    {        *(rEvent->pbSended)=TRUE;    //发送线程已经发出事件    }        ret = pthread_cond_signal(&rEvent->cond_h);        pthread_mutex_unlock(&rEvent->mutex_h);    return EVENT_OK;}#endif/************************************************************************ * Function ID: nDestroyEvent                                           * * Description:                                                         * *          1、                                                         * * Input Param:                                                         * *          recEvent    *rEvent                                         * * Output Param:                                                        * *          无                                                          * * Return:                                                              * *          0           - 成功                                          * *          -1          - 失败                                          * ************************************************************************/#ifdef WIN32int nDestroyEvent(recEvent *rEvent){    if (rEvent == NULL)        return EVENT_FAIL;    if(!CloseHandle(rEvent->cond_h))		return EVENT_FAIL;    return EVENT_OK;}#endif#if defined(_HPUX_SOURCE) || defined(_LINUX_SOURCE)int nDestroyEvent(recEvent *rEvent){    if (rEvent == NULL)        return EVENT_FAIL;    pthread_cond_destroy(&rEvent->cond_h);    pthread_mutex_destroy(&rEvent->mutex_h);    free( rEvent->pbSended );    rEvent->pbSended = NULL;    return EVENT_OK;}#endif

⌨️ 快捷键说明

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