Thread.cpp 2.7 KB

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