Mercurial > love-experiments
comparison src/modules/thread/wrap_ThreadModule.cpp @ 1304:ee1448bb9680 minor
Fix race conditions and deadlocks in Channels (hopefully) (issue #554)
Also, this is a giant commit because of the official Code Style (tm) was applied.
| author | Bart van Strien <bart.bes+projects@gmail.com> |
|---|---|
| date | Wed, 30 Jan 2013 17:15:12 +0100 |
| parents | a74672ba1320 |
| children |
comparison
equal
deleted
inserted
replaced
| 1197:1a59c21cd2f3 | 1304:ee1448bb9680 |
|---|---|
| 26 | 26 |
| 27 namespace love | 27 namespace love |
| 28 { | 28 { |
| 29 namespace thread | 29 namespace thread |
| 30 { | 30 { |
| 31 static ThreadModule *instance = 0; | 31 static ThreadModule *instance = 0; |
| 32 | 32 |
| 33 int w_newThread(lua_State *L) | 33 int w_newThread(lua_State *L) |
| 34 { | |
| 35 std::string name = "Thread code"; | |
| 36 love::Data *data; | |
| 37 if (lua_isstring(L, 1)) | |
| 38 luax_convobj(L, 1, "filesystem", "newFile"); | |
| 39 if (luax_istype(L, 1, FILESYSTEM_FILE_T)) | |
| 34 { | 40 { |
| 35 std::string name = "Thread code"; | 41 try |
| 36 love::Data *data; | |
| 37 if (lua_isstring(L, 1)) | |
| 38 luax_convobj(L, 1, "filesystem", "newFile"); | |
| 39 if (luax_istype(L, 1, FILESYSTEM_FILE_T)) | |
| 40 { | 42 { |
| 41 try | 43 love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T); |
| 42 { | 44 name = std::string("@") + file->getFilename(); |
| 43 love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T); | 45 data = file->read(); |
| 44 name = std::string("@") + file->getFilename(); | |
| 45 data = file->read(); | |
| 46 } | |
| 47 catch (love::Exception & e) | |
| 48 { | |
| 49 return luaL_error(L, "%s", e.what()); | |
| 50 } | |
| 51 } | 46 } |
| 52 else if (luax_istype(L, 1, FILESYSTEM_FILE_DATA_T)) | 47 catch (love::Exception & e) |
| 53 { | 48 { |
| 54 love::filesystem::FileData * fdata = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T); | 49 return luaL_error(L, "%s", e.what()); |
| 55 name = std::string("@") + fdata->getFilename(); | |
| 56 data = fdata; | |
| 57 data->retain(); | |
| 58 } | 50 } |
| 59 else | 51 } |
| 52 else if (luax_istype(L, 1, FILESYSTEM_FILE_DATA_T)) | |
| 53 { | |
| 54 love::filesystem::FileData * fdata = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T); | |
| 55 name = std::string("@") + fdata->getFilename(); | |
| 56 data = fdata; | |
| 57 data->retain(); | |
| 58 } | |
| 59 else | |
| 60 { | |
| 61 data = luax_checktype<love::Data>(L, 1, "Data", DATA_T); | |
| 62 data->retain(); | |
| 63 } | |
| 64 LuaThread *t = instance->newThread(name, data); | |
| 65 // do not worry, file->read() returns retained data | |
| 66 data->release(); | |
| 67 luax_newtype(L, "Thread", THREAD_THREAD_T, (void *)t); | |
| 68 return 1; | |
| 69 } | |
| 70 | |
| 71 int w_newChannel(lua_State *L) | |
| 72 { | |
| 73 Channel *c = instance->newChannel(); | |
| 74 luax_newtype(L, "Channel", THREAD_CHANNEL_T, (void *)c); | |
| 75 return 1; | |
| 76 } | |
| 77 | |
| 78 int w_getChannel(lua_State *L) | |
| 79 { | |
| 80 std::string name = luax_checkstring(L, 1); | |
| 81 Channel *c = instance->getChannel(name); | |
| 82 luax_newtype(L, "Channel", THREAD_CHANNEL_T, (void *)c); | |
| 83 return 1; | |
| 84 } | |
| 85 | |
| 86 // List of functions to wrap. | |
| 87 static const luaL_Reg module_functions[] = { | |
| 88 { "newThread", w_newThread }, | |
| 89 { "newChannel", w_newChannel }, | |
| 90 { "getChannel", w_getChannel }, | |
| 91 { 0, 0 } | |
| 92 }; | |
| 93 | |
| 94 static const lua_CFunction types[] = { | |
| 95 luaopen_thread, | |
| 96 luaopen_channel, | |
| 97 0 | |
| 98 }; | |
| 99 | |
| 100 extern "C" int luaopen_love_thread(lua_State *L) | |
| 101 { | |
| 102 if (instance == 0) | |
| 103 { | |
| 104 try | |
| 60 { | 105 { |
| 61 data = luax_checktype<love::Data>(L, 1, "Data", DATA_T); | 106 instance = new ThreadModule(); |
| 62 data->retain(); | |
| 63 } | 107 } |
| 64 LuaThread *t = instance->newThread(name, data); | 108 catch (Exception & e) |
| 65 // do not worry, file->read() returns retained data | 109 { |
| 66 data->release(); | 110 return luaL_error(L, "%s", e.what()); |
| 67 luax_newtype(L, "Thread", THREAD_THREAD_T, (void*)t); | 111 } |
| 68 return 1; | |
| 69 } | 112 } |
| 113 else | |
| 114 instance->retain(); | |
| 70 | 115 |
| 71 int w_newChannel(lua_State *L) | 116 WrappedModule w; |
| 72 { | 117 w.module = instance; |
| 73 Channel *c = instance->newChannel(); | 118 w.name = "thread"; |
| 74 luax_newtype(L, "Channel", THREAD_CHANNEL_T, (void*)c); | 119 w.flags = MODULE_T; |
| 75 return 1; | 120 w.functions = module_functions; |
| 76 } | 121 w.types = types; |
| 77 | 122 |
| 78 int w_getChannel(lua_State *L) | 123 return luax_register_module(L, w); |
| 79 { | |
| 80 std::string name = luax_checkstring(L, 1); | |
| 81 Channel *c = instance->getChannel(name); | |
| 82 luax_newtype(L, "Channel", THREAD_CHANNEL_T, (void*)c); | |
| 83 return 1; | |
| 84 } | |
| 85 | |
| 86 // List of functions to wrap. | |
| 87 static const luaL_Reg module_functions[] = { | |
| 88 { "newThread", w_newThread }, | |
| 89 { "newChannel", w_newChannel }, | |
| 90 { "getChannel", w_getChannel }, | |
| 91 { 0, 0 } | |
| 92 }; | |
| 93 | |
| 94 static const lua_CFunction types[] = { | |
| 95 luaopen_thread, | |
| 96 luaopen_channel, | |
| 97 0 | |
| 98 }; | |
| 99 | |
| 100 extern "C" int luaopen_love_thread(lua_State *L) | |
| 101 { | |
| 102 if (instance == 0) | |
| 103 { | |
| 104 try | |
| 105 { | |
| 106 instance = new ThreadModule(); | |
| 107 } | |
| 108 catch (Exception & e) | |
| 109 { | |
| 110 return luaL_error(L, "%s", e.what()); | |
| 111 } | |
| 112 } | |
| 113 else | |
| 114 instance->retain(); | |
| 115 | |
| 116 WrappedModule w; | |
| 117 w.module = instance; | |
| 118 w.name = "thread"; | |
| 119 w.flags = MODULE_T; | |
| 120 w.functions = module_functions; | |
| 121 w.types = types; | |
| 122 | |
| 123 return luax_register_module(L, w); | |
| 124 } | |
| 125 } | 124 } |
| 126 } | 125 } |
| 126 } |
