<commit_before>


/*-
 * MIT License
 *
 * Copyright (c) 2017 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
/**
 * @file ssnlib_ring.h
 * @brief wrap rte_ring.h
 * @author slankdev
 */



#pragma once
#include <ssnlib_log.h>
#include <ssnlib_mempool.h>
#include <queue> // for Ring_stdqueue

namespace ssnlib {

static inline void __attribute__((always_inline))
rte_pktmbuf_free_bulk(struct rte_mbuf *m_list[], int16_t npkts)
{
	while (npkts--) {
		rte_pktmbuf_free(*m_list++);
    }
}


class Ring_interface {
public:
    virtual void push_bulk(rte_mbuf** obj_table, size_t n) = 0;
    virtual bool pop_bulk(rte_mbuf** obj_table, size_t n)  = 0;
    virtual size_t count() const = 0;
    virtual size_t size() const  = 0;
    virtual bool empty() const   = 0;
};


class Ring_stdqueue : public Ring_interface {
    std::queue<rte_mbuf*> queue_;
public:
    Ring_stdqueue(size_t, size_t, size_t, const char*) {}
    void push_bulk(rte_mbuf** obj_table, size_t n) override
    {
        for (size_t i=0; i<n; i++) {
            queue_.push(obj_table[i]);
        }
    }
    bool pop_bulk(rte_mbuf** obj_table, size_t n) override
    {
        if (queue_.size() < n) {
            return false;
        } else {
            printf("GAAAA\n");
        }

        for (size_t i=0; i<n; i++) {
            obj_table[i] = queue_.front();
            queue_.pop();
        }
        return true;
    }
    size_t count() const override { return queue_.size(); }
    size_t size()  const override { return queue_.size(); }
    bool   empty() const override { return queue_.empty(); }
};

class Ring_dpdk : public Ring_interface {
protected:
    struct rte_ring* ring_;
	size_t ring_depth;
public:
    Ring_dpdk(size_t count, size_t p, size_t q, const char* nameprefix) : ring_depth(count)
    {
        std::string rn = nameprefix + std::to_string(p) + ":" + std::to_string(q);
        uint16_t socket_id = rte_socket_id();
<commit_msg>FIX: Ring_stdqueue, thread safe. is it safe...? (T.T)<commit_after>


/*-
 * MIT License
 *
 * Copyright (c) 2017 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
/**
 * @file ssnlib_ring.h
 * @brief wrap rte_ring.h
 * @author slankdev
 */



#pragma once
#include <ssnlib_log.h>
#include <ssnlib_mempool.h>
#include <queue> // for Ring_stdqueue
#include <mutex> // lock

namespace ssnlib {

static inline void __attribute__((always_inline))
rte_pktmbuf_free_bulk(struct rte_mbuf *m_list[], int16_t npkts)
{
	while (npkts--) {
		rte_pktmbuf_free(*m_list++);
    }
}


class Ring_interface {
public:
    virtual void push_bulk(rte_mbuf** obj_table, size_t n) = 0;
    virtual bool pop_bulk(rte_mbuf** obj_table, size_t n)  = 0;
    virtual size_t count() const = 0;
    virtual size_t size() const  = 0;
    virtual bool empty() const   = 0;
};


class Ring_stdqueue : public Ring_interface {
    std::queue<rte_mbuf*> queue_;
    mutable std::mutex* m;
    using auto_lock = std::lock_guard<std::mutex>;
public:
    Ring_stdqueue(size_t, size_t, size_t, const char*) { m = new std::mutex; }
    virtual ~Ring_stdqueue() { delete(m); }
    Ring_stdqueue(const Ring_stdqueue&) { m = new std::mutex; }
    Ring_stdqueue(Ring_stdqueue&&) = default;
    void push_bulk(rte_mbuf** obj_table, size_t n) override
    {
        auto_lock lg(*m);
        for (size_t i=0; i<n; i++) {
            queue_.push(obj_table[i]);
        }
    }
    bool pop_bulk(rte_mbuf** obj_table, size_t n) override
    {
        auto_lock lg(*m);
        if (queue_.size() < n)
            return false;

        for (size_t i=0; i<n; i++) {
            obj_table[i] = queue_.front();
            queue_.pop();
        }
        return true;
    }
    size_t count() const override { auto_lock lg(*m); return queue_.size(); }
    size_t size()  const override { auto_lock lg(*m); return queue_.size(); }
    bool   empty() const override { auto_lock lg(*m); return queue_.empty(); }
};

class Ring_dpdk : public Ring_interface {
protected:
    struct rte_ring* ring_;
	size_t ring_depth;
public:
    Ring_dpdk(size_t count, size_t p, size_t q, const char* nameprefix) : ring_depth(count)
    {
        std::string rn = nameprefix + std::to_string(p) + ":" + std::to_string(q);
        uint16_t socket_id = rte_socket_id();

{'fraction_non_alphanumeric': 0.1050460171374167, 'fraction_numerical': 0.004760393525864805, 'mean_word_length': 3.346896551724138, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3841466', 'n_tokens_mistral': 2071, 'n_tokens_neox': 2007, 'n_words': 689}