source: lib/data-struct/message_queue.c @ d6a56b6

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

Move the data structures out of the way and into there own folder and tidy file naming.

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