/*
 * Copyright (C) 2008 The Trustees of Columbia University in the City of New York. 
 * All rights reserved.
 *
 * See the file LICENSE included in this distribution for details.
 */


#include "threadi.h"

#ifdef _LINUX_
#include <pthread.h>
#endif

#ifdef WIN32
#include <winsock.h>
#endif


int32 CreateThreadI(void *(*start_routine)(void*), void *arg) {

#ifdef _LINUX_
  pthread_t tid;
  pthread_create(&tid, NULL, start_routine, arg);
  return (int32)tid;
#endif

#ifdef WIN32
  DWORD tid;
  HANDLE t_h;
  t_h = CreateThread( 0, 0, (LPTHREAD_START_ROUTINE)start_routine, arg, 0, &tid);
  return (int32)t_h;
#endif

  return 1;
}//CreateThreadI



int32 WaitForThread(int32 iThread) {

#ifdef _LINUX_
  pthread_join((pthread_t)iThread, NULL);
#endif

#ifdef WIN32  
  WaitForMultipleObjects( 1, (HANDLE*)&iThread, TRUE, INFINITE);
#endif

  return 1;
}//WaitForThread



int32 ExitThread(int32 iThread) {

#ifdef _LINUX_
  pthread_exit(NULL);
#endif

#ifdef WIN32

#endif

  return 1;
}//ExitThread


int32 KillThread(int32 iThread) {

#ifdef _LINUX_
  //pthread_kill((pthread_t)iThread,0);
#endif
  
  return 1;
}//KillThread


int32 SuspendThread(int32 iThread) {

#ifdef _LINUX_
  //  pthread_suspend((pthread_t)iThread);
#endif
  
  return 1;
}//SuspendThread


int32 ResumeThread(int32 iThread) {

#ifdef _LINUX_
  //  pthread_resume((pthread_t)iThread);
#endif
  
  return 1;
}//ResumeThread


uint32 AllocMutexI(struct mutex_i& hMutex) {

#ifdef WIN32
  hMutex.uiMutex=CreateMutex(NULL, FALSE, NULL);
  if(NULL == hMutex.uiMutex) {
    return 0;
  }
#endif

  //we allocate the mutex successfully
  return 1;
}//CreateMutex


uint32 LockMutexI(struct mutex_i& hMutex) {

  uint32 ret=0;

#ifdef WIN32
  ret=WaitForSingleObject( hMutex.uiMutex, INFINITE );
#endif

#ifdef _LINUX_
#endif

  return ret;
}//LockMutex


uint32 UnlockMutexI(struct mutex_i& hMutex) {
  uint32 ret=0;

#ifdef WIN32
  ret=ReleaseMutex( hMutex.uiMutex);
#endif

#ifdef _LINUX_
#endif

  return ret;
}//UnlockMutexI


uint32 DeallocMutexI(struct mutex_i& hMutex) {
  uint32 ret=0;

#ifdef WIN32
  ret=CloseHandle(hMutex.uiMutex);
#endif

#ifdef _LINUX_
#endif

  return ret;
}//DeallocMutexI

