/**
@file         SprtLock.cpp
@brief        Sprt锁操作类,SprtLock类实现

@author      cxw
@version     版本编号:1.0 日期:2013-10-28 修订者:Long 修订内容:按照《C++编码规范》修改源文件
*/
#include "stdafx.h"
#include "SprtLock.h"

SprtLock::SprtLock(void)
{
    createLock = false;
    InitialLock();
}

SprtLock::~SprtLock()
{
#ifdef WIN32
    CloseHandle(m_hMutex);
#else
    pthread_mutex_destroy(&m_lock);
#endif
}

void SprtLock::InitialLock()
{
    if (!createLock)
    {
#ifdef WIN32
        m_hMutex = CreateMutex(NULL,FALSE,NULL);
#else
        pthread_mutex_init(&m_lock,NULL);
#endif
        createLock = true;
    }
}

int SprtLock::Lock()
{
    //return WaitForSingleObject(this->m_lock,INFINITE);
#ifdef WIN32
    return WaitForSingleObject(m_hMutex,INFINITE);
#else
    return pthread_mutex_lock(&m_lock);
#endif    
}

int SprtLock::Unlock()
{
    //return SetEvent(this->m_lock);
#ifdef WIN32
    return ReleaseMutex(m_hMutex);
#else
    return pthread_mutex_unlock(&m_lock);
#endif
}

//void SprtLock::InitialWaite()
//{
//    this->m_wait = CreateEvent(NULL, TRUE, FALSE, NULL);
//
//}
//
//int SprtLock::WaitProc()
//{
//    if (WaitForSingleObject(this->m_wait, 0) == WAIT_OBJECT_0)  // 这个KillClient在主线程里创建,用于控制线程退出
//    {
//        return Successful
//    }
//    else
//    {
//        return Failed
//    }
//}
//
//int SprtLock::UnWait()
//{
//    return SetEvent(this->m_wait);
//}