/extlibs/SFML/src/SFML/System/Thread.cpp

https://bitbucket.org/hugoruscitti/pilascpp · C++ · 110 lines · 53 code · 24 blank · 33 comment · 3 complexity · 1dd8acf76b371ee5bfae00d6a69790ed MD5 · raw file

  1. ////////////////////////////////////////////////////////////
  2. //
  3. // SFML - Simple and Fast Multimedia Library
  4. // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
  5. //
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it freely,
  11. // subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented;
  14. // you must not claim that you wrote the original software.
  15. // If you use this software in a product, an acknowledgment
  16. // in the product documentation would be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such,
  19. // and must not be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //
  23. ////////////////////////////////////////////////////////////
  24. ////////////////////////////////////////////////////////////
  25. // Headers
  26. ////////////////////////////////////////////////////////////
  27. #include <SFML/System/Thread.hpp>
  28. #if defined(SFML_SYSTEM_WINDOWS)
  29. #include <SFML/System/Win32/ThreadImpl.hpp>
  30. #else
  31. #include <SFML/System/Unix/ThreadImpl.hpp>
  32. #endif
  33. namespace sf
  34. {
  35. ////////////////////////////////////////////////////////////
  36. Thread::Thread() :
  37. myThreadImpl(NULL),
  38. myFunction (NULL),
  39. myUserData (NULL)
  40. {
  41. }
  42. ////////////////////////////////////////////////////////////
  43. Thread::Thread(Thread::FuncType function, void* userData) :
  44. myThreadImpl(NULL),
  45. myFunction (function),
  46. myUserData (userData)
  47. {
  48. }
  49. ////////////////////////////////////////////////////////////
  50. Thread::~Thread()
  51. {
  52. Wait();
  53. }
  54. ////////////////////////////////////////////////////////////
  55. void Thread::Launch()
  56. {
  57. Wait();
  58. myThreadImpl = new priv::ThreadImpl(this);
  59. }
  60. ////////////////////////////////////////////////////////////
  61. void Thread::Wait()
  62. {
  63. if (myThreadImpl)
  64. {
  65. myThreadImpl->Wait();
  66. delete myThreadImpl;
  67. myThreadImpl = NULL;
  68. }
  69. }
  70. ////////////////////////////////////////////////////////////
  71. void Thread::Terminate()
  72. {
  73. if (myThreadImpl)
  74. {
  75. myThreadImpl->Terminate();
  76. delete myThreadImpl;
  77. myThreadImpl = NULL;
  78. }
  79. }
  80. ////////////////////////////////////////////////////////////
  81. void Thread::Run()
  82. {
  83. if (myFunction)
  84. myFunction(myUserData);
  85. }
  86. } // namespace sf