Exemple #1
0
HANDLE WINAPI CreateThread(
      LPSECURITY_ATTRIBUTES lpThreadAttributes,
        SIZE_T dwStackSize,
          LPTHREAD_START_ROUTINE lpStartAddress,
            LPVOID lpParameter,
        DWORD dwCreationFlags,
          LPDWORD lpThreadId
    ) {

        // a thread handle would actually contain an event
        // the event would mark if the thread is running or not. it will be used in the Wait functions.
  // DO NOT use SDL_WaitThread for waiting. it will delete the thread object.
  HANDLE h = CreateEvent(NULL, TRUE, FALSE, NULL);
  h->ChangeType(CXHandle::HND_THREAD);
  InternalThreadParam *pParam = new InternalThreadParam;
  pParam->threadFunc = lpStartAddress;
  pParam->data = lpParameter;
  pParam->handle = h;

  h->m_nRefCount++;
  h->m_hThread = SDL_CreateThread(InternalThreadFunc, (void*)pParam);
  if (lpThreadId)
    *lpThreadId = SDL_GetThreadID(h->m_hThread);
  return h;

}
Exemple #2
0
HANDLE WINAPI CreateThread(
      LPSECURITY_ATTRIBUTES lpThreadAttributes,
        SIZE_T dwStackSize,
          LPTHREAD_START_ROUTINE lpStartAddress,
            LPVOID lpParameter,
        DWORD dwCreationFlags,
          LPDWORD lpThreadId
    ) {

  // a thread handle would actually contain an event
  // the event would mark if the thread is running or not. it will be used in the Wait functions.
  HANDLE h = CreateEvent(NULL, TRUE, FALSE, NULL);
  h->ChangeType(CXHandle::HND_THREAD);
#ifdef __APPLE__
  h->m_machThreadPort = MACH_PORT_NULL;
#endif
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  if (dwStackSize > PTHREAD_STACK_MIN)
    pthread_attr_setstacksize(&attr, dwStackSize);
  if (pthread_create(&(h->m_hThread), &attr, (void*(*)(void*))lpStartAddress, lpParameter) == 0)
    h->m_threadValid = true;
  else
  {
    CloseHandle(h);
    h = NULL;
  }
  pthread_attr_destroy(&attr);

  if (h && lpThreadId)
    // WARNING: This can truncate thread IDs on x86_64.
    *lpThreadId = (DWORD)h->m_hThread;
  return h;
}
bool InitializeRecursiveMutex(HANDLE hMutex, BOOL bInitialOwner) {
  if (!hMutex)
    return false;

  // we use semaphores instead of the mutex because in SDL we CANT wait for a mutex
  // to lock with timeout.
  hMutex->m_pSem    = new CSemaphore(bInitialOwner?0:1);
  hMutex->m_hMutex  = SDL_CreateMutex();
  hMutex->ChangeType(CXHandle::HND_MUTEX);

  if (bInitialOwner) {
    hMutex->OwningThread  = pthread_self();
    hMutex->RecursionCount  = 1;
  }

  return true;
}