[asterisk-commits] nadi: branch group/trunk-cm-csel-hash r47724 - in /team/group/trunk-cm-csel-h...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Nov 16 06:22:24 MST 2006


Author: nadi
Date: Thu Nov 16 07:22:24 2006
New Revision: 47724

URL: http://svn.digium.com/view/asterisk?view=rev&rev=47724
Log:
statemachine locking

Modified:
    team/group/trunk-cm-csel-hash/include/asterisk/statemachine.h
    team/group/trunk-cm-csel-hash/res/res_statemachine.c

Modified: team/group/trunk-cm-csel-hash/include/asterisk/statemachine.h
URL: http://svn.digium.com/view/asterisk/team/group/trunk-cm-csel-hash/include/asterisk/statemachine.h?view=diff&rev=47724&r1=47723&r2=47724
==============================================================================
--- team/group/trunk-cm-csel-hash/include/asterisk/statemachine.h (original)
+++ team/group/trunk-cm-csel-hash/include/asterisk/statemachine.h Thu Nov 16 07:22:24 2006
@@ -45,6 +45,8 @@
 #define RE_BREAK                    RE_STATE_EVENT(STATE_DEFAULT, EVENT_BREAK)
 #define RE_NONE                     RE_STATE_EVENT(STATE_DEFAULT, EVENT_NONE)
 
+struct statemachine;
+
 struct statemachine_handle_retval {
 	int      state;
 	int      event;
@@ -56,15 +58,6 @@
 	int      state_next;
 	int      event_out;
 	struct statemachine_handle_retval (*handle)(void *p, int state, int event);
-};
-
-struct statemachine {
-	void     *p;
-	int      state;
-	struct statemachine_transition *table;
-	int      num_rows;
-	int      (*send_event)(void *p, int event);
-	void     (*log_event)(void *p, int direction, int state, int event);
 };
 
 struct statemachine * statemachine_create (void *p,

Modified: team/group/trunk-cm-csel-hash/res/res_statemachine.c
URL: http://svn.digium.com/view/asterisk/team/group/trunk-cm-csel-hash/res/res_statemachine.c?view=diff&rev=47724&r1=47723&r2=47724
==============================================================================
--- team/group/trunk-cm-csel-hash/res/res_statemachine.c (original)
+++ team/group/trunk-cm-csel-hash/res/res_statemachine.c Thu Nov 16 07:22:24 2006
@@ -38,6 +38,19 @@
 #include <stdlib.h>
 #include <string.h>
 
+#define LOCK(sm)	ast_mutex_lock(&(sm)->lock)
+#define UNLOCK(sm)	ast_mutex_unlock(&(sm)->lock)
+
+struct statemachine {
+	ast_mutex_t                     lock;
+	void                            *p;
+	int                             state;
+	struct statemachine_transition  *table;
+	int                             num_rows;
+	int                             (*send_event)(void *p, int event);
+	void                            (*log_event)(void *p, int direction, int state, int event);
+};
+
 /* State Machine: Generic */
 struct statemachine * statemachine_create (void *p,
 										   int state,
@@ -57,6 +70,7 @@
 		sm->num_rows = num_rows;
 		sm->send_event = send_event;
 		sm->log_event = log_event;
+		ast_mutex_init(&sm->lock);
 	}
 
 	return sm;
@@ -66,6 +80,7 @@
 {
 	if (sm) {
 		ast_log(LOG_VERBOSE, "Destroying statemachine at state %d ...\n", sm->state);
+		ast_mutex_destroy(&sm->lock);
 		free(sm);
 		ast_module_unref(ast_module_info->self);
 	}
@@ -79,12 +94,15 @@
 		event_out = EVENT_DEFAULT,
 		err,
 		retval = -1,
-		state = sm->state,
+		state,
 		leave = 0;
+
+	LOCK(sm);
 
 	if (sm->log_event)
 		sm->log_event(sm->p, LOG_RECEIVE, sm->state, event);
 
+	state = sm->state;
 	for (; i < sm->num_rows; ++i) {
 		t = &sm->table[i];
 		if ((t->state == state || t->state == STATE_ANY) &&
@@ -128,16 +146,26 @@
 	}
 
 out:
+	UNLOCK(sm);
+
 	return retval;
 }
 
 int statemachine_get_state (struct statemachine *sm)
 {
-	return sm->state;
+	int state;
+
+	LOCK(sm);
+	state = sm->state;
+	UNLOCK(sm);
+
+	return state;
 }
 void statemachine_set_state (struct statemachine *sm, int state)
 {
+	LOCK(sm);
 	sm->state = state;
+	UNLOCK(sm);
 }
 
 static int load_module (void)



More information about the asterisk-commits mailing list