Skip to content

Commit

Permalink
lib: monkey: sync changes for event-select backend
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Mar 28, 2018
1 parent c7c1134 commit 53e2b25
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions lib/monkey/mk_core/mk_event_select.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@

struct fd_timer {
int fd;
int run;
time_t sec;
long nsec;
pthread_t tid;
};

static inline int _mk_event_init()
Expand Down Expand Up @@ -162,7 +164,7 @@ void _timeout_worker(void *arg)
t_spec.tv_sec = timer->sec;
t_spec.tv_nsec = timer->nsec;

while (1) {
while (timer->run == MK_TRUE) {
/* sleep for a while */
nanosleep(&t_spec, NULL);

Expand All @@ -174,8 +176,7 @@ void _timeout_worker(void *arg)
}
}

close(timer->fd);
free(timer);
pthread_exit(NULL);
}

/*
Expand All @@ -191,7 +192,6 @@ static inline int _mk_event_timeout_create(struct mk_event_ctx *ctx,
int fd[2];
struct mk_event *event;
struct fd_timer *timer;
pthread_t tid;

timer = mk_mem_alloc(sizeof(struct fd_timer));
if (!timer) {
Expand All @@ -217,9 +217,12 @@ static inline int _mk_event_timeout_create(struct mk_event_ctx *ctx,
timer->fd = fd[1];
timer->sec = sec;
timer->nsec = nsec;
timer->run = MK_TRUE;

event->data = timer;

/* Now the dirty workaround, create a thread */
ret = mk_utils_worker_spawn(_timeout_worker, timer, &tid);
ret = mk_utils_worker_spawn(_timeout_worker, timer, &timer->tid);
if (ret < 0) {
close(fd[0]);
close(fd[1]);
Expand All @@ -230,6 +233,30 @@ static inline int _mk_event_timeout_create(struct mk_event_ctx *ctx,
return fd[0];
}

static inline int _mk_event_timeout_destroy(struct mk_event_ctx *ctx, void *data)
{
int fd;
struct mk_event *event;
struct fd_timer *timer;

event = (struct mk_event *) data;

fd = event->fd;
_mk_event_del(ctx, event);

timer = event->data;
timer->run = MK_FALSE;

/* Wait for the background worker to finish */
pthread_join(timer->tid, NULL);

/* Cleanup */
close(timer->fd);
close(fd);
mk_mem_free(timer);
return 0;
}

static inline int _mk_event_channel_create(struct mk_event_ctx *ctx,
int *r_fd, int *w_fd, void *data)
{
Expand Down

0 comments on commit 53e2b25

Please sign in to comment.