2
0
Fork 0

made log refcounting, srv->exiting and srv->rotate_logs atomic operations; moved server and connection structs from base.h into server.h and connection.h respectively

personal/stbuehler/wip
Thomas Porzelt 2008-07-20 18:28:58 +02:00
parent e80f384fac
commit 5045a5ee04
6 changed files with 58 additions and 49 deletions

View File

@ -11,52 +11,15 @@ typedef struct server server;
struct connection;
typedef struct connection connection;
#include "server.h"
#include "plugin.h"
#include "actions.h"
#include "request.h"
#include "log.h"
#include "connection.h"
#define SERVER_VERSION ((guint) 0x01FF0000)
struct server {
guint version;
GHashTable *plugins;
size_t option_count;
GHashTable *options;
gpointer *option_def_values;
gboolean exiting;
GMutex *mutex;
/* logs */
gboolean rotate_logs;
GHashTable *logs;
struct log_t *log_stderr;
struct log_t *log_syslog;
GAsyncQueue *log_queue;
GThread *log_thread;
GMutex *log_mutex;
};
struct connection {
sock_addr dst_addr, src_addr;
GString *dst_addr_str, *src_addr_str;
action_stack action_stack;
request request;
physical physical;
GMutex *mutex;
struct log_t *log;
gint log_level;
};
server* server_new();
void server_free(server* srv);
#endif

16
src/connection.h Normal file
View File

@ -0,0 +1,16 @@
struct connection {
sock_addr dst_addr, src_addr;
GString *dst_addr_str, *src_addr_str;
action_stack action_stack;
request request;
physical physical;
GMutex *mutex;
struct log_t *log;
gint log_level;
};

View File

@ -69,7 +69,7 @@ int main(int argc, char *argv[]) {
TRACE(srv, "%s", "Test!");
srv->log_stderr = log_new(srv, LOG_TYPE_FILE, g_string_new("lightytest.log"));
//srv->log_stderr = log_new(srv, LOG_TYPE_FILE, g_string_new("lightytest.log"));
log_write_(srv, NULL, LOG_LEVEL_WARNING, "test %s", "foo1");
log_write_(srv, NULL, LOG_LEVEL_WARNING, "test %s", "foo1");
log_write_(srv, NULL, LOG_LEVEL_WARNING, "test %s", "foo2");
@ -77,9 +77,11 @@ int main(int argc, char *argv[]) {
log_thread_start(srv);
sleep(3);
log_error(srv, NULL, "error %d", 23);
g_atomic_int_set(&srv->rotate_logs, TRUE);
log_write_(srv, NULL, LOG_LEVEL_WARNING, "test %s", "foo3");
log_write_(srv, NULL, LOG_LEVEL_WARNING, "test %s", "foo4");
srv->exiting = TRUE;
g_atomic_int_set(&srv->exiting, TRUE);
log_thread_wakeup(srv);
g_thread_join(srv->log_thread);

View File

@ -99,11 +99,11 @@ gpointer log_thread(server *srv) {
while (TRUE) {
/* lighty is exiting, end logging thread */
if (srv->exiting && g_async_queue_length(srv->log_queue) == 0)
if (g_atomic_int_get(&srv->exiting) && g_async_queue_length(srv->log_queue) == 0)
break;
if (srv->rotate_logs) {
srv->rotate_logs = FALSE;
if (g_atomic_int_get(&srv->rotate_logs)) {
g_atomic_int_set(&srv->rotate_logs, FALSE);
g_mutex_lock(srv->log_mutex);
g_hash_table_foreach(srv->logs, (GHFunc) log_rotate, srv);
g_mutex_unlock(srv->log_mutex);
@ -174,11 +174,11 @@ void log_rotate(gchar * path, log_t *log, server * UNUSED_PARAM(srv)) {
void log_ref(log_t *log) {
log->refcount++;
g_atomic_int_inc(&log->refcount);
}
void log_unref(server *srv, log_t *log) {
if (--log->refcount == 0)
if (g_atomic_int_dec_and_test(&log->refcount))
log_free(srv, log);
}

View File

@ -90,7 +90,7 @@ typedef enum {
struct log_t {
log_type_t type;
GString *path;
guint refcount;
gint refcount;
gint fd;
GString *lastmsg;
guint lastmsg_count;

28
src/server.h Normal file
View File

@ -0,0 +1,28 @@
struct server {
guint version;
GHashTable *plugins;
size_t option_count;
GHashTable *options;
gpointer *option_def_values;
gboolean exiting;
GMutex *mutex;
/* logs */
gboolean rotate_logs;
GHashTable *logs;
struct log_t *log_stderr;
struct log_t *log_syslog;
GAsyncQueue *log_queue;
GThread *log_thread;
GMutex *log_mutex;
};
server* server_new();
void server_free(server* srv);