thread.c 4.54 KB
Newer Older
Hardy Simpson's avatar
Hardy Simpson committed
1
/*
Hardy Simpson's avatar
Hardy Simpson committed
2
 * This file is part of the zlog Library.
Hardy Simpson's avatar
Hardy Simpson committed
3
 *
Hardy Simpson's avatar
Hardy Simpson committed
4
 * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
Hardy Simpson's avatar
Hardy Simpson committed
5
 *
6
 * Licensed under the LGPL v2.1, see the file COPYING in base directory.
Hardy Simpson's avatar
Hardy Simpson committed
7 8
 */

Hardy Simpson's avatar
Hardy Simpson committed
9 10 11 12 13 14 15 16 17
#include <pthread.h>
#include <errno.h>

#include "zc_defs.h"
#include "event.h"
#include "buf.h"
#include "thread.h"
#include "mdc.h"

18 19 20
void zlog_thread_profile(zlog_thread_t * a_thread, int flag)
{
	zc_assert(a_thread,);
21
	zc_profile(flag, "--thread[%p][%p][%p][%p,%p,%p,%p,%p]--",
22 23 24 25 26
			a_thread,
			a_thread->mdc,
			a_thread->event,
			a_thread->pre_path_buf,
			a_thread->path_buf,
27
			a_thread->archive_path_buf,
28 29 30 31 32 33 34
			a_thread->pre_msg_buf,
			a_thread->msg_buf);

	zlog_mdc_profile(a_thread->mdc, flag);
	zlog_event_profile(a_thread->event, flag);
	zlog_buf_profile(a_thread->pre_path_buf, flag);
	zlog_buf_profile(a_thread->path_buf, flag);
35
	zlog_buf_profile(a_thread->archive_path_buf, flag);
36 37 38 39
	zlog_buf_profile(a_thread->pre_msg_buf, flag);
	zlog_buf_profile(a_thread->msg_buf, flag);
	return;
}
Hardy Simpson's avatar
Hardy Simpson committed
40
/*******************************************************************************/
41
void zlog_thread_del(zlog_thread_t * a_thread)
Hardy Simpson's avatar
Hardy Simpson committed
42
{
43
	zc_assert(a_thread,);
Hardy Simpson's avatar
Hardy Simpson committed
44
	if (a_thread->mdc)
Hardy Simpson's avatar
Hardy Simpson committed
45
		zlog_mdc_del(a_thread->mdc);
Hardy Simpson's avatar
Hardy Simpson committed
46
	if (a_thread->event)
Hardy Simpson's avatar
Hardy Simpson committed
47
		zlog_event_del(a_thread->event);
Hardy Simpson's avatar
Hardy Simpson committed
48
	if (a_thread->pre_path_buf)
Hardy Simpson's avatar
Hardy Simpson committed
49
		zlog_buf_del(a_thread->pre_path_buf);
Hardy Simpson's avatar
Hardy Simpson committed
50
	if (a_thread->path_buf)
Hardy Simpson's avatar
Hardy Simpson committed
51
		zlog_buf_del(a_thread->path_buf);
52 53
	if (a_thread->archive_path_buf)
		zlog_buf_del(a_thread->archive_path_buf);
Hardy Simpson's avatar
Hardy Simpson committed
54
	if (a_thread->pre_msg_buf)
Hardy Simpson's avatar
Hardy Simpson committed
55
		zlog_buf_del(a_thread->pre_msg_buf);
Hardy Simpson's avatar
Hardy Simpson committed
56
	if (a_thread->msg_buf)
Hardy Simpson's avatar
Hardy Simpson committed
57
		zlog_buf_del(a_thread->msg_buf);
Hardy Simpson's avatar
Hardy Simpson committed
58 59

	free(a_thread);
60
	zc_debug("zlog_thread_del[%p]", a_thread);
Hardy Simpson's avatar
Hardy Simpson committed
61 62 63
	return;
}

64
zlog_thread_t *zlog_thread_new(int init_version, size_t buf_size_min, size_t buf_size_max, int time_cache_count)
Hardy Simpson's avatar
Hardy Simpson committed
65
{
Hardy Simpson's avatar
Hardy Simpson committed
66
	zlog_thread_t *a_thread;
Hardy Simpson's avatar
Hardy Simpson committed
67

Hardy Simpson's avatar
Hardy Simpson committed
68
	a_thread = calloc(1, sizeof(zlog_thread_t));
Hardy Simpson's avatar
Hardy Simpson committed
69 70 71 72 73
	if (!a_thread) {
		zc_error("calloc fail, errno[%d]", errno);
		return NULL;
	}

74 75
	a_thread->init_version = init_version;

Hardy Simpson's avatar
Hardy Simpson committed
76
	a_thread->mdc = zlog_mdc_new();
Hardy Simpson's avatar
Hardy Simpson committed
77
	if (!a_thread->mdc) {
Hardy Simpson's avatar
Hardy Simpson committed
78
		zc_error("zlog_mdc_new fail");
79
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
80 81
	}

82
	a_thread->event = zlog_event_new(time_cache_count);
Hardy Simpson's avatar
Hardy Simpson committed
83
	if (!a_thread->event) {
Hardy Simpson's avatar
Hardy Simpson committed
84
		zc_error("zlog_event_new fail");
85
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
86 87
	}

88
	a_thread->pre_path_buf = zlog_buf_new(MAXLEN_PATH + 1, MAXLEN_PATH + 1, NULL);
Hardy Simpson's avatar
Hardy Simpson committed
89
	if (!a_thread->pre_path_buf) {
Hardy Simpson's avatar
Hardy Simpson committed
90
		zc_error("zlog_buf_new fail");
91
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
92 93
	}

94
	a_thread->path_buf = zlog_buf_new(MAXLEN_PATH + 1, MAXLEN_PATH + 1, NULL);
Hardy Simpson's avatar
Hardy Simpson committed
95
	if (!a_thread->path_buf) {
Hardy Simpson's avatar
Hardy Simpson committed
96
		zc_error("zlog_buf_new fail");
97
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
98 99
	}

100 101 102 103 104 105
	a_thread->archive_path_buf = zlog_buf_new(MAXLEN_PATH + 1, MAXLEN_PATH + 1, NULL);
	if (!a_thread->archive_path_buf) {
		zc_error("zlog_buf_new fail");
		goto err;
	}

106
	a_thread->pre_msg_buf = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
Hardy Simpson's avatar
Hardy Simpson committed
107
	if (!a_thread->pre_msg_buf) {
Hardy Simpson's avatar
Hardy Simpson committed
108
		zc_error("zlog_buf_new fail");
109
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
110 111
	}

112
	a_thread->msg_buf = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
Hardy Simpson's avatar
Hardy Simpson committed
113
	if (!a_thread->msg_buf) {
Hardy Simpson's avatar
Hardy Simpson committed
114
		zc_error("zlog_buf_new fail");
115
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
116 117 118
	}


119
	//zlog_thread_profile(a_thread, ZC_DEBUG);
120 121 122 123
	return a_thread;
err:
	zlog_thread_del(a_thread);
	return NULL;
Hardy Simpson's avatar
Hardy Simpson committed
124 125
}

126
/*******************************************************************************/
127
int zlog_thread_rebuild_msg_buf(zlog_thread_t * a_thread, size_t buf_size_min, size_t buf_size_max)
Hardy Simpson's avatar
Hardy Simpson committed
128
{
129 130
	zlog_buf_t *pre_msg_buf_new = NULL;
	zlog_buf_t *msg_buf_new = NULL;
131 132
	zc_assert(a_thread, -1);

133 134
	if ( (a_thread->msg_buf->size_min == buf_size_min)
		&& (a_thread->msg_buf->size_max == buf_size_max)) {
135
		zc_debug("buf size not changed, no need rebuild");
136 137
		return 0;
	}
138

139 140
	pre_msg_buf_new = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
	if (!pre_msg_buf_new) {
Hardy Simpson's avatar
Hardy Simpson committed
141
		zc_error("zlog_buf_new fail");
142
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
143 144
	}

145 146
	msg_buf_new = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
	if (!msg_buf_new) {
Hardy Simpson's avatar
Hardy Simpson committed
147
		zc_error("zlog_buf_new fail");
148
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
149 150
	}

151 152 153 154 155 156
	zlog_buf_del(a_thread->pre_msg_buf);
	a_thread->pre_msg_buf = pre_msg_buf_new;

	zlog_buf_del(a_thread->msg_buf);
	a_thread->msg_buf = msg_buf_new;

157 158
	return 0;
err:
159 160
	if (pre_msg_buf_new) zlog_buf_del(pre_msg_buf_new);
	if (msg_buf_new) zlog_buf_del(msg_buf_new);
161
	return -1;
Hardy Simpson's avatar
Hardy Simpson committed
162 163
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
int zlog_thread_rebuild_event(zlog_thread_t * a_thread, int time_cache_count)
{
	zlog_event_t *event_new = NULL;
	zc_assert(a_thread, -1);

	event_new = zlog_event_new(time_cache_count);
	if (!event_new) {
		zc_error("zlog_event_new fail");
		goto err;
	}

	zlog_event_del(a_thread->event);
	a_thread->event = event_new;
	return 0;
err:
	if (event_new) zlog_event_del(event_new);
	return -1;
}


Hardy Simpson's avatar
Hardy Simpson committed
184
/*******************************************************************************/