source: lib/libtrace_message_queue.c @ 29ba7c2

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 29ba7c2 was 29bbef0, checked in by Richard Sanger <rsangerarj@…>, 7 years ago

My work from over summer, with a few things tidied up and updated to include recent commits/patches to bring this up to date. Still very much work in progress.

  • Property mode set to 100644
File size: 4.6 KB
Line 
1#include <unistd.h>
2#include <stdio.h>
3#include "libtrace_message_queue.h"
4#include <limits.h>
5
6/**
7 * TODO look into using eventfd instead of a pipe if we have it available XXX
8 */
9
10/**
11 * @param mq A pointer to allocated space for a libtrace message queue
12 * @param message_len The size in bytes of the message item, to ensure thread safety this
13 *                should be less than PIPE_BUF (normally at least 512bytes)
14 *                see: man 7 pipe notes on atomic operations
15 */
16inline void libtrace_message_queue_init(libtrace_message_queue_t *mq, size_t message_len)
17{
18        assert(pipe(mq->pipefd) != -1);
19        mq->message_count = 0;
20        if (message_len > PIPE_BUF)
21                fprintf(stderr, "Warning message queue wont be atomic (thread safe) message_len(%zu) > PIPE_BUF(%d)\n",
22                                        message_len, PIPE_BUF);
23        mq->message_len = message_len;
24        pthread_spin_init(&mq->spin, 0);
25}
26
27/**
28 * Posts a message to the given message queue.
29 *
30 * This will block if a reader is not keeping up and the underlying pipe
31 * fills up.
32 *
33 * @param mq A pointer to a initilised libtrace message queue structure (NOT NULL)
34 * @param message A pointer to the message data you wish to send
35 * @return A number representing the number of messages already in the queue,
36 *         0 implies a thread was waiting and will read your message, negative
37 *         numbers implies threads are still waiting. Positive implies a backlog
38 *         of messages.
39 */
40inline int libtrace_message_queue_put(libtrace_message_queue_t *mq, const void *message)
41{
42        int ret;
43        assert(write(mq->pipefd[1], message, mq->message_len) == (int) mq->message_len);
44        // Update after we've written
45        pthread_spin_lock(&mq->spin);
46        ret = ++mq->message_count; // Should be CAS!
47        pthread_spin_unlock(&mq->spin);
48        return ret;
49}
50
51/**
52 * Retrieves a message from the given message queue.
53 *
54 * This will block if a reader is not keeping up and the underlying pipe
55 * fills up.
56 *
57 * @param mq A pointer to a initilised libtrace message queue structure (NOT NULL)
58 * @param message A pointer to the message data you wish to send
59 * @return The number of messages remaining in the queue less any threads waiting,
60 *         0 implies a thread was waiting and will read your message, negative
61 *         numbers implies threads are still waiting. Positive implies a backlog
62 *         of messages.
63 */
64inline int libtrace_message_queue_get(libtrace_message_queue_t *mq, void *message)
65{
66        int ret;
67        // Safely decrease count first - Yes this might make us negative, however thats ok once a write comes in everything will be fine
68        pthread_spin_lock(&mq->spin);
69        ret = mq->message_count--;
70        pthread_spin_unlock(&mq->spin);
71        assert(read(mq->pipefd[0], message, mq->message_len) == (int) mq->message_len);
72        return ret; 
73}
74
75/**
76 * Trys to retrieve a message from the given message queue.
77 *
78 * This will not block and instead returns LIBTRACE_MQ_FAILED if
79 * no message is available.
80 *
81 * @param mq A pointer to a initilised libtrace message queue structure (NOT NULL)
82 * @param message A pointer to the message data you wish to send
83 * @return The number of messages remaining in the queue less any threads waiting,
84 *         0 implies a thread was waiting and will read your message, negative
85 *         numbers implies threads are still waiting. Positive implies a backlog
86 *         of messages.
87 */
88inline int libtrace_message_queue_try_get(libtrace_message_queue_t *mq, void *message)
89{
90        int ret;
91        // Safely decrease count first - Yes this might make us negative, however thats ok once a write comes in everything will be fine
92        // ->Fast path avoid the lock
93        if (mq->message_count <= 0)
94                return LIBTRACE_MQ_FAILED;
95        // Else grab lock and confirm this is so
96        pthread_spin_lock(&mq->spin);
97        if (mq->message_count > 0) {
98                ret = --mq->message_count;
99                // :( read(...) needs to be done within the *spin* lock otherwise blocking might steal our read
100                assert(read(mq->pipefd[0], message, mq->message_len) == (int) mq->message_len);
101        } else {
102                ret = LIBTRACE_MQ_FAILED;
103        }
104        pthread_spin_unlock(&mq->spin);
105        return ret;
106}
107
108/**
109 * May be negative if threads blocking and waiting for a message.
110 */
111inline int libtrace_message_queue_count(const libtrace_message_queue_t *mq)
112{
113        // This is only ok because we know int is atomic
114        return mq->message_count;
115}
116
117inline void libtrace_message_queue_destroy(libtrace_message_queue_t *mq)
118{
119        mq->message_count = 0;
120        mq->message_len = 0;
121        close(mq->pipefd[0]);
122        close(mq->pipefd[1]);
123        pthread_spin_destroy(&mq->spin);
124}
125
126/**
127 * @return a file descriptor for the queue, can be used with select() poll() etc.
128 */
129inline int libtrace_message_queue_get_fd(libtrace_message_queue_t *mq)
130{
131        return mq->pipefd[0];
132}
Note: See TracBrowser for help on using the repository browser.