summaryrefslogtreecommitdiffstats
path: root/ffd/queue.h
diff options
context:
space:
mode:
Diffstat (limited to 'ffd/queue.h')
-rw-r--r--ffd/queue.h69
1 files changed, 69 insertions, 0 deletions
diff --git a/ffd/queue.h b/ffd/queue.h
new file mode 100644
index 0000000..9fae53d
--- /dev/null
+++ b/ffd/queue.h
@@ -0,0 +1,69 @@
+/*
+ Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#ifndef _FFD_QUEUE_H_
+#define _FFD_QUEUE_H_
+
+#include "util.h"
+
+
+typedef void (*ffd_queue_cb)(const struct timespec *timeout, void *arg);
+
+typedef struct _ffd_queue_head {
+ struct _ffd_queue_head *next;
+ ffd_queue_cb cb;
+ struct timespec timeout;
+ void *arg;
+} ffd_queue_head;
+
+
+void ffd_queue_put(ffd_queue_head **queue, ffd_queue_cb cb, const struct timespec *timeout, void *arg);
+
+
+static inline void ffd_queue_put_delayed(ffd_queue_head **queue, ffd_queue_cb cb, const struct timespec *timeout, int delay, void *arg) {
+ struct timespec timeout_delayed = *timeout;
+ add_interval(&timeout_delayed, delay);
+ ffd_queue_put(queue, cb, &timeout_delayed, arg);
+}
+
+static inline void ffd_queue_run(ffd_queue_head **queue) {
+ while (*queue && timespec_after(&now, &(*queue)->timeout)) {
+ ffd_queue_head *entry = *queue;
+ *queue = (*queue)->next;
+
+ entry->cb(&entry->timeout, entry->arg);
+ free(entry);
+ }
+}
+
+static inline int ffd_queue_timeout(ffd_queue_head *const *queue) {
+ if (!*queue)
+ return -1;
+
+ return max(timespec_diff(&(*queue)->timeout, &now), 0);
+}
+
+#endif /* _FFD_QUEUE_H_ */