driver/lcd4linux: rework thread handling

Use std::thread for simplified handling.

Added bool exit_proc to allow left the thread callback, because if user
will change on/off/automatic modes inside the lcd4linux-setup menu,
he will be trapped in this menu and neutrino is never usable.
Btw. added some temporarily debug outputs to track this behavior.
This commit is contained in:
2021-06-06 13:53:08 +02:00
parent e1810b62a2
commit 6edf6e0a25
2 changed files with 38 additions and 24 deletions

View File

@@ -31,7 +31,7 @@
#include <config.h> #include <config.h>
#endif #endif
#include <pthread.h>
#include <sstream> #include <sstream>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@@ -44,7 +44,7 @@
#include <timerdclient/timerdclient.h> #include <timerdclient/timerdclient.h>
#include <system/helpers.h> #include <system/helpers.h>
#include <system/debug.h>
#include <driver/record.h> #include <driver/record.h>
#include <driver/audioplay.h> #include <driver/audioplay.h>
#include <driver/radiotext.h> #include <driver/radiotext.h>
@@ -68,7 +68,7 @@ extern CPictureViewer *g_PicViewer;
#define LCD_ICONSDIR TARGET_PREFIX "/share/lcd/icons/" #define LCD_ICONSDIR TARGET_PREFIX "/share/lcd/icons/"
#define ICONSEXT ".png" #define ICONSEXT ".png"
#define LOGO_DUMMY ICONSDIR "/blank.png" #define LOGO_DUMMY ICONSDIR "blank.png"
#define BRIGHTNESS LCD_DATADIR "brightness" #define BRIGHTNESS LCD_DATADIR "brightness"
#define BRIGHTNESS_STANDBY LCD_DATADIR "brightness_standby" #define BRIGHTNESS_STANDBY LCD_DATADIR "brightness_standby"
@@ -154,8 +154,13 @@ static void lcd4linux(bool run)
} }
else else
{ {
if (my_system(3, "killall", "-9", buf) != 0) if (file_exists(PIDFILE))
printf("[CLCD4l] %s: terminating '%s' failed\n", __FUNCTION__, buf); {
if (my_system(3, "killall", "-9", buf) != 0)
printf("[CLCD4l] %s: terminating '%s' failed\n", __FUNCTION__, buf);
}
else
dprintf(DEBUG_NORMAL,"\033[33m[CLCD4l] [%s - %d] %s is not running \033[0m\n", __func__, __LINE__);
} }
} }
@@ -163,14 +168,17 @@ static void lcd4linux(bool run)
CLCD4l::CLCD4l() CLCD4l::CLCD4l()
{ {
thrLCD4l = 0; thrLCD4l = NULL;
exit_proc = false;
} }
CLCD4l::~CLCD4l() CLCD4l::~CLCD4l()
{ {
exit_proc = true;
if (thrLCD4l) if (thrLCD4l)
pthread_cancel(thrLCD4l); thrLCD4l->join();
thrLCD4l = 0; delete thrLCD4l;
thrLCD4l = NULL;
} }
/* ----------------------------------------------------------------- */ /* ----------------------------------------------------------------- */
@@ -179,32 +187,39 @@ void CLCD4l::InitLCD4l()
{ {
if (thrLCD4l) if (thrLCD4l)
{ {
printf("[CLCD4l] %s: initializing\n", __FUNCTION__); dprintf(DEBUG_NORMAL,"\033[32m[CLCD4l] [%s - %d] initializing %d \033[0m\n", __func__, __LINE__);
Init(); Init();
} }
} }
void CLCD4l::StartLCD4l() void CLCD4l::StartLCD4l()
{ {
if (!thrLCD4l && (g_settings.lcd4l_support == 1 || g_settings.lcd4l_support == 2)) if (!thrLCD4l && g_settings.lcd4l_support)
{ {
printf("[CLCD4l] %s: starting thread\n", __FUNCTION__); dprintf(DEBUG_NORMAL,"\033[32m[CLCD4l] [%s - %d] starting thread with mode %d \033[0m\n", __func__, __LINE__, g_settings.lcd4l_support);
pthread_create(&thrLCD4l, NULL, LCD4lProc, (void*) this);
pthread_detach(thrLCD4l); exit_proc = false;
lcd4linux(true); thrLCD4l = new std::thread (LCD4lProc, this);
dprintf(DEBUG_NORMAL,"\033[32m[CLCD4l] [%s - %d] thread [%p] is running\033[0m\n", __func__, __LINE__, thrLCD4l);
} }
if (g_settings.lcd4l_support) lcd4linux(true);
exec_initscript("lcd4linux", "start"); exec_initscript("lcd4linux", "start");
} }
void CLCD4l::StopLCD4l() void CLCD4l::StopLCD4l()
{ {
if (thrLCD4l) if (thrLCD4l)
{ {
printf("[CLCD4l] %s: stopping thread\n", __FUNCTION__); dprintf(DEBUG_NORMAL,"\033[32m[CLCD4l] [%s - %d] stopping thread [%p]\033[0m\n", __func__, __LINE__, thrLCD4l);
pthread_cancel(thrLCD4l);
thrLCD4l = 0;
lcd4linux(false); lcd4linux(false);
exit_proc = true;
thrLCD4l->join();
dprintf(DEBUG_NORMAL,"\033[32m[CLCD4l] [%s - %d] thread [%p] joined\033[0m\n", __func__, __LINE__, thrLCD4l);
delete thrLCD4l;
thrLCD4l = NULL;
dprintf(DEBUG_NORMAL,"\033[32m[CLCD4l] [%s - %d] thread [%p] terminated\033[0m\n", __func__, __LINE__, thrLCD4l);
} }
exec_initscript("lcd4linux", "stop"); exec_initscript("lcd4linux", "stop");
} }
@@ -322,9 +337,6 @@ void CLCD4l::Init()
void* CLCD4l::LCD4lProc(void* arg) void* CLCD4l::LCD4lProc(void* arg)
{ {
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
CLCD4l *PLCD4l = static_cast<CLCD4l*>(arg); CLCD4l *PLCD4l = static_cast<CLCD4l*>(arg);
PLCD4l->Init(); PLCD4l->Init();
@@ -336,7 +348,7 @@ void* CLCD4l::LCD4lProc(void* arg)
bool NewParseID = false; bool NewParseID = false;
//printf("[CLCD4l] %s: starting loop\n", __FUNCTION__); //printf("[CLCD4l] %s: starting loop\n", __FUNCTION__);
while(1) while(!PLCD4l->exit_proc)
{ {
if ( (!access(PIDFILE, F_OK) == 0) && (!FirstRun) ) if ( (!access(PIDFILE, F_OK) == 0) && (!FirstRun) )
{ {

View File

@@ -31,6 +31,7 @@
#define __lcd4l__ #define __lcd4l__
#include <string> #include <string>
#include <thread>
class CLCD4l class CLCD4l
{ {
@@ -66,8 +67,9 @@ class CLCD4l
void ResetParseID() { m_ParseID = 0; } void ResetParseID() { m_ParseID = 0; }
private: private:
pthread_t thrLCD4l; std::thread *thrLCD4l;
static void* LCD4lProc(void *arg); static void* LCD4lProc(void *arg);
bool exit_proc;
struct tm *tm_struct; struct tm *tm_struct;
bool wait4daemon; bool wait4daemon;