Change in ...libosmocore[master]: add osmo_fsm_inst_dispatch_and_watch()

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

neels gerrit-no-reply at lists.osmocom.org
Wed Oct 2 19:10:33 UTC 2019


neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/15661


Change subject: add osmo_fsm_inst_dispatch_and_watch()
......................................................................

add osmo_fsm_inst_dispatch_and_watch()

The most common use case for osmo_fsm_inst_watch() is to dispatch an event and
determine whether that caused deallocation. Add this convenience wrapper to
simplify implementing this use case.

This:

   void event_action()
   {
           struct osmo_fsm_inst_watcher watch_bar;

           osmo_fsm_inst_watch(&watch_bar, bar);
           osmo_fsm_inst_dispatch(foo, FOO_EVENT, NULL);
           osmo_fsm_inst_unwatch(&watch_bar);

           if (watch_bar.exists)
                   osmo_fsm_inst_dispatch(bar, BAR_EVENT, NULL);
   }

becomes:

   void event_action()
   {
           if (osmo_fsm_inst_dispatch_and_watch(foo, FOO_EVENT, NULL,
	                                        bar, NULL))
                   osmo_fsm_inst_dispatch(bar, BAR_EVENT, NULL);
   }

Change-Id: Ie9a77783d2052d4677fc5d0a7eb15f9e3b0fda8f
---
M include/osmocom/core/fsm.h
M src/fsm.c
2 files changed, 41 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/61/15661/1

diff --git a/include/osmocom/core/fsm.h b/include/osmocom/core/fsm.h
index 2487124..8e9ed77 100644
--- a/include/osmocom/core/fsm.h
+++ b/include/osmocom/core/fsm.h
@@ -301,6 +301,20 @@
 int _osmo_fsm_inst_dispatch(struct osmo_fsm_inst *fi, uint32_t event, void *data,
 			    const char *file, int line);
 
+/*! dispatch an event to an FSM instance, while watching out for deallocation of this or another instance.
+ *
+ * This is a convenience wrapper for using osmo_fsm_inst_watch() and osmo_fsm_inst_dispatch().
+ *
+ * This is a macro that calls _osmo_fsm_inst_dispatch_and_watch() with the given
+ * parameters as well as the caller's source file and line number for logging
+ * purposes. See there for documentation.
+ */
+#define osmo_fsm_inst_dispatch_and_watch(fi, event, data, watch_fi, rc_p) \
+	_osmo_fsm_inst_dispatch_and_watch(fi, event, data, watch_fi, rc_p, __FILE__, __LINE__)
+bool _osmo_fsm_inst_dispatch_and_watch(struct osmo_fsm_inst *fi, uint32_t event, void *data,
+				       struct osmo_fsm_inst *watch_fi, int *rc_p,
+				       const char *file, int line);
+
 /*! Terminate FSM instance with given cause
  *
  *  This is a macro that calls _osmo_fsm_inst_term() with the given parameters
diff --git a/src/fsm.c b/src/fsm.c
index 03f22cb..526a7ad 100644
--- a/src/fsm.c
+++ b/src/fsm.c
@@ -836,6 +836,33 @@
 	return 0;
 }
 
+/*! dispatch an event to an FSM instance, while watching out for deallocation of this or another instance.
+ *
+ *  This is a convenience wrapper for using osmo_fsm_inst_watch() and osmo_fsm_inst_dispatch().
+ *
+ *  \param[in] fi  FSM instance to dispatch event to.
+ *  \param[in] event  Event to dispatch.
+ *  \param[in] data  Data to pass along with the event.
+ *  \param[in] watch_fi  FSM instance to watch deallocation of (possibly but not necessarily identical to fi).
+ *  \param[out] rc_p  If not NULL, return the _osmo_fsm_inst_dispatch() return value in this int.
+ *  \param[in] file  Calling source file (from osmo_fsm_inst_dispatch macro)
+ *  \param[in] line  Calling source line (from osmo_fsm_inst_dispatch macro)
+ *  \returns true if watch_fi still exists after dispatching the event, false if it was deallocated or was NULL.
+ */
+bool _osmo_fsm_inst_dispatch_and_watch(struct osmo_fsm_inst *fi, uint32_t event, void *data,
+				       struct osmo_fsm_inst *watch_fi, int *rc_p,
+				       const char *file, int line)
+{
+	int rc;
+	struct osmo_fsm_inst_watcher watcher;
+	osmo_fsm_inst_watch(&watcher, watch_fi);
+	rc = _osmo_fsm_inst_dispatch(fi, event, data, file, line);
+	osmo_fsm_inst_unwatch(&watcher);
+	if (rc_p)
+		*rc_p = rc;
+	return watcher.exists;
+}
+
 /*! Terminate FSM instance with given cause
  *
  *  This safely terminates the given FSM instance by first iterating

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/15661
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Ie9a77783d2052d4677fc5d0a7eb15f9e3b0fda8f
Gerrit-Change-Number: 15661
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20191002/5d1b50c2/attachment.htm>


More information about the gerrit-log mailing list