Ver Fonte

Adding simple base imp for future lib

Vinicius Teshima há 11 meses atrás
pai
commit
b78b73c8c8
2 ficheiros alterados com 58 adições e 5 exclusões
  1. 14 4
      src/future.h
  2. 44 1
      src/poll.c

+ 14 - 4
src/future.h

@@ -1,15 +1,25 @@
 #ifndef POLLABLE_H
 #define POLLABLE_H
 
-struct pollable {
-	char _reserved;
+typedef void *(*future_poll_func)(void *env, void *data);
+
+struct future {
+	future_poll_func f;
+	void *env;
 };
 
-void *pollable_pool(struct pollable p, void *data);
+void *future_poll(struct future p, void *data);
 
 #if defined(IMP) || defined(POLLABLE_IMP)
 
-
+void *
+future_poll(struct future p, void *data)
+{
+	if ( p.f == NULL ) {
+		return NULL;
+	}
+	return p.f(p.env, data);
+}
 
 #endif /* defined(IMP) || defined(POLLABLE_IMP) */
 

+ 44 - 1
src/poll.c

@@ -1,12 +1,55 @@
 #include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
 
 #define IMP
 #include "./future.h"
 
+struct counter {
+	uint32_t count;
+	uint32_t end;
+};
+
+void *counter_future(void *env, void *data);
+
 int
 main(int argc, char *argv[])
 {
-	printf("Hello World!\n");
+	struct future counter_f1 = {0};
+	struct future counter_f2 = {0};
+	struct counter c1 = {0, 5};
+	struct counter c2 = {5, 10};
+
+	counter_f1.f = &counter_future;
+	counter_f1.env = &c1;
+	counter_f2.f = &counter_future;
+	counter_f2.env = &c2;
+
+	while ( true ) {
+		void *r1 = NULL;
+		void *r2 = NULL;
+		r1 = future_poll(counter_f1, NULL);
+		r2 = future_poll(counter_f2, NULL);
+		if ( r1 != NULL && r2 != NULL ) {
+			break;
+		}
+	}
+
 	(void) argc; (void) argv;
 	return 0;
 }
+
+void *
+counter_future(void *env, void *data)
+{
+	struct counter *this = (struct counter *) env;
+	if ( this->count >= this->end ) {
+		return &this->count;
+	}
+
+	printf("Counted: %d\n", this->count);
+	++this->count;
+
+	(void) data;
+	return NULL;
+}