[asterisk-commits] mmichelson: branch mmichelson/pool_shark2 r381915 - in /team/mmichelson/pool_...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Feb 23 14:28:22 CST 2013


Author: mmichelson
Date: Sat Feb 23 14:28:18 2013
New Revision: 381915

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=381915
Log:
Edit documentation explaining how incoming messages are distributed to servants.

This explains that incoming requests and responses are automatically distributed
to servant threads.

While writing the documentation I realized that there was no easy way to discern
whether you are currently in a servant thread or not. So I've modified SIP thread
startup in order to set a thread-local identifier. Then I added a function called
ast_sip_thread_is_servant() that can be used to find if the current thread is
a SIP servant thread. It is not used anywhere yet since the only PJSIP callbacks
that may be called have to be unconditionally pushed to serializers anyway.
However, it could be useful in certain cases.


Modified:
    team/mmichelson/pool_shark2/include/asterisk/res_sip.h
    team/mmichelson/pool_shark2/res/res_sip.c

Modified: team/mmichelson/pool_shark2/include/asterisk/res_sip.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/pool_shark2/include/asterisk/res_sip.h?view=diff&rev=381915&r1=381914&r2=381915
==============================================================================
--- team/mmichelson/pool_shark2/include/asterisk/res_sip.h (original)
+++ team/mmichelson/pool_shark2/include/asterisk/res_sip.h Sat Feb 23 14:28:18 2013
@@ -496,11 +496,15 @@
  *
  * PJSIP threads are those that originate from handling of PJSIP events, such
  * as an incoming SIP request or response, or a transaction timeout. The role
- * of these threads is to process information as quickly as possible. When your
- * code gets called into from one of these threads, your goal should be to do
- * as little as possible before handing the majority of processing off to a
- * servant. Operations such as remote procedure calls or DNS lookups are never
- * to be done in these threads since it can cause performance issues.
+ * of these threads is to process information as quickly as possible so that
+ * the next item on the SIP socket(s) can be serviced. On incoming messages,
+ * Asterisk automatically will push the request to a servant thread. When your
+ * module callback is called, processing will already be in a servant. However,
+ * for other PSJIP events, such as transaction state changes due to timer
+ * expirations, your module will be called into from a PJSIP thread. If you
+ * are called into from a PJSIP thread, then you should push whatever processing
+ * is needed to a servant as soon as possible. You can discern if you are currently
+ * in a SIP servant thread using the \ref ast_sip_thread_is_servant function.
  *
  * \par Servants
  *
@@ -570,6 +574,14 @@
  * \retval -1 Failure
  */
 int ast_sip_push_task_synchronous(struct ast_taskprocessor *serializer, int (*sip_task)(void *), void *task_data);
+
+/*!
+ * \brief Determine if the current thread is a SIP servant thread
+ *
+ * \retval 0 This is not a SIP servant thread
+ * \retval 1 This is a SIP servant thread
+ */
+int ast_sip_thread_is_servant(void);
 
 /*!
  * \brief SIP body description

Modified: team/mmichelson/pool_shark2/res/res_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/pool_shark2/res/res_sip.c?view=diff&rev=381915&r1=381914&r2=381915
==============================================================================
--- team/mmichelson/pool_shark2/res/res_sip.c (original)
+++ team/mmichelson/pool_shark2/res/res_sip.c Sat Feb 23 14:28:18 2013
@@ -451,11 +451,21 @@
 }
 
 AST_THREADSTORAGE(pj_thread_storage);
+AST_THREADSTORAGE(servant_id_storage);
+#define SIP_SERVANT_ID 0xDEFECA7E
 
 static void sip_thread_start(void)
 {
 	pj_thread_desc *desc;
 	pj_thread_t *thread;
+	uint32_t *servant_id;
+
+	servant_id = ast_threadstorage_get(&servant_id_storage, sizeof(*servant_id));
+	if (servant_id) {
+		ast_log(LOG_ERROR, "Could not set SIP servant ID in thread-local storage.\n");
+		return;
+	}
+	*servant_id = SIP_SERVANT_ID;
 
 	desc = ast_threadstorage_get(&pj_thread_storage, sizeof(pj_thread_desc));
 	if (!desc) {
@@ -467,6 +477,18 @@
 	if (pj_thread_register("Asterisk Thread", *desc, &thread) != PJ_SUCCESS) {
 		ast_log(LOG_ERROR, "Couldn't register thread with PJLIB.\n");
 	}
+}
+
+int ast_sip_thread_is_servant(void)
+{
+	uint32_t *servant_id;
+
+	servant_id = ast_threadstorage_get(&servant_id_storage, sizeof(*servant_id));
+	if (!servant_id) {
+		return 0;
+	}
+
+	return *servant_id == SIP_SERVANT_ID;
 }
 
 static int load_module(void)




More information about the asterisk-commits mailing list