[config] Try to set notifier file descriptor to non-blocking mode; add start/stop_notifier functions

If setting to non-blocking fails, just closing the fd should kill it anyway
pull/109/head
jackun 4 years ago
parent 841d76c6af
commit e72a7bdd76
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3

@ -73,7 +73,7 @@ static std::string deviceName;
// seems to quit by itself though
static std::unique_ptr<notify_thread, std::function<void(notify_thread *)>>
stop_notifier(&notifier, [](notify_thread *n){ n->quit = true; });
stop_it(&notifier, [](notify_thread *n){ stop_notifier(*n); });
void imgui_init()
{
@ -81,7 +81,7 @@ void imgui_init()
return;
parse_overlay_config(&params, getenv("MANGOHUD_CONFIG"));
notifier.params = &params;
pthread_create(&fileChange, NULL, &fileChanged, &notifier);
start_notifier(notifier);
window_size = ImVec2(params.width, params.height);
init_system_info();
cfg_inited = true;

@ -1,4 +1,7 @@
#include <thread>
#include <chrono>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include "config.h"
@ -9,22 +12,13 @@ pthread_t fileChange;
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
void *fileChanged(void *params_void){
static void *fileChanged(void *params_void) {
notify_thread *nt = reinterpret_cast<notify_thread *>(params_void);
int length, i = 0;
int fd;
int wd;
char buffer[EVENT_BUF_LEN];
fd = inotify_init();
wd = inotify_add_watch( fd, nt->params->config_file_path.c_str(), IN_MODIFY);
if (wd < 0) {
close(fd);
return nullptr;
}
while (!nt->quit) {
length = read( fd, buffer, EVENT_BUF_LEN );
length = read( nt->fd, buffer, EVENT_BUF_LEN );
while (i < length) {
struct inotify_event *event =
(struct inotify_event *) &buffer[i];
@ -35,9 +29,39 @@ void *fileChanged(void *params_void){
}
}
i = 0;
printf("File Changed\n");
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
inotify_rm_watch(fd, wd);
close(fd);
printf("%s quit\n", __func__);
return nullptr;
}
bool start_notifier(notify_thread& nt)
{
nt.fd = inotify_init();
nt.wd = inotify_add_watch( nt.fd, nt.params->config_file_path.c_str(), IN_MODIFY);
int flags = fcntl(nt.fd, F_GETFL, 0);
if (fcntl(nt.fd, F_SETFL, flags | O_NONBLOCK))
perror("Set non-blocking failed");
if (nt.wd < 0) {
close(nt.fd);
nt.fd = -1;
return false;
}
pthread_create(&fileChange, NULL, &fileChanged, &nt);
return true;
}
void stop_notifier(notify_thread& nt)
{
if (nt.fd < 0)
return;
nt.quit = true;
inotify_rm_watch(nt.fd, nt.wd);
close(nt.fd);
nt.fd = -1;
}

@ -1,13 +1,13 @@
#include <thread>
#include <mutex>
#include "overlay_params.h"
struct notify_thread
{
int fd = -1, wd = -1;
overlay_params *params = nullptr;
bool quit = false;
std::mutex mutex;
};
extern pthread_t fileChange;
extern void *fileChanged(void *params_void);
bool start_notifier(notify_thread& nt);
void stop_notifier(notify_thread& nt);

@ -2659,7 +2659,7 @@ static VkResult overlay_CreateInstance(
parse_overlay_config(&instance_data->params, getenv("MANGOHUD_CONFIG"));
instance_data->notifier.params = &instance_data->params;
pthread_create(&fileChange, NULL, &fileChanged, &instance_data->notifier);
start_notifier(instance_data->notifier);
init_cpu_stats(instance_data->params);
@ -2685,7 +2685,7 @@ static void overlay_DestroyInstance(
struct instance_data *instance_data = FIND(struct instance_data, instance);
instance_data_map_physical_devices(instance_data, false);
instance_data->vtable.DestroyInstance(instance, pAllocator);
instance_data->notifier.quit = true;
stop_notifier(instance_data->notifier);
destroy_instance_data(instance_data);
}

Loading…
Cancel
Save