Thread.cpp 3.2 KB

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