thread.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "core/platform.h"
  7. #include "core/thread/semaphore.h"
  8. #include "core/types.h"
  9. #if CROWN_PLATFORM_POSIX
  10. #include <pthread.h>
  11. #elif CROWN_PLATFORM_WINDOWS
  12. #ifndef WIN32_LEAN_AND_MEAN
  13. #define WIN32_LEAN_AND_MEAN
  14. #endif
  15. #include <windows.h>
  16. #include <process.h>
  17. #endif
  18. /// @defgroup Thread Thread
  19. /// @ingroup Core
  20. namespace crown
  21. {
  22. /// Thread.
  23. ///
  24. /// @ingroup Thread.
  25. struct Thread
  26. {
  27. typedef s32 (*ThreadFunction)(void* data);
  28. ThreadFunction _function;
  29. void* _user_data;
  30. Semaphore _sem;
  31. bool _is_running;
  32. #if CROWN_PLATFORM_POSIX
  33. pthread_t _handle;
  34. #elif CROWN_PLATFORM_WINDOWS
  35. HANDLE _handle;
  36. #endif
  37. Thread();
  38. ~Thread();
  39. Thread(const Thread&) = delete;
  40. Thread& operator=(const Thread&) = delete;
  41. ///
  42. void start(ThreadFunction func, void* user_data = NULL, u32 stack_size = 0);
  43. ///
  44. void stop();
  45. ///
  46. bool is_running();
  47. /// Do not call explicitly.
  48. s32 run();
  49. };
  50. } // namespace crown