-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmelang.c
354 lines (309 loc) · 9.23 KB
/
melang.c
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (C) Niklaus F.Schen.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include "mln_framework.h"
#include "mln_string.h"
#include "mln_lang.h"
#include "mln_lex.h"
#include "mln_log.h"
#include "mln_conf.h"
#include "mln_iothread.h"
#include "mln_rbtree.h"
#include "mln_tools.h"
typedef struct mln_fd_node_s {
pthread_t tid;
int signal_fd;
int unused_fd;
struct mln_fd_node_s *prev;
struct mln_fd_node_s *next;
} mln_fd_node_t;
struct mln_thread_args_s {
mln_event_t *ev;
mln_iothread_t *t;
};
static int mln_signal(mln_lang_t *lang);
static int mln_clear(mln_lang_t *lang);
static void mln_params_check(int argc, char *argv[]);
static void mln_run_all(int argc, char *argv[]);
static void mln_task_checker(mln_event_t *ev, void *data);
static mln_fd_node_t *mln_fd_node_new(void);
static void mln_fd_node_free(mln_fd_node_t *n);
static int mln_fd_node_cmp(const mln_fd_node_t *n1, const mln_fd_node_t *n2);
static void mln_iothread_msg_handler(mln_iothread_t *t, mln_iothread_ep_type_t from, mln_iothread_msg_t *msg);
MLN_CHAIN_FUNC_DECLARE(static inline, mln_fd_node, mln_fd_node_t,);
MLN_CHAIN_FUNC_DEFINE(static inline, mln_fd_node, mln_fd_node_t, prev, next);
static mln_rbtree_t *fd_tree = NULL;
static mln_fd_node_t *head = NULL;
static mln_fd_node_t *tail = NULL;
static pthread_mutex_t lock;
__thread mln_fd_node_t *t_node;
#if !defined(WIN32)
static mln_conf_item_t daemon_conf;
static int daemon_flag = 0;
#endif
int main(int argc, char *argv[])
{
struct mln_rbtree_attr rbattr;
#if defined(WIN32)
WSADATA wsaData = {0};
int rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (rc < 0) {
fprintf(stderr, "WSAStartup faiiled. %d\n", WSAGetLastError());
return -1;
}
#endif
mln_params_check(argc, argv);
mln_conf_load();
if (mln_log_init(NULL) < 0) {
fprintf(stderr, "init log failed.\n");
return -1;
}
#if !defined(WIN32)
if (daemon_flag) {
mln_conf_t *cf;
mln_conf_cmd_t *cc;
mln_conf_domain_t *cd;
if ((cf = mln_conf()) == NULL) {
mln_log(error, "Configuration error.\n");
return -1;
}
if ((cd = cf->search(cf, "main")) == NULL) {
mln_log(error, "Configuration error.\n");
return -1;
}
if ((cc = cd->search(cd, "daemon")) == NULL) {
if ((cc = cd->insert(cd, "daemon")) == NULL) {
mln_log(error, "insert configuration command 'daemon' failed.\n");
return -1;
}
}
daemon_conf.type = CONF_BOOL;
daemon_conf.val.b = 1;
if (cc->update(cc, &daemon_conf, 1) < 0) {
mln_log(error, "update configuration command 'daemon' failed.\n");
return -1;
}
if (mln_daemon() < 0) {
return -1;
}
}
#endif
rbattr.pool = NULL;
rbattr.pool_alloc = NULL;
rbattr.pool_free = NULL;
rbattr.cmp = (rbtree_cmp)mln_fd_node_cmp;
rbattr.data_free = (rbtree_free_data)mln_fd_node_free;
if ((fd_tree = mln_rbtree_new(&rbattr)) == NULL) {
fprintf(stderr, "Init fd tree failed.\n");
return -1;
}
if (pthread_mutex_init(&lock, NULL) != 0) {
fprintf(stderr, "Init fd tree mutex failed.\n");
return -1;
}
mln_run_all(argc, argv);
#if defined(WIN32)
WSACleanup();
#endif
return 0;
}
static void mln_params_check(int argc, char *argv[])
{
int i = 1, fd, nofile = 1;
for (; i < argc; ++i) {
if (!strcmp(argv[i], "-v")) {
printf("Melang 0.6.0.\n");
} else if (!strcmp(argv[i], "-h")) {
printf("Melang Usage:\n");
printf("\tmelang <script-file> ...\n");
printf("\t-v\t\t\tshow version\n");
printf("\t-t=number_of_threads\tspecify the number of threads. Default is 1\n");
#if !defined(WIN32)
printf("\t-d\t\t\trunning as a daemon process\n");
#endif
} else if (!strncmp(argv[i], "-t=", 3)) {
/* do nothing */
} else if (!strcmp(argv[i], "-d")) {
#if defined(WIN32)
fprintf(stderr, "-d is not supported on Windows\n");
#else
daemon_flag = 1;
#endif
} else {
if ((fd = open(argv[i], O_RDONLY)) < 0) {
fprintf(stderr, "File %s cannot be read, %s\n", argv[i], strerror(errno));
exit(1);
}
close(fd);
nofile = 0;
}
}
if (nofile) {
exit(0);
}
}
static void *mln_iothread_entry(void *args)
{
int rc;
struct mln_thread_args_s *a = (struct mln_thread_args_s *)args;
mln_rbtree_node_t *rn;
mln_fd_node_t *n = mln_fd_node_new();
if (n == NULL) {
mln_log(error, "No memory\n");
return NULL;
}
t_node = n;
pthread_mutex_lock(&lock);
mln_fd_node_chain_add(&head, &tail, n);
if ((rn = mln_rbtree_node_new(fd_tree, n)) == NULL) {
pthread_mutex_unlock(&lock);
mln_log(error, "No memory\n");
return NULL;
}
mln_rbtree_insert(fd_tree, rn);
pthread_mutex_unlock(&lock);
while (!(rc = mln_iothread_recv(a->t, user_thread)))
;
pthread_mutex_lock(&lock);
mln_fd_node_chain_del(&head, &tail, n);
pthread_mutex_unlock(&lock);
mln_event_dispatch(a->ev);
return NULL;
}
static void mln_iothread_msg_handler(mln_iothread_t *t, mln_iothread_ep_type_t from, mln_iothread_msg_t *msg)
{
mln_lang_t *lang = (mln_lang_t *)mln_iothread_msg_data(msg);
mln_lang_signal_get(lang)(lang);
}
static void mln_run_all(int argc, char *argv[])
{
int i, nth = 1;
mln_iothread_t t;
mln_string_t path;
mln_event_t *ev;
mln_lang_t *lang;
mln_rbtree_node_t *rn;
mln_fd_node_t *n;
struct mln_thread_args_s args;
if ((ev = mln_event_new()) == NULL) {
mln_log(error, "ev init failed.\n");
exit(1);
}
for (i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "-h")) {
continue;
} else if (!strncmp(argv[i], "-t=", 3)) {
nth = atoi(&argv[i][3]);
break;
}
}
args.ev = ev;
args.t = &t;
if (nth > 1) {
if (mln_iothread_init(&t, nth - 1, (mln_iothread_entry_t)mln_iothread_entry, &args, mln_iothread_msg_handler) < 0) {
mln_log(error, "iothread init failed.\n");
exit(1);
}
}
n = mln_fd_node_new();
if (n == NULL) {
mln_log(error, "No memory\n");
exit(1);
}
t_node = n;
pthread_mutex_lock(&lock);
if ((rn = mln_rbtree_node_new(fd_tree, n)) == NULL) {
pthread_mutex_unlock(&lock);
mln_log(error, "No memory\n");
exit(1);
}
mln_rbtree_insert(fd_tree, rn);
pthread_mutex_unlock(&lock);
if ((lang = mln_lang_new(ev, mln_signal, mln_clear)) == NULL) {
mln_log(error, "lang init failed.\n");
exit(1);
}
mln_lang_cache_set(lang);
for (i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "-h") || !strncmp(argv[i], "-t=", 3)) {
continue;
}
mln_string_nset(&path, argv[i], strlen(argv[i]));
mln_lang_job_new(lang, NULL, M_INPUT_T_FILE, &path, NULL, NULL);
}
if (mln_event_timer_set(ev, 1, lang, mln_task_checker) < 0) {
mln_log(error, "Set timer failed.\n");
exit(1);
}
if (nth > 1) {
while (1) {
pthread_mutex_lock(&lock);
if (head != NULL) {
pthread_mutex_unlock(&lock);
mln_iothread_send(&t, 0, lang, io_thread, 0);
} else {
pthread_mutex_unlock(&lock);
break;
}
}
}
mln_event_dispatch(ev);
}
static void mln_task_checker(mln_event_t *ev, void *data)
{
mln_lang_t *lang = (mln_lang_t *)data;
mln_lang_mutex_lock(lang);
if (mln_lang_task_empty(lang)) {
mln_lang_mutex_unlock(lang);
mln_event_break_set(ev);
return;
}
mln_lang_signal_get(lang)(lang);
mln_lang_mutex_unlock(lang);
mln_event_timer_set(ev, 1, lang, mln_task_checker);
}
static int mln_signal(mln_lang_t *lang)
{
return mln_event_fd_set(mln_lang_event_get(lang), t_node->signal_fd, M_EV_SEND|M_EV_ONESHOT, M_EV_UNLIMITED, lang, mln_lang_launcher_get(lang));
}
static int mln_clear(mln_lang_t *lang)
{
return mln_event_fd_set(mln_lang_event_get(lang), t_node->signal_fd, M_EV_CLR, M_EV_UNLIMITED, NULL, NULL);
}
/*
* mln_fd_node_t
*/
static mln_fd_node_t *mln_fd_node_new(void)
{
int fds[2];
mln_fd_node_t *n = (mln_fd_node_t *)malloc(sizeof(mln_fd_node_t));
if (n == NULL) return NULL;
n->tid = pthread_self();
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
free(n);
return NULL;
}
n->signal_fd = fds[0];
n->unused_fd = fds[1];
n->prev = n->next = NULL;
return n;
}
static void mln_fd_node_free(mln_fd_node_t *n)
{
if (n == NULL) return;
mln_socket_close(n->signal_fd);
mln_socket_close(n->unused_fd);
free(n);
}
static int mln_fd_node_cmp(const mln_fd_node_t *n1, const mln_fd_node_t *n2)
{
if (n1->tid > n2->tid) return 1;
if (n1->tid < n2->tid) return -1;
return 0;
}