forked from mirrors/kore
Add initial stab at asynchronous background tasks.
More to follow.
This commit is contained in:
parent
437f2e9ed2
commit
cf700b34f7
6
Makefile
6
Makefile
|
@ -28,6 +28,12 @@ ifneq ("$(PGSQL)", "")
|
||||||
CFLAGS+=-I$(shell pg_config --includedir) -DKORE_USE_PGSQL
|
CFLAGS+=-I$(shell pg_config --includedir) -DKORE_USE_PGSQL
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifneq ("$(TASKS)", "")
|
||||||
|
S_SRC+=contrib/tasks/kore_tasks.c
|
||||||
|
LDFLAGS+=-lpthread
|
||||||
|
CFLAGS+=-DKORE_USE_TASKS
|
||||||
|
endif
|
||||||
|
|
||||||
OSNAME=$(shell uname -s | sed -e 's/[-_].*//g' | tr A-Z a-z)
|
OSNAME=$(shell uname -s | sed -e 's/[-_].*//g' | tr A-Z a-z)
|
||||||
ifeq ("$(OSNAME)", "darwin")
|
ifeq ("$(OSNAME)", "darwin")
|
||||||
CFLAGS+=-I/opt/local/include/
|
CFLAGS+=-I/opt/local/include/
|
||||||
|
|
|
@ -0,0 +1,159 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014 Joris Vink <joris@coders.se>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <sys/queue.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "kore.h"
|
||||||
|
#include "http.h"
|
||||||
|
#include "kore_tasks.h"
|
||||||
|
|
||||||
|
static u_int8_t threads;
|
||||||
|
static pthread_mutex_t task_lock;
|
||||||
|
static pthread_cond_t task_broadcast;
|
||||||
|
|
||||||
|
static TAILQ_HEAD(, kore_task) task_list;
|
||||||
|
static TAILQ_HEAD(, kore_task_thread) task_threads;
|
||||||
|
|
||||||
|
static void *task_thread(void *);
|
||||||
|
static void task_thread_spawn(void);
|
||||||
|
|
||||||
|
void
|
||||||
|
kore_task_init(void)
|
||||||
|
{
|
||||||
|
threads = 0;
|
||||||
|
|
||||||
|
TAILQ_INIT(&task_list);
|
||||||
|
TAILQ_INIT(&task_threads);
|
||||||
|
|
||||||
|
pthread_mutex_init(&task_lock, NULL);
|
||||||
|
pthread_cond_init(&task_broadcast, NULL);
|
||||||
|
|
||||||
|
task_thread_spawn();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
kore_task_setup(struct http_request *req)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < HTTP_TASK_MAX; i++)
|
||||||
|
req->tasks[i] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
kore_task_create(struct http_request *req, int idx,
|
||||||
|
void (*entry)(struct kore_task *))
|
||||||
|
{
|
||||||
|
if (idx >= HTTP_TASK_MAX)
|
||||||
|
fatal("kore_task_create: idx > HTTP_TASK_MAX");
|
||||||
|
if (req->tasks[idx] != NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
req->flags |= HTTP_REQUEST_SLEEPING;
|
||||||
|
|
||||||
|
req->tasks[idx] = kore_malloc(sizeof(struct kore_task));
|
||||||
|
req->tasks[idx]->owner = req;
|
||||||
|
req->tasks[idx]->entry = entry;
|
||||||
|
req->tasks[idx]->type = KORE_TYPE_TASK;
|
||||||
|
|
||||||
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, req->tasks[idx]->fds) == -1)
|
||||||
|
fatal("kore_task_create: socketpair() %s", errno_s);
|
||||||
|
|
||||||
|
kore_platform_schedule_read(req->tasks[idx]->fds[0], req->tasks[idx]);
|
||||||
|
|
||||||
|
pthread_mutex_lock(&task_lock);
|
||||||
|
TAILQ_INSERT_TAIL(&task_list, req->tasks[idx], list);
|
||||||
|
pthread_mutex_unlock(&task_lock);
|
||||||
|
|
||||||
|
pthread_cond_broadcast(&task_broadcast);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
kore_task_finish(struct kore_task *t)
|
||||||
|
{
|
||||||
|
kore_debug("kore_task_finished: %p", t);
|
||||||
|
|
||||||
|
close(t->fds[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
kore_task_channel_write(struct kore_task *t, void *data, u_int32_t len)
|
||||||
|
{
|
||||||
|
kore_debug("kore_task_channel_write: %p <- %p (%ld)", t, data, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
kore_task_handle(struct kore_task *t, int finished)
|
||||||
|
{
|
||||||
|
struct http_request *req = t->owner;
|
||||||
|
|
||||||
|
kore_debug("kore_task_handle: %p, %d", t, finished);
|
||||||
|
|
||||||
|
if (finished) {
|
||||||
|
close(t->fds[0]);
|
||||||
|
req->flags &= ~HTTP_REQUEST_SLEEPING;
|
||||||
|
kore_mem_free(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
task_thread_spawn(void)
|
||||||
|
{
|
||||||
|
struct kore_task_thread *tt;
|
||||||
|
|
||||||
|
tt = kore_malloc(sizeof(*tt));
|
||||||
|
tt->idx = threads++;
|
||||||
|
TAILQ_INSERT_TAIL(&task_threads, tt, list);
|
||||||
|
|
||||||
|
if (pthread_create(&(tt->tid), NULL, task_thread, tt) != 0)
|
||||||
|
fatal("pthread_create: %s", errno_s);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *
|
||||||
|
task_thread(void *arg)
|
||||||
|
{
|
||||||
|
struct kore_task *t;
|
||||||
|
struct kore_task_thread *tt = arg;
|
||||||
|
|
||||||
|
kore_debug("task_thread: #%d starting", tt->idx);
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
pthread_mutex_lock(&task_lock);
|
||||||
|
if (TAILQ_EMPTY(&task_list))
|
||||||
|
pthread_cond_wait(&task_broadcast, &task_lock);
|
||||||
|
|
||||||
|
kore_debug("task_thread#%d: woke up", tt->idx);
|
||||||
|
|
||||||
|
t = TAILQ_FIRST(&task_list);
|
||||||
|
TAILQ_REMOVE(&task_list, t, list);
|
||||||
|
pthread_mutex_unlock(&task_lock);
|
||||||
|
|
||||||
|
kore_debug("task_thread#%d: executing %p", tt->idx, t);
|
||||||
|
t->thread = tt;
|
||||||
|
t->entry(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_exit(NULL);
|
||||||
|
|
||||||
|
/* NOTREACHED */
|
||||||
|
return (NULL);
|
||||||
|
}
|
|
@ -121,7 +121,9 @@ struct http_file {
|
||||||
#define HTTP_REQUEST_SLEEPING 0x04
|
#define HTTP_REQUEST_SLEEPING 0x04
|
||||||
|
|
||||||
#define HTTP_PGSQL_MAX 20
|
#define HTTP_PGSQL_MAX 20
|
||||||
|
#define HTTP_TASK_MAX 10
|
||||||
struct kore_pgsql;
|
struct kore_pgsql;
|
||||||
|
struct kore_task;
|
||||||
|
|
||||||
struct http_request {
|
struct http_request {
|
||||||
u_int8_t method;
|
u_int8_t method;
|
||||||
|
@ -142,6 +144,7 @@ struct http_request {
|
||||||
|
|
||||||
struct kore_module_handle *hdlr;
|
struct kore_module_handle *hdlr;
|
||||||
struct kore_pgsql *pgsql[HTTP_PGSQL_MAX];
|
struct kore_pgsql *pgsql[HTTP_PGSQL_MAX];
|
||||||
|
struct kore_task *tasks[HTTP_TASK_MAX];
|
||||||
|
|
||||||
TAILQ_HEAD(, http_header) req_headers;
|
TAILQ_HEAD(, http_header) req_headers;
|
||||||
TAILQ_HEAD(, http_header) resp_headers;
|
TAILQ_HEAD(, http_header) resp_headers;
|
||||||
|
|
|
@ -107,6 +107,7 @@ TAILQ_HEAD(netbuf_head, netbuf);
|
||||||
#define KORE_TYPE_LISTENER 1
|
#define KORE_TYPE_LISTENER 1
|
||||||
#define KORE_TYPE_CONNECTION 2
|
#define KORE_TYPE_CONNECTION 2
|
||||||
#define KORE_TYPE_PGSQL_CONN 3
|
#define KORE_TYPE_PGSQL_CONN 3
|
||||||
|
#define KORE_TYPE_TASK 4
|
||||||
|
|
||||||
struct listener {
|
struct listener {
|
||||||
u_int8_t type;
|
u_int8_t type;
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014 Joris Vink <joris@coders.se>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __H_KORE_TASKS
|
||||||
|
#define __H_KORE_TASKS
|
||||||
|
|
||||||
|
struct kore_task {
|
||||||
|
u_int8_t type;
|
||||||
|
|
||||||
|
int fds[2];
|
||||||
|
void *owner;
|
||||||
|
void *thread;
|
||||||
|
void (*entry)(struct kore_task *);
|
||||||
|
|
||||||
|
TAILQ_ENTRY(kore_task) list;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct kore_task_thread {
|
||||||
|
u_int8_t idx;
|
||||||
|
pthread_t tid;
|
||||||
|
|
||||||
|
TAILQ_ENTRY(kore_task_thread) list;
|
||||||
|
};
|
||||||
|
|
||||||
|
void kore_task_init(void);
|
||||||
|
void kore_task_finish(struct kore_task *);
|
||||||
|
void kore_task_destroy(struct kore_task *);
|
||||||
|
void kore_task_setup(struct http_request *);
|
||||||
|
void kore_task_handle(struct kore_task *, int);
|
||||||
|
void kore_task_create(struct http_request *, int,
|
||||||
|
void (*entry)(struct kore_task *));
|
||||||
|
|
||||||
|
void kore_task_channel_write(struct kore_task *, void *, u_int32_t);
|
||||||
|
void kore_task_channel_read(struct kore_task *,
|
||||||
|
u_int8_t **, u_int32_t *);
|
||||||
|
|
||||||
|
#endif
|
|
@ -27,6 +27,10 @@
|
||||||
#include "contrib/postgres/kore_pgsql.h"
|
#include "contrib/postgres/kore_pgsql.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(KORE_USE_TASKS)
|
||||||
|
#include "kore_tasks.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
static char *http_status_text(int);
|
static char *http_status_text(int);
|
||||||
static int http_post_data_recv(struct netbuf *);
|
static int http_post_data_recv(struct netbuf *);
|
||||||
static char *http_post_data_text(struct http_request *);
|
static char *http_post_data_text(struct http_request *);
|
||||||
|
@ -137,6 +141,10 @@ http_request_new(struct connection *c, struct spdy_stream *s, char *host,
|
||||||
req->agent = kore_strdup("unknown");
|
req->agent = kore_strdup("unknown");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(KORE_USE_TASKS)
|
||||||
|
kore_task_setup(req);
|
||||||
|
#endif
|
||||||
|
|
||||||
http_request_count++;
|
http_request_count++;
|
||||||
TAILQ_INSERT_HEAD(&http_requests, req, list);
|
TAILQ_INSERT_HEAD(&http_requests, req, list);
|
||||||
TAILQ_INSERT_TAIL(&(c->http_requests), req, olist);
|
TAILQ_INSERT_TAIL(&(c->http_requests), req, olist);
|
||||||
|
|
15
src/linux.c
15
src/linux.c
|
@ -25,6 +25,10 @@
|
||||||
#include "contrib/postgres/kore_pgsql.h"
|
#include "contrib/postgres/kore_pgsql.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(KORE_USE_TASKS)
|
||||||
|
#include "kore_tasks.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
static int efd = -1;
|
static int efd = -1;
|
||||||
static u_int32_t event_count = 0;
|
static u_int32_t event_count = 0;
|
||||||
static struct epoll_event *events = NULL;
|
static struct epoll_event *events = NULL;
|
||||||
|
@ -103,6 +107,12 @@ kore_platform_event_wait(void)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(KORE_USE_TASKS)
|
||||||
|
if (type == KORE_TYPE_TASK) {
|
||||||
|
kore_task_handle(events[i].data.ptr, 1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
c = (struct connection *)events[i].data.ptr;
|
c = (struct connection *)events[i].data.ptr;
|
||||||
kore_connection_disconnect(c);
|
kore_connection_disconnect(c);
|
||||||
continue;
|
continue;
|
||||||
|
@ -140,6 +150,11 @@ kore_platform_event_wait(void)
|
||||||
case KORE_TYPE_PGSQL_CONN:
|
case KORE_TYPE_PGSQL_CONN:
|
||||||
kore_pgsql_handle(events[i].data.ptr, 0);
|
kore_pgsql_handle(events[i].data.ptr, 0);
|
||||||
break;
|
break;
|
||||||
|
#endif
|
||||||
|
#if defined(KORE_USE_TASK)
|
||||||
|
case KORE_TYPE_TASK:
|
||||||
|
kore_task_handle(events[i].data.ptr, 0);
|
||||||
|
break;
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
fatal("wrong type in event %d", type);
|
fatal("wrong type in event %d", type);
|
||||||
|
|
|
@ -29,6 +29,10 @@
|
||||||
#include "contrib/postgres/kore_pgsql.h"
|
#include "contrib/postgres/kore_pgsql.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(KORE_USE_TASKS)
|
||||||
|
#include "kore_tasks.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(WORKER_DEBUG)
|
#if defined(WORKER_DEBUG)
|
||||||
#define worker_debug(fmt, ...) printf(fmt, ##__VA_ARGS__)
|
#define worker_debug(fmt, ...) printf(fmt, ##__VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
|
@ -229,6 +233,10 @@ kore_worker_entry(struct kore_worker *kw)
|
||||||
kore_pgsql_init();
|
kore_pgsql_init();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(KORE_USE_TASKS)
|
||||||
|
kore_task_init();
|
||||||
|
#endif
|
||||||
|
|
||||||
worker->accept_treshold = worker_max_connections / 10;
|
worker->accept_treshold = worker_max_connections / 10;
|
||||||
kore_log(LOG_NOTICE, "worker %d started (cpu#%d)", kw->id, kw->cpu);
|
kore_log(LOG_NOTICE, "worker %d started (cpu#%d)", kw->id, kw->cpu);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue