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 2a15561
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 233 deletions.
485 changes: 299 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
22 changes: 13 additions & 9 deletions src/cpvt.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "at_queue.h" /* struct at_queue_task */
#include "chan_quectel.h" /* struct pvt */
#include "channel.h"
#include "helpers.h"
#include "mutils.h" /* ARRAY_LEN() */

const char* call_state2str(call_state_t state)
Expand Down Expand Up @@ -126,12 +127,15 @@ void cpvt_free(struct cpvt* cpvt)
ast_debug(3, "[%s] Destroy cpvt - idx:%d dir:%d state:%s flags:%d channel:%s\n", PVT_ID(pvt), cpvt->call_idx, CPVT_DIRECTION(cpvt),
call_state2str(cpvt->state), cpvt->flags, cpvt->channel ? "attached" : "detached");

if (PVT_NO_CHANS(pvt)) {
pvt_on_remove_last_channel(pvt);
pvt_try_restate(pvt);
if (!CPVT_TEST_FLAG(cpvt, CALL_FLAG_DISCONNECTING)) {
if (PVT_NO_CHANS(pvt)) {
pvt_on_remove_last_channel(pvt);
pvt_try_restate(pvt);
}

decrease_chan_counters(cpvt, pvt);
}

decrease_chan_counters(cpvt, pvt);
relink_to_sys_chan(cpvt, pvt);

ast_free(cpvt->buffer);
Expand Down Expand Up @@ -370,14 +374,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;
return -1;
}

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

void cpvt_try_lock(struct cpvt* const cpvt)
Expand All @@ -392,9 +396,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
5 changes: 3 additions & 2 deletions src/cpvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ typedef enum {
CALL_FLAG_MULTIPARTY = 256, /*!< internal, CLCC mpty is 1 */
CALL_FLAG_DIRECTION = 512, /*!< call direction */
CALL_FLAG_LOCAL_CHANNEL = 1024, /*!< local channel flag */
CALL_FLAG_INTERNAL_REQUEST = 2048 /*!< internal request */
CALL_FLAG_INTERNAL_REQUEST = 2048, /*!< internal request */
CALL_FLAG_DISCONNECTING = 4096 /*!< object is disconnecting */
} call_flag_t;

#define CALL_DIR_INCOMING 1u
Expand Down Expand Up @@ -99,7 +100,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
23 changes: 22 additions & 1 deletion src/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ int is_valid_phone_number(const char* number)
return 1;
}

int __ao2_unlock_and_unref(void* obj, const char* file, const char* func, int line, const char* var)
{
if (__ao2_unlock(obj, file, func, line, var)) {
return 0;
}
__ao2_ref(obj, -1, NULL, file, line, func);

return 1;
}

int __ao2_ref_and_lock(void* obj, const char* file, const char* func, int line, const char* var)
{
__ao2_ref(obj, +1, NULL, file, line, func);
if (__ao2_lock(obj, AO2_LOCK_REQ_MUTEX, file, func, line, var)) {
__ao2_ref(obj, -1, NULL, file, line, func);
return 0;
}
return 1;
}

static struct pvt* get_pvt(const char* dev_name, int online)
{
struct pvt* const pvt = pvt_find_by_ext(dev_name);
Expand All @@ -58,7 +78,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
7 changes: 7 additions & 0 deletions src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
#ifndef CHAN_QUECTEL_HELPERS_H_INCLUDED
#define CHAN_QUECTEL_HELPERS_H_INCLUDED

#include <asterisk/astobj2.h>
#include <asterisk/localtime.h>
#include <asterisk/strings.h>

#include "chan_quectel.h" /* restate_time_t */
#include "dc_config.h" /* call_waiting_t */

int __ao2_unlock_and_unref(void* obj, const char* file, const char* func, int line, const char* var);
#define AO2_UNLOCK_AND_UNREF(a) __ao2_unlock_and_unref(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)

int __ao2_ref_and_lock(void* obj, const char* file, const char* func, int line, const char* var);
#define AO2_REF_AND_LOCK(a) __ao2_ref_and_lock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)

/* return status string of sending, status arg is optional */
int send_ussd(const char* dev_name, const char* ussd);
int send_sms(const char* const resource, const char* const sca, const char* const destination, const char* const message, int validity, int report);
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 2a15561

Please sign in to comment.