Skip to content

Commit

Permalink
add worker source file
Browse files Browse the repository at this point in the history
  • Loading branch information
pquerna committed Mar 17, 2012
1 parent 5d4bcc8 commit 9bd2da6
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/spedye_worker.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2012 The Spedye Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "spedye.h"
#include <stdlib.h>

void worker_shutdown_closecb(uv_handle_t* handle)
{
spedye_worker_t *w = handle->data;
uv_unref(w->loop);
}

static void
worker_shutdown(uv_async_t* handle, int status)
{
spedye_worker_t *w = handle->data;
w->state = SPEDYE_STOPING;
uv_close((uv_handle_t*)&w->worker_wakeup, worker_shutdown_closecb);
}

static void
worker_entry(void* arg)
{
spedye_worker_t *w = arg;
w->state = SPEDYE_RUNNING;
uv_run(w->loop);
}

int
spedye_worker_create(spedye_worker_t **w_out, spedye_master_t *m)
{
int rc;
spedye_worker_t *w;

*w_out = NULL;

w = calloc(1, sizeof(spedye_worker_t));
w->loop = uv_loop_new();
w->state = SPEDYE_STARTING;
uv_ref(w->loop);

rc = uv_async_init(w->loop, &w->worker_wakeup, worker_shutdown);

w->worker_wakeup.data = m;

*w_out = w;

return rc;

}

int spedye_worker_run(spedye_worker_t *w)
{
int rc;

rc = uv_thread_create(&w->worker_thread, worker_entry, (void*)w);

return 0;
}

void spedye_worker_destroy(spedye_worker_t *w)
{
/* Send wakeup */
uv_async_send(&w->worker_wakeup);
uv_thread_join(&w->worker_thread);
w->state = SPEDYE_STOPPED;
uv_loop_delete(w->loop);
free(w);
}

0 comments on commit 9bd2da6

Please sign in to comment.