summaryrefslogtreecommitdiff
path: root/accel-pptpd/triton/triton.c
blob: 9aa7462b7aa7a04249c7d0b106cac9f1a331f764 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "triton_p.h"

int thread_count = 4;
int max_events = 64;

static spinlock_t threads_lock = SPINLOCK_INITIALIZER;
static LIST_HEAD(threads);
static LIST_HEAD(sleep_threads);

static LIST_HEAD(ctx_queue);

static spinlock_t ctx_list_lock = SPINLOCK_INITIALIZER;
static LIST_HEAD(ctx_list);

struct triton_ctx_t *default_ctx;
static int terminate;

static mempool_t *ctx_pool;

void triton_thread_wakeup(struct _triton_thread_t *thread)
{
	pthread_kill(thread->thread, SIGUSR1);
}

static void* triton_thread(struct _triton_thread_t *thread)
{
	struct _triton_md_handler_t *h;
	struct _triton_timer_t *t;
	sigset_t set;
	int sig;
	uint64_t tt;

	sigemptyset(&set);
	sigaddset(&set, SIGUSR1);
	sigaddset(&set, SIGQUIT);

	while(1){
		sigwait(&set, &sig);

cont:
		if (thread->ctx->need_close) {
			if (thread->ctx->ud->close)
				thread->ctx->ud->close(thread->ctx->ud);
			thread->ctx->need_close = 0;
		}

		while (1) {
			spin_lock(&thread->ctx->lock);
			if (!list_empty(&thread->ctx->pending_timers)) {
				t = list_entry(thread->ctx->pending_timers.next, typeof(*t), entry2);
				list_del(&t->entry2);
				t->pending = 0;
				spin_unlock(&thread->ctx->lock);
				read(t->fd, &tt, sizeof(tt));
				t->ud->expire(t->ud);
			}
			if (!list_empty(&thread->ctx->pending_handlers)) {
				h = list_entry(thread->ctx->pending_handlers.next, typeof(*h), entry2);
				list_del(&h->entry2);
				h->pending = 0;
				spin_unlock(&thread->ctx->lock);

				if (h->trig_epoll_events & (EPOLLIN | EPOLLERR | EPOLLHUP))
					if (h->ud->read)
						if (h->ud->read(h->ud))
							continue;
				if (h->trig_epoll_events & (EPOLLOUT | EPOLLERR | EPOLLHUP))
					if (h->ud->write)
						if (h->ud->write(h->ud))
							continue;
				h->trig_epoll_events = 0;
				continue;
			}
			thread->ctx->thread = NULL;
			spin_unlock(&thread->ctx->lock);
			if (thread->ctx->need_free)
				mempool_free(thread->ctx);
			thread->ctx = NULL;
			break;
		}
	
		spin_lock(&threads_lock);
		if (!list_empty(&ctx_queue)) {
			thread->ctx = list_entry(ctx_queue.next, typeof(*thread->ctx), entry2);
			list_del(&thread->ctx->entry2);
			spin_unlock(&threads_lock);
			spin_lock(&thread->ctx->lock);
			thread->ctx->thread = thread;
			thread->ctx->queued = 0;
			spin_unlock(&thread->ctx->lock);
			goto cont;
		} else {
			if (!terminate)
				list_add(&thread->entry2, &sleep_threads);
			spin_unlock(&threads_lock);
			if (terminate)
				return NULL;
		}
	}
}

struct _triton_thread_t *create_thread()
{
	struct _triton_thread_t *thread = malloc(sizeof(*thread));
	if (!thread)
		return NULL;

	memset(thread, 0, sizeof(*thread));
	if (pthread_create(&thread->thread, NULL, (void*(*)(void*))triton_thread, thread)) {
		triton_log_error("pthread_create: %s", strerror(errno));
		return NULL;
	}

	return thread;
}

int triton_queue_ctx(struct _triton_ctx_t *ctx)
{
	if (ctx->thread || ctx->queued)
		return 0;

	spin_lock(&threads_lock);
	if (list_empty(&sleep_threads)) {
		list_add_tail(&ctx->entry2, &ctx_queue);
		spin_unlock(&threads_lock);
		ctx->queued = 1;
		return 0;
	}

	ctx->thread = list_entry(sleep_threads.next, typeof(*ctx->thread), entry2);
	ctx->thread->ctx = ctx;
	list_del(&ctx->thread->entry2);
	spin_unlock(&threads_lock);

	return 1;
}

void __export triton_register_ctx(struct triton_ctx_t *ud)
{
	struct _triton_ctx_t *ctx = mempool_alloc(ctx_pool);

	memset(ctx, 0, sizeof(*ctx));
	ctx->ud = ud;
	spinlock_init(&ctx->lock);
	INIT_LIST_HEAD(&ctx->handlers);
	INIT_LIST_HEAD(&ctx->timers);
	INIT_LIST_HEAD(&ctx->pending_handlers);
	INIT_LIST_HEAD(&ctx->pending_timers);

	ud->tpd = ctx;

	spin_lock(&ctx_list_lock);
	list_add_tail(&ctx->entry, &ctx_list);
	spin_unlock(&ctx_list_lock);
}

void __export triton_unregister_ctx(struct triton_ctx_t *ud)
{
	struct _triton_ctx_t *ctx = (struct _triton_ctx_t *)ud->tpd;

	if (!list_empty(&ctx->handlers)) {
		triton_log_error("BUG:ctx:triton_unregister_ctx: handlers is not empty");
		abort();
	}
	if (!list_empty(&ctx->pending_handlers)) {
		triton_log_error("BUG:ctx:triton_unregister_ctx: pending_handlers is not empty");
		abort();
	}
	if (!list_empty(&ctx->timers)) {
		triton_log_error("BUG:ctx:triton_unregister_ctx: timers is not empty");
		abort();
	}
	if (!list_empty(&ctx->pending_timers)) {
		triton_log_error("BUG:ctx:triton_unregister_ctx: pending_timers is not empty");
		abort();
	}

	ctx->need_free = 1;
	spin_lock(&ctx_list_lock);
	list_del(&ctx->entry);
	spin_unlock(&ctx_list_lock);
}

int __export triton_init(const char *conf_file, const char *mod_sect)
{
	ctx_pool = mempool_create(sizeof(struct _triton_ctx_t));

	default_ctx = malloc(sizeof(*default_ctx));
	if (!default_ctx) {
		fprintf(stderr,"cann't allocate memory\n");
		return -1;
	}
	triton_register_ctx(default_ctx);	

	if (conf_load(conf_file))
		return -1;

	if (log_init())
		return -1;

	if (md_init())
		return -1;

	if (timer_init())
		return -1;

	if (load_modules(mod_sect))
		return -1;

	return 0;
}

void __export triton_run()
{
	struct _triton_thread_t *t;
	int i;

	for(i = 0; i < thread_count; i++) {
		t = create_thread();
		if (!t)
			_exit(-1);

		list_add_tail(&t->entry, &threads);
		list_add_tail(&t->entry2, &sleep_threads);
	}

	md_run();
	timer_run();
}

void __export triton_terminate()
{
	struct _triton_ctx_t *ctx;
	struct _triton_thread_t *t;
	
	md_terminate();
	timer_terminate();
	
	spin_lock(&ctx_list_lock);
	list_for_each_entry(ctx, &ctx_list, entry) {
		spin_lock(&ctx->lock);
		ctx->need_close = 1;
		triton_queue_ctx(ctx);
		spin_unlock(&ctx->lock);
	}
	spin_unlock(&ctx_list_lock);

	spin_lock(&threads_lock);
	terminate = 1;
	spin_unlock(&threads_lock);
	
	list_for_each_entry(t, &threads, entry)
		triton_thread_wakeup(t);
	
	list_for_each_entry(t, &threads, entry)
		pthread_join(t->thread, NULL);
}