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