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/.
Hoernchen gerrit-no-reply at lists.osmocom.orgHoernchen has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-mgw/+/25431 ) Change subject: add a lock-free bounded spsc interthread queue ...................................................................... add a lock-free bounded spsc interthread queue Not entirely wait-free: allows waiting on a fd, and in general fd notifications for poll-loop integration between threads. Change-Id: I4f17042baf76d086ce6b20eb99402dc64c22c657 --- A include/osmocom/mgcp/mgcp_threads_queue.h A src/libosmo-mgcp/mgcp_threads_queue.c 2 files changed, 224 insertions(+), 0 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-mgw refs/changes/31/25431/1 diff --git a/include/osmocom/mgcp/mgcp_threads_queue.h b/include/osmocom/mgcp/mgcp_threads_queue.h new file mode 100644 index 0000000..41f02e8 --- /dev/null +++ b/include/osmocom/mgcp/mgcp_threads_queue.h @@ -0,0 +1,52 @@ +/* + * (C) 2021 by sysmocom s.f.m.c. GmbH <info at sysmocom.de> + * All Rights Reserved + * + * Author: Eric Wild + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include <stdatomic.h> +#include <stdbool.h> +#include <stdlib.h> + +struct spsc { + atomic_uint readptr; + atomic_uint writeptr; + + int efd_r, efd_w; + + int count; + int size_per_buf; + + void *buf; + uintptr_t data[0]; +}; + +struct qchan { + struct spsc *a; + struct spsc *b; +}; + +bool spsc_push(struct spsc *q, void *elem); +bool spsc_pop(struct spsc *q, void *elem); +ssize_t prep_pop(struct spsc *q); +int get_a_rdfd(struct qchan *q); + +struct qchan chan_init(unsigned int count, unsigned size_per_buf); +struct qchan chan_init_ex(unsigned int count, unsigned size_per_buf, bool blockr_a, bool blockw_a, bool blockr_b, + bool blockw_b); +void chan_close(struct qchan *q); diff --git a/src/libosmo-mgcp/mgcp_threads_queue.c b/src/libosmo-mgcp/mgcp_threads_queue.c new file mode 100644 index 0000000..c67bcb3 --- /dev/null +++ b/src/libosmo-mgcp/mgcp_threads_queue.c @@ -0,0 +1,172 @@ +/* + * (C) 2021 by sysmocom s.f.m.c. GmbH <info at sysmocom.de> + * All Rights Reserved + * + * Author: Eric Wild + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include <inttypes.h> +#include <stdatomic.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <sys/eventfd.h> +#include <sys/types.h> +#include <unistd.h> + +#include <osmocom/mgcp/mgcp_threads_queue.h> + +/* +classic lamport circular lockfree spsc queue: +every "side" only writes its own ptr, but may read the other sides ptr +-> own ptr relaxed, other one not so much... + +notify reader using eventfd as soon as element is added, reader then reads until +read fails +-> reader pops in a loop until FALSE and might get spurious events because it +read before it was notified, which is fine +-> writing pushes *the same data* in a loop until TRUE, blocks + +shutting this down requires +1) to stop reading and pushing +2) ONE side to take care of the eventfds +*/ + +static struct spsc *spsc_init(unsigned int count, unsigned int size_per_buf, bool blockr, bool blockw) +{ + struct spsc *q = calloc(1, sizeof(struct spsc) + sizeof(uintptr_t) * count); + atomic_init(&q->readptr, 0); + atomic_init(&q->writeptr, 0); + q->efd_r = eventfd(0, blockr ? 0 : EFD_NONBLOCK); + q->efd_w = eventfd(1, blockw ? 0 : EFD_NONBLOCK); + q->count = count; + q->size_per_buf = size_per_buf; + q->buf = calloc(count, size_per_buf); + + for (int i = 0; i < count; i++) + q->data[i] = (uintptr_t)q->buf + i * size_per_buf; + return q; +} + +static void spsc_deinit(struct spsc *q) +{ + free(q->buf); + close(q->efd_r); + close(q->efd_w); + free(q); +} + +static ssize_t spsc_check_r(struct spsc *q) +{ + uint64_t efdr; + return read(q->efd_r, &efdr, sizeof(uint64_t)); +} +static ssize_t spsc_check_w(struct spsc *q) +{ + uint64_t efdr; + return read(q->efd_w, &efdr, sizeof(uint64_t)); +} +static void spsc_notify_r(struct spsc *q) +{ + uint64_t efdu = 1; + write(q->efd_r, &efdu, sizeof(uint64_t)); +} +static void spsc_notify_w(struct spsc *q) +{ + uint64_t efdu = 1; + write(q->efd_w, &efdu, sizeof(uint64_t)); +} + +bool spsc_push(struct spsc *q, void *elem) +{ + size_t cur_wp, cur_rp; + cur_wp = atomic_load_explicit(&q->writeptr, memory_order_relaxed); + cur_rp = atomic_load_explicit(&q->readptr, memory_order_acquire); + if ((cur_wp + 1) % q->count == cur_rp) { + spsc_check_w(q); // blocks, ensures next (!) call succeeds + return false; + } + memcpy((void *)q->data[cur_wp], elem, q->size_per_buf); + atomic_store_explicit(&q->writeptr, (cur_wp + 1) % q->count, memory_order_release); + spsc_notify_r(q); // fine after release + return true; +} + +/* just clear it - pop is called in a loop until empty, and next notification + * can't happen until it's not empty */ +ssize_t prep_pop(struct spsc *q) +{ + return spsc_check_r(q); +} + +bool spsc_pop(struct spsc *q, void *elem) +{ + size_t cur_wp, cur_rp; + cur_wp = atomic_load_explicit(&q->writeptr, memory_order_acquire); + cur_rp = atomic_load_explicit(&q->readptr, memory_order_relaxed); + + if (cur_wp == cur_rp) /* blocks via prep_pop */ + return false; + memcpy(elem, (void *)q->data[cur_rp], q->size_per_buf); + atomic_store_explicit(&q->readptr, (cur_rp + 1) % q->count, memory_order_release); + spsc_notify_w(q); + return true; +} + +/* +master: writes a, waits for rdfd of b +slave: waits for rdfd of a, writes b +wrfd used interally to block writers +*/ +struct qchan chan_init_ex(unsigned int count, unsigned size_per_buf, bool blockr_a, bool blockw_a, bool blockr_b, + bool blockw_b) +{ + struct qchan q; // = calloc(1, sizeof(struct qchan)); + q.a = spsc_init(count, size_per_buf, blockr_a, blockw_a); + q.b = spsc_init(count, size_per_buf, blockr_b, blockw_b); + return q; +} + +struct qchan chan_init(unsigned int count, unsigned size_per_buf) +{ + return chan_init_ex(count, size_per_buf, false, true, false, true); +} + +void chan_close(struct qchan *q) +{ + spsc_deinit(q->a); + spsc_deinit(q->b); + free(q); +} + +int get_a_rdfd(struct qchan *q) +{ + return q->a->efd_r; +} +int get_b_rdfd(struct qchan *q) +{ + return q->b->efd_r; +} +int get_a_wrfd(struct qchan *q) +{ + return q->a->efd_w; +} +int get_b_wrfd(struct qchan *q) +{ + return q->b->efd_w; +} -- To view, visit https://gerrit.osmocom.org/c/osmo-mgw/+/25431 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: osmo-mgw Gerrit-Branch: master Gerrit-Change-Id: I4f17042baf76d086ce6b20eb99402dc64c22c657 Gerrit-Change-Number: 25431 Gerrit-PatchSet: 1 Gerrit-Owner: Hoernchen <ewild at sysmocom.de> Gerrit-MessageType: newchange -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210909/b8f2dd2c/attachment.htm>