summaryrefslogtreecommitdiffstats
path: root/server/periodic.c
blob: 839b406f64d0ead18fdc98f655b14542999ed559 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "conf.h"
#include "cdb.h"
#include "log.h"
#include "memdb.h"
#include "mysqldb.h"
#include "thread.h"
#include "periodic.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

RCSID("$Id: periodic.c,v 1.9 2000/05/17 21:52:34 shmit Exp $");

struct _per_thread_t {
	cond_t *cond;
	thread_t *tid;
};
typedef struct _per_thread_t per_thread_t;

static void *
periodic_looper(void *arg)
{
	per_thread_t *self;
	int count;

	self = (per_thread_t *)arg;
	pthread_detach(pthread_self());

	count = 0;
	for (;;) {
		struct timeval tv;
		struct timespec ts;

		gettimeofday(&tv, NULL);
		memset(&ts, 0, sizeof(ts));
		ts.tv_sec = tv.tv_sec + 1;
		(void)cond_timedwait(self->cond, &ts);

		count++;
		if (count == PERIODICITY) {
			cdb_periodic();
			count = 0;
		}

		cdb_collate();
		mysqldb_collate();
		memdb_collate();
	}
	return NULL;
}

void
periodic_delete(per_thread_t *thr)
{
	if (thr == NULL)
		return;

	if (thr->cond) {
		cond_destroy(thr->cond);
		free(thr->cond);
		thr->cond = NULL;
	}

	if (thr->tid) {
		free(thr->tid);
		thr->tid = NULL;
	}
	free(thr);
}

int
periodic_new()
{
	per_thread_t *per_thread;
	int rc;

	per_thread = malloc(sizeof(per_thread_t));
	if (per_thread == NULL) {
		log_err("Couldn't allocate periodic thread: %s.",
			strerror(errno));
		return -1;
	}
	per_thread->cond = NULL;
	per_thread->tid = NULL;

	per_thread->cond = malloc(sizeof(cond_t));
	if (per_thread->cond == NULL) {
		log_err("Couldn't allocate periodic condition: %s.",
			strerror(errno));
		periodic_delete(per_thread);
		return -1;
	}

	rc = cond_new(per_thread->cond);
	if (rc) {
		log_err("Couldn't initialise periodic condition: %s.",
			strerror(rc));
		periodic_delete(per_thread);
		return -1;
	}

	per_thread->tid = malloc(sizeof(thread_t));
	if (per_thread->tid == NULL) {
		log_err("Couldn't allocate periodic thread: %s.",
			strerror(rc));
		periodic_delete(per_thread);
		return -1;
	}

	rc = thread_new(per_thread->tid, periodic_looper, per_thread);
	if (rc) {
		log_err("Couldn't start periodic thread: %s.",
			strerror(rc));
		periodic_delete(per_thread);
		return -1;
	}

	return 0;
}