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

develop
Last change on this file since 2193905 was 2193905, checked in by Jacob Van Walraven <jcv9@…>, 2 years ago

Apply changes required for pull request #81

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 *
3 * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand.
4 * All rights reserved.
5 *
6 * This file is part of libtrace.
7 *
8 * This code has been developed by the University of Waikato WAND
9 * research group. For further information please see http://www.wand.net.nz/
10 *
11 * libtrace is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * libtrace is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 *
25 */
26#include "message_queue.h"
27
28#include <unistd.h>
29#include <stdio.h>
30#include <limits.h>
31#include <assert.h>
32
33/**
34 * TODO look into using eventfd instead of a pipe if we have it available XXX
35 */
36
37/**
38 * @param mq A pointer to allocated space for a libtrace message queue
39 * @param message_len The size in bytes of the message item, to ensure thread safety this
40 *                should be less than PIPE_BUF (normally at least 512bytes)
41 *                see: man 7 pipe notes on atomic operations
42 */
43void libtrace_message_queue_init(libtrace_message_queue_t *mq, size_t message_len)
44{
45        if (!message_len) {
46                fprintf(stderr, "Message length cannot be 0 in libtrace_message_queue_init()\n");
47                return;
48        }
49        ASSERT_RET(pipe(mq->pipefd), != -1);
50        mq->message_count = 0;
51        if (message_len > PIPE_BUF)
52                fprintf(stderr, "Warning message queue wont be atomic (thread safe) message_len(%zu) > PIPE_BUF(%d)\n",
53                                        message_len, PIPE_BUF);
54        mq->message_len = message_len;
55        pthread_spin_init(&mq->spin, 0);
56}
57
58/**
59 * Posts a message to the given message queue.
60 *
61 * This will block if a reader is not keeping up and the underlying pipe
62 * fills up.
63 *
64 * @param mq A pointer to a initilised libtrace message queue structure (NOT NULL)
65 * @param message A pointer to the message data you wish to send
66 * @return A number representing the number of messages already in the queue,
67 *         0 implies a thread was waiting and will read your message, negative
68 *         numbers implies threads are still waiting. Positive implies a backlog
69 *         of messages.
70 */
71int libtrace_message_queue_put(libtrace_message_queue_t *mq, const void *message)
72{
73        int ret;
74        if (!mq->message_len) {
75                fprintf(stderr, "Message queue must be initialised with libtrace_message_queue_init()"
76                        "before inserting messages in libtrace_message_queue_put()\n");
77                return 0;
78        }
79        ASSERT_RET(write(mq->pipefd[1], message, mq->message_len), == (int) mq->message_len);
80        // Update after we've written
81        pthread_spin_lock(&mq->spin);
82        ret = ++mq->message_count; // Should be CAS!
83        pthread_spin_unlock(&mq->spin);
84        return ret;
85}
86
87/**
88 * Retrieves a message from the given message queue.
89 *
90 * This will block if a reader is not keeping up and the underlying pipe
91 * fills up.
92 *
93 * @param mq A pointer to a initilised libtrace message queue structure (NOT NULL)
94 * @param message A pointer to the message data you wish to send
95 * @return The number of messages remaining in the queue less any threads waiting,
96 *         0 implies a thread was waiting and will read your message, negative
97 *         numbers implies threads are still waiting. Positive implies a backlog
98 *         of messages.
99 */
100int libtrace_message_queue_get(libtrace_message_queue_t *mq, void *message)
101{
102        int ret;
103        // Safely decrease count first - Yes this might make us negative, however thats ok once a write comes in everything will be fine
104        pthread_spin_lock(&mq->spin);
105        ret = mq->message_count--;
106        pthread_spin_unlock(&mq->spin);
107        ASSERT_RET(read(mq->pipefd[0], message, mq->message_len), == (int) mq->message_len);
108        return ret;
109}
110
111/**
112 * Trys to retrieve a message from the given message queue.
113 *
114 * This will not block and instead returns LIBTRACE_MQ_FAILED if
115 * no message is available.
116 *
117 * @param mq A pointer to a initilised libtrace message queue structure (NOT NULL)
118 * @param message A pointer to the message data you wish to send
119 * @return The number of messages remaining in the queue less any threads waiting,
120 *         0 implies a thread was waiting and will read your message, negative
121 *         numbers implies threads are still waiting. Positive implies a backlog
122 *         of messages.
123 */
124int libtrace_message_queue_try_get(libtrace_message_queue_t *mq, void *message)
125{
126        int ret;
127        // Safely decrease count first - Yes this might make us negative, however thats ok once a write comes in everything will be fine
128        // ->Fast path avoid the lock
129        if (mq->message_count <= 0)
130                return LIBTRACE_MQ_FAILED;
131        // Else grab lock and confirm this is so
132        pthread_spin_lock(&mq->spin);
133        if (mq->message_count > 0) {
134                ret = --mq->message_count;
135                // :( read(...) needs to be done within the *spin* lock otherwise blocking might steal our read
136                ASSERT_RET(read(mq->pipefd[0], message, mq->message_len), == (int) mq->message_len);
137        } else {
138                ret = LIBTRACE_MQ_FAILED;
139        }
140        pthread_spin_unlock(&mq->spin);
141        return ret;
142}
143
144/**
145 * May be negative if threads blocking and waiting for a message.
146 */
147int libtrace_message_queue_count(const libtrace_message_queue_t *mq)
148{
149        // This is only ok because we know int is atomic
150        return mq->message_count;
151}
152
153void libtrace_message_queue_destroy(libtrace_message_queue_t *mq)
154{
155        mq->message_count = 0;
156        mq->message_len = 0;
157        close(mq->pipefd[0]);
158        close(mq->pipefd[1]);
159        pthread_spin_destroy(&mq->spin);
160}
161
162/**
163 * @return a file descriptor for the queue, can be used with select() poll() etc.
164 */
165int libtrace_message_queue_get_fd(libtrace_message_queue_t *mq)
166{
167        return mq->pipefd[0];
168}
Note: See TracBrowser for help on using the repository browser.