Browse Source

Add more atomic protection

personal/stbuehler/wip
Stefan Bühler 14 years ago
parent
commit
c62f8f7945
  1. 4
      src/plugin.c
  2. 17
      src/server.c
  3. 15
      src/server.h

4
src/plugin.c

@ -50,7 +50,7 @@ static void plugin_free_setups(server *srv, plugin *p) {
void plugin_free(server *srv, plugin *p) {
if (!p) return;
if (srv->state == SERVER_RUNNING) {
if (g_atomic_int_get(&srv->state) == SERVER_RUNNING) {
ERROR(srv, "Cannot free plugin '%s' while server is running", p->name);
return;
}
@ -71,7 +71,7 @@ gboolean plugin_register(server *srv, const gchar *name, PluginInit init) {
return FALSE;
}
if (srv->state != SERVER_STARTING) {
if (g_atomic_int_get(&srv->state) != SERVER_STARTING) {
ERROR(srv, "Cannot register plugin '%s' after server was started", name);
return FALSE;
}

17
src/server.c

@ -34,7 +34,7 @@ static void sigint_cb(struct ev_loop *loop, struct ev_signal *w, int revents) {
server *srv = (server*) w->data;
UNUSED(revents);
if (!srv->exiting) {
if (!g_atomic_int_get(&srv->exiting)) {
INFO(srv, "Got signal, shutdown");
server_stop(srv);
} else {
@ -84,7 +84,6 @@ server* server_new() {
void server_free(server* srv) {
if (!srv) return;
srv->exiting = TRUE;
server_stop(srv);
/* join all workers */
@ -209,11 +208,12 @@ static connection* con_get(server *srv) {
WORKER_LOCK(srv, &srv->lock_con);
if (srv->connections_active >= srv->connections->len) {
con = connection_new(srv);
con->idx = srv->connections_active++;
con->idx = srv->connections_active;
g_array_append_val(srv->connections, con);
} else {
con = g_array_index(srv->connections, connection*, srv->connections_active++);
con = g_array_index(srv->connections, connection*, srv->connections_active);
}
g_atomic_int_inc((gint*) &srv->connections_active);
WORKER_UNLOCK(srv, &srv->lock_con);
return con;
}
@ -225,7 +225,7 @@ void con_put(connection *con) {
g_atomic_int_add((gint*) &con->wrk->connection_load, -1);
WORKER_LOCK(srv, &srv->lock_con);
con->wrk = NULL;
srv->connections_active--;
g_atomic_int_add((gint*) &srv->connections_active, -1);
if (con->idx != srv->connections_active) {
/* Swap [con->idx] and [srv->connections_active] */
connection *tmp;
@ -305,7 +305,7 @@ void server_listen(server *srv, int fd) {
fd_init(fd);
ev_init(&sock->watcher, server_listen_cb);
ev_io_set(&sock->watcher, fd, EV_READ);
if (srv->state == SERVER_RUNNING) ev_io_start(srv->main_worker->loop, &sock->watcher);
if (g_atomic_int_get(&srv->state) == SERVER_RUNNING) ev_io_start(srv->main_worker->loop, &sock->watcher);
g_array_append_val(srv->sockets, sock);
}
@ -314,8 +314,9 @@ void server_start(server *srv) {
guint i;
GHashTableIter iter;
gpointer k, v;
if (srv->state == SERVER_STOPPING || srv->state == SERVER_RUNNING) return; /* no restart after stop */
srv->state = SERVER_RUNNING;
server_state srvstate = g_atomic_int_get(&srv->state);
if (srvstate == SERVER_STOPPING || srvstate == SERVER_RUNNING) return; /* no restart after stop */
g_atomic_int_set(&srv->state, SERVER_RUNNING);
if (!srv->mainaction) {
ERROR(srv, "%s", "No action handlers defined");

15
src/server.h

@ -35,12 +35,11 @@ struct worker;
struct server {
guint32 magic; /** server magic version, check against LIGHTTPD_SERVER_MAGIC in plugins */
server_state state;
server_state state; /** atomic access */
struct worker *main_worker;
guint worker_count;
GArray *workers;
GThreadPool *worker_pool;
guint loop_flags;
ev_signal
@ -50,10 +49,10 @@ struct server {
ev_prepare srv_prepare;
ev_check srv_check;
/** this lock protects:
* srv->connections_active (read with g_atomic_get)
/** this lock protects: (atomic access means here: normal read + atomic write with lock, atomic read without)
* srv->connections_active (atomic access)
* srv->connections
* wkr->connection_load (read with g_atomic_get)
* wrk->connection_load (atomic access)
*/
GStaticRecMutex lock_con;
guint connections_active; /** 0..con_act-1: active connections, con_act..used-1: free connections */
@ -74,10 +73,10 @@ struct server {
gpointer *option_def_values;
struct action *mainaction;
gboolean exiting;
gboolean exiting; /** atomic access */
/* logs */
gboolean rotate_logs;
gboolean rotate_logs; /** atomic access */
GHashTable *logs;
struct log_t *log_stderr;
struct log_t *log_syslog;
@ -86,7 +85,7 @@ struct server {
GMutex *log_mutex; /* manage access for the logs hashtable */
ev_tstamp started;
statistics_t stats;
statistics_t stats; /** TODO: sync/worker split */
/* keep alive timeout */
guint keep_alive_queue_timeout;

Loading…
Cancel
Save