-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
skynet_start.c
194 lines (172 loc) · 3.72 KB
/
skynet_start.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
#include "skynet.h"
#include "skynet_server.h"
#include "skynet_imp.h"
#include "skynet_mq.h"
#include "skynet_handle.h"
#include "skynet_module.h"
#include "skynet_timer.h"
#include "skynet_harbor.h"
#include "skynet_group.h"
#include "skynet_monitor.h"
#include "skynet_socket.h"
#include <pthread.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct monitor {
int count;
struct skynet_monitor ** m;
pthread_cond_t cond;
pthread_mutex_t mutex;
int sleep;
};
struct worker_parm {
struct monitor *m;
int id;
};
#define CHECK_ABORT if (skynet_context_total()==0) break;
static void
wakeup(struct monitor *m, int busy) {
if (m->sleep >= m->count - busy) {
pthread_mutex_lock(&m->mutex);
pthread_cond_signal(&m->cond);
pthread_mutex_unlock(&m->mutex);
}
}
static void *
_socket(void *p) {
struct monitor * m = p;
for (;;) {
int r = skynet_socket_poll();
if (r==0)
break;
if (r<0)
continue;
// FIXME: wakeup will kill some performance when lots of connections
wakeup(m,0);
}
return NULL;
}
static void *
_monitor(void *p) {
struct monitor * m = p;
int i;
int n = m->count;
for (;;) {
for (i=0;i<n;i++) {
skynet_monitor_check(m->m[i]);
}
CHECK_ABORT
sleep(5);
}
for (i=0;i<n;i++) {
skynet_monitor_delete(m->m[i]);
}
free(m->m);
free(m);
return NULL;
}
static void *
_timer(void *p) {
struct monitor * m = p;
for (;;) {
skynet_updatetime();
CHECK_ABORT
wakeup(m,1);
usleep(2500);
}
return NULL;
}
static void *
_worker(void *p) {
struct worker_parm *wp = p;
int id = wp->id;
struct monitor *m = wp->m;
struct skynet_monitor *sm = m->m[id];
for (;;) {
if (skynet_context_message_dispatch(sm)) {
CHECK_ABORT
pthread_mutex_lock(&m->mutex);
++ m->sleep;
pthread_cond_wait(&m->cond, &m->mutex);
-- m->sleep;
pthread_mutex_unlock(&m->mutex);
}
}
return NULL;
}
static void
_start(int thread) {
pthread_t pid[thread+3];
struct monitor *m = malloc(sizeof(*m));
memset(m, 0, sizeof(*m));
m->count = thread;
m->sleep = 0;
m->m = malloc(thread * sizeof(struct skynet_monitor *));
int i;
for (i=0;i<thread;i++) {
m->m[i] = skynet_monitor_new(i);
}
pthread_mutex_init(&m->mutex, NULL);
pthread_cond_init(&m->cond, NULL);
pthread_create(&pid[0], NULL, _monitor, m);
pthread_create(&pid[1], NULL, _timer, m);
pthread_create(&pid[2], NULL, _socket, m);
struct worker_parm wp[thread];
for (i=0;i<thread;i++) {
wp[i].m = m;
wp[i].id = i;
pthread_create(&pid[i+3], NULL, _worker, &wp[i]);
}
for (i=1;i<thread+3;i++) {
pthread_join(pid[i], NULL);
}
}
static int
_start_master(const char * master) {
struct skynet_context *ctx = skynet_context_new("master", master);
if (ctx == NULL)
return 1;
return 0;
}
void
skynet_start(struct skynet_config * config) {
skynet_group_init();
skynet_harbor_init(config->harbor);
skynet_handle_init(config->harbor);
skynet_mq_init();
skynet_module_init(config->module_path);
skynet_timer_init();
skynet_socket_init();
if (config->standalone) {
if (_start_master(config->standalone)) {
fprintf(stderr, "Init fail : mater");
return;
}
}
// harbor must be init first
if (skynet_harbor_start(config->master , config->local)) {
fprintf(stderr, "Init fail : no master");
return;
}
struct skynet_context *ctx;
ctx = skynet_context_new("logger", config->logger);
if (ctx == NULL) {
fprintf(stderr,"launch logger error");
exit(1);
}
ctx = skynet_context_new("localcast", NULL);
if (ctx == NULL) {
fprintf(stderr,"launch local cast error");
exit(1);
}
ctx = skynet_context_new("snlua", "launcher");
if (ctx) {
skynet_command(ctx, "REG", ".launcher");
ctx = skynet_context_new("snlua", config->start);
}
_start(config->thread);
skynet_socket_free();
}