[Asterisk-code-review] stasis: Remove silly usage of RAII VAR. (asterisk[13])
Corey Farrell
asteriskteam at digium.com
Tue Jan 9 18:38:47 CST 2018
Corey Farrell has uploaded this change for review. ( https://gerrit.asterisk.org/7902
Change subject: stasis: Remove silly usage of RAII_VAR.
......................................................................
stasis: Remove silly usage of RAII_VAR.
Change-Id: Ib11193531e797bcb16bba560a408eab155f706d1
---
M main/stasis.c
1 file changed, 57 insertions(+), 29 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/02/7902/1
diff --git a/main/stasis.c b/main/stasis.c
index 5f080d1..bb1fbcd 100644
--- a/main/stasis.c
+++ b/main/stasis.c
@@ -424,10 +424,10 @@
{
/* Notify that the final message has been received */
if (stasis_subscription_final_message(sub, message)) {
- SCOPED_AO2LOCK(lock, sub);
-
+ ao2_lock(sub);
sub->final_message_rxed = 1;
ast_cond_signal(&sub->join_cond);
+ ao2_unlock(sub);
}
/* Since sub is mostly immutable, no need to lock sub */
@@ -435,10 +435,10 @@
/* Notify that the final message has been processed */
if (stasis_subscription_final_message(sub, message)) {
- SCOPED_AO2LOCK(lock, sub);
-
+ ao2_lock(sub);
sub->final_message_processed = 1;
ast_cond_signal(&sub->join_cond);
+ ao2_unlock(sub);
}
}
@@ -456,7 +456,7 @@
int needs_mailbox,
int use_thread_pool)
{
- RAII_VAR(struct stasis_subscription *, sub, NULL, ao2_cleanup);
+ struct stasis_subscription *sub;
if (!topic) {
return NULL;
@@ -488,6 +488,8 @@
sub->mailbox = ast_taskprocessor_get(tps_name, TPS_REF_DEFAULT);
}
if (!sub->mailbox) {
+ ao2_ref(sub, -1);
+
return NULL;
}
ast_taskprocessor_set_local(sub->mailbox, sub);
@@ -502,11 +504,12 @@
ast_cond_init(&sub->join_cond, NULL);
if (topic_add_subscription(topic, sub) != 0) {
+ ao2_ref(sub, -1);
+
return NULL;
}
send_subscription_subscribe(topic, sub);
- ao2_ref(sub, +1);
return sub;
}
@@ -537,18 +540,21 @@
{
/* The subscription may be the last ref to this topic. Hold
* the topic ref open until after the unlock. */
- RAII_VAR(struct stasis_topic *, topic,
- ao2_bump(sub ? sub->topic : NULL), ao2_cleanup);
+ struct stasis_topic *topic;
if (!sub) {
return NULL;
}
+
+ topic = ao2_bump(sub->topic);
/* We have to remove the subscription first, to ensure the unsubscribe
* is the final message */
if (topic_remove_subscription(sub->topic, sub) != 0) {
ast_log(LOG_ERROR,
"Internal error: subscription has invalid topic\n");
+ ao2_cleanup(topic);
+
return NULL;
}
@@ -562,6 +568,8 @@
/* Unsubscribing unrefs the subscription */
ao2_cleanup(sub);
+ ao2_cleanup(topic);
+
return NULL;
}
@@ -580,22 +588,26 @@
void stasis_subscription_join(struct stasis_subscription *subscription)
{
if (subscription) {
- SCOPED_AO2LOCK(lock, subscription);
-
+ ao2_lock(subscription);
/* Wait until the processed flag has been set */
while (!subscription->final_message_processed) {
ast_cond_wait(&subscription->join_cond,
ao2_object_get_lockaddr(subscription));
}
+ ao2_unlock(subscription);
}
}
int stasis_subscription_is_done(struct stasis_subscription *subscription)
{
if (subscription) {
- SCOPED_AO2LOCK(lock, subscription);
+ int ret;
- return subscription->final_message_rxed;
+ ao2_lock(subscription);
+ ret = subscription->final_message_rxed;
+ ao2_unlock(subscription);
+
+ return ret;
}
/* Null subscription is about as done as you can get */
@@ -623,13 +635,15 @@
if (sub) {
size_t i;
struct stasis_topic *topic = sub->topic;
- SCOPED_AO2LOCK(lock_topic, topic);
+ ao2_lock(topic);
for (i = 0; i < AST_VECTOR_SIZE(&topic->subscribers); ++i) {
if (AST_VECTOR_GET(&topic->subscribers, i) == sub) {
+ ao2_unlock(topic);
return 1;
}
}
+ ao2_unlock(topic);
}
return 0;
@@ -670,8 +684,8 @@
static int topic_add_subscription(struct stasis_topic *topic, struct stasis_subscription *sub)
{
size_t idx;
- SCOPED_AO2LOCK(lock, topic);
+ ao2_lock(topic);
/* The reference from the topic to the subscription is shared with
* the owner of the subscription, which will explicitly unsubscribe
* to release it.
@@ -684,6 +698,7 @@
topic_add_subscription(
AST_VECTOR_GET(&topic->upstream_topics, idx), sub);
}
+ ao2_unlock(topic);
return 0;
}
@@ -691,15 +706,17 @@
static int topic_remove_subscription(struct stasis_topic *topic, struct stasis_subscription *sub)
{
size_t idx;
- SCOPED_AO2LOCK(lock_topic, topic);
+ ao2_lock(topic);
for (idx = 0; idx < AST_VECTOR_SIZE(&topic->upstream_topics); ++idx) {
topic_remove_subscription(
AST_VECTOR_GET(&topic->upstream_topics, idx), sub);
}
-
- return AST_VECTOR_REMOVE_ELEM_UNORDERED(&topic->subscribers, sub,
+ idx = AST_VECTOR_REMOVE_ELEM_UNORDERED(&topic->subscribers, sub,
AST_VECTOR_ELEM_CLEANUP_NOOP);
+ ao2_unlock(topic);
+
+ return idx;
}
/*!
@@ -1216,25 +1233,25 @@
struct ast_multi_object_blob *ast_multi_object_blob_create(struct ast_json *blob)
{
int type;
- RAII_VAR(struct ast_multi_object_blob *, multi,
- ao2_alloc(sizeof(*multi), multi_object_blob_dtor),
- ao2_cleanup);
+ struct ast_multi_object_blob *multi;
ast_assert(blob != NULL);
+ multi = ao2_alloc(sizeof(*multi), multi_object_blob_dtor);
if (!multi) {
return NULL;
}
for (type = 0; type < STASIS_UMOS_MAX; ++type) {
if (AST_VECTOR_INIT(&multi->snapshots[type], 0)) {
+ ao2_ref(multi, -1);
+
return NULL;
}
}
multi->blob = ast_json_ref(blob);
- ao2_ref(multi, +1);
return multi;
}
@@ -1251,9 +1268,9 @@
void ast_multi_object_blob_single_channel_publish(struct ast_channel *chan,
struct stasis_message_type *type, struct ast_json *blob)
{
- RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
- RAII_VAR(struct ast_channel_snapshot *, channel_snapshot, NULL, ao2_cleanup);
- RAII_VAR(struct ast_multi_object_blob *, multi, NULL, ao2_cleanup);
+ struct stasis_message *message;
+ struct ast_channel_snapshot *channel_snapshot;
+ struct ast_multi_object_blob *multi;
if (!type) {
return;
@@ -1265,13 +1282,20 @@
}
channel_snapshot = ast_channel_snapshot_create(chan);
- ao2_ref(channel_snapshot, +1);
+ if (!channel_snapshot) {
+ ao2_ref(multi, -1);
+ return;
+ }
+
+ /* this call steals the channel_snapshot reference */
ast_multi_object_blob_add(multi, STASIS_UMOS_CHANNEL, channel_snapshot);
message = stasis_message_create(type, multi);
+ ao2_ref(multi, -1);
if (message) {
/* app_userevent still publishes to channel */
stasis_publish(ast_channel_topic(chan), message);
+ ao2_ref(message, -1);
}
}
@@ -1280,7 +1304,7 @@
struct stasis_message *message,
const struct stasis_message_sanitizer *sanitize)
{
- RAII_VAR(struct ast_json *, out, NULL, ast_json_unref);
+ struct ast_json *out;
struct ast_multi_object_blob *multi = stasis_message_data(message);
struct ast_json *blob = multi->blob;
const struct timeval *tv = stasis_message_timestamp(message);
@@ -1322,7 +1346,8 @@
}
}
}
- return ast_json_ref(out);
+
+ return out;
}
/*! \internal \brief convert multi object blob to ami string */
@@ -1515,17 +1540,19 @@
int stasis_message_type_declined(const char *name)
{
- RAII_VAR(struct stasis_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
+ struct stasis_config *cfg = ao2_global_obj_ref(globals);
char *name_in_declined;
int res;
if (!cfg || !cfg->declined_message_types) {
+ ao2_cleanup(cfg);
return 0;
}
name_in_declined = ao2_find(cfg->declined_message_types->declined, name, OBJ_SEARCH_KEY);
res = name_in_declined ? 1 : 0;
ao2_cleanup(name_in_declined);
+ ao2_ref(cfg, -1);
if (res) {
ast_log(LOG_NOTICE, "Declining to allocate Stasis message type '%s' due to configuration\n", name);
}
@@ -1571,7 +1598,7 @@
int stasis_init(void)
{
- RAII_VAR(struct stasis_config *, cfg, NULL, ao2_cleanup);
+ struct stasis_config *cfg;
int cache_init;
struct ast_threadpool_options threadpool_opts = { 0, };
@@ -1632,6 +1659,7 @@
threadpool_opts.max_size = cfg->threadpool_options->max_size;
threadpool_opts.idle_timeout = cfg->threadpool_options->idle_timeout_sec;
pool = ast_threadpool_create("stasis-core", NULL, &threadpool_opts);
+ ao2_ref(cfg, -1);
if (!pool) {
ast_log(LOG_ERROR, "Failed to create 'stasis-core' threadpool\n");
return -1;
--
To view, visit https://gerrit.asterisk.org/7902
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib11193531e797bcb16bba560a408eab155f706d1
Gerrit-Change-Number: 7902
Gerrit-PatchSet: 1
Gerrit-Owner: Corey Farrell <git at cfware.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180109/9719ffa6/attachment-0001.html>
More information about the asterisk-code-review
mailing list