annotate projects/heavy/circularBuffer/MessageQueue.h @ 163:20b52283c7b4 heavy-updated

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