Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Use less kernel resources in device manager
Browse files Browse the repository at this point in the history
Use only one eventfd.
  • Loading branch information
RoEdAl committed Jun 10, 2024
1 parent e3cabf5 commit 37b566f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
62 changes: 31 additions & 31 deletions src/chan_quectel.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,40 +302,42 @@ static void pvt_destroy(struct pvt* const pvt)
pvt_free(pvt);
}

static int waitfor_n_fd(int* fd, int* ms)
{
int exception;

const int outfd = ast_waitfor_n_fd(fd, 2, ms, &exception);

if (outfd < 0) {
return 0;
}
// device manager

return outfd;
}
static const eventfd_t DEV_MANAGER_CMD_SCAN = 1;
static const eventfd_t DEV_MANAGER_CMD_STOP = 2;

static void dev_manager_threadproc_state(struct public_state* const state)
{
int manager_interval = SCONF_GLOBAL(state, manager_interval);
int fd[2] = {state->dev_manager_event_stop, state->dev_manager_event_signal};
const int fd = state->dev_manager_event;

auto int ev_wait()
{
int t = manager_interval;
return waitfor_n_fd(fd, &t);
return at_wait(fd, &t);
}

while (1) {
const int waitfd = ev_wait();
if (waitfd == fd[0]) { // exit
ast_debug(3, "[dev-manager] Got exit event\n");
break;
} else if (waitfd == fd[1]) { // reload
ast_debug(3, "[dev-manager] Got signal\n");
manager_interval = SCONF_GLOBAL(state, manager_interval);
eventfd_reset(fd[1]);
if (ev_wait() == fd) {
eventfd_t val = 0;
if (eventfd_read(fd, &val)) {
ast_log(LOG_ERROR, "[dev-manager] Fail to read command - exiting\n");
break;
}

if (val == DEV_MANAGER_CMD_SCAN) {
ast_debug(3, "[dev-manager] Got scan event\n");
manager_interval = SCONF_GLOBAL(state, manager_interval);
} else if (val == DEV_MANAGER_CMD_STOP) {
ast_debug(3, "[dev-manager] Got exit event\n");
break;
} else {
ast_log(LOG_WARNING, "[dev-manager] Unknown command: %d - exiting\n", (int)val);
continue;
}
}

// timeout
struct pvt* pvt;
/* read lock for avoid deadlock when IMEI/IMSI discovery */
Expand Down Expand Up @@ -422,16 +424,16 @@ static int dev_manager_start(struct public_state* const state)
return 0;
}

static void dev_manager_signal(const struct public_state* const state)
static void dev_manager_scan(const struct public_state* const state)
{
if (eventfd_set(state->dev_manager_event_signal)) {
if (eventfd_write(state->dev_manager_event, DEV_MANAGER_CMD_SCAN)) {
ast_log(LOG_ERROR, "Unable to signal device manager thread\n");
}
}

static void dev_manager_stop(struct public_state* const state)
{
if (eventfd_set(state->dev_manager_event_stop)) {
if (eventfd_write(state->dev_manager_event, DEV_MANAGER_CMD_STOP)) {
ast_log(LOG_ERROR, "Unable to signal device manager thread\n");
}

Expand Down Expand Up @@ -1159,7 +1161,7 @@ void pvt_try_restate(struct pvt* pvt)
{
if (pvt_time4restate(pvt)) {
pvt->restart_time = RESTATE_TIME_NOW;
dev_manager_signal(gpublic);
dev_manager_scan(gpublic);
}
}

Expand Down Expand Up @@ -1451,9 +1453,8 @@ static int public_state_init(struct public_state* state)
return rv;
}

state->dev_manager_event_signal = eventfd_create();
state->dev_manager_event_stop = eventfd_create();
state->dev_manager_thread = AST_PTHREADT_NULL;
state->dev_manager_event = eventfd_create();
state->dev_manager_thread = AST_PTHREADT_NULL;

AST_RWLIST_HEAD_INIT(&state->devices);

Expand Down Expand Up @@ -1535,8 +1536,7 @@ static void public_state_fini(struct public_state* const state)
dev_manager_stop(state);
devices_destroy(state);

eventfd_close(&state->dev_manager_event_signal);
eventfd_close(&state->dev_manager_event_stop);
eventfd_close(&state->dev_manager_event);
AST_RWLIST_HEAD_DESTROY(&state->devices);

ast_threadpool_shutdown(gpublic->threadpool);
Expand All @@ -1558,7 +1558,7 @@ void pvt_reload(restate_time_t when)

reload_config(gpublic, 1, when, &dev_reload);
if (dev_reload > 0) {
dev_manager_signal(gpublic);
dev_manager_scan(gpublic);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/chan_quectel.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ typedef struct public_state {
AST_RWLIST_HEAD(devices, pvt) devices;
struct ast_threadpool* threadpool;
pthread_t dev_manager_thread;
int dev_manager_event_signal;
int dev_manager_event_stop;
int dev_manager_event;
struct dc_gconfig global_settings;
} public_state_t;

Expand Down
3 changes: 1 addition & 2 deletions src/eventfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
*/

#include <errno.h>
#include <sys/eventfd.h>
#include <unistd.h>

#include "eventfd.h"

int eventfd_create() { return eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); }

int eventfd_set(int fd) { return eventfd_write(fd, 1); }
int eventfd_signal(int fd) { return eventfd_write(fd, 1); }

int eventfd_reset(int fd)
{
Expand Down
4 changes: 3 additions & 1 deletion src/eventfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#ifndef CHAN_QUECTEL_EVENTFD_H_INCLUDED
#define CHAN_QUECTEL_EVENTFD_H_INCLUDED

#include <sys/eventfd.h>

int eventfd_create();
int eventfd_set(int fd);
int eventfd_signal(int fd);
int eventfd_reset(int fd);
void eventfd_close(int* fd);

Expand Down

0 comments on commit 37b566f

Please sign in to comment.