chris@160
|
1 /**
|
chris@160
|
2 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
|
chris@160
|
3 *
|
chris@160
|
4 * Permission to use, copy, modify, and/or distribute this software for any
|
chris@160
|
5 * purpose with or without fee is hereby granted, provided that the above
|
chris@160
|
6 * copyright notice and this permission notice appear in all copies.
|
chris@160
|
7 *
|
chris@160
|
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
chris@160
|
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
chris@160
|
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
chris@160
|
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
chris@160
|
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
chris@160
|
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
chris@160
|
14 * PERFORMANCE OF THIS SOFTWARE.
|
chris@160
|
15 */
|
chris@160
|
16
|
chris@160
|
17 #ifndef _MESSAGE_QUEUE_H_
|
chris@160
|
18 #define _MESSAGE_QUEUE_H_
|
chris@160
|
19
|
chris@160
|
20 #include "HvMessage.h"
|
chris@160
|
21 #include "MessagePool.h"
|
chris@160
|
22
|
chris@160
|
23 struct HvBase;
|
chris@160
|
24
|
chris@160
|
25 typedef struct MessageNode {
|
chris@160
|
26 struct MessageNode *prev; // doubly linked list
|
chris@160
|
27 struct MessageNode *next;
|
chris@160
|
28 HvMessage *m;
|
chris@160
|
29 void (*sendMessage)(struct HvBase *, int, const HvMessage *);
|
chris@160
|
30 int let;
|
chris@160
|
31 } MessageNode;
|
chris@160
|
32
|
chris@160
|
33 /** A doubly linked list containing scheduled messages. */
|
chris@160
|
34 typedef struct MessageQueue {
|
chris@160
|
35 MessageNode *head; // the head of the queue
|
chris@160
|
36 MessageNode *tail; // the tail of the queue
|
chris@160
|
37 MessageNode *pool; // the head of the reserve pool
|
chris@160
|
38 MessagePool mp;
|
chris@160
|
39 } MessageQueue;
|
chris@160
|
40
|
chris@160
|
41 hv_size_t mq_init(MessageQueue *q);
|
chris@160
|
42
|
chris@160
|
43 void mq_initWithPoolSize(MessageQueue *q, hv_size_t poolSizeKB);
|
chris@160
|
44
|
chris@160
|
45 void mq_free(MessageQueue *q);
|
chris@160
|
46
|
chris@160
|
47 int mq_size(MessageQueue *q);
|
chris@160
|
48
|
chris@160
|
49 static inline HvMessage *mq_node_getMessage(MessageNode *n) {
|
chris@160
|
50 return n->m;
|
chris@160
|
51 }
|
chris@160
|
52
|
chris@160
|
53 static inline int mq_node_getLet(MessageNode *n) {
|
chris@160
|
54 return n->let;
|
chris@160
|
55 }
|
chris@160
|
56
|
chris@160
|
57 static inline bool mq_hasMessage(MessageQueue *q) {
|
chris@160
|
58 return (q->head != NULL);
|
chris@160
|
59 }
|
chris@160
|
60
|
chris@160
|
61 // true if there is a message and it occurs before (<) timestamp
|
chris@160
|
62 static inline bool mq_hasMessageBefore(MessageQueue *const q, const hv_uint32_t timestamp) {
|
chris@160
|
63 return mq_hasMessage(q) && (msg_getTimestamp(mq_node_getMessage(q->head)) < timestamp);
|
chris@160
|
64 }
|
chris@160
|
65
|
chris@160
|
66 static inline MessageNode *mq_peek(MessageQueue *q) {
|
chris@160
|
67 return q->head;
|
chris@160
|
68 }
|
chris@160
|
69
|
chris@160
|
70 /** Appends the message to the end of the queue. */
|
chris@160
|
71 HvMessage *mq_addMessage(MessageQueue *q, const HvMessage *m, int let,
|
chris@160
|
72 void (*sendMessage)(struct HvBase *, int, const HvMessage *));
|
chris@160
|
73
|
chris@160
|
74 /** Insert in ascending order the message acccording to its timestamp. */
|
chris@160
|
75 HvMessage *mq_addMessageByTimestamp(MessageQueue *q, HvMessage *m, int let,
|
chris@160
|
76 void (*sendMessage)(struct HvBase *, int, const HvMessage *));
|
chris@160
|
77
|
chris@160
|
78 /** Pop the message at the head of the queue (and free its memory). */
|
chris@160
|
79 void mq_pop(MessageQueue *q);
|
chris@160
|
80
|
chris@160
|
81 /** Remove a message from the queue (and free its memory) */
|
chris@160
|
82 void mq_removeMessage(MessageQueue *q, HvMessage *m,
|
chris@160
|
83 void (*sendMessage)(struct HvBase *, int, const HvMessage *));
|
chris@160
|
84
|
chris@160
|
85 /** Clears (and frees) all messages in the queue. */
|
chris@160
|
86 void mq_clear(MessageQueue *q);
|
chris@160
|
87
|
chris@160
|
88 /** Removes all messages occuring at or after the given timestamp. */
|
chris@160
|
89 void mq_clearAfter(MessageQueue *q, const double timestamp);
|
chris@160
|
90
|
chris@160
|
91 #endif // _MESSAGE_QUEUE_H_
|