Thread.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Copyright (c) 2006-2010 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_SDL_THREAD_H
  21. #define LOVE_THREAD_SDL_THREAD_H
  22. // SDL
  23. #include <SDL_thread.h>
  24. #include <SDL_mutex.h>
  25. // STL
  26. #include <map>
  27. #include <string>
  28. // LOVE
  29. #include <thread/ThreadModule.h>
  30. #include <filesystem/File.h>
  31. #include <common/runtime.h>
  32. namespace love
  33. {
  34. namespace thread
  35. {
  36. namespace sdl
  37. {
  38. enum ThreadVariantType
  39. {
  40. UNKNOWN = 0,
  41. BOOLEAN,
  42. NUMBER,
  43. STRING,
  44. LUSERDATA,
  45. FUSERDATA
  46. };
  47. class ThreadVariant : public love::Object
  48. {
  49. public:
  50. ThreadVariant(bool boolean);
  51. ThreadVariant(double number);
  52. ThreadVariant(const char *string);
  53. ThreadVariant(void *userdata);
  54. ThreadVariant(Type udatatype, void *userdata);
  55. virtual ~ThreadVariant();
  56. ThreadVariantType type;
  57. union
  58. {
  59. bool boolean;
  60. double number;
  61. const char *string;
  62. void *userdata;
  63. } data;
  64. Type udatatype;
  65. bits flags;
  66. };
  67. class ThreadData
  68. {
  69. private:
  70. char *code;
  71. char *name;
  72. std::map<std::string, ThreadVariant*> shared;
  73. public:
  74. ThreadData(const char *name, const char *code, void *mutex, void *cond);
  75. ~ThreadData();
  76. const char *getCode();
  77. const char *getName();
  78. ThreadVariant* getValue(const std::string & name);
  79. void clearValue(const std::string & name);
  80. void setValue(const std::string & name, ThreadVariant *v);
  81. void *mutex;
  82. void *cond;
  83. };
  84. class Thread : public love::Object
  85. {
  86. private:
  87. SDL_Thread *handle;
  88. love::thread::ThreadModule *module;
  89. ThreadData *comm;
  90. std::string name;
  91. char *data;
  92. SDL_mutex *mutex;
  93. SDL_cond *cond;
  94. bool isThread;
  95. public:
  96. Thread(love::thread::ThreadModule *module, const std::string & name, love::Data *data);
  97. Thread(love::thread::ThreadModule *module, const std::string & name);
  98. virtual ~Thread();
  99. void start();
  100. void kill();
  101. void wait();
  102. std::string getName();
  103. ThreadVariant *receive(const std::string & name);
  104. ThreadVariant *demand(const std::string & name);
  105. void clear(const std::string & name);
  106. void send(const std::string & name, ThreadVariant *v);
  107. void lock();
  108. void unlock();
  109. }; // Thread
  110. typedef std::map<std::string, Thread*> threadlist_t;
  111. class ThreadModule : public love::thread::ThreadModule
  112. {
  113. private:
  114. threadlist_t threads;
  115. public:
  116. ThreadModule();
  117. virtual ~ThreadModule();
  118. Thread *newThread(const std::string & name, love::Data *data);
  119. void getThreads(Thread ** list);
  120. Thread *getThread(const std::string & name);
  121. unsigned getThreadCount() const;
  122. void unregister(const std::string & name);
  123. const char *getName() const;
  124. }; // ThreadModule
  125. } // sdl
  126. } // thread
  127. } // love
  128. #endif // LOVE_THREAD_SDL_THREAD_H