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

Commit

Permalink
Use AO2 list of private objects
Browse files Browse the repository at this point in the history
  • Loading branch information
RoEdAl committed Jun 16, 2024
1 parent 5552c36 commit 0702991
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 227 deletions.
478 changes: 292 additions & 186 deletions src/chan_quectel.c

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions src/chan_quectel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

#include "ast_config.h"

#include <asterisk/astobj2.h>
#include <asterisk/json.h>
#include <asterisk/linkedlists.h>
#include <asterisk/localtime.h>
#include <asterisk/lock.h>
#include <asterisk/strings.h>
Expand Down Expand Up @@ -91,9 +91,6 @@ typedef struct pvt_stat {
struct at_queue_task;

typedef struct pvt {
AST_LIST_ENTRY(pvt) entry; /*!< linked list pointers */

ast_mutex_t lock; /*!< pvt lock */
AST_LIST_HEAD_NOLOCK(, at_queue_task) at_queue; /*!< queue for commands to modem */

AST_LIST_HEAD_NOLOCK(, cpvt) chans; /*!< list of channels */
Expand Down Expand Up @@ -195,7 +192,7 @@ typedef struct pvt {
#define PVT_STAT(pvt, name) PVT_STAT_T(&(pvt)->stat, name)

typedef struct public_state {
AST_RWLIST_HEAD(devices, pvt) devices;
struct ao2_container* pvts;
struct ast_threadpool* threadpool;
pthread_t dev_manager_thread;
int dev_manager_event;
Expand Down
18 changes: 10 additions & 8 deletions src/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ static int channel_call(struct ast_channel* channel, const char* dest, attribute
return -1;
}

SCOPED_MUTEX(pvt_lock, &pvt->lock);
SCOPED_AO2LOCK(pvt_lock, pvt);

// FIXME: check if bridged on same device with CALL_FLAG_HOLD_OTHER
if (!pvt_ready4voice_call(pvt, cpvt, opts)) {
Expand Down Expand Up @@ -285,7 +285,7 @@ static int channel_hangup(struct ast_channel* channel)
if (cpvt && cpvt->channel == channel && cpvt->pvt) {
struct pvt* const pvt = cpvt->pvt;

SCOPED_MUTEX(pvt_lock, &pvt->lock);
SCOPED_AO2LOCK(pvt_lock, pvt);

const int need_hangup = CPVT_TEST_FLAG(cpvt, CALL_FLAG_NEED_HANGUP) ? 1 : 0;
const int hangup_cause = ast_channel_hangupcause(channel);
Expand Down Expand Up @@ -327,7 +327,7 @@ static int channel_answer(struct ast_channel* channel)
}
struct pvt* const pvt = cpvt->pvt;

SCOPED_MUTEX(pvt_lock, &pvt->lock);
SCOPED_AO2LOCK(pvt_lock, pvt);

if (CPVT_DIR_INCOMING(cpvt)) {
if (at_enqueue_answer(cpvt)) {
Expand All @@ -350,7 +350,7 @@ static int channel_digit_begin(struct ast_channel* channel, char digit)
}
struct pvt* const pvt = cpvt->pvt;

SCOPED_MUTEX(pvt_lock, &pvt->lock);
SCOPED_AO2LOCK(pvt_lock, pvt);

const int rv = at_enqueue_dtmf(cpvt, digit);
if (rv) {
Expand Down Expand Up @@ -855,7 +855,7 @@ static int channel_fixup(struct ast_channel* oldchannel, struct ast_channel* new

struct pvt* const pvt = cpvt->pvt;

SCOPED_MUTEX(pvt_lock, &pvt->lock);
SCOPED_AO2LOCK(pvt_lock, pvt);

if (cpvt->channel == oldchannel) {
cpvt->channel = newchannel;
Expand Down Expand Up @@ -931,7 +931,7 @@ static int channel_indicate(struct ast_channel* channel, int condition, const vo
if (!pvt || CONF_SHARED(pvt, moh)) {
ast_moh_start(channel, data, NULL);
} else {
SCOPED_MUTEX(pvt_lock, &pvt->lock);
SCOPED_AO2LOCK(pvt_lock, pvt);
at_enqueue_mute(cpvt, 1);
}
break;
Expand All @@ -940,14 +940,14 @@ static int channel_indicate(struct ast_channel* channel, int condition, const vo
if (!pvt || CONF_SHARED(pvt, moh)) {
ast_moh_stop(channel);
} else {
SCOPED_MUTEX(pvt_lock, &pvt->lock);
SCOPED_AO2LOCK(pvt_lock, pvt);
at_enqueue_mute(cpvt, 0);
}
break;

case AST_CONTROL_CONNECTED_LINE: {
struct ast_party_connected_line* const cnncd = ast_channel_connected(channel);
SCOPED_MUTEX(pvt_lock, &pvt->lock);
SCOPED_AO2LOCK(pvt_lock, pvt);
ast_log(LOG_NOTICE, "[%s] Connected party is now %s <%s>\n", PVT_ID(pvt), S_COR(cnncd->id.name.valid, cnncd->id.name.str, ""),
S_COR(cnncd->id.number.valid, cnncd->id.number.str, ""));
break;
Expand Down Expand Up @@ -998,6 +998,7 @@ struct ast_channel* channel_new(struct pvt* pvt, int ast_state, const char* cid_
struct ast_format_cap* const cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
ast_format_cap_append_by_type(cap, AST_MEDIA_TYPE_TEXT);
ast_channel_nativeformats_set(channel, cap);
ao2_ref(cap, -1);
} else {
struct ast_format* const fmt = (struct ast_format*)pvt_get_audio_format(pvt);
#if PTIME_USE_DEFAULT
Expand All @@ -1009,6 +1010,7 @@ struct ast_channel* channel_new(struct pvt* pvt, int ast_state, const char* cid_
ast_format_cap_append(cap, fmt, ms);
ast_format_cap_set_framing(cap, ms);
ast_channel_nativeformats_set(channel, cap);
ao2_ref(cap, -1);

ast_channel_set_rawreadformat(channel, fmt);
ast_channel_set_rawwriteformat(channel, fmt);
Expand Down
30 changes: 17 additions & 13 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,26 @@

static char* complete_device(const char* word, int state)
{
char* res = NULL;
int which = 0;
const size_t wordlen = strlen(word);
struct pvt* pvt;
char* res = NULL;
int which = 0;
int wordlen = strlen(word);

AST_RWLIST_RDLOCK(&gpublic->devices);
AST_RWLIST_TRAVERSE(&gpublic->devices, pvt, entry) {
SCOPED_MUTEX(pvt_lock, &pvt->lock);
struct ao2_iterator i = ao2_iterator_init(gpublic->pvts, 0);
while ((pvt = ao2_iterator_next(&i))) {
if (ao2_lock(pvt)) {
ao2_ref(pvt, -1);
continue;
}
if (!strncasecmp(PVT_ID(pvt), word, wordlen) && ++which > state) {
res = ast_strdup(PVT_ID(pvt));
ao2_unlock_and_unref(pvt);
break;
}
}
AST_RWLIST_UNLOCK(&gpublic->devices);

ao2_unlock_and_unref(pvt);
}
ao2_iterator_destroy(&i);
return res;
}

Expand All @@ -87,14 +92,13 @@ static char* cli_show_devices(struct ast_cli_entry* e, int cmd, struct ast_cli_a

ast_cli(a->fd, FORMAT1, "ID", "Group", "State", "RSSI", "Mode", "Provider Name", "Model", "Firmware", "Number");

AST_RWLIST_RDLOCK(&gpublic->devices);
AST_RWLIST_TRAVERSE(&gpublic->devices, pvt, entry) {
SCOPED_MUTEX(pvt_lock, &pvt->lock);
struct ao2_iterator i = ao2_iterator_init(gpublic->pvts, 0);
while ((pvt = ao2_iterator_next(&i))) {
SCOPED_AO2LOCK(pvt_lock, pvt);
ast_cli(a->fd, FORMAT2, PVT_ID(pvt), CONF_SHARED(pvt, group), pvt_str_state(pvt), pvt->rssi, pvt->act, pvt->provider_name, pvt->model, pvt->firmware,
pvt->imei, pvt->imsi, pvt->subscriber_number);
}
AST_RWLIST_UNLOCK(&gpublic->devices);

ao2_iterator_destroy(&i);
return CLI_SUCCESS;
}

Expand Down
8 changes: 4 additions & 4 deletions src/cpvt.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,14 @@ int cpvt_change_state(struct cpvt* const cpvt, call_state_t newstate, int cause)
return 1;
}

void cpvt_lock(struct cpvt* const cpvt)
int cpvt_lock(struct cpvt* const cpvt)
{
struct pvt* const pvt = cpvt->pvt;
if (!pvt) {
return;
}

ast_mutex_trylock(&pvt->lock);
return ao2_ref_and_lock(pvt);
}

void cpvt_try_lock(struct cpvt* const cpvt)
Expand All @@ -392,9 +392,9 @@ void cpvt_try_lock(struct cpvt* const cpvt)
return;
}

ast_mutex_t* const mutex = &pvt->lock;
ao2_ref(pvt, 1);

while (ast_mutex_trylock(mutex)) {
while (ao2_trylock(pvt)) {
CHANNEL_DEADLOCK_AVOIDANCE(channel);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cpvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ typedef struct cpvt {
struct cpvt* cpvt_alloc(struct pvt* pvt, int call_idx, unsigned dir, call_state_t statem, unsigned local_channel);
void cpvt_free(struct cpvt* cpvt);

void cpvt_lock(struct cpvt* const);
int cpvt_lock(struct cpvt* const);
void cpvt_try_lock(struct cpvt* const);
void cpvt_unlock(struct cpvt* const);

Expand Down
3 changes: 2 additions & 1 deletion src/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ static struct pvt* get_pvt(const char* dev_name, int online)
}

if (!pvt->connected || (online && !(pvt->initialized && pvt->gsm_registered))) {
ast_mutex_unlock(&pvt->lock);
ao2_unlock(pvt);
ao2_ref(pvt, -1);
chan_quectel_err = E_DEVICE_DISABLED;
return NULL;
}
Expand Down
21 changes: 12 additions & 9 deletions src/monitor_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ static void monitor_threadproc_pvt(struct pvt* const pvt)

RAII_VAR(struct ast_str* const, result, ast_str_create(RINGBUFFER_SIZE), ast_free);

ast_mutex_lock(&pvt->lock);
ao2_lock(pvt);
RAII_VAR(char* const, dev, ast_strdup(PVT_ID(pvt)), ast_free);

RAII_VAR(struct ast_taskprocessor*, tps, threadpool_serializer(gpublic->threadpool, dev), ast_taskprocessor_unreference);
Expand All @@ -197,15 +197,15 @@ static void monitor_threadproc_pvt(struct pvt* const pvt)
goto e_cleanup;
}

ast_mutex_unlock(&pvt->lock);
ao2_unlock(pvt);

int read_result = 0;
while (1) {
if (ast_taskprocessor_push(tps, handle_expired_reports_taskproc, pvt)) {
ast_debug(5, "[%s] Unable to handle exprired reports\n", dev);
}

if (ast_mutex_trylock(&pvt->lock)) { // pvt unlocked
if (ao2_trylock(pvt)) { // pvt unlocked
int t = RESPONSE_READ_TIMEOUT;
if (!at_wait(fd, &t)) {
if (ast_taskprocessor_push(tps, at_enqueue_ping_taskproc, pvt)) {
Expand All @@ -229,7 +229,7 @@ static void monitor_threadproc_pvt(struct pvt* const pvt)
is_cmd_timeout = 0;
}

ast_mutex_unlock(&pvt->lock);
ao2_unlock(pvt);

if (is_cmd_timeout) {
if (t <= 0) {
Expand Down Expand Up @@ -276,9 +276,9 @@ static void monitor_threadproc_pvt(struct pvt* const pvt)
break;
}

if (!ast_mutex_trylock(&pvt->lock)) {
if (!ao2_trylock(pvt)) {
PVT_STAT(pvt, d_read_bytes) += iovcnt;
ast_mutex_unlock(&pvt->lock);
ao2_unlock(pvt);
}

struct iovec iov[2];
Expand All @@ -303,7 +303,7 @@ static void monitor_threadproc_pvt(struct pvt* const pvt)
}
}

ast_mutex_lock(&pvt->lock);
ao2_unlock(pvt);

e_cleanup:
if (!pvt->initialized) {
Expand All @@ -316,20 +316,23 @@ static void monitor_threadproc_pvt(struct pvt* const pvt)
e_restart:
pvt_disconnect(pvt);
// pvt->monitor_running = 0;
ast_mutex_unlock(&pvt->lock);
ao2_unlock(pvt);
}

static void* monitor_threadproc(void* _pvt)
{
struct pvt* const pvt = _pvt;
ao2_ref(pvt, -1);
monitor_threadproc_pvt(pvt);
/* TODO: wakeup discovery thread after some delay */
return NULL;
}

int pvt_monitor_start(struct pvt* pvt)
{
ao2_ref(pvt, 1);
if (ast_pthread_create_background(&pvt->monitor_thread, NULL, monitor_threadproc, pvt) < 0) {
ao2_ref(pvt, -1);
pvt->monitor_thread = AST_PTHREADT_NULL;
return 0;
}
Expand All @@ -348,7 +351,7 @@ void pvt_monitor_stop(struct pvt* pvt)

{
const pthread_t id = pvt->monitor_thread;
SCOPED_LOCK(pvt_lock, &pvt->lock, ast_mutex_unlock, ast_mutex_lock); // scoped UNlock
SCOPED_LOCK(pvt_lock, pvt, ao2_unlock, ao2_lock); // scoped UNlock
pthread_join(id, NULL);
}

Expand Down

0 comments on commit 0702991

Please sign in to comment.