diff --git a/machinarium/machinarium.h b/machinarium/machinarium.h new file mode 100644 index 00000000..6c0118f3 --- /dev/null +++ b/machinarium/machinarium.h @@ -0,0 +1,54 @@ +#ifndef MACHINARIUM_H_ +#define MACHINARIUM_H_ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +#include +#include +#include + +#if __GNUC__ >= 4 +# define MM_API __attribute__((visibility("default"))) +#else +# define MM_API +#endif + +typedef void (*mmfunction_t)(void *arg); + +typedef void* mm_t; +typedef void* mmio_t; + +MM_API mm_t +mm_new(void); + +MM_API int mm_free(mm_t); +MM_API int64_t mm_create(mm_t, mmfunction_t, void *arg); +MM_API int mm_is_online(mm_t); +MM_API int mm_is_cancel(mm_t); +MM_API void mm_start(mm_t); +MM_API void mm_stop(mm_t); +MM_API void mm_sleep(mm_t, uint64_t time_ms); +MM_API int mm_wait(mm_t, uint64_t id); +MM_API int mm_cancel(mm_t, uint64_t id); + +MM_API mmio_t +mm_io_new(mm_t); + +MM_API void mm_close(mmio_t); +MM_API int mm_fd(mmio_t); +MM_API int mm_is_connected(mmio_t); +MM_API int mm_connect(mmio_t, char *addr, int port, uint64_t time_ms); +MM_API int mm_connect_is_timeout(mmio_t); +MM_API int mm_bind(mmio_t, char *addr, int port); +MM_API int mm_accept(mmio_t, mmio_t *client); +MM_API int mm_read(mmio_t, int size, uint64_t time_ms); +MM_API int mm_read_is_timeout(mmio_t); +MM_API char *mm_read_buf(mmio_t); +MM_API int mm_write(mmio_t, char *buf, int size, uint64_t time_ms); +MM_API int mm_write_is_timeout(mmio_t); + +#endif diff --git a/machinarium/machinarium_private.h b/machinarium/machinarium_private.h new file mode 100644 index 00000000..6178c6c0 --- /dev/null +++ b/machinarium/machinarium_private.h @@ -0,0 +1,30 @@ +#ifndef MM_PRIVATE_H_ +#define MM_PRIVATE_H_ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +#define _GNU_SOURCE 1 + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include "mm_macro.h" +#include "mm_list.h" +#include "mm_buf.h" +#include "mm_context.h" + +#endif diff --git a/machinarium/makefile b/machinarium/makefile new file mode 100644 index 00000000..405eb98c --- /dev/null +++ b/machinarium/makefile @@ -0,0 +1,17 @@ +# libmachinarium makefile. +# +CC = gcc +AR = ar +RM = rm +CFLAGS = -I. -Wall -g -O0 -pedantic +OBJECTS = mm_context.o \ + mm_fiber.o \ + mm_scheduler.o \ + mm.o +LIB = libmachinarium.a +$(LIB): clean $(OBJECTS) + $(AR) cr $(LIB) $(OBJECTS) +.c.o: + $(CC) $(CFLAGS) -c $< +clean: + $(RM) -f $(OBJECTS) $(LIB) diff --git a/machinarium/mm_list.h b/machinarium/mm_list.h new file mode 100644 index 00000000..3863e4ab --- /dev/null +++ b/machinarium/mm_list.h @@ -0,0 +1,61 @@ +#ifndef MM_LIST_H_ +#define MM_LIST_H_ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +typedef struct mmlist mmlist; + +struct mmlist { + mmlist *next, *prev; +}; + +static inline void +mm_listinit(mmlist *h) +{ + h->next = h->prev = h; +} + +static inline void +mm_listappend(mmlist *h, mmlist *n) +{ + n->next = h; + n->prev = h->prev; + n->prev->next = n; + n->next->prev = n; +} + +static inline void +mm_listunlink(mmlist *n) +{ + n->prev->next = n->next; + n->next->prev = n->prev; +} + +static inline void +mm_listpush(mmlist *h, mmlist *n) +{ + n->next = h->next; + n->prev = h; + n->prev->next = n; + n->next->prev = n; +} + +static inline mmlist* +mm_listpop(mmlist *h) +{ + register mmlist *pop = h->next; + mm_listunlink(pop); + return pop; +} + +#define mm_listforeach(H, I) \ + for (I = (H)->next; I != H; I = (I)->next) + +#define mm_listforeach_safe(H, I, N) \ + for (I = (H)->next; I != H && (N = I->next); I = N) + +#endif diff --git a/machinarium/mm_macro.h b/machinarium/mm_macro.h new file mode 100644 index 00000000..1f856d9b --- /dev/null +++ b/machinarium/mm_macro.h @@ -0,0 +1,13 @@ +#ifndef MM_MACRO_H_ +#define MM_MACRO_H_ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +#define mm_container_of(ptr, t, f) \ + ((t*)((char*)(ptr) - __builtin_offsetof(t, f))) + +#endif