summaryrefslogtreecommitdiffstats
path: root/include/libtf
diff options
context:
space:
mode:
Diffstat (limited to 'include/libtf')
-rw-r--r--include/libtf/atomic.h19
-rw-r--r--include/libtf/defines.h67
-rw-r--r--include/libtf/fiber.h52
-rw-r--r--include/libtf/list.h194
-rw-r--r--include/libtf/tf.h20
5 files changed, 352 insertions, 0 deletions
diff --git a/include/libtf/atomic.h b/include/libtf/atomic.h
new file mode 100644
index 0000000..ec9f1e0
--- /dev/null
+++ b/include/libtf/atomic.h
@@ -0,0 +1,19 @@
+/* atomic.h - wrapper for atomic gcc built-ins
+ *
+ * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 or later as
+ * published by the Free Software Foundation.
+ *
+ * See http://www.gnu.org/ for details.
+ */
+
+#ifndef TF_ATOMIC_H
+#define TF_ATOMIC_H
+
+#define tf_atomic_inc(var) __sync_add_and_fetch(&(var), 1)
+#define tf_atomic_dec(var) __sync_add_and_fetch(&(var), -1)
+
+#endif
diff --git a/include/libtf/defines.h b/include/libtf/defines.h
new file mode 100644
index 0000000..7e35ff7
--- /dev/null
+++ b/include/libtf/defines.h
@@ -0,0 +1,67 @@
+/* tf.h - libtf helper defines
+ *
+ * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 or later as
+ * published by the Free Software Foundation.
+ *
+ * See http://www.gnu.org/ for details.
+ */
+
+#ifndef TF_DEFINES_H
+#define TF_DEFINES_H
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#ifndef NULL
+#define NULL 0L
+#endif
+
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
+#ifndef array_size
+#define array_size(array) (sizeof(array) / sizeof((array)[0]))
+#endif
+
+#ifndef offsetof
+#define offsetof(type, member) __builtin_offsetof (type, member)
+#endif
+
+#ifndef container_of
+#define container_of(ptr, type, member) ({ \
+ const typeof( ((type *)0)->member ) *__mptr = (ptr); \
+ (type *)( (char *)__mptr - offsetof(type,member) );})
+#endif
+
+#ifndef likely
+#define likely(x) __builtin_expect(!!(x), 1)
+#endif
+
+#ifndef unlikely
+#define unlikely(x) __builtin_expect(!!(x), 0)
+#endif
+
+#define attribute_never_inline __attribute__((noinline))
+#define attribute_weak_function __attribute__((weak))
+
+#define TF_BUG_ON(cond) if (cond) { \
+ fprintf(stderr, "BUG: failure at %s:%d/%s(): %s!\n", \
+ __FILE__, __LINE__, __func__, #cond); \
+ abort(); \
+}
+
+#define TF_ALIGN(size,align) ((((size_t)(size)) + (align)-1) & ~((align)-1))
+
+#define TF_EMPTY_ARRAY 0
+
+#endif
diff --git a/include/libtf/fiber.h b/include/libtf/fiber.h
new file mode 100644
index 0000000..48d4924
--- /dev/null
+++ b/include/libtf/fiber.h
@@ -0,0 +1,52 @@
+/* tf.h - libtf main include
+ *
+ * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 or later as
+ * published by the Free Software Foundation.
+ *
+ * See http://www.gnu.org/ for details.
+ */
+
+#ifndef TF_FIBER_H
+#define TF_FIBER_H
+
+#include <errno.h>
+#include <libtf/defines.h>
+#include <libtf/atomic.h>
+#include <libtf/list.h>
+
+#ifndef TF_STACK_SIZE
+#define TF_STACK_SIZE 4096
+#endif
+
+struct tf_fiber {
+ unsigned int ref_count;
+ struct tf_list_node queue_node;
+ char data[TF_EMPTY_ARRAY];
+};
+
+typedef void (*tf_fiber_proc)(void *fiber);
+
+int tf_main(tf_fiber_proc fiber_main);
+
+void *tf_fiber_create(tf_fiber_proc fiber_main, int private_size);
+void *tf_fiber_get(void *data);
+void tf_fiber_put(void *data);
+
+int tf_schedule(int err);
+void tf_kill(void *fiber);
+
+static inline int tf_yield(void)
+{
+ return tf_schedule(EAGAIN);
+}
+
+static inline int tf_exit(void)
+{
+ return tf_schedule(EFAULT);
+}
+
+#endif
diff --git a/include/libtf/list.h b/include/libtf/list.h
new file mode 100644
index 0000000..22b76a8
--- /dev/null
+++ b/include/libtf/list.h
@@ -0,0 +1,194 @@
+/* list.h - libtf single and double linked list macros
+ *
+ * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 or later as
+ * published by the Free Software Foundation.
+ *
+ * See http://www.gnu.org/ for details.
+ *
+ * This based to some degree to the code in the linux kernel. This is subset
+ * of the functionality, and tf_list has separate head and node types.
+ */
+
+#ifndef TF_LIST_H
+#define TF_LIST_H
+
+#include <libtf/defines.h>
+
+struct tf_hlist_head {
+ struct tf_hlist_node *first;
+};
+
+struct tf_hlist_node {
+ struct tf_hlist_node *next;
+ struct tf_hlist_node **pprev;
+};
+
+static inline int tf_hlist_empty(const struct tf_hlist_head *h)
+{
+ return !h->first;
+}
+
+static inline int tf_hlist_hashed(const struct tf_hlist_node *n)
+{
+ return n->pprev != NULL;
+}
+
+static inline void tf_hlist_del(struct tf_hlist_node *n)
+{
+ *n->pprev = n->next;
+ n->next = NULL;
+ n->pprev = NULL;
+}
+
+static inline void tf_hlist_add_head(struct tf_hlist_node *n, struct tf_hlist_head *h)
+{
+ struct tf_hlist_node *first = h->first;
+
+ n->next = first;
+ n->pprev = &h->first;
+ h->first = n;
+}
+
+static inline void tf_hlist_add_after(struct tf_hlist_node *n, struct tf_hlist_node *prev)
+{
+ n->next = prev->next;
+ n->pprev = &prev->next;
+ prev->next = n;
+}
+
+static inline struct tf_hlist_node **tf_hlist_tail_ptr(struct tf_hlist_head *h)
+{
+ struct tf_hlist_node *n = h->first;
+ if (n == NULL)
+ return &h->first;
+ while (n->next != NULL)
+ n = n->next;
+ return &n->next;
+}
+
+#define tf_hlist_entry(ptr, type, member) container_of(ptr,type,member)
+
+#define tf_hlist_for_each(pos, head) \
+ for (pos = (head)->first; pos; pos = pos->next)
+
+#define tf_hlist_for_each_safe(pos, n, head) \
+ for (pos = (head)->first; pos && ({ n = pos->next; 1; }); pos = n)
+
+#define tf_hlist_for_each_entry(tpos, pos, head, member) \
+ for (pos = (head)->first; pos && \
+ ({ tpos = tf_hlist_entry(pos, typeof(*tpos), member); 1;}); \
+ pos = pos->next)
+
+#define tf_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
+ for (pos = (head)->first; \
+ pos && ({ n = pos->next; 1; }) && \
+ ({ tpos = tf_hlist_entry(pos, typeof(*tpos), member); 1;}); \
+ pos = n)
+
+
+struct tf_list_node {
+ struct tf_list_node *next, *prev;
+};
+
+struct tf_list_head {
+ struct tf_list_node node;
+};
+
+#define TF_LIST_INITIALIZER(l) { .next = &l, .prev = &l }
+#define TF_LIST_HEAD_INITIALIZER(l) { .node = TF_LIST_INITIALIZER((l).node) }
+
+static inline void tf_list_init(struct tf_list_node *list)
+{
+ list->next = list;
+ list->prev = list;
+}
+
+static inline void tf_list_init_head(struct tf_list_head *head)
+{
+ tf_list_init(&head->node);
+}
+
+static inline void __tf_list_add(struct tf_list_node *new,
+ struct tf_list_node *prev,
+ struct tf_list_node *next)
+{
+ next->prev = new;
+ new->next = next;
+ new->prev = prev;
+ prev->next = new;
+}
+
+static inline void tf_list_add_after(struct tf_list_node *new, struct tf_list_node *head)
+{
+ __tf_list_add(new, head, head->next);
+}
+
+static inline void tf_list_add_before(struct tf_list_node *new, struct tf_list_node *head)
+{
+ __tf_list_add(new, head->prev, head);
+}
+
+static inline void tf_list_add_head(struct tf_list_node *new, struct tf_list_head *head)
+{
+ tf_list_add_after(new, &head->node);
+}
+
+static inline void tf_list_add_tail(struct tf_list_node *new, struct tf_list_head *head)
+{
+ tf_list_add_before(new, &head->node);
+}
+
+static inline void __tf_list_del(struct tf_list_node * prev, struct tf_list_node *next)
+{
+ next->prev = prev;
+ prev->next = next;
+}
+
+static inline void tf_list_del(struct tf_list_node *entry)
+{
+ __tf_list_del(entry->prev, entry->next);
+ entry->next = NULL;
+ entry->prev = NULL;
+}
+
+static inline int tf_list_hashed(const struct tf_list_node *n)
+{
+ return n->next != n && n->next != NULL;
+}
+
+static inline int tf_list_empty(const struct tf_list_head *h)
+{
+ return !tf_list_hashed(&h->node);
+}
+
+#define tf_list_next(ptr, type, member) \
+ (tf_list_hashed(ptr) ? container_of((ptr)->next,type,member) : NULL)
+
+#define tf_list_first(head, type, member) \
+ tf_list_next(&((head)->node), type, member)
+
+#define tf_list_entry(ptr, type, member) container_of(ptr,type,member)
+
+#define tf_list_for_each(pos, head) \
+ for (pos = (head)->next; pos != (head); pos = pos->next)
+
+#define tf_list_for_each_safe(pos, n, head) \
+ for (pos = (head)->next, n = pos->next; pos != (head); \
+ pos = n, n = pos->next)
+
+#define tf_list_for_each_entry(pos, head, member) \
+ for (pos = tf_list_entry((head)->next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = tf_list_entry(pos->member.next, typeof(*pos), member))
+
+#define tf_list_for_each_entry_safe(pos, n, head, member) \
+ for (pos = tf_list_entry((head)->next, typeof(*pos), member), \
+ n = tf_list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = tf_list_entry(n->member.next, typeof(*n), member))
+
+#endif
diff --git a/include/libtf/tf.h b/include/libtf/tf.h
new file mode 100644
index 0000000..7ff7b25
--- /dev/null
+++ b/include/libtf/tf.h
@@ -0,0 +1,20 @@
+/* tf.h - libtf umbrella include
+ *
+ * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 or later as
+ * published by the Free Software Foundation.
+ *
+ * See http://www.gnu.org/ for details.
+ */
+
+#ifndef TF_H
+#define TF_H
+
+#define TF_UCTX_H "uctx-setjmp.h"
+
+#include <libtf/fiber.h>
+
+#endif