comparison src/modules/thread/LuaThread.cpp @ 1098:a74672ba1320 minor

Merge in channels from love-experiments Rewritten love.thread and changed communication method from weird system before, to channels.
author Bart van Strien <bart.bes+projects@gmail.com>
date Wed, 04 Jul 2012 17:15:16 +0200
parents
children ee1448bb9680
comparison
equal deleted inserted replaced
1097:4819322d9cdc 1098:a74672ba1320
1 /**
2 * Copyright (c) 2006-2012 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
21 #include "LuaThread.h"
22 #include <common/config.h>
23
24 #ifdef LOVE_BUILD_STANDALONE
25 extern "C" int luaopen_love(lua_State * L);
26 #endif // LOVE_BUILD_STANDALONE
27 extern "C" int luaopen_love_thread(lua_State *L);
28
29 namespace love
30 {
31 namespace thread
32 {
33 LuaThread::LuaThread(const std::string &name, love::Data *code)
34 : name(name), code(code)
35 {
36 code->retain();
37 }
38
39 LuaThread::~LuaThread()
40 {
41 code->release();
42 }
43
44 void LuaThread::threadFunction()
45 {
46 this->retain();
47 lua_State * L = lua_open();
48 luaL_openlibs(L);
49 #ifdef LOVE_BUILD_STANDALONE
50 love::luax_preload(L, luaopen_love, "love");
51 luaopen_love(L);
52 #endif // LOVE_BUILD_STANDALONE
53 luaopen_love_thread(L);
54 if (luaL_loadbuffer(L, (const char*) code->getData(), code->getSize(), name.c_str()) != 0)
55 error = luax_tostring(L, -1);
56 else
57 if (lua_pcall(L, 0, 0, 0) != 0)
58 error = luax_tostring(L, -1);
59 lua_close(L);
60 this->release();
61 }
62
63 const std::string &LuaThread::getError()
64 {
65 return error;
66 }
67 } // thread
68 } // love