threads.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. #include "threads/threads.hh"
  9. #include "error/error.hh"
  10. #include <windows.h>
  11. #include <process.h>
  12. static w32 i4_thread_count=0;
  13. static i4_critical_section_class i4_thread_lock;
  14. int i4_main_thread_id;
  15. void i4_wait_threads() // waits for all threads to terminate (don't call from a thread!)
  16. {
  17. while (i4_thread_count!=0)
  18. i4_thread_yield();
  19. }
  20. static i4_critical_section_class i4_thread_start_lock;
  21. static i4_thread_func_type i4_thread_to_start;
  22. void i4_thread_starter(void *arg)
  23. {
  24. i4_thread_func_type start=i4_thread_to_start;
  25. i4_thread_start_lock.unlock();
  26. start(arg);
  27. i4_thread_lock.lock();
  28. i4_thread_count--;
  29. i4_thread_lock.unlock();
  30. _endthread();
  31. }
  32. void i4_add_thread(i4_thread_func_type fun, w32 stack_size, void *arg_list)
  33. {
  34. i4_thread_start_lock.lock();
  35. i4_thread_to_start=fun;
  36. i4_thread_lock.lock();
  37. i4_thread_count++;
  38. i4_thread_lock.unlock();
  39. _beginthread(i4_thread_starter, stack_size, arg_list);
  40. }
  41. void i4_thread_yield()
  42. {
  43. Sleep(0);
  44. }
  45. int i4_get_thread_id()
  46. {
  47. return GetCurrentThreadId();
  48. }