Eric | e03e34f | 2021-09-06 20:07:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2021 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * Author: Eric Wild |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <stdatomic.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdlib.h> |
| 25 | |
| 26 | struct spsc { |
| 27 | atomic_uint readptr; |
| 28 | atomic_uint writeptr; |
| 29 | |
| 30 | int efd_r, efd_w; /* eventfds used to block/notify readers/writers */ |
| 31 | |
| 32 | int count; |
| 33 | int size_per_buf; |
| 34 | |
| 35 | void *buf; /* buffer size count*size_per_buf */ |
| 36 | uintptr_t data[0]; /* count sized array of pointers to size_per_buf chunks in buf array*/ |
| 37 | }; |
| 38 | |
| 39 | struct qchan { |
| 40 | struct spsc *a; |
| 41 | struct spsc *b; |
| 42 | }; |
| 43 | |
| 44 | bool spsc_push(struct spsc *q, void *elem); |
| 45 | bool spsc_pop(struct spsc *q, void *elem); |
| 46 | ssize_t spsc_prep_pop(struct spsc *q); |
| 47 | int spsc_get_a_rdfd(struct qchan *q); |
| 48 | |
| 49 | struct qchan spsc_chan_init(void *talloc_ctx, unsigned int count, unsigned int size_per_buf); |
| 50 | struct qchan spsc_chan_init_ex(void *talloc_ctx, unsigned int count, unsigned int size_per_buf, bool blockr_a, |
| 51 | bool blockw_a, bool blockr_b, bool blockw_b); |
| 52 | void spsc_chan_close(struct qchan *q); |