/****************************************************************************
** File name    :  HTThread.h
** Description  :  define server worker thread group
** Create date  :  2018.09.01
** Auther by    :  Liuyx
** Version info :  V1.0.01
** Copyrigth By:  xi'an huatek, Inc Co., Ltd
** Update record:  
** DATE         AUTHER   DESC 
** -------------------------------------------------------------------------               
** 2018.09.01   Liuyx    first build
****************************************************************************/
#include "HTGlobal.h"
#include "HTThread.h"

static const char *_FILE_ ="HTThread.cpp";

/********************************************************************
 * function  : 创建线程句柄
 * param in  : none
 * param out : None
 * return val: 0  -- successed
 *           : <0 -- failed
********************************************************************/
static int HTNewThreadHandle(pthread_t **pThreadHandle, int thread_count)
{
    *pThreadHandle = (pthread_t*)calloc(1, sizeof(pthread_t)*thread_count);
    if(*pThreadHandle == NULL) {
        vPrtLogMsg(LOG_ERROR, errno, "calloc for thread handle failed.");
        return -1;
    }
    memset(*pThreadHandle, 0xff, sizeof(pthread_t)*thread_count);
    return 0;
}
// 释放系统线程句柄
static int HTDelThreadHandle(pthread_t **pThreadHandle)
{
    if(*pThreadHandle) {
        free(*pThreadHandle);
        *pThreadHandle = NULL;
    }
    return 0;
}
// 释放句柄
void HTFreeThreadHandle(void)
{
    //HTDelThreadHandle(&thread_handle_recv);
    //HTDelThreadHandle(&thread_handle_parse);
    //HTDelThreadHandle(&thread_handle_setdb);
    //HTDelThreadHandle(&thread_handle_respone);
}

/********************************************************************
 * function  : 初始化线程句柄
 * param in  : none
 * param out : None
 * return val: 0  -- successed
 *           : <0 -- failed
********************************************************************/
int HTInitThreadHandle(void)
{
//     if(HTNewThreadHandle(&thread_handle_recv,g_TConfig.getRecvThreadNumber()) != RET_OK) {
//         HTFreeThreadHandle();
//         return RET_FAIL; 
//     }
//     if(HTNewThreadHandle(&thread_handle_parse,g_TConfig.getParseThreadNumber()) != RET_OK) {
//         HTFreeThreadHandle();
//         return RET_FAIL;
//     }
    //if(HTNewThreadHandle(&thread_handle_setdb,g_TConfig.getOprationThreadNumber()) != RET_OK) {
    //    HTFreeThreadHandle();
    //    return RET_FAIL;
    //}
//     if(HTNewThreadHandle(&thread_handle_respone,g_TConfig.getResponeThreadNumber()) != RET_OK) {
//         HTFreeThreadHandle();
//         return RET_FAIL;
//     }

    return 0;
}

/********************************************************************
 * function  : 创建链接政府监管平台主链路线程
 * param in  : none
 * param out : None
 * return val: 0  -- successed
 *           : <0 -- failed
********************************************************************/
int  ht_pthread_create_background(pthread_t *thread_handle, void *(*pfunc)(void *), void *data)
{
#ifdef _WIN32
    HANDLE  handle = NULL;
	handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)pfunc,(LPVOID)data, 0, thread_handle);
    if (!handle) {
		::CloseHandle(handle);
        return -1 ;
    }
#else
    if(pthread_create(thread_handle, NULL, pfunc, data) !=0 ) {
    	return -1 ;
    }
#endif
    return 0 ;
}

void pthread_testcancels(void)
{
#ifndef _WIN32
    pthread_testcancel();
#endif
}



