2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-03-31 09:36:00 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
typedef struct threadparam_s
|
|
|
|
{
|
2012-04-16 08:36:50 +02:00
|
|
|
ThreadProc callback;
|
2012-03-31 09:36:00 +02:00
|
|
|
void *param;
|
|
|
|
} threadparam_t;
|
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
/**
|
|
|
|
* ThreadStartProc
|
|
|
|
*
|
|
|
|
* Helper function that deals with OS-specific differences in the thread
|
|
|
|
* proc's function signature.
|
|
|
|
*/
|
2012-03-31 09:36:00 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
static DWORD WINAPI ThreadStartProc(LPVOID param)
|
|
|
|
{
|
|
|
|
threadparam_t *tparam = (threadparam_t *)param;
|
|
|
|
tparam->callback(tparam->param);
|
|
|
|
delete tparam;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* _WIN32 */
|
|
|
|
static void *ThreadStartProc(void *param)
|
|
|
|
{
|
|
|
|
threadparam_t *tparam = (threadparam_t *)param;
|
|
|
|
tparam->callback(tparam->param);
|
|
|
|
delete tparam;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
/**
|
|
|
|
* Thread
|
|
|
|
*
|
|
|
|
* Constructor for the thread class. Creates a new thread that begins
|
|
|
|
* executing immediately.
|
|
|
|
*/
|
2012-05-10 13:46:04 +02:00
|
|
|
Thread::Thread(ThreadProc callback, void *param)
|
2012-03-31 09:36:00 +02:00
|
|
|
{
|
|
|
|
threadparam_t *tparam = new threadparam_t();
|
|
|
|
|
|
|
|
if (tparam == NULL)
|
2012-04-22 16:45:31 +02:00
|
|
|
throw OutOfMemoryException("Out of memory");
|
2012-03-31 09:36:00 +02:00
|
|
|
|
2012-05-10 13:46:04 +02:00
|
|
|
tparam->callback = callback;
|
|
|
|
tparam->param = param;
|
|
|
|
|
2012-03-31 09:36:00 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
m_Thread = CreateThread(NULL, 0, ThreadStartProc, tparam, CREATE_SUSPENDED, NULL);
|
2012-04-22 16:45:31 +02:00
|
|
|
|
|
|
|
if (m_Thread == NULL)
|
|
|
|
throw Win32Exception("CreateThread failed.", GetLastError());
|
2012-03-31 09:36:00 +02:00
|
|
|
#else /* _WIN32 */
|
|
|
|
pthread_create(&m_Thread, NULL, ThreadStartProc, &tparam);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
/**
|
|
|
|
* ~Thread
|
|
|
|
*
|
|
|
|
* Destructor for the Thread class. Cleans up the resources associated
|
|
|
|
* with the thread.
|
|
|
|
*/
|
2012-04-16 08:36:50 +02:00
|
|
|
Thread::~Thread(void)
|
2012-03-31 09:36:00 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
CloseHandle(m_Thread);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
/* nothing to do here */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
/**
|
|
|
|
* Join
|
|
|
|
*
|
|
|
|
* Waits until the thread has finished executing.
|
|
|
|
*/
|
2012-04-16 08:36:50 +02:00
|
|
|
void Thread::Join(void)
|
2012-03-31 09:36:00 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
WaitForSingleObject(m_Thread, INFINITE);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
pthread_join(m_Thread, NULL);
|
|
|
|
#endif
|
2012-04-02 10:26:38 +02:00
|
|
|
}
|