summaryrefslogtreecommitdiff
path: root/accel-pptpd/triton/md.c
blob: 444aebf0d7be5924d3da75bfa2cddcf5c3bcbe5d (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
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include "triton_p.h"

#include "memdebug.h"

extern int max_events;

static int epoll_fd;
static struct epoll_event *epoll_events;

static pthread_t md_thr;
static void *md_thread(void *arg);

static mempool_t *md_pool;

int md_init(void)
{
	epoll_fd = epoll_create(1);
	if (epoll_fd < 0) {
		perror("md:epoll_create");
		return -1;
	}

	epoll_events = _malloc(max_events * sizeof(struct epoll_event));
	if (!epoll_events) {
		fprintf(stderr,"md:cann't allocate memory\n");
		return -1;
	}

	md_pool = mempool_create(sizeof(struct _triton_md_handler_t));

	return 0;
}
void md_run(void)
{
	if (pthread_create(&md_thr, NULL, md_thread, NULL)) {
		triton_log_error("md:pthread_create: %s", strerror(errno));
		_exit(-1);
	}
}

void md_terminate(void)
{
	pthread_cancel(md_thr);	
	pthread_join(md_thr, NULL);	
}

static void *md_thread(void *arg)
{
	int i,n,r;
	struct _triton_md_handler_t *h;
	sigset_t set;

	sigfillset(&set);
	sigdelset(&set, SIGKILL);
	sigdelset(&set, SIGSTOP);
	pthread_sigmask(SIG_BLOCK, &set, NULL);

	while(1) {
		n = epoll_wait(epoll_fd, epoll_events, max_events, -1);
		if (n < 0) {
			if (errno == EINTR)
				continue;
			triton_log_error("md:epoll_wait: %s\n", strerror(errno));
			_exit(-1);
		}
		
		for(i = 0; i < n; i++) {
			h = (struct _triton_md_handler_t *)epoll_events[i].data.ptr;
			spin_lock(&h->ctx->lock);
			if (h->ud) {
				h->trig_epoll_events |= epoll_events[i].events;
				if (!h->pending) {
					list_add_tail(&h->entry2, &h->ctx->pending_handlers);
					h->pending = 1;
					r = triton_queue_ctx(h->ctx);
				} else
					r = 0;
			} else
				r = 0;
			spin_unlock(&h->ctx->lock);
			if (r)
				triton_thread_wakeup(h->ctx->thread);
		}
	}

	return NULL;
}

void __export triton_md_register_handler(struct triton_context_t *ctx, struct triton_md_handler_t *ud)
{
	struct _triton_md_handler_t *h = mempool_alloc(md_pool);
	memset(h, 0, sizeof(*h));
	h->ud = ud;
	h->epoll_event.data.ptr = h;
	if (ctx)
		h->ctx = (struct _triton_context_t *)ctx->tpd;
	else
		h->ctx = (struct _triton_context_t *)default_ctx->tpd;
	ud->tpd = h;
	spin_lock(&h->ctx->lock);
	list_add_tail(&h->entry, &h->ctx->handlers);
	spin_unlock(&h->ctx->lock);

	__sync_fetch_and_add(&triton_stat.md_handler_count, 1);
}
void __export triton_md_unregister_handler(struct triton_md_handler_t *ud)
{
	struct _triton_md_handler_t *h = (struct _triton_md_handler_t *)ud->tpd;
	triton_md_disable_handler(ud, MD_MODE_READ | MD_MODE_WRITE);
	spin_lock(&h->ctx->lock);
	list_del(&h->entry);
	if (h->pending)
		list_del(&h->entry2);
	h->ud = NULL;
	spin_unlock(&h->ctx->lock);
	sched_yield();
	mempool_free(h);
	
	__sync_fetch_and_sub(&triton_stat.md_handler_count, 1);
}
int __export triton_md_enable_handler(struct triton_md_handler_t *ud, int mode)
{
	struct _triton_md_handler_t *h = (struct _triton_md_handler_t *)ud->tpd;
	int r;
	int events = h->epoll_event.events;

	if (mode & MD_MODE_READ)
		h->epoll_event.events |= EPOLLIN;
	if (mode & MD_MODE_WRITE)
		h->epoll_event.events |= EPOLLOUT;
	
	if (!h->trig_level)
		h->epoll_event.events |= EPOLLET;
	
	if (events)
		r = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, h->ud->fd, &h->epoll_event);
	else
		r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, h->ud->fd, &h->epoll_event);

	if (r) {
		triton_log_error("md:epoll_ctl: %s\n",strerror(errno));
		abort();
	}

	return r;
}
int __export triton_md_disable_handler(struct triton_md_handler_t *ud,int mode)
{
	struct _triton_md_handler_t *h = (struct _triton_md_handler_t *)ud->tpd;
	int r=0;

	if (!h->epoll_event.events)
		return -1;
	
	if (mode & MD_MODE_READ)
		h->epoll_event.events &= ~EPOLLIN;
	if (mode & MD_MODE_WRITE)
		h->epoll_event.events &= ~EPOLLOUT;

	if (h->epoll_event.events & (EPOLLIN | EPOLLOUT))
		r = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, h->ud->fd, &h->epoll_event);
	else {
		h->epoll_event.events = 0;
		r = epoll_ctl(epoll_fd, EPOLL_CTL_DEL, h->ud->fd, NULL);
	}

	if (r) {
		triton_log_error("md:epoll_ctl: %s\n",strerror(errno));
		abort();
	}

	return r;
}

void __export triton_md_set_trig(struct triton_md_handler_t *ud, int mode)
{
	struct _triton_md_handler_t *h = (struct _triton_md_handler_t *)ud->tpd;
	h->trig_level = mode;
}