mirror of
https://github.com/tuxbox-neutrino/neutrino.git
synced 2025-08-27 15:32:59 +02:00
system/luaserver: fix destructor
This commit is contained in:
@@ -28,7 +28,7 @@ end
|
||||
return "ok"
|
||||
EOT
|
||||
|
||||
followed by luaclient test a b c d
|
||||
luaclient test a b c d
|
||||
#endif
|
||||
|
||||
#include <config.h>
|
||||
@@ -50,6 +50,7 @@ followed by luaclient test a b c d
|
||||
#include "luaserver.h"
|
||||
|
||||
static CLuaServer *instance = NULL;
|
||||
static pthread_mutex_t mutex;
|
||||
|
||||
CLuaServer *CLuaServer::getInstance()
|
||||
{
|
||||
@@ -60,33 +61,37 @@ CLuaServer *CLuaServer::getInstance()
|
||||
|
||||
void CLuaServer::destroyInstance()
|
||||
{
|
||||
Lock();
|
||||
if (instance) {
|
||||
delete instance;
|
||||
instance = NULL;
|
||||
}
|
||||
UnLock();
|
||||
}
|
||||
|
||||
CLuaServer::CLuaServer()
|
||||
{
|
||||
count = 0;
|
||||
did_run = false;
|
||||
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
|
||||
pthread_mutex_init(&mutex, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
|
||||
sem_init(&may_run, 0, 0);
|
||||
sem_init(&did_run, 0, 0);
|
||||
|
||||
pthread_create (&thr, NULL, luaserver_main_thread, (void *) NULL);
|
||||
}
|
||||
|
||||
CLuaServer::~CLuaServer()
|
||||
{
|
||||
sem_destroy(&may_run);
|
||||
sem_destroy(&did_run);
|
||||
instance = NULL;
|
||||
pthread_cancel(thr);
|
||||
pthread_join(thr, NULL);
|
||||
sem_destroy(&may_run);
|
||||
pthread_mutex_destroy(&mutex);
|
||||
instance = NULL;
|
||||
}
|
||||
|
||||
void CLuaServer::UnBlock()
|
||||
@@ -97,10 +102,10 @@ void CLuaServer::UnBlock()
|
||||
bool CLuaServer::Block(const neutrino_msg_t msg, const neutrino_msg_data_t data)
|
||||
{
|
||||
sem_wait(&may_run);
|
||||
if (!sem_trywait(&did_run)) {
|
||||
if (did_run) {
|
||||
if (msg != CRCInput::RC_timeout)
|
||||
g_RCInput->postMsg(msg, data);
|
||||
while (!sem_trywait(&did_run));
|
||||
did_run = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -125,20 +130,22 @@ class luaserver_data
|
||||
|
||||
void CLuaServer::Lock(void)
|
||||
{
|
||||
pthread_mutex_lock(&instance->mutex);
|
||||
pthread_mutex_lock(&mutex);
|
||||
}
|
||||
|
||||
void CLuaServer::UnLock(void)
|
||||
{
|
||||
pthread_mutex_unlock(&instance->mutex);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}
|
||||
|
||||
void *CLuaServer::luaserver_thread(void *arg) {
|
||||
set_threadname(__func__);
|
||||
Lock();
|
||||
instance->count++;
|
||||
if (instance) {
|
||||
instance->count++;
|
||||
instance->did_run = true;
|
||||
}
|
||||
UnLock();
|
||||
sem_post(&instance->did_run);
|
||||
|
||||
luaserver_data *lsd = (class luaserver_data *)arg;
|
||||
|
||||
@@ -166,9 +173,11 @@ void *CLuaServer::luaserver_thread(void *arg) {
|
||||
|
||||
delete lsd;
|
||||
Lock();
|
||||
instance->count--;
|
||||
if (!instance->count)
|
||||
sem_post(&instance->may_run);
|
||||
if (instance) {
|
||||
instance->count--;
|
||||
if (!instance->count)
|
||||
sem_post(&instance->may_run);
|
||||
}
|
||||
UnLock();
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
@@ -190,9 +199,14 @@ bool CLuaServer::luaserver_parse_command(CBasicMessage::Header &rmsg __attribute
|
||||
fprintf(stderr, "%s %s %d: unterminated string\n", __FILE__, __func__, __LINE__);
|
||||
return true;
|
||||
}
|
||||
std::string luascript(LUAPLUGINDIR "/");
|
||||
luascript += data;
|
||||
luascript += ".lua";
|
||||
std::string luascript;
|
||||
if (data[0] == '/')
|
||||
luascript = data;
|
||||
else {
|
||||
luascript = LUAPLUGINDIR "/";
|
||||
luascript += data;
|
||||
luascript += ".lua";
|
||||
}
|
||||
if (access(luascript, R_OK)) {
|
||||
fprintf(stderr, "%s %s %d: %s not found\n", __FILE__, __func__, __LINE__, luascript.c_str());
|
||||
const char *result_code = "-1";
|
||||
@@ -200,7 +214,7 @@ bool CLuaServer::luaserver_parse_command(CBasicMessage::Header &rmsg __attribute
|
||||
std::string error_string = luascript + " not found\n";
|
||||
size_t result_code_len = strlen(result_code) + 1;
|
||||
size_t result_string_len = strlen(result_string) + 1;
|
||||
size_t error_string_len = strlen(error_string.c_str()) + 1;
|
||||
size_t error_string_len = error_string.size() + 1;
|
||||
size = result_code_len + result_string_len + error_string_len;
|
||||
char result[size + sizeof(size)];
|
||||
char *rp = result;
|
||||
@@ -239,6 +253,7 @@ bool CLuaServer::luaserver_parse_command(CBasicMessage::Header &rmsg __attribute
|
||||
|
||||
void *CLuaServer::luaserver_main_thread(void *) {
|
||||
set_threadname(__func__);
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
|
||||
|
||||
CBasicServer server;
|
||||
if (!server.prepare(LUACLIENT_UDS_NAME)) {
|
||||
@@ -246,7 +261,6 @@ void *CLuaServer::luaserver_main_thread(void *) {
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
|
||||
server.run(luaserver_parse_command, LUACLIENT_VERSION);
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
Reference in New Issue
Block a user