context: Add support for [per-thread] global talloc contexts

Rather than having applications maintain their own talloc cotexts,
let's offer some root talloc contexts in libosmocore.  Let's also
make them per thread right from the beginning.  This will help
some multi-threaded applications to use talloc in a thread-safe
way.

Change-Id: Iae39cd57274bf6753ecaf186f229e582b42662e3
diff --git a/src/select.c b/src/select.c
index 7ce135f..394a59d 100644
--- a/src/select.c
+++ b/src/select.c
@@ -36,6 +36,8 @@
 #include <osmocom/core/linuxlist.h>
 #include <osmocom/core/timer.h>
 #include <osmocom/core/logging.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/utils.h>
 
 #include "../config.h"
 
@@ -233,11 +235,7 @@
 	return work;
 }
 
-/*! select main loop integration
- *  \param[in] polling should we pollonly (1) or block on select (0)
- *  \returns 0 if no fd handled; 1 if fd handled; negative in case of error
- */
-int osmo_select_main(int polling)
+static int _osmo_select_main(int polling)
 {
 	fd_set readset, writeset, exceptset;
 	int rc;
@@ -259,10 +257,42 @@
 	/* fire timers */
 	osmo_timers_update();
 
+	OSMO_ASSERT(osmo_ctx->select);
+
 	/* call registered callback functions */
 	return osmo_fd_disp_fds(&readset, &writeset, &exceptset);
 }
 
+/*! select main loop integration
+ *  \param[in] polling should we pollonly (1) or block on select (0)
+ *  \returns 0 if no fd handled; 1 if fd handled; negative in case of error
+ */
+int osmo_select_main(int polling)
+{
+	int rc = _osmo_select_main(polling);
+#ifndef EMBEDDED
+	if (talloc_total_size(osmo_ctx->select) != 0) {
+		osmo_panic("You cannot use the 'select' volatile "
+			   "context if you don't use osmo_select_main_ctx()!\n");
+	}
+#endif
+	return rc;
+}
+
+#ifndef EMBEDDED
+/*! select main loop integration with temporary select-dispatch talloc context
+ *  \param[in] polling should we pollonly (1) or block on select (0)
+ *  \returns 0 if no fd handled; 1 if fd handled; negative in case of error
+ */
+int osmo_select_main_ctx(int polling)
+{
+	int rc = _osmo_select_main(polling);
+	/* free all the children of the volatile 'select' scope context */
+	talloc_free_children(osmo_ctx->select);
+	return rc;
+}
+#endif
+
 /*! find an osmo_fd based on the integer fd
  *  \param[in] fd file descriptor to use as search key
  *  \returns \ref osmo_fd for \ref fd; NULL in case it doesn't exist */