Thread.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #include <common/config.h>
  21. #include "Thread.h"
  22. #ifdef LOVE_BUILD_STANDALONE
  23. extern "C" int luaopen_love(lua_State * L);
  24. #endif // LOVE_BUILD_STANDALONE
  25. extern "C" int luaopen_love_thread(lua_State *L);
  26. namespace love
  27. {
  28. namespace thread
  29. {
  30. namespace sdl
  31. {
  32. int threadfunc(ThreadData *comm)
  33. {
  34. lua_State * L = lua_open();
  35. luaL_openlibs(L);
  36. #ifdef LOVE_BUILD_STANDALONE
  37. love::luax_preload(L, luaopen_love, "love");
  38. luaopen_love(L);
  39. #endif // LOVE_BUILD_STANDALONE
  40. luaopen_love_thread(L);
  41. lua_pushstring(L, comm->getName());
  42. luax_convobj(L, lua_gettop(L), "thread", "getThread");
  43. lua_getglobal(L, "love");
  44. lua_pushvalue(L, -2);
  45. lua_setfield(L, -2, "_curthread");
  46. if(luaL_dostring(L, comm->getCode()) == 1)
  47. {
  48. ThreadVariant *v = new ThreadVariant(lua_tostring(L, -1));
  49. comm->setValue("error", v);
  50. v->release();
  51. }
  52. lua_close(L);
  53. return 0;
  54. }
  55. ThreadVariant::ThreadVariant(bool boolean)
  56. {
  57. type = BOOLEAN;
  58. data.boolean = boolean;
  59. }
  60. ThreadVariant::ThreadVariant(double number)
  61. {
  62. type = NUMBER;
  63. data.number = number;
  64. }
  65. ThreadVariant::ThreadVariant(const char *string)
  66. {
  67. type = STRING;
  68. size_t len = strlen(string);
  69. char *buf = new char[len+1];
  70. memset(buf, 0, len+1);
  71. memcpy(buf, string, len);
  72. data.string = buf;
  73. }
  74. ThreadVariant::ThreadVariant(void *userdata)
  75. {
  76. type = LUSERDATA;
  77. data.userdata = userdata;
  78. }
  79. ThreadVariant::ThreadVariant(Type udatatype, void *userdata)
  80. {
  81. type = FUSERDATA;
  82. this->udatatype = udatatype;
  83. Proxy *p = (Proxy *) userdata;
  84. flags = p->flags;
  85. data.userdata = p->data;
  86. ((love::Object *) data.userdata)->retain();
  87. }
  88. ThreadVariant::~ThreadVariant()
  89. {
  90. switch(type)
  91. {
  92. case STRING:
  93. delete[] data.string;
  94. break;
  95. case FUSERDATA:
  96. ((love::Object *) data.userdata)->release();
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. ThreadData::ThreadData(const char *name, const char *code)
  103. {
  104. size_t len = strlen(name);
  105. this->name = new char[len+1];
  106. memset(this->name, 0, len+1);
  107. memcpy(this->name, name, len);
  108. if (code)
  109. {
  110. len = strlen(code);
  111. this->code = new char[len+1];
  112. memset(this->code, 0, len+1);
  113. memcpy(this->code, code, len);
  114. }
  115. else
  116. this->code = 0;
  117. }
  118. ThreadData::~ThreadData()
  119. {
  120. delete[] name;
  121. delete[] code;
  122. }
  123. const char *ThreadData::getCode()
  124. {
  125. return code;
  126. }
  127. const char *ThreadData::getName()
  128. {
  129. return name;
  130. }
  131. ThreadVariant* ThreadData::getValue(const std::string & name)
  132. {
  133. if (shared.count(name) == 0)
  134. return 0;
  135. return shared[name];
  136. }
  137. void ThreadData::clearValue(const std::string & name)
  138. {
  139. if (shared.count(name) == 0)
  140. return;
  141. shared[name]->release();
  142. shared.erase(name);
  143. }
  144. void ThreadData::setValue(const std::string & name, ThreadVariant *v)
  145. {
  146. if (shared.count(name) != 0)
  147. shared[name]->release();
  148. v->retain();
  149. shared[name] = v;
  150. }
  151. Thread::Thread(love::thread::ThreadModule *module, const std::string & name, love::Data *data)
  152. : handle(0), module(module), name(name), isThread(true)
  153. {
  154. module->retain();
  155. unsigned int len = data->getSize();
  156. this->data = new char[len+1];
  157. memset(this->data, 0, len+1);
  158. memcpy(this->data, data->getData(), len);
  159. comm = new ThreadData(name.c_str(), this->data);
  160. mutex = SDL_CreateMutex();
  161. cond = SDL_CreateCond();
  162. }
  163. Thread::Thread(love::thread::ThreadModule *module, const std::string & name)
  164. : handle(0), module(module), name(name), data(0), isThread(false)
  165. {
  166. module->retain();
  167. comm = new ThreadData(name.c_str(), NULL);
  168. mutex = SDL_CreateMutex();
  169. cond = SDL_CreateCond();
  170. }
  171. Thread::~Thread()
  172. {
  173. if (data)
  174. delete[] data;
  175. delete comm;
  176. module->unregister(name);
  177. SDL_DestroyMutex(mutex);
  178. SDL_DestroyCond(cond);
  179. module->release();
  180. }
  181. void Thread::start()
  182. {
  183. if (!handle && isThread)
  184. handle = SDL_CreateThread((int (*)(void*)) threadfunc, (void*) comm);
  185. }
  186. void Thread::kill()
  187. {
  188. if (handle)
  189. {
  190. SDL_mutexP((SDL_mutex *) _gcmutex);
  191. SDL_KillThread(handle);
  192. handle = 0;
  193. SDL_mutexV((SDL_mutex *) _gcmutex);
  194. }
  195. }
  196. void Thread::wait()
  197. {
  198. if (handle)
  199. {
  200. SDL_WaitThread(handle, NULL);
  201. handle = 0;
  202. }
  203. }
  204. void Thread::lock()
  205. {
  206. SDL_mutexP(mutex);
  207. }
  208. void Thread::unlock()
  209. {
  210. SDL_mutexV(mutex);
  211. }
  212. std::string Thread::getName()
  213. {
  214. return name;
  215. }
  216. ThreadVariant *Thread::receive(const std::string & name)
  217. {
  218. lock();
  219. ThreadVariant *v = comm->getValue(name);
  220. unlock();
  221. return v;
  222. }
  223. ThreadVariant *Thread::demand(const std::string & name)
  224. {
  225. lock();
  226. ThreadVariant *v = comm->getValue(name);
  227. while (!v)
  228. {
  229. if (comm->getValue("error"))
  230. return 0;
  231. SDL_CondWait(cond, mutex);
  232. v = comm->getValue(name);
  233. }
  234. unlock();
  235. return v;
  236. }
  237. void Thread::clear(const std::string & name)
  238. {
  239. lock();
  240. comm->clearValue(name);
  241. unlock();
  242. }
  243. void Thread::send(const std::string & name, ThreadVariant *v)
  244. {
  245. lock();
  246. comm->setValue(name, v);
  247. unlock();
  248. SDL_CondBroadcast(cond);
  249. }
  250. ThreadModule::ThreadModule()
  251. {
  252. threads["main"] = new Thread(this, "main");
  253. }
  254. ThreadModule::~ThreadModule()
  255. {
  256. for (threadlist_t::iterator i = threads.begin(); i != threads.end(); i++)
  257. {
  258. i->second->kill();
  259. }
  260. }
  261. Thread *ThreadModule::newThread(const std::string & name, love::Data *data)
  262. {
  263. if (threads.count(name) != 0)
  264. return 0;
  265. Thread *t = new Thread(this, name, data);
  266. threads[name] = t;
  267. return t;
  268. }
  269. Thread *ThreadModule::getThread(const std::string & name)
  270. {
  271. if (threads.count(name) == 0)
  272. return 0;
  273. threadlist_t::iterator i = threads.find(name);
  274. return i->second;
  275. }
  276. void ThreadModule::getThreads(Thread ** list)
  277. {
  278. int c = 0;
  279. for (threadlist_t::iterator i = threads.begin(); i != threads.end(); i++, c++)
  280. {
  281. list[c] = i->second;
  282. }
  283. }
  284. unsigned ThreadModule::getThreadCount() const
  285. {
  286. return threads.size();
  287. }
  288. void ThreadModule::unregister(const std::string & name)
  289. {
  290. if (threads.count(name) == 0)
  291. return;
  292. threadlist_t::iterator i = threads.find(name);
  293. threads.erase(i);
  294. }
  295. const char *ThreadModule::getName() const
  296. {
  297. return "love.thread.sdl";
  298. }
  299. } // sdl
  300. } // thread
  301. } // love