mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
226 lines
4.3 KiB
C
226 lines
4.3 KiB
C
#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_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
|
|
create_thread(pthread_t *thread, void *(*start_routine) (void *), void *arg) {
|
|
if (pthread_create(thread,NULL, start_routine, arg)) {
|
|
fprintf(stderr, "Create thread failed");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
static void
|
|
wakeup(struct monitor *m, int busy) {
|
|
if (m->sleep >= m->count - busy) {
|
|
// signal sleep worker, "spurious wakeup" is harmless
|
|
pthread_cond_signal(&m->cond);
|
|
}
|
|
}
|
|
|
|
static void *
|
|
_socket(void *p) {
|
|
struct monitor * m = p;
|
|
for (;;) {
|
|
int r = skynet_socket_poll();
|
|
if (r==0)
|
|
break;
|
|
if (r<0) {
|
|
CHECK_ABORT
|
|
continue;
|
|
}
|
|
wakeup(m,0);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static void
|
|
free_monitor(struct monitor *m) {
|
|
int i;
|
|
int n = m->count;
|
|
for (i=0;i<n;i++) {
|
|
skynet_monitor_delete(m->m[i]);
|
|
}
|
|
pthread_mutex_destroy(&m->mutex);
|
|
pthread_cond_destroy(&m->cond);
|
|
skynet_free(m->m);
|
|
skynet_free(m);
|
|
}
|
|
|
|
static void *
|
|
_monitor(void *p) {
|
|
struct monitor * m = p;
|
|
int i;
|
|
int n = m->count;
|
|
for (;;) {
|
|
CHECK_ABORT
|
|
for (i=0;i<n;i++) {
|
|
skynet_monitor_check(m->m[i]);
|
|
}
|
|
for (i=0;i<5;i++) {
|
|
CHECK_ABORT
|
|
sleep(1);
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static void *
|
|
_timer(void *p) {
|
|
struct monitor * m = p;
|
|
for (;;) {
|
|
skynet_updatetime();
|
|
CHECK_ABORT
|
|
wakeup(m,m->count-1);
|
|
usleep(2500);
|
|
}
|
|
// wakeup socket thread
|
|
skynet_socket_exit();
|
|
// wakeup all worker thread
|
|
pthread_cond_broadcast(&m->cond);
|
|
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
|
|
if (pthread_mutex_lock(&m->mutex) == 0) {
|
|
++ m->sleep;
|
|
// "spurious wakeup" is harmless,
|
|
// because skynet_context_message_dispatch() can be call at any time.
|
|
pthread_cond_wait(&m->cond, &m->mutex);
|
|
-- m->sleep;
|
|
if (pthread_mutex_unlock(&m->mutex)) {
|
|
fprintf(stderr, "unlock mutex error");
|
|
exit(1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static void
|
|
_start(int thread) {
|
|
pthread_t pid[thread+3];
|
|
|
|
struct monitor *m = skynet_malloc(sizeof(*m));
|
|
memset(m, 0, sizeof(*m));
|
|
m->count = thread;
|
|
m->sleep = 0;
|
|
|
|
m->m = skynet_malloc(thread * sizeof(struct skynet_monitor *));
|
|
int i;
|
|
for (i=0;i<thread;i++) {
|
|
m->m[i] = skynet_monitor_new();
|
|
}
|
|
if (pthread_mutex_init(&m->mutex, NULL)) {
|
|
fprintf(stderr, "Init mutex error");
|
|
exit(1);
|
|
}
|
|
if (pthread_cond_init(&m->cond, NULL)) {
|
|
fprintf(stderr, "Init cond error");
|
|
exit(1);
|
|
}
|
|
|
|
create_thread(&pid[0], _monitor, m);
|
|
create_thread(&pid[1], _timer, m);
|
|
create_thread(&pid[2], _socket, m);
|
|
|
|
struct worker_parm wp[thread];
|
|
for (i=0;i<thread;i++) {
|
|
wp[i].m = m;
|
|
wp[i].id = i;
|
|
create_thread(&pid[i+3], _worker, &wp[i]);
|
|
}
|
|
|
|
for (i=0;i<thread+3;i++) {
|
|
pthread_join(pid[i], NULL);
|
|
}
|
|
|
|
free_monitor(m);
|
|
}
|
|
|
|
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_harbor_init(config->harbor);
|
|
skynet_handle_init(config->harbor);
|
|
skynet_mq_init();
|
|
skynet_module_init(config->module_path);
|
|
skynet_timer_init();
|
|
skynet_socket_init();
|
|
|
|
struct skynet_context *ctx;
|
|
ctx = skynet_context_new("logger", config->logger);
|
|
if (ctx == NULL) {
|
|
fprintf(stderr,"launch logger error");
|
|
exit(1);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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();
|
|
}
|
|
|