threads.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Copyright (c) 2006-2014 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_THREAD_THREADS_H
  21. #define LOVE_THREAD_THREADS_H
  22. // LOVE
  23. #include "common/config.h"
  24. #include "Thread.h"
  25. // C++
  26. #include <string>
  27. namespace love
  28. {
  29. namespace thread
  30. {
  31. class Mutex
  32. {
  33. public:
  34. virtual ~Mutex() {}
  35. virtual void lock() = 0;
  36. virtual void unlock() = 0;
  37. };
  38. class Conditional
  39. {
  40. public:
  41. virtual ~Conditional() {}
  42. virtual void signal() = 0;
  43. virtual void broadcast() = 0;
  44. virtual bool wait(Mutex *mutex, int timeout=-1) = 0;
  45. };
  46. class Lock
  47. {
  48. public:
  49. Lock(Mutex *m);
  50. Lock(Mutex &m);
  51. ~Lock();
  52. private:
  53. Mutex *mutex;
  54. };
  55. class EmptyLock
  56. {
  57. public:
  58. EmptyLock();
  59. ~EmptyLock();
  60. void setLock(Mutex *m);
  61. void setLock(Mutex &m);
  62. private:
  63. Mutex *mutex;
  64. };
  65. class Threadable
  66. {
  67. public:
  68. Threadable();
  69. virtual ~Threadable();
  70. virtual void threadFunction() = 0;
  71. bool start();
  72. void wait();
  73. bool isRunning() const;
  74. const char *getThreadName() const;
  75. protected:
  76. Thread *owner;
  77. std::string threadName;
  78. };
  79. Mutex *newMutex();
  80. Conditional *newConditional();
  81. Thread *newThread(Threadable *t);
  82. } // thread
  83. } // love
  84. #endif /* LOVE_THREAD_THREADS_H */