コード例 #1
0
ファイル: ASync_Semaphore.cpp プロジェクト: achacha/AOS
ASync_Semaphore::ASync_Semaphore(
  const AString& strName,
  size_t maxCount,                    // = 1
  ASynchronization::eInitialState i   // = UNLOCKED
) :
  m_maxCount(maxCount),
  m_handle(NULL),
  mstr_Name(strName)
{
#if defined(__WINDOWS__)
  //a_First try to open a semaphore
  m_handle = ::OpenSemaphore(SEMAPHORE_ALL_ACCESS, TRUE, strName.c_str());

  //a_Create if it doesn't exist
  if (!m_handle)
    m_handle = ::CreateSemaphore(NULL, (i == UNLOCKED) ? maxCount : maxCount-1, maxCount, strName.c_str());

  if (!m_handle)
    ATHROW_LAST_OS_ERROR(this);
#elif defined(__LINUX__)
  //a_Open or create
  m_handle = sem_open(strName.c_str(), O_CREAT);

  //a_On failure throw
  if (!m_handle)
  {
      ATHROW_LAST_OS_ERROR(this);
  }
  else
    sem_init(m_handle, 0, maxCount);
#endif
}
コード例 #2
0
bool removeService()
{
  AFILE_TRACER_DEBUG_SCOPE("removeService", NULL);
  SC_HANDLE schManager = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  if (NULL == schManager)
  {
    AFILE_TRACER_DEBUG_MESSAGE("Unable to open service control manager", NULL);
    ATHROW_LAST_OS_ERROR(NULL);
  }

  AFILE_TRACER_DEBUG_MESSAGE((AString("Trying to open service: ")+AOS_SERVICE_NAME).c_str(), NULL);
  SC_HANDLE schService = ::OpenService(schManager, AOS_SERVICE_NAME, SERVICE_ALL_ACCESS);
  if (NULL == schService)
  {
    //a_Open service failed
    AFILE_TRACER_DEBUG_MESSAGE("Unable to open service", NULL);
    u4 lastOSError = ::GetLastError();
    closeService(schManager, schService);
    ATHROW_LAST_OS_ERROR_KNOWN_EX(NULL, lastOSError, AString("Unable to open service: ")+AOS_SERVICE_NAME);
  }
  AFILE_TRACER_DEBUG_MESSAGE((ASWNL("Service opened: ")+AOS_SERVICE_NAME).c_str(), NULL);

  if(schService && !DeleteService(schService))
  {
    //a_Unable to delete
    DWORD errorNumber = GetLastError();
    closeService(schManager, schService);
    ATHROW_LAST_OS_ERROR_KNOWN(NULL, errorNumber);
  }

  AFILE_TRACER_DEBUG_MESSAGE("Service removed.", NULL);
  closeService(schManager, schService);
  return 1;
}
コード例 #3
0
ファイル: ASync_Semaphore.cpp プロジェクト: achacha/AOS
void ASync_Semaphore::unlock()
{
#if defined(__WINDOWS__)
  if (m_handle)
  {
    if (!::ReleaseSemaphore( m_handle, 1, NULL ))
      ATHROW_LAST_OS_ERROR(this);
  }
  else
    ATHROW(this, AException::InvalidObject);
#elif defined(__LINUX__)
  if (m_handle)
  {
    if (sem_post(m_handle))
      ATHROW_LAST_OS_ERROR(this);
  }
  else
    ATHROW(this, AException::InvalidObject);
#endif
}
コード例 #4
0
ファイル: ASync_Mutex.cpp プロジェクト: achacha/AOS
ASync_Mutex::ASync_Mutex(
  u4 timeout,                         // = INFINITE
  ASynchronization::eInitialState i   // = UNLOCKED
):
  m_Timeout(timeout)
{
  ATextGenerator::generateUniqueId(mstr_Name);
  m_handle = ::CreateMutex(NULL, i == LOCKED, mstr_Name.c_str());
  if (!m_handle)
    ATHROW_LAST_OS_ERROR(this);
}
コード例 #5
0
ファイル: ASync_Event.cpp プロジェクト: achacha/AOS
ASync_Event::ASync_Event(
    u4 timeout,                        // = INFINITE
    bool autoReset,                    // = false
    ASynchronization::eInitialState i  // = UNLOCKED
):
    m_Timeout(timeout)
{
    ATextGenerator::generateUniqueId(mstr_Name, 32);
    m_handle = ::CreateEvent(NULL, autoReset, i == LOCKED, mstr_Name.c_str());
    if (!m_handle)
        ATHROW_LAST_OS_ERROR(this);
}
コード例 #6
0
ファイル: ASync_Mutex.cpp プロジェクト: achacha/AOS
void ASync_Mutex::lock()
{
  if (m_handle)
  {
  	if (WAIT_FAILED == ::WaitForSingleObject(m_handle, m_Timeout))
      ATHROW_LAST_OS_ERROR(this);
  }
  else
  {
    ATHROW(this, AException::InvalidObject);
  }
}
コード例 #7
0
ファイル: ASync_Mutex.cpp プロジェクト: achacha/AOS
ASync_Mutex::ASync_Mutex(
  const AString& strName, 
  u4 timeout,                         // = INFINITE
  ASynchronization::eInitialState i   // = UNLOCKED
):
  mstr_Name(strName),
  m_Timeout(timeout)
{
  m_handle = ::CreateMutex(NULL, i == LOCKED, strName.c_str());
  if (!m_handle)
    ATHROW_LAST_OS_ERROR(this);
}
コード例 #8
0
ASync_CriticalSectionSpinLock::ASync_CriticalSectionSpinLock(
  size_t spinCount,                    // = 4000
  ASynchronization::eInitialState i    // = UNLOCKED
) :
  mp_CriticalSection(new CRITICAL_SECTION),
  m_SpinCount(spinCount)
{
  if (!::InitializeCriticalSectionAndSpinCount(mp_CriticalSection, (DWORD)m_SpinCount))
    ATHROW_LAST_OS_ERROR(this);

  if (i==LOCKED) 
    lock();
}
コード例 #9
0
ファイル: ASync_Event.cpp プロジェクト: achacha/AOS
ASync_Event::ASync_Event(
    const AString& strName,
    u4 timeout,                        // = INFINITE
    bool autoReset,                    // = false
    ASynchronization::eInitialState i  // = UNLOCKED
):
    mstr_Name(strName),
    m_Timeout(timeout)
{
    m_handle = ::CreateEvent(NULL, autoReset, i == LOCKED, strName.c_str());
    if (!m_handle)
        ATHROW_LAST_OS_ERROR(this);
}
コード例 #10
0
ファイル: ASync_Semaphore.cpp プロジェクト: achacha/AOS
void ASync_Semaphore::lock()
{
#if defined(__WINDOWS__)
  if (m_handle)
  {
    if (WAIT_FAILED == ::WaitForSingleObjectEx( m_handle, INFINITE, TRUE ))
      ATHROW_LAST_OS_ERROR(this);
  }
  else
  {
    ATHROW(this, AException::InvalidObject);
  }
#elif defined(__LINUX__)
  if (m_handle)
  {
    if (sem_wait(m_handle))
      ATHROW_LAST_OS_ERROR(this);
  }
  else
  {
    ATHROW(this, AException::InvalidObject);
  }
#endif
}
コード例 #11
0
bool installService()
{
  AFILE_TRACER_DEBUG_SCOPE("installService", NULL);
  
  AFILE_TRACER_DEBUG_MESSAGE("Service connected to controller.", NULL);
  SC_HANDLE schManager = ::OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS | SC_MANAGER_CREATE_SERVICE);
  if(!schManager)
  {
    AFILE_TRACER_DEBUG_MESSAGE("Unable to open service manager", NULL);
    ATHROW_LAST_OS_ERROR(NULL);
  }

  //a_Now that the service manager is open, let's open the service
  char sPath[1024];   //a_Who knows, long filenames may be really long
	GetModuleFileName(GetModuleHandle(NULL), sPath, 1023);

  // "Normalize" the name and add the arguments.
  AString imagePath;
  if ( sPath[0] != '\"' )
  {
    imagePath  = '\"';
    imagePath += sPath;
    imagePath += '\"';
  }
  else
  {
    imagePath = sPath;
  }

  SC_HANDLE schService = ::CreateService(
    schManager,                        // SC_HANDLE hSCManager,      
    AOS_SERVICE_NAME,                  // LPCSTR lpServiceName,     
    AOS_SERVICE_NAME,                  // LPCSTR lpDisplayName,     
    SERVICE_ALL_ACCESS,                // DWORD dwDesiredAccess,     
    SERVICE_WIN32_OWN_PROCESS,         // DWORD dwServiceType,       
    SERVICE_AUTO_START,                // DWORD dwStartType,         
    SERVICE_ERROR_NORMAL,              // DWORD dwErrorControl,      
    TEXT(imagePath.c_str()),           // LPCSTR lpBinaryPathName,  
    NULL,                              // LPCSTR lpLoadOrderGroup,  
    NULL,                              // LPDWORD lpdwTagId,         
    NULL,                              // LPCSTR lpDependencies,    
    NULL,                              // LPCSTR lpServiceStartName,
    NULL                               // LPCSTR lpPassword         
  );

  if (NULL == schService)
  {
    AFILE_TRACER_DEBUG_MESSAGE("Unable to create service", NULL);
    u4 lastOSError = ::GetLastError();
    closeService(schManager, schService);
    ATHROW_LAST_OS_ERROR_KNOWN(NULL, lastOSError);
  }
  else
  {
    //a_Set service optional info after create
    SERVICE_DESCRIPTION sd;
    sd.lpDescription = AOS_SERVICE_DESC;
    if (!::ChangeServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, &sd))
      ATHROW_LAST_OS_ERROR(NULL);
  }
  closeService(schManager, schService);
  AFILE_TRACER_DEBUG_MESSAGE("Service installed.", NULL);

  return true;
}
コード例 #12
0
ファイル: ASync_Event.cpp プロジェクト: achacha/AOS
void ASync_Event::unlock()
{
    if (!::SetEvent(m_handle))
        ATHROW_LAST_OS_ERROR(this);
}
コード例 #13
0
ファイル: ASync_Mutex.cpp プロジェクト: achacha/AOS
void ASync_Mutex::unlock()
{
	if (!::ReleaseMutex(m_handle))
    ATHROW_LAST_OS_ERROR(this);
}