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/.
Harald Welte gerrit-no-reply at lists.osmocom.orgHarald Welte has uploaded this change for review. ( https://gerrit.osmocom.org/13312 Change subject: context: Add support for [per-thread] global talloc contexts ...................................................................... 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 --- M include/Makefile.am A include/osmocom/core/context.h M src/Makefile.am A src/context.c M src/select.c 5 files changed, 58 insertions(+), 2 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/12/13312/1 diff --git a/include/Makefile.am b/include/Makefile.am index 17f7d1c..7389e63 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -13,6 +13,7 @@ osmocom/core/bitvec.h \ osmocom/core/bitcomp.h \ osmocom/core/byteswap.h \ + osmocom/core/context.h \ osmocom/core/conv.h \ osmocom/core/counter.h \ osmocom/core/crc16.h \ diff --git a/include/osmocom/core/context.h b/include/osmocom/core/context.h new file mode 100644 index 0000000..28a8dae --- /dev/null +++ b/include/osmocom/core/context.h @@ -0,0 +1,20 @@ +#pragma once + +/*! per-thread talloc contexts. This works around the problem that talloc is not + * thread-safe. However, one can simply have a different set of talloc contexts for each + * thread, and ensure that allocations made on one thread are always only free'd on that + * very same thread. + * WARNING: Users must make sure they free() on the same thread as they allocate!! */ +struct osmo_talloc_contexts { + /*! global per-thread talloc context. */ + void *global; + /*! volatile select-dispatch context. This context is completely free'd and + * re-created every time the main select loop in osmo_select_main() returns from + * select(2) and calls per-fd callback functions. This allows users of this + * facility to allocate temporary objects like string buffers, message buffers + * and the like which are automatically free'd when going into the next select() + * system call */ + void *select; +}; + +extern __thread struct osmo_talloc_contexts *osmo_ctx; diff --git a/src/Makefile.am b/src/Makefile.am index 27ab702..54e9280 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,7 +13,7 @@ lib_LTLIBRARIES = libosmocore.la libosmocore_la_LIBADD = $(BACKTRACE_LIB) $(TALLOC_LIBS) $(LIBRARY_RT) -libosmocore_la_SOURCES = timer.c timer_gettimeofday.c timer_clockgettime.c \ +libosmocore_la_SOURCES = context.c timer.c timer_gettimeofday.c timer_clockgettime.c \ select.c signal.c msgb.c bits.c \ bitvec.c bitcomp.c counter.c fsm.c \ write_queue.c utils.c socket.c \ diff --git a/src/context.c b/src/context.c new file mode 100644 index 0000000..7163178 --- /dev/null +++ b/src/context.c @@ -0,0 +1,22 @@ +#include <string.h> +#include <errno.h> +#include <osmocom/core/talloc.h> +#include <osmocom/core/context.h> + +__thread struct osmo_talloc_contexts *osmo_ctx; + +int osmo_ctx_init(const char *id) +{ + osmo_ctx = talloc_named(NULL, sizeof(*osmo_ctx), "global-%s", id); + if (!osmo_ctx) + return -ENOMEM; + memset(osmo_ctx, 0, sizeof(*osmo_ctx)); + osmo_ctx->global = osmo_ctx; + return 0; +} + +/* initialize osmo_ctx on main tread */ +static __attribute__((constructor)) void on_dso_load_ctx(void) +{ + osmo_ctx_init("main"); +} diff --git a/src/select.c b/src/select.c index 4e7be35..f5ad174 100644 --- a/src/select.c +++ b/src/select.c @@ -36,6 +36,9 @@ #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 <osmocom/core/context.h> #include "../config.h" @@ -259,8 +262,18 @@ /* fire timers */ osmo_timers_update(); + /* create new volatile 'select' scope context */ + OSMO_ASSERT(!osmo_ctx->select); + osmo_ctx->select = talloc_named_const(osmo_ctx->global, 0, "select"); + OSMO_ASSERT(osmo_ctx->select); + /* call registered callback functions */ - return osmo_fd_disp_fds(&readset, &writeset, &exceptset); + rc = osmo_fd_disp_fds(&readset, &writeset, &exceptset); + + /* free the volatile 'select' scope context and any of its siblings */ + talloc_free(osmo_ctx->select); + osmo_ctx->select = NULL; + return rc; } /*! find an osmo_fd based on the integer fd -- To view, visit https://gerrit.osmocom.org/13312 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: newchange Gerrit-Change-Id: Iae39cd57274bf6753ecaf186f229e582b42662e3 Gerrit-Change-Number: 13312 Gerrit-PatchSet: 1 Gerrit-Owner: Harald Welte <laforge at gnumonks.org> -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190319/634636a1/attachment.htm>