source: lib/data-struct/message_queue.c @ 88b9798

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

More assertion cleanups

  • Property mode set to 100644
File size: 5.6 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        ASSERT_RET(write(mq->pipefd[1], message, mq->message_len), == (int) mq->message_len);
77        // Update after we've written
78        pthread_spin_lock(&mq->spin);
79        ret = ++mq->message_count; // Should be CAS!
80        pthread_spin_unlock(&mq->spin);
81        return ret;
82}
83
84/**
85 * Retrieves a message from the given message queue.
86 *
87 * This will block if a reader is not keeping up and the underlying pipe
88 * fills up.
89 *
90 * @param mq A pointer to a initilised libtrace message queue structure (NOT NULL)
91 * @param message A pointer to the message data you wish to send
92 * @return The number of messages remaining in the queue less any threads waiting,
93 *         0 implies a thread was waiting and will read your message, negative
94 *         numbers implies threads are still waiting. Positive implies a backlog
95 *         of messages.
96 */
97int libtrace_message_queue_get(libtrace_message_queue_t *mq, void *message)
98{
99        int ret;
100        // Safely decrease count first - Yes this might make us negative, however thats ok once a write comes in everything will be fine
101        pthread_spin_lock(&mq->spin);
102        ret = mq->message_count--;
103        pthread_spin_unlock(&mq->spin);
104        ASSERT_RET(read(mq->pipefd[0], message, mq->message_len), == (int) mq->message_len);
105        return ret;
106}
107
108/**
109 * Trys to retrieve a message from the given message queue.
110 *
111 * This will not block and instead returns LIBTRACE_MQ_FAILED if
112 * no message is available.
113 *
114 * @param mq A pointer to a initilised libtrace message queue structure (NOT NULL)
115 * @param message A pointer to the message data you wish to send
116 * @return The number of messages remaining in the queue less any threads waiting,
117 *         0 implies a thread was waiting and will read your message, negative
118 *         numbers implies threads are still waiting. Positive implies a backlog
119 *         of messages.
120 */
121int libtrace_message_queue_try_get(libtrace_message_queue_t *mq, void *message)
122{
123        int ret;
124        // Safely decrease count first - Yes this might make us negative, however thats ok once a write comes in everything will be fine
125        // ->Fast path avoid the lock
126        if (mq->message_count <= 0)
127                return LIBTRACE_MQ_FAILED;
128        // Else grab lock and confirm this is so
129        pthread_spin_lock(&mq->spin);
130        if (mq->message_count > 0) {
131                ret = --mq->message_count;
132                // :( read(...) needs to be done within the *spin* lock otherwise blocking might steal our read
133                ASSERT_RET(read(mq->pipefd[0], message, mq->message_len), == (int) mq->message_len);
134        } else {
135                ret = LIBTRACE_MQ_FAILED;
136        }
137        pthread_spin_unlock(&mq->spin);
138        return ret;
139}
140
141/**
142 * May be negative if threads blocking and waiting for a message.
143 */
144int libtrace_message_queue_count(const libtrace_message_queue_t *mq)
145{
146        // This is only ok because we know int is atomic
147        return mq->message_count;
148}
149
150void libtrace_message_queue_destroy(libtrace_message_queue_t *mq)
151{
152        mq->message_count = 0;
153        mq->message_len = 0;
154        close(mq->pipefd[0]);
155        close(mq->pipefd[1]);
156        pthread_spin_destroy(&mq->spin);
157}
158
159/**
160 * @return a file descriptor for the queue, can be used with select() poll() etc.
161 */
162int libtrace_message_queue_get_fd(libtrace_message_queue_t *mq)
163{
164        return mq->pipefd[0];
165}
Note: See TracBrowser for help on using the repository browser.