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