thread.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef _THREAD_H
  2. #define _THREAD_H
  3. #include <bfc/common.h>
  4. #if !defined(WIN32) && !defined(LINUX)
  5. #error port me!
  6. #endif
  7. #ifdef WIN32
  8. #define THREADCALL DWORD WINAPI
  9. #endif
  10. #ifdef LINUX
  11. #define THREADCALL void *
  12. #endif
  13. /**
  14. class Thread is a portable wrapper for an operating system thread. A thread
  15. is a thread of execution in a process. Ask Google if you don't know what that
  16. means.
  17. To use this class, derive from Thread and override the threadProc() method.
  18. Simply return from this method and the thread will end.
  19. Our Thread works on the kill-switch concept. You should write your thread
  20. code to continually check the getKillSwitch() method to determine if you
  21. should exit. When either getKillSwitch() returns TRUE or your processing is
  22. done, just return from the method and the thread will end.
  23. @short Thread object.
  24. @author Nullsoft
  25. @see threadProc()
  26. @see getKillSwitch()
  27. @see CriticalSection
  28. */
  29. class Thread {
  30. public:
  31. /**
  32. Creates a new Thread object. You can optionally pass in the handle of another
  33. thread. If you do, the getKillSwitch function will check if this thread is
  34. still running, and return TRUE if it has died. Usually this other thread
  35. will be the main thread.
  36. Threads are created in a suspended state. To start them, call start().
  37. @see threadProc()
  38. @see getKillSwitch()
  39. @see ComponentAPI::main_getMainThreadHandle()
  40. @see start()
  41. */
  42. Thread(HANDLE parent_handle=0);
  43. virtual ~Thread();
  44. /**
  45. Starts the thread. You must call this at some point after creating the Thread.
  46. @ret Boolean. TRUE on success, FALSE on failure.
  47. */
  48. int start();
  49. /**
  50. Checks whether the thread is still running.
  51. @ret Boolean. TRUE if thread is still running, FALSE if it has ended.
  52. */
  53. bool running();
  54. /**
  55. Waits until thread ends. Does not kill the thread, though, so you might want
  56. to call setKillSwitch(TRUE) to encourage the thread to end.
  57. @see setKillSwitch()
  58. */
  59. int end(); // waits for thread to end, might want to throw killswitch too
  60. int kill(); // does not wait, simply kills the thread, use with care
  61. /**
  62. Sets the internal kill switch to k.
  63. @param k Boolean. TRUE to force getKillSwitch() to return TRUE.
  64. @see getKillSwitch()
  65. */
  66. void setKillSwitch(int k);
  67. THREADID getThreadId() const;
  68. /**
  69. Sets the parent handle. If a parent handle is set, the getKillSwitch()
  70. function will return TRUE when the parent thread ends.
  71. @param parent_handle The handle of the thread to monitor.
  72. @param auto_close If TRUE, the parent handle will be closed on shutdown. Normally this is the desired behavior.
  73. */
  74. void setParentHandle(HANDLE parent_handle, int auto_close=TRUE);
  75. /**
  76. Checks if the parent thread is still running (if a handle was provided.)
  77. @see Thread()
  78. @see getKillSwitch()
  79. @ret Boolean, TRUE if parent is still running, FALSE otherwise.
  80. */
  81. int parentRunning(); // if parent handle given, is it still running
  82. /**
  83. Override this method to do your thread's work.
  84. */
  85. virtual int threadProc()=0;
  86. enum { // these have to match std.cpp
  87. PRIORITY_IDLE=-32767,
  88. PRIORITY_LOWEST=-2,
  89. PRIORITY_BELOW_NORMAL=-1,
  90. PRIORITY_NORMAL=0,
  91. PRIORITY_HIGH=1,
  92. PRIORITY_HIGHEST=2,
  93. PRIORITY_TIME_CRITICAL=32767,
  94. };
  95. /**
  96. Sets the thread's processing priority.
  97. @param priority The new priority for the thread.
  98. @see Std::setThreadPriority()
  99. */
  100. void setPriority(int priority);
  101. protected:
  102. /**
  103. Your thread should call this at intervals to determine if it should exit.
  104. @ret Boolean of whether the thread should exit.
  105. @see threadProc()
  106. */
  107. int getKillSwitch(); // check this and return when it's nonzero
  108. private:
  109. static THREADCALL ThreadProc(void *param);
  110. int killswitch;
  111. HANDLE handle, parent_handle;
  112. int auto_close_parent;
  113. THREADID threadid;
  114. bool isrunning;
  115. int nicekill;
  116. };
  117. #endif