skynet_start.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include "skynet.h"
  2. #include "skynet_server.h"
  3. #include "skynet_imp.h"
  4. #include "skynet_mq.h"
  5. #include "skynet_handle.h"
  6. #include "skynet_module.h"
  7. #include "skynet_timer.h"
  8. #include "skynet_monitor.h"
  9. #include "skynet_socket.h"
  10. #include "skynet_daemon.h"
  11. #include "skynet_harbor.h"
  12. #include <pthread.h>
  13. #include <unistd.h>
  14. #include <assert.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <signal.h>
  19. struct monitor {
  20. int count;
  21. struct skynet_monitor ** m;
  22. pthread_cond_t cond;
  23. pthread_mutex_t mutex;
  24. int sleep;
  25. int quit;
  26. };
  27. struct worker_parm {
  28. struct monitor *m;
  29. int id;
  30. int weight;
  31. };
  32. static volatile int SIG = 0;
  33. static void
  34. handle_hup(int signal) {
  35. if (signal == SIGHUP) {
  36. SIG = 1;
  37. }
  38. }
  39. #define CHECK_ABORT if (skynet_context_total()==0) break;
  40. static void
  41. create_thread(pthread_t *thread, void *(*start_routine) (void *), void *arg) {
  42. if (pthread_create(thread,NULL, start_routine, arg)) {
  43. fprintf(stderr, "Create thread failed");
  44. exit(1);
  45. }
  46. }
  47. static void
  48. wakeup(struct monitor *m, int busy) {
  49. if (m->sleep >= m->count - busy) {
  50. // signal sleep worker, "spurious wakeup" is harmless
  51. pthread_cond_signal(&m->cond);
  52. }
  53. }
  54. static void *
  55. thread_socket(void *p) {
  56. struct monitor * m = p;
  57. skynet_initthread(THREAD_SOCKET);
  58. for (;;) {
  59. int r = skynet_socket_poll();
  60. if (r==0)
  61. break;
  62. if (r<0) {
  63. CHECK_ABORT
  64. continue;
  65. }
  66. wakeup(m,0);
  67. }
  68. return NULL;
  69. }
  70. static void
  71. free_monitor(struct monitor *m) {
  72. int i;
  73. int n = m->count;
  74. for (i=0;i<n;i++) {
  75. skynet_monitor_delete(m->m[i]);
  76. }
  77. pthread_mutex_destroy(&m->mutex);
  78. pthread_cond_destroy(&m->cond);
  79. skynet_free(m->m);
  80. skynet_free(m);
  81. }
  82. static void *
  83. thread_monitor(void *p) {
  84. struct monitor * m = p;
  85. int i;
  86. int n = m->count;
  87. skynet_initthread(THREAD_MONITOR);
  88. for (;;) {
  89. CHECK_ABORT
  90. for (i=0;i<n;i++) {
  91. skynet_monitor_check(m->m[i]);
  92. }
  93. for (i=0;i<5;i++) {
  94. CHECK_ABORT
  95. sleep(1);
  96. }
  97. }
  98. return NULL;
  99. }
  100. static void
  101. signal_hup() {
  102. // make log file reopen
  103. struct skynet_message smsg;
  104. smsg.source = 0;
  105. smsg.session = 0;
  106. smsg.data = NULL;
  107. smsg.sz = (size_t)PTYPE_SYSTEM << MESSAGE_TYPE_SHIFT;
  108. uint32_t logger = skynet_handle_findname("logger");
  109. if (logger) {
  110. skynet_context_push(logger, &smsg);
  111. }
  112. }
  113. static void *
  114. thread_timer(void *p) {
  115. struct monitor * m = p;
  116. skynet_initthread(THREAD_TIMER);
  117. for (;;) {
  118. skynet_updatetime();
  119. skynet_socket_updatetime();
  120. CHECK_ABORT
  121. wakeup(m,m->count-1);
  122. usleep(2500);
  123. if (SIG) {
  124. signal_hup();
  125. SIG = 0;
  126. }
  127. }
  128. // wakeup socket thread
  129. skynet_socket_exit();
  130. // wakeup all worker thread
  131. pthread_mutex_lock(&m->mutex);
  132. m->quit = 1;
  133. pthread_cond_broadcast(&m->cond);
  134. pthread_mutex_unlock(&m->mutex);
  135. return NULL;
  136. }
  137. static void *
  138. thread_worker(void *p) {
  139. struct worker_parm *wp = p;
  140. int id = wp->id;
  141. int weight = wp->weight;
  142. struct monitor *m = wp->m;
  143. struct skynet_monitor *sm = m->m[id];
  144. skynet_initthread(THREAD_WORKER);
  145. struct message_queue * q = NULL;
  146. while (!m->quit) {
  147. q = skynet_context_message_dispatch(sm, q, weight);
  148. if (q == NULL) {
  149. if (pthread_mutex_lock(&m->mutex) == 0) {
  150. ++ m->sleep;
  151. // "spurious wakeup" is harmless,
  152. // because skynet_context_message_dispatch() can be call at any time.
  153. if (!m->quit)
  154. pthread_cond_wait(&m->cond, &m->mutex);
  155. -- m->sleep;
  156. if (pthread_mutex_unlock(&m->mutex)) {
  157. fprintf(stderr, "unlock mutex error");
  158. exit(1);
  159. }
  160. }
  161. }
  162. }
  163. return NULL;
  164. }
  165. static void
  166. start(int thread) {
  167. pthread_t pid[thread+3];
  168. struct monitor *m = skynet_malloc(sizeof(*m));
  169. memset(m, 0, sizeof(*m));
  170. m->count = thread;
  171. m->sleep = 0;
  172. m->m = skynet_malloc(thread * sizeof(struct skynet_monitor *));
  173. int i;
  174. for (i=0;i<thread;i++) {
  175. m->m[i] = skynet_monitor_new();
  176. }
  177. if (pthread_mutex_init(&m->mutex, NULL)) {
  178. fprintf(stderr, "Init mutex error");
  179. exit(1);
  180. }
  181. if (pthread_cond_init(&m->cond, NULL)) {
  182. fprintf(stderr, "Init cond error");
  183. exit(1);
  184. }
  185. create_thread(&pid[0], thread_monitor, m);
  186. create_thread(&pid[1], thread_timer, m);
  187. create_thread(&pid[2], thread_socket, m);
  188. static int weight[] = {
  189. -1, -1, -1, -1, 0, 0, 0, 0,
  190. 1, 1, 1, 1, 1, 1, 1, 1,
  191. 2, 2, 2, 2, 2, 2, 2, 2,
  192. 3, 3, 3, 3, 3, 3, 3, 3, };
  193. struct worker_parm wp[thread];
  194. for (i=0;i<thread;i++) {
  195. wp[i].m = m;
  196. wp[i].id = i;
  197. if (i < sizeof(weight)/sizeof(weight[0])) {
  198. wp[i].weight= weight[i];
  199. } else {
  200. wp[i].weight = 0;
  201. }
  202. create_thread(&pid[i+3], thread_worker, &wp[i]);
  203. }
  204. for (i=0;i<thread+3;i++) {
  205. pthread_join(pid[i], NULL);
  206. }
  207. free_monitor(m);
  208. }
  209. static void
  210. bootstrap(struct skynet_context * logger, const char * cmdline) {
  211. int sz = strlen(cmdline);
  212. char name[sz+1];
  213. char args[sz+1];
  214. int arg_pos;
  215. sscanf(cmdline, "%s", name);
  216. arg_pos = strlen(name);
  217. if (arg_pos < sz) {
  218. while(cmdline[arg_pos] == ' ') {
  219. arg_pos++;
  220. }
  221. strncpy(args, cmdline + arg_pos, sz);
  222. } else {
  223. args[0] = '\0';
  224. }
  225. struct skynet_context *ctx = skynet_context_new(name, args);
  226. if (ctx == NULL) {
  227. skynet_error(NULL, "Bootstrap error : %s\n", cmdline);
  228. skynet_context_dispatchall(logger);
  229. exit(1);
  230. }
  231. }
  232. void
  233. skynet_start(struct skynet_config * config) {
  234. // register SIGHUP for log file reopen
  235. struct sigaction sa;
  236. sa.sa_handler = &handle_hup;
  237. sa.sa_flags = SA_RESTART;
  238. sigfillset(&sa.sa_mask);
  239. sigaction(SIGHUP, &sa, NULL);
  240. if (config->daemon) {
  241. if (daemon_init(config->daemon)) {
  242. exit(1);
  243. }
  244. }
  245. skynet_harbor_init(config->harbor);
  246. skynet_handle_init(config->harbor);
  247. skynet_mq_init();
  248. skynet_module_init(config->module_path);
  249. skynet_timer_init();
  250. skynet_socket_init();
  251. skynet_profile_enable(config->profile);
  252. struct skynet_context *ctx = skynet_context_new(config->logservice, config->logger);
  253. if (ctx == NULL) {
  254. fprintf(stderr, "Can't launch %s service\n", config->logservice);
  255. exit(1);
  256. }
  257. skynet_handle_namehandle(skynet_context_handle(ctx), "logger");
  258. bootstrap(ctx, config->bootstrap);
  259. start(config->thread);
  260. // harbor_exit may call socket send, so it should exit before socket_free
  261. skynet_harbor_exit();
  262. skynet_socket_free();
  263. if (config->daemon) {
  264. daemon_exit(config->daemon);
  265. }
  266. }