Skip to content

Commit

Permalink
spawn and kill off things correct with the ref count on the uv loop
Browse files Browse the repository at this point in the history
  • Loading branch information
pquerna committed Mar 17, 2012
1 parent fb4f774 commit afae74f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
1 change: 1 addition & 0 deletions spedye.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'sources': [
'src/spedye_init.c',
'src/spedye_master.c',
'src/spedye_worker.c',
],
'include_dirs': [
'src',
Expand Down
28 changes: 26 additions & 2 deletions src/spedye.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,43 @@

#include "uv.h"

typedef enum {
SPEDYE_WORKER_THREADED,
SPEDYE_WORKER_PROCESS,
} spedye_worker_type_e;

typedef struct spedye_conf_t {
int worker_count;
spedye_worker_type_e worker_type;
/* TODO: array of certificates */
const char *certpath;
const char *keypath;
} spedye_conf_t;


typedef enum {
SPEDYE_STARTING,
SPEDYE_RUNNING,
SPEDYE_STOPING,
SPEDYE_STOPPED,
} spedye_master_state;
} spedye_state_e;

typedef struct spedye_worker_t {
spedye_worker_type_e worker_type;
spedye_state_e state;
/* TODO: subprocess support */
uv_thread_t worker_thread;
uv_async_t worker_wakeup;
uv_loop_t *loop;
} spedye_worker_t;

typedef struct spedye_master_t {
uv_thread_t master_thread;
uv_async_t master_wakeup;
spedye_master_state state;
spedye_state_e state;
uv_loop_t *loop;
spedye_conf_t *conf;
spedye_worker_t **workers;
} spedye_master_t;


Expand All @@ -47,4 +67,8 @@ int spedye_master_create(spedye_master_t **m, spedye_conf_t *conf, uv_loop_t *lo
int spedye_master_run(spedye_master_t *m);
void spedye_master_destroy(spedye_master_t *m);

int spedye_worker_create(spedye_worker_t **w, spedye_master_t *m);
int spedye_worker_run(spedye_worker_t *w);
void spedye_worker_destroy(spedye_worker_t *w);

#endif
48 changes: 45 additions & 3 deletions src/spedye_master.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,59 @@
#include <stdio.h>



static void
master_create_workers(spedye_master_t *m)
{
int i;
for (i = 0; i < m->conf->worker_count; i++) {
spedye_worker_t *w = m->workers[i];
spedye_worker_create(&w, m);
}
}

static void
master_start_workers(spedye_master_t *m)
{
int i;
for (i = 0; i < m->conf->worker_count; i++) {
spedye_worker_t *w = m->workers[i];
spedye_worker_run(w);
}
}

static void
master_destroy_workers(spedye_master_t *m)
{
int i;
for (i = 0; i < m->conf->worker_count; i++) {
spedye_worker_t *w = m->workers[i];
spedye_worker_destroy(w);
}
}

static void
master_entry(void* arg)
{
spedye_master_t *m = arg;
m->state = SPEDYE_RUNNING;
fprintf(stderr, "%p\n", m);
master_start_workers(m);
uv_run(m->loop);
}

void master_shutdown_closecb(uv_handle_t* handle)
{
spedye_master_t *m = handle->data;
uv_unref(m->loop);
}

static void
master_shutdown(uv_async_t* handle, int status)
{
spedye_master_t *m = handle->data;
m->state = SPEDYE_STOPING;
m->master_wakeup.data = NULL;
master_destroy_workers(m);
uv_close((uv_handle_t*)&m->master_wakeup, master_shutdown_closecb);
}

int
Expand All @@ -47,12 +86,16 @@ spedye_master_create(spedye_master_t **m_out, spedye_conf_t *conf, uv_loop_t *lo
m = calloc(1, sizeof(spedye_master_t));
m->loop = loop;
m->state = SPEDYE_STARTING;
m->conf = conf;
m->workers = calloc(conf->worker_count, sizeof(spedye_worker_t));
uv_ref(m->loop);

rc = uv_async_init(m->loop, &m->master_wakeup, master_shutdown);

m->master_wakeup.data = m;

master_create_workers(m);

*m_out = m;

return rc;
Expand All @@ -75,7 +118,6 @@ spedye_master_destroy(spedye_master_t *m)
uv_async_send(&m->master_wakeup);
uv_thread_join(&m->master_thread);
m->state = SPEDYE_STOPPED;
uv_unref(m->loop);
free(m);
}

0 comments on commit afae74f

Please sign in to comment.