Commit 4165081e authored by Geoff Simmons's avatar Geoff Simmons

varnishevent: clean up threading and writer.c

parent e2eb137f
......@@ -41,7 +41,7 @@
static const char *statename[3] = { "EMPTY", "OPEN", "DONE" };
static pthread_mutex_t freelist_lock;
static pthread_mutex_t freelist_lock = PTHREAD_MUTEX_INITIALIZER;
static char *bufptr;
static int ntags;
......@@ -164,7 +164,6 @@ DATA_Init(void)
assert(bufidx == nrecords);
AZ(pthread_mutex_init(&freelist_lock, &attr_lock));
data_open = data_done = data_occ_hi = 0;
global_nfree = config.max_data;
......
......@@ -40,7 +40,7 @@
static int run;
static pthread_t monitor;
static pthread_mutex_t stats_lock;
static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
static void
log_output(void)
......@@ -103,7 +103,6 @@ MON_Shutdown(void)
void
MON_Start(void)
{
AZ(pthread_mutex_init(&stats_lock, &attr_lock));
AZ(pthread_create(&monitor, NULL, monitor_main, NULL));
}
......
......@@ -718,12 +718,6 @@ main(int argc, char *argv[])
if (!EMPTY(config.zformat))
z_flag = 1;
AZ(pthread_mutexattr_init(&attr_lock));
AZ(pthread_condattr_init(&attr_cond));
AZ(pthread_mutexattr_setpshared(&attr_lock, PTHREAD_PROCESS_PRIVATE));
AZ(pthread_condattr_setpshared(&attr_cond, PTHREAD_PROCESS_PRIVATE));
if ((errnum = DATA_Init()) != 0) {
LOG_Log(LOG_ALERT, "Cannot init data table: %s\n",
strerror(errnum));
......@@ -741,6 +735,11 @@ main(int argc, char *argv[])
fd_tbl[k].state = FD_EMPTY;
}
AZ(pthread_cond_init(&data_ready_cond, NULL));
AZ(pthread_mutex_init(&data_ready_lock, NULL));
AZ(pthread_cond_init(&spscq_ready_cond, NULL));
AZ(pthread_mutex_init(&spscq_ready_lock, NULL));
if (config.monitor_interval > 0)
MON_Start();
else
......@@ -788,6 +787,10 @@ main(int argc, char *argv[])
SPSCQ_Shutdown();
MON_Shutdown();
FMT_Shutdown();
AZ(pthread_cond_destroy(&data_ready_cond));
AZ(pthread_mutex_destroy(&data_ready_lock));
AZ(pthread_cond_destroy(&spscq_ready_cond));
AZ(pthread_mutex_destroy(&spscq_ready_lock));
LOG_Log0(LOG_INFO, "Exiting");
LOG_Close();
......
......@@ -123,10 +123,6 @@ pthread_mutex_t data_ready_lock;
pthread_cond_t spscq_ready_cond;
pthread_mutex_t spscq_ready_lock;
/* Used for all mutexes and condition variables to set them PROCESS_PRIVATE */
pthread_mutexattr_t attr_lock;
pthread_condattr_t attr_cond;
struct config {
char pid_file[BUFSIZ];
......
......@@ -73,7 +73,7 @@ static struct vsb *os;
static FILE *fo;
static char *obuf = NULL;
static pthread_mutex_t reopen_lock;
static pthread_mutex_t reopen_lock = PTHREAD_MUTEX_INITIALIZER;
/* stats */
static unsigned deqs = 0;
......@@ -90,7 +90,7 @@ typedef struct writer_data_s {
static writer_data_t wrt_data;
static unsigned run, cleaned = 0, reopen = 0;
static unsigned run, reopen = 0;
static int
open_log(void)
......@@ -246,23 +246,14 @@ static void
pthread_exit((void *) wrt);
}
static void wrt_cleanup(void)
{
if (cleaned) return;
cleaned = 1;
}
int
WRT_Init(void)
{
int err;
if ((err = open_log()) != 0)
return err;
AZ(pthread_mutex_init(&reopen_lock, &attr_lock));
wrt_data.magic = WRITER_DATA_MAGIC;
wrt_data.state = WRT_NOTSTARTED;
......@@ -317,7 +308,7 @@ WRT_Halt(void)
AZ(pthread_mutex_lock(&spscq_ready_lock));
run = 0;
AZ(pthread_cond_broadcast(&spscq_ready_cond));
AZ(pthread_cond_signal(&spscq_ready_cond));
AZ(pthread_mutex_unlock(&spscq_ready_lock));
AZ(pthread_join(writer, (void **) &wrt));
......@@ -329,6 +320,10 @@ WRT_Halt(void)
void
WRT_Shutdown(void)
{
/* XXX: error if run=1? */
wrt_cleanup();
/* WRT_Halt() must always be called first */
AZ(run);
fclose(fo);
free(obuf);
VSB_delete(os);
AZ(pthread_mutex_destroy(&reopen_lock));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment