Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try out linked list based data structure #7

Open
Iyengar111 opened this issue Oct 17, 2016 · 2 comments
Open

Try out linked list based data structure #7

Iyengar111 opened this issue Oct 17, 2016 · 2 comments

Comments

@Iyengar111
Copy link
Owner

Iyengar111 commented Oct 17, 2016

struct LinkedList : public BufferBase
{
    LinkedList() : m_head(nullptr)
    {
    }

    struct Item
    {
        Item(NanoLogLine && logline_) : next(nullptr), logline(std::move(logline_))
        {}
        std::atomic < Item * > next;
        NanoLogLine logline;
    };

    void push(NanoLogLine && logline) override
    {
        Item * item = new Item(std::move(logline));
        Item * head = m_head.load(std::memory_order_relaxed);
        do
        {
            item->next = head;
        } while(!m_head.compare_exchange_weak(head, item, std::memory_order_release, std::memory_order_relaxed));
    }

    bool try_pop(NanoLogLine & logline) override
    {
        if (!m_read_buffer.empty())
        {
            logline = std::move(m_read_buffer.front());
            m_read_buffer.pop_front();
            return true;
        }
        Item * head = get_current_head();
        while (head != nullptr)
        {
            Item * next = head->next.load(std::memory_order_acquire);
            m_read_buffer.push_front(std::move(head->logline));
            delete head;
            head = next;
        };
        if (m_read_buffer.empty())
            return false;
        else
            return try_pop(logline);
    }

    Item * get_current_head()
    {
        Item * current_head = m_head.load(std::memory_order_acquire);
        while (!m_head.compare_exchange_weak(current_head, nullptr, std::memory_order_release, std::memory_order_relaxed));
        return current_head;
    }


private:
    std::atomic < Item * > m_head;
    std::deque < NanoLogLine > m_read_buffer;
};
@damondd
Copy link

damondd commented Feb 1, 2017

any benchmark ?

@Iyengar111
Copy link
Owner Author

Latency numbers were pretty much same with linked list so I did not pursue this further...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants