[asterisk-commits] mmichelson: branch mmichelson/pool_shark r380290 - in /team/mmichelson/pool_s...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Mon Jan 28 17:05:54 CST 2013
Author: mmichelson
Date: Mon Jan 28 17:05:51 2013
New Revision: 380290
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=380290
Log:
Make gulp_request use the threadpool.
This marks the first time that we've needed a synchronous
use of the threadpool. If I come across another such instance,
I'll make the method more generic so that there is not much
repeated code.
Modified:
team/mmichelson/pool_shark/channels/chan_gulp.c
team/mmichelson/pool_shark/include/asterisk/res_sip_session.h
team/mmichelson/pool_shark/res/res_sip_session.c
Modified: team/mmichelson/pool_shark/channels/chan_gulp.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/pool_shark/channels/chan_gulp.c?view=diff&rev=380290&r1=380289&r2=380290
==============================================================================
--- team/mmichelson/pool_shark/channels/chan_gulp.c (original)
+++ team/mmichelson/pool_shark/channels/chan_gulp.c Mon Jan 28 17:05:51 2013
@@ -131,18 +131,6 @@
.get_codec = gulp_get_codec,
.update_peer = gulp_set_rtp_peer,
};
-
-static void pj_thread_register_check(void)
-{
- pj_thread_desc desc;
- pj_thread_t *thread;
-
- if (pj_thread_is_registered() == PJ_TRUE) {
- return;
- }
-
- pj_thread_register("Asterisk Thread", desc, &thread);
-}
/*! \brief Function called to create a new Gulp Asterisk channel */
static struct ast_channel *gulp_new(struct ast_sip_session *session, int state, const char *exten, const char *title, const char *linkedid, const char *cid_name)
@@ -610,30 +598,108 @@
return -1;
}
+struct request_data {
+ ast_cond_t cond;
+ ast_mutex_t lock;
+ struct ast_sip_session *session;
+ int success;
+ int complete;
+ struct ast_sip_work *work;
+ const char *dest;
+};
+
+static void request_data_destroy(void *obj)
+{
+ struct request_data *req_data = obj;
+ ast_mutex_destroy(&req_data->lock);
+ ast_cond_destroy(&req_data->cond);
+ /* Don't unref the session or destroy the work since they
+ * are now owned by other structures. They'll be destroyed
+ * in due time
+ */
+}
+
+static struct request_data *request_data_alloc(struct ast_sip_work *work, const char *dest)
+{
+ struct request_data *req_data = ao2_alloc(sizeof(*req_data), request_data_destroy);
+ if (!req_data) {
+ return NULL;
+ }
+ ast_mutex_init(&req_data->lock);
+ ast_cond_init(&req_data->cond, NULL);
+ req_data->dest = ast_strdup(dest);
+ if (!req_data->dest) {
+ ao2_cleanup(req_data);
+ return NULL;
+ }
+ req_data->work = work;
+ return req_data;
+}
+
+static int request(void *obj)
+{
+ struct request_data *req_data = obj;
+ struct ast_sip_endpoint *endpoint = ast_sip_endpoint_alloc("constant");
+ struct ast_sip_session *session = NULL;
+
+ if (!endpoint) {
+ goto end;;
+ }
+
+ ast_string_field_set(endpoint, context, "default");
+ ast_parse_allow_disallow(&endpoint->prefs, endpoint->codecs, "ulaw", 1);
+
+ if (!(session = ast_sip_session_create_outgoing(endpoint, req_data->dest, req_data->work))) {
+ goto end;
+ }
+
+ req_data->success = 1;
+ req_data->session = session;
+
+end:
+ ast_mutex_lock(&req_data->lock);
+ req_data->complete = 1;
+ ast_cond_signal(&req_data->cond);
+ ast_mutex_unlock(&req_data->lock);
+ return 0;
+}
+
/*! \brief Function called by core to create a new outgoing Gulp session */
static struct ast_channel *gulp_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause)
{
- struct ast_sip_endpoint *endpoint = ast_sip_endpoint_alloc("constant");
- struct ast_sip_session *session = NULL;
-
- if (!endpoint) {
+ struct ast_sip_work *work = ast_sip_create_work();
+ struct request_data *req_data = request_data_alloc(work, data);
+ struct ast_sip_session *session;
+ if (!work) {
return NULL;
}
- ast_string_field_set(endpoint, context, "default");
- ast_parse_allow_disallow(&endpoint->prefs, endpoint->codecs, "ulaw", 1);
-
- pj_thread_register_check();
-
- if (!(session = ast_sip_session_create_outgoing(endpoint, data))) {
+ /* XXX This probably needs to be made more generic. It may be
+ * necessary to push tasks synchronously in other places, and repeating
+ * this logic is cray-cray.
+ */
+ if (ast_sip_push_task(work, request, req_data)) {
+ ao2_cleanup(req_data);
+ ast_sip_destroy_work(work);
return NULL;
}
+
+ ast_mutex_lock(&req_data->lock);
+ while (!req_data->complete) {
+ ast_cond_wait(&req_data->cond, &req_data->lock);
+ }
+
+ if (!req_data->success) {
+ return NULL;
+ }
+ session = req_data->session;
if (!(session->channel = gulp_new(session, AST_STATE_DOWN, NULL, NULL, requestor ? ast_channel_linkedid(requestor) : NULL, NULL))) {
/* Session needs to be terminated prematurely */
return NULL;
}
+ ao2_cleanup(req_data);
return session->channel;
}
Modified: team/mmichelson/pool_shark/include/asterisk/res_sip_session.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/pool_shark/include/asterisk/res_sip_session.h?view=diff&rev=380290&r1=380289&r2=380290
==============================================================================
--- team/mmichelson/pool_shark/include/asterisk/res_sip_session.h (original)
+++ team/mmichelson/pool_shark/include/asterisk/res_sip_session.h Mon Jan 28 17:05:51 2013
@@ -149,8 +149,9 @@
*
* \param endpoint The endpoint that this session uses for settings
* \param uri The URI to call
- */
-struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint *endpoint, const char *uri);
+ * \param work SIP work queue to use for this session. May be NULL.
+ */
+struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint *endpoint, const char *uri, struct ast_sip_work *work);
/*!
* \brief Register an SDP handler
Modified: team/mmichelson/pool_shark/res/res_sip_session.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/pool_shark/res/res_sip_session.c?view=diff&rev=380290&r1=380289&r2=380290
==============================================================================
--- team/mmichelson/pool_shark/res/res_sip_session.c (original)
+++ team/mmichelson/pool_shark/res/res_sip_session.c Mon Jan 28 17:05:51 2013
@@ -539,7 +539,7 @@
return 0;
}
-struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint *endpoint, const char *uri)
+struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint *endpoint, const char *uri, struct ast_sip_work *work)
{
pj_str_t local_uri = pj_str("sip:temp at localhost"), remote_uri = pj_str((char*)uri);
pjsip_dialog *dlg = NULL;
@@ -595,7 +595,7 @@
return NULL;
}
- if (!(session = ast_sip_session_alloc(endpoint, inv_session, NULL))) {
+ if (!(session = ast_sip_session_alloc(endpoint, inv_session, work))) {
pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
return NULL;
}
More information about the asterisk-commits
mailing list