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

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

Missing assert.h in message_queue.c

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