Thread.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. int threadfunc(const char *data)
  26. {
  27. lua_State * L = lua_open();
  28. luaL_openlibs(L);
  29. #ifdef LOVE_BUILD_STANDALONE
  30. love::luax_preload(L, luaopen_love, "love");
  31. luaopen_love(L);
  32. #endif // LOVE_BUILD_STANDALONE
  33. luaL_dostring(L, data);
  34. lua_close(L);
  35. return 0;
  36. }
  37. namespace love
  38. {
  39. namespace thread
  40. {
  41. namespace sdl
  42. {
  43. Thread::Thread(ThreadModuleRegistrar *reg, std::string name, love::Data *data)
  44. : reg(reg), name(name), handle(0)
  45. {
  46. unsigned int len = data->getSize();
  47. this->data = new char[len];
  48. memcpy(this->data, data->getData(), len);
  49. }
  50. Thread::~Thread()
  51. {
  52. delete[] data;
  53. if (handle)
  54. SDL_KillThread(handle);
  55. reg->unregister(name);
  56. }
  57. void Thread::start()
  58. {
  59. if (!handle)
  60. SDL_CreateThread((int (*)(void*)) threadfunc, (void*) data);
  61. }
  62. void Thread::kill()
  63. {
  64. if (handle)
  65. SDL_KillThread(handle);
  66. }
  67. std::string Thread::getName()
  68. {
  69. return name;
  70. }
  71. ThreadModule::~ThreadModule()
  72. {
  73. for (threadlist_t::iterator i = threads.begin(); i != threads.end(); i++)
  74. {
  75. i->second->kill();
  76. }
  77. }
  78. Thread *ThreadModule::newThread(std::string name, love::Data *data)
  79. {
  80. if (threads.count(name) != 0)
  81. return 0;
  82. Thread *t = new Thread(this, name, data);
  83. threads[name] = t;
  84. return t;
  85. }
  86. Thread *ThreadModule::getThread(std::string name)
  87. {
  88. if (threads.count(name) == 0)
  89. return 0;
  90. threadlist_t::iterator i = threads.find(name);
  91. return i->second;
  92. }
  93. Thread **ThreadModule::getThreads()
  94. {
  95. Thread **list = new Thread*[threads.size()];
  96. int c = 0;
  97. for (threadlist_t::iterator i = threads.begin(); i != threads.end(); i++, c++)
  98. {
  99. list[c] = i->second;
  100. }
  101. return NULL;
  102. }
  103. void ThreadModule::unregister(std::string name)
  104. {
  105. if (threads.count(name) == 0)
  106. return;
  107. threadlist_t::iterator i = threads.find(name);
  108. threads.erase(i);
  109. }
  110. const char *ThreadModule::getName() const
  111. {
  112. return "love.thread.sdl";
  113. }
  114. } // sdl
  115. } // thread
  116. } // love