Allow arbitrary number of connections (#13)

* refactor plasma to use an event loop

* unify comment style

* Clean up Makefile flags.

* Randomize socket names in tests so multiple copies of the tests can be run in parallel without conflict.
This commit is contained in:
Philipp Moritz
2016-09-07 20:19:37 -07:00
committed by Robert Nishihara
parent dd23a68f79
commit a62c0f8fac
13 changed files with 595 additions and 264 deletions
+85
View File
@@ -0,0 +1,85 @@
#include "event_loop.h"
#include <assert.h>
#include <unistd.h>
UT_icd item_icd = { sizeof(event_loop_item), NULL, NULL, NULL };
UT_icd poll_icd = { sizeof(struct pollfd), NULL, NULL, NULL };
/* Initializes the event loop.
* This function needs to be called before any other event loop function. */
void event_loop_init(event_loop *loop) {
utarray_new(loop->items, &item_icd);
utarray_new(loop->waiting, &poll_icd);
}
/* Add a new file descriptor fd to the event loop.
* This function sets a user defined type and id for the file descriptor
* which can be queried using event_loop_type and event_loop_id. The parameter
* events is the same as in http://linux.die.net/man/2/poll.
* Returns the index of the item in the event loop. */
int64_t event_loop_attach(event_loop *loop, int type, data_connection* connection, int fd, int events) {
assert(utarray_len(loop->items) == utarray_len(loop->waiting));
int64_t index = utarray_len(loop->items);
event_loop_item item = { .type = type };
if (connection) {
item.connection = *connection;
}
utarray_push_back(loop->items, &item );
struct pollfd waiting = { .fd = fd, .events = events };
utarray_push_back(loop->waiting, &waiting);
return index;
}
/* Detach a file descriptor from the event loop.
* This invalidates all other indices into the event loop items, but leaves
* the ids of the event loop items valid. */
void event_loop_detach(event_loop *loop, int64_t index, int shall_close) {
struct pollfd *waiting_item = (struct pollfd*) utarray_eltptr(loop->waiting, index);
struct pollfd *waiting_back = (struct pollfd*) utarray_back(loop->waiting);
if (shall_close) {
close(waiting_item->fd);
}
*waiting_item = *waiting_back;
utarray_pop_back(loop->waiting);
event_loop_item *items_item = (event_loop_item*) utarray_eltptr(loop->items, index);
event_loop_item *items_back = (event_loop_item*) utarray_back(loop->items);
*items_item = *items_back;
utarray_pop_back(loop->items);
}
/* Poll the file descriptors associated to this event loop.
* See http://linux.die.net/man/2/poll */
int event_loop_poll(event_loop *loop) {
return poll((struct pollfd*) utarray_front(loop->waiting), utarray_len(loop->waiting), -1);
}
/* Get the total number of file descriptors participating in the event loop. */
int64_t event_loop_size(event_loop *loop) {
return utarray_len(loop->waiting);
}
/* Get the pollfd structure associated to a file descriptor participating in the event loop. */
struct pollfd *event_loop_get(event_loop *loop, int64_t index) {
return (struct pollfd*) utarray_eltptr(loop->waiting, index);
}
/* Set the data connection information for participant in the event loop. */
void event_loop_set_connection(event_loop *loop, int64_t index, const data_connection* conn) {
event_loop_item *item = (event_loop_item*) utarray_eltptr(loop->items, index);
item->connection = *conn;
}
/* Get the data connection information for participant in the event loop. */
data_connection* event_loop_get_connection(event_loop *loop, int64_t index) {
event_loop_item *item = (event_loop_item*) utarray_eltptr(loop->items, index);
return &item->connection;
}
/* Free the space associated to the event loop.
* Does not free the event_loop datastructure itself. */
void event_loop_free(event_loop *loop) {
utarray_free(loop->items);
utarray_free(loop->waiting);
}