Ejemplo n.º 1
0
bool FHelperThread::LaunchThread ()
{
	int i;

	MessageHead = MessageTail = 0;
	for (i = 0; i < MSG_QUEUE_SIZE; i++)
	{
		if ((Messages[i].CompletionEvent = CreateEvent (NULL, FALSE, i > 0, NULL)) == NULL)
			break;
	}
	if (i < MSG_QUEUE_SIZE)
	{
		for (; i >= 0; i--)
		{
			CloseHandle (Messages[i].CompletionEvent);
		}
		return false;
	}
	InitializeCriticalSection (&Thread_Critical);
	if ((Thread_Events[0] = CreateEvent (NULL, TRUE, FALSE, NULL)))
	{
		if ((Thread_Events[1] = CreateEvent (NULL, TRUE, FALSE, NULL)))
		{
			if ((ThreadHandle = CreateThread (NULL, 0, ThreadLaunch, (LPVOID)this, 0, &ThreadID)))
			{
				HANDLE waiters[2] = { Messages[0].CompletionEvent, ThreadHandle };

				if (WaitForMultipleObjects (2, waiters, FALSE, INFINITE) == WAIT_OBJECT_0+1)
				{ // Init failed, and the thread exited
					DestroyThread ();
					return false;
				}
#if defined(_MSC_VER) && defined(_DEBUG)
				// Give the thread a name in the debugger
				struct
				{
					DWORD type;
					LPCSTR name;
					DWORD threadID;
					DWORD flags;
				} info =
				{
					0x1000, ThreadName(), ThreadID, 0
				};
				__try
				{
					RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD), (ULONG_PTR *)&info );
				}
				__except(EXCEPTION_CONTINUE_EXECUTION)
				{
				}
#endif

				return true;
			}
			CloseHandle (Thread_Events[1]);
		}
		CloseHandle (Thread_Events[0]);
	}
Ejemplo n.º 2
0
void __cdecl ThreadFunc(void *p)
{
	core_ForkThreadParam *ftp = (core_ForkThreadParam*)p;

	lua_rawgeti(ftp->L, LUA_REGISTRYINDEX, ftp->functionRef);
	luaM_pcall(ftp->L, 0, 1);
	DestroyThread(ftp);
}
Ejemplo n.º 3
0
CNetWatcher::~CNetWatcher ()
{
    DestroyThread ();

    delete [] (unsigned char * ) m_list;

    DeleteCriticalSection(&m_cs);    
}
Ejemplo n.º 4
0
static int core_TerminateThread(lua_State *L)
{
	HANDLE hThread = (HANDLE)(intptr_t)luaL_checknumber(L, 1);
	auto it = lstThreads.find(hThread);
	if (it != lstThreads.end())
	{
		DestroyThread(it->second);
		lua_pushboolean(L, TerminateThread(hThread, 0));
	}
	else lua_pushboolean(L, 0);
	return 1;
}
Ejemplo n.º 5
0
void gxThread::RebuildThreadPool(thrPool *thread_pool)
// Remove all the threads in the pool that have exited
// or have been canceled.
{
  if(!thread_pool) return; // The pool pointer has not been initialized
  
  // Start rebuilding at the top of the pool to allow the lower
  // prioity threads to finish executing.
  thrPoolNode *next = thread_pool->GetHead();

  thrPoolNode *curr;
  gxThread_t *thread;
  gxThreadState state ;

  while(next) {
    state = next->GetThreadPtr()->GetThreadState();
    
    if((state == gxTHREAD_STATE_CANCELED) || (state == gxTHREAD_STATE_EXITED))
      {
	curr = next;
	if(next->GetNext() == 0) { // This is the last node in the pool
	  thread = thread_pool->RemoveNode(curr);
	  if(thread) DestroyThread(thread, 0);
	  thread_pool->MakeEmpty(); // The pool is now empty
	  return;
	}
	else { // We are somewhere in the middle of the list
	  next = next->GetNext();
	  thread = thread_pool->RemoveNode(curr);
	  if(thread) DestroyThread(thread, 0);
	  continue;
	}
      }
    
    next = next->GetNext();    
  }

  return;
}
Ejemplo n.º 6
0
void CNetWatcher::SetCallback ( CKAHFW::NETWORKCHANGEPROC proc, void *aux )
{
	//log.WriteFormat (_T("[CNetWatcher] SetCallback begin"), PEL_INFO);

    DestroyThread ();

    if (proc)
    {
        InitStatus ();

        m_callback = proc;
        m_aux = aux;

        CreateThread ();
    }
	//log.WriteFormat (_T("[CNetWatcher] SetCallback end"), PEL_INFO);
}
Ejemplo n.º 7
0
WELS_THREAD_ERROR_CODE CWelsThreadPool::Uninit() {
  WELS_THREAD_ERROR_CODE iReturn = WELS_THREAD_ERROR_OK;
  CWelsAutoLock  cLock (m_cLockPool);

  iReturn = StopAllRunning();
  if (WELS_THREAD_ERROR_OK != iReturn) {
    return iReturn;
  }

  m_cLockIdleTasks.Lock();
  while (m_cIdleThreads->size() > 0) {
    DestroyThread (m_cIdleThreads->begin());
    m_cIdleThreads->pop_front();
  }
  m_cLockIdleTasks.Unlock();

  Kill();

  WELS_DELETE_OP(m_cWaitedTasks);
  WELS_DELETE_OP(m_cIdleThreads);
  WELS_DELETE_OP(m_cBusyThreads);

  return iReturn;
}
Ejemplo n.º 8
0
void CompositorParent::ShutDown()
{
  DestroyThread();
  DestroyCompositorMap();
}
Ejemplo n.º 9
0
CThread::~CThread()
{
	DestroyThread();
	CloseHandle(m_hObject);
	delete m_pCriticalSection;
}
Ejemplo n.º 10
0
void threadlib_destroy_sem(HSEM *e)
{
	DestroyThread(*e);
}
Ejemplo n.º 11
0
FHelperThread::~FHelperThread ()
{
	DestroyThread ();
}
Ejemplo n.º 12
0
int gxThread::DestroyThreadPool(thrPool *thread_pool, int check_state)
// Destroy a previously created thread pool. If the "check_state" variable
// is true the current state of each thread will be checked before it is
// destroyed. If a thread cannot be canceled and the "check_state" variable
// is true, this function will return a non-zero value.
{
  if(!thread_pool) return 1;

  thrPoolNode *ptr;
  gxThread_t *thread = 0;
  gxThreadType type;
  gxThreadState state;
  
  while(!thread_pool->IsEmpty()) {
    // Start checking at the tail since the higher priority threads
    // at the head of the pool would most likely have exited first.
    ptr = thread_pool->GetTail();
    state = ptr->GetThreadPtr()->GetThreadState();
    type = ptr->GetThreadPtr()->GetThreadType();
    switch(state) {
      case gxTHREAD_STATE_INVALID :
	// We don't know what state this thread is in so lets delete it
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      case gxTHREAD_STATE_CANCELED :
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      case gxTHREAD_STATE_EXITED :
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      case gxTHREAD_STATE_NEW :
	// Newly created thread that was never executed
	// Lets assume that is not intended to be executed
	// at this point.
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      case gxTHREAD_STATE_RUNNING :
	if((check_state) && (type != gxTHREAD_TYPE_DETACHED)) {
	  // PC-lint 09/14/2005: thread may not be initalized 
	  thread = ptr->GetThreadPtr();
	  if(thread) {
	    if(CancelThread(thread) != 0) return 1;
	  }
	}
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      case gxTHREAD_STATE_SUSPENDED :
	if((check_state) && (type != gxTHREAD_TYPE_DETACHED)) {
	  // PC-lint 09/14/2005: thread may not be initalized 
	  thread = ptr->GetThreadPtr();
	  if(thread) {
	    if(CancelThread(thread) != 0) return 1;
	  }
	}
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      case gxTHREAD_STATE_WAITING :
	if((check_state) && (type != gxTHREAD_TYPE_DETACHED)) {
	  // PC-lint 09/14/2005: thread may not be initalized 
	  thread = ptr->GetThreadPtr();
	  if(thread) {
	    if(CancelThread(thread) != 0) return 1;
	  }
	}
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      default:
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
    }
  }

  delete thread_pool;
  thread_pool = 0;
  return 0;
}