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