chris@160: /** chris@160: * Copyright (c) 2014, 2015, Enzien Audio Ltd. chris@160: * chris@160: * Permission to use, copy, modify, and/or distribute this software for any chris@160: * purpose with or without fee is hereby granted, provided that the above chris@160: * copyright notice and this permission notice appear in all copies. chris@160: * chris@160: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH chris@160: * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY chris@160: * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, chris@160: * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM chris@160: * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR chris@160: * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR chris@160: * PERFORMANCE OF THIS SOFTWARE. chris@160: */ chris@160: chris@160: #ifndef _MESSAGE_QUEUE_H_ chris@160: #define _MESSAGE_QUEUE_H_ chris@160: chris@160: #include "HvMessage.h" chris@160: #include "MessagePool.h" chris@160: chris@160: struct HvBase; chris@160: chris@160: typedef struct MessageNode { chris@160: struct MessageNode *prev; // doubly linked list chris@160: struct MessageNode *next; chris@160: HvMessage *m; chris@160: void (*sendMessage)(struct HvBase *, int, const HvMessage *); chris@160: int let; chris@160: } MessageNode; chris@160: chris@160: /** A doubly linked list containing scheduled messages. */ chris@160: typedef struct MessageQueue { chris@160: MessageNode *head; // the head of the queue chris@160: MessageNode *tail; // the tail of the queue chris@160: MessageNode *pool; // the head of the reserve pool chris@160: MessagePool mp; chris@160: } MessageQueue; chris@160: chris@160: hv_size_t mq_init(MessageQueue *q); chris@160: chris@160: void mq_initWithPoolSize(MessageQueue *q, hv_size_t poolSizeKB); chris@160: chris@160: void mq_free(MessageQueue *q); chris@160: chris@160: int mq_size(MessageQueue *q); chris@160: chris@160: static inline HvMessage *mq_node_getMessage(MessageNode *n) { chris@160: return n->m; chris@160: } chris@160: chris@160: static inline int mq_node_getLet(MessageNode *n) { chris@160: return n->let; chris@160: } chris@160: chris@160: static inline bool mq_hasMessage(MessageQueue *q) { chris@160: return (q->head != NULL); chris@160: } chris@160: chris@160: // true if there is a message and it occurs before (<) timestamp chris@160: static inline bool mq_hasMessageBefore(MessageQueue *const q, const hv_uint32_t timestamp) { chris@160: return mq_hasMessage(q) && (msg_getTimestamp(mq_node_getMessage(q->head)) < timestamp); chris@160: } chris@160: chris@160: static inline MessageNode *mq_peek(MessageQueue *q) { chris@160: return q->head; chris@160: } chris@160: chris@160: /** Appends the message to the end of the queue. */ chris@160: HvMessage *mq_addMessage(MessageQueue *q, const HvMessage *m, int let, chris@160: void (*sendMessage)(struct HvBase *, int, const HvMessage *)); chris@160: chris@160: /** Insert in ascending order the message acccording to its timestamp. */ chris@160: HvMessage *mq_addMessageByTimestamp(MessageQueue *q, HvMessage *m, int let, chris@160: void (*sendMessage)(struct HvBase *, int, const HvMessage *)); chris@160: chris@160: /** Pop the message at the head of the queue (and free its memory). */ chris@160: void mq_pop(MessageQueue *q); chris@160: chris@160: /** Remove a message from the queue (and free its memory) */ chris@160: void mq_removeMessage(MessageQueue *q, HvMessage *m, chris@160: void (*sendMessage)(struct HvBase *, int, const HvMessage *)); chris@160: chris@160: /** Clears (and frees) all messages in the queue. */ chris@160: void mq_clear(MessageQueue *q); chris@160: chris@160: /** Removes all messages occuring at or after the given timestamp. */ chris@160: void mq_clearAfter(MessageQueue *q, const double timestamp); chris@160: chris@160: #endif // _MESSAGE_QUEUE_H_