Annotation of libelwix/src/ring.c, revision 1.1.2.1

1.1.2.1 ! misho       1: /*************************************************************************
        !             2: * (C) 2025 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
        !             3: *  by Michael Pounov <misho@elwix.org>
        !             4: *
        !             5: * $Author: misho $
        !             6: * $Id: array.c,v 1.10 2024/10/28 09:58:51 misho Exp $
        !             7: *
        !             8: **************************************************************************
        !             9: The ELWIX and AITNET software is distributed under the following
        !            10: terms:
        !            11: 
        !            12: All of the documentation and software included in the ELWIX and AITNET
        !            13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
        !            14: 
        !            15: Copyright 2004 - 2025
        !            16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
        !            17: 
        !            18: Redistribution and use in source and binary forms, with or without
        !            19: modification, are permitted provided that the following conditions
        !            20: are met:
        !            21: 1. Redistributions of source code must retain the above copyright
        !            22:    notice, this list of conditions and the following disclaimer.
        !            23: 2. Redistributions in binary form must reproduce the above copyright
        !            24:    notice, this list of conditions and the following disclaimer in the
        !            25:    documentation and/or other materials provided with the distribution.
        !            26: 3. All advertising materials mentioning features or use of this software
        !            27:    must display the following acknowledgement:
        !            28: This product includes software developed by Michael Pounov <misho@elwix.org>
        !            29: ELWIX - Embedded LightWeight unIX and its contributors.
        !            30: 4. Neither the name of AITNET nor the names of its contributors
        !            31:    may be used to endorse or promote products derived from this software
        !            32:    without specific prior written permission.
        !            33: 
        !            34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
        !            35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            44: SUCH DAMAGE.
        !            45: */
        !            46: #include "global.h"
        !            47: 
        !            48: 
        !            49: /*
        !            50:  * rbuf_init() - Init ring buffer
        !            51:  *
        !            52:  * @rbuf = Ring buffer
        !            53:  * @num = Number of elements in buffer
        !            54:  * return: -1 error or 0 ok
        !            55:  */
        !            56: int
        !            57: rbuf_init(ringbuf_t *rbuf, int num)
        !            58: {
        !            59:        if (!rbuf)
        !            60:                return -1;
        !            61: 
        !            62:        atomic_store_explicit(&rbuf->rb_head, 0, memory_order_relaxed);
        !            63:        atomic_store_explicit(&rbuf->rb_tail, 0, memory_order_relaxed);
        !            64: 
        !            65:        rbuf->rb_buffer = e_calloc(num, sizeof(struct iovec));
        !            66:        if (!rbuf->rb_buffer)
        !            67:                return -1;
        !            68:        else
        !            69:                rbuf->rb_bufnum = num;
        !            70:        memset(rbuf->rb_buffer, 0, num * sizeof(struct iovec));
        !            71: 
        !            72:        return 0;
        !            73: }
        !            74: 
        !            75: /*
        !            76:  * rbuf_free() - Free ring buffer
        !            77:  *
        !            78:  * @rbuf = Ring buffer
        !            79:  * return: none
        !            80:  */
        !            81: void
        !            82: rbuf_free(ringbuf_t *rbuf)
        !            83: {
        !            84:        if (!rbuf)
        !            85:                return;
        !            86: 
        !            87:        if (rbuf->rb_buffer) {
        !            88:                e_free(rbuf->rb_buffer);
        !            89:                rbuf->rb_buffer = NULL;
        !            90:                rbuf->rb_bufnum = 0;
        !            91:        }
        !            92: 
        !            93:        atomic_store_explicit(&rbuf->rb_head, 0, memory_order_relaxed);
        !            94:        atomic_store_explicit(&rbuf->rb_tail, 0, memory_order_relaxed);
        !            95: }
        !            96: 
        !            97: /*
        !            98:  * rbuf_purge() - Purge all buffer
        !            99:  *
        !           100:  * @rbuf = Ring buffer
        !           101:  * return: none
        !           102:  */
        !           103: void
        !           104: rbuf_purge(ringbuf_t *rbuf)
        !           105: {
        !           106:        if (!rbuf)
        !           107:                return;
        !           108: 
        !           109:        if (rbuf->rb_buffer)
        !           110:                memset(rbuf->rb_buffer, 0, rbuf->rb_bufnum * sizeof(struct iovec));
        !           111: 
        !           112:        atomic_store_explicit(&rbuf->rb_head, 0, memory_order_relaxed);
        !           113:        atomic_store_explicit(&rbuf->rb_tail, 0, memory_order_relaxed);
        !           114: }
        !           115: 
        !           116: /*
        !           117:  * rbuf_isempty() - Check buffer is empty
        !           118:  *
        !           119:  * @rbuf = Ring buffer
        !           120:  * return: -1 error, 0 it isn't empty
        !           121:  */
        !           122: int
        !           123: rbuf_isempty(ringbuf_t *rbuf)
        !           124: {
        !           125:        if (!rbuf)
        !           126:                return -1;
        !           127: 
        !           128:        return (atomic_load_explicit(&rbuf->rb_head, memory_order_acquire) ==
        !           129:                atomic_load_explicit(&rbuf->rb_tail, memory_order_acquire));
        !           130: }
        !           131: 
        !           132: /*
        !           133:  * rbuf_isfull() - Check buffer is full
        !           134:  *
        !           135:  * @rbuf = Ring buffer
        !           136:  * return: -1 error or 0 it isn't full
        !           137:  */
        !           138: int
        !           139: rbuf_isfull(ringbuf_t *rbuf)
        !           140: {
        !           141:        if (!rbuf)
        !           142:                return -1;
        !           143: 
        !           144:        return (((atomic_load_explicit(&rbuf->rb_head, memory_order_relaxed) + 1) % rbuf->rb_bufnum) ==
        !           145:                atomic_load_explicit(&rbuf->rb_tail, memory_order_acquire));
        !           146: }
        !           147: 
        !           148: /*
        !           149:  * rbuf_enqueue() - Enqueue data to buffer
        !           150:  *
        !           151:  * @rbuf = Ring buffer
        !           152:  * @data = Data
        !           153:  * @len = Length
        !           154:  * return: -1 error, 1 buffer is full or 0 ok
        !           155:  */
        !           156: int
        !           157: rbuf_enqueue(ringbuf_t *rbuf, void *data, size_t len)
        !           158: {
        !           159:        int h, t, n;
        !           160:        struct iovec *iov;
        !           161: 
        !           162:        if (!rbuf)
        !           163:                return -1;
        !           164: 
        !           165:        h = atomic_load_explicit(&rbuf->rb_head, memory_order_relaxed);
        !           166:        t = atomic_load_explicit(&rbuf->rb_tail, memory_order_acquire);
        !           167:        n = (h + 1) % rbuf->rb_bufnum;
        !           168: 
        !           169:        if (n == t)
        !           170:                return 1;
        !           171: 
        !           172:        iov = rbuf->rb_buffer + h;
        !           173:        iov->iov_len = len;
        !           174:        iov->iov_base = data;
        !           175: 
        !           176:        atomic_store_explicit(&rbuf->rb_head, n, memory_order_release);
        !           177:        return 0;
        !           178: }
        !           179: 
        !           180: /*
        !           181:  * rbuf_dequeue() - Dequeue data from buffer
        !           182:  *
        !           183:  * @rbuf = Ring buffer
        !           184:  * @out = Data, if =NULL, just dequeue data
        !           185:  * return: -1 error, 1 buffer is empty or 0 ok
        !           186:  */
        !           187: int
        !           188: rbuf_dequeue(ringbuf_t *rbuf, struct iovec *out)
        !           189: {
        !           190:        int h, t, n;
        !           191: 
        !           192:        if (!rbuf)
        !           193:                return -1;
        !           194: 
        !           195:        h = atomic_load_explicit(&rbuf->rb_head, memory_order_acquire);
        !           196:        t = atomic_load_explicit(&rbuf->rb_tail, memory_order_relaxed);
        !           197:        n = (t + 1) % rbuf->rb_bufnum;
        !           198: 
        !           199:        if (h == t)
        !           200:                return 1;
        !           201: 
        !           202:        if (out)
        !           203:                out = rbuf->rb_buffer + t;
        !           204: 
        !           205:        atomic_store_explicit(&rbuf->rb_tail, n, memory_order_release);
        !           206:        return 0;
        !           207: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>