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