source: lib/data-struct/message_queue.c @ 0a474e3

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

And more..

  • Property mode set to 100644
File size: 5.9 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        /*assert(message_len);*/
46        if (!message_len) {
47                fprintf(stderr, "Message length cannot be 0 in libtrace_message_queue_init()\n");
48                return;
49        }
50        ASSERT_RET(pipe(mq->pipefd), != -1);
51        mq->message_count = 0;
52        if (message_len > PIPE_BUF)
53                fprintf(stderr, "Warning message queue wont be atomic (thread safe) message_len(%zu) > PIPE_BUF(%d)\n",
54                                        message_len, PIPE_BUF);
55        mq->message_len = message_len;
56        pthread_spin_init(&mq->spin, 0);
57}
58
59/**
60 * Posts a message to the given message queue.
61 *
62 * This will block if a reader is not keeping up and the underlying pipe
63 * fills up.
64 *
65 * @param mq A pointer to a initilised libtrace message queue structure (NOT NULL)
66 * @param message A pointer to the message data you wish to send
67 * @return A number representing the number of messages already in the queue,
68 *         0 implies a thread was waiting and will read your message, negative
69 *         numbers implies threads are still waiting. Positive implies a backlog
70 *         of messages.
71 */
72int libtrace_message_queue_put(libtrace_message_queue_t *mq, const void *message)
73{
74        int ret;
75        /*assert(mq->message_len);*/
76        //if (!mq->message_len) {
77        //      fprintf(stderr, "Message queue must be initialised with libtrace_message_queue_init()"
78        //              "before inserting messages in libtrace_message_queue_put()\n");
79        //      /* Can we return -1 here as this could imply threads are waiting */
80        //      return -1;
81        //}
82        ASSERT_RET(write(mq->pipefd[1], message, mq->message_len), == (int) mq->message_len);
83        // Update after we've written
84        pthread_spin_lock(&mq->spin);
85        ret = ++mq->message_count; // Should be CAS!
86        pthread_spin_unlock(&mq->spin);
87        return ret;
88}
89
90/**
91 * Retrieves a message from the given message queue.
92 *
93 * This will block if a reader is not keeping up and the underlying pipe
94 * fills up.
95 *
96 * @param mq A pointer to a initilised libtrace message queue structure (NOT NULL)
97 * @param message A pointer to the message data you wish to send
98 * @return The number of messages remaining in the queue less any threads waiting,
99 *         0 implies a thread was waiting and will read your message, negative
100 *         numbers implies threads are still waiting. Positive implies a backlog
101 *         of messages.
102 */
103int libtrace_message_queue_get(libtrace_message_queue_t *mq, void *message)
104{
105        int ret;
106        // Safely decrease count first - Yes this might make us negative, however thats ok once a write comes in everything will be fine
107        pthread_spin_lock(&mq->spin);
108        ret = mq->message_count--;
109        pthread_spin_unlock(&mq->spin);
110        ASSERT_RET(read(mq->pipefd[0], message, mq->message_len), == (int) mq->message_len);
111        return ret;
112}
113
114/**
115 * Trys to retrieve a message from the given message queue.
116 *
117 * This will not block and instead returns LIBTRACE_MQ_FAILED if
118 * no message is available.
119 *
120 * @param mq A pointer to a initilised libtrace message queue structure (NOT NULL)
121 * @param message A pointer to the message data you wish to send
122 * @return The number of messages remaining in the queue less any threads waiting,
123 *         0 implies a thread was waiting and will read your message, negative
124 *         numbers implies threads are still waiting. Positive implies a backlog
125 *         of messages.
126 */
127int libtrace_message_queue_try_get(libtrace_message_queue_t *mq, void *message)
128{
129        int ret;
130        // Safely decrease count first - Yes this might make us negative, however thats ok once a write comes in everything will be fine
131        // ->Fast path avoid the lock
132        if (mq->message_count <= 0)
133                return LIBTRACE_MQ_FAILED;
134        // Else grab lock and confirm this is so
135        pthread_spin_lock(&mq->spin);
136        if (mq->message_count > 0) {
137                ret = --mq->message_count;
138                // :( read(...) needs to be done within the *spin* lock otherwise blocking might steal our read
139                ASSERT_RET(read(mq->pipefd[0], message, mq->message_len), == (int) mq->message_len);
140        } else {
141                ret = LIBTRACE_MQ_FAILED;
142        }
143        pthread_spin_unlock(&mq->spin);
144        return ret;
145}
146
147/**
148 * May be negative if threads blocking and waiting for a message.
149 */
150int libtrace_message_queue_count(const libtrace_message_queue_t *mq)
151{
152        // This is only ok because we know int is atomic
153        return mq->message_count;
154}
155
156void libtrace_message_queue_destroy(libtrace_message_queue_t *mq)
157{
158        mq->message_count = 0;
159        mq->message_len = 0;
160        close(mq->pipefd[0]);
161        close(mq->pipefd[1]);
162        pthread_spin_destroy(&mq->spin);
163}
164
165/**
166 * @return a file descriptor for the queue, can be used with select() poll() etc.
167 */
168int libtrace_message_queue_get_fd(libtrace_message_queue_t *mq)
169{
170        return mq->pipefd[0];
171}
Note: See TracBrowser for help on using the repository browser.