hmain.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. #include "hmain.h"
  2. #include "hbase.h"
  3. #include "hlog.h"
  4. #include "herr.h"
  5. #include "htime.h"
  6. #include "hthread.h"
  7. #ifdef OS_DARWIN
  8. #include <crt_externs.h>
  9. #define environ (*_NSGetEnviron())
  10. #endif
  11. main_ctx_t g_main_ctx;
  12. static void init_arg_kv(int maxsize) {
  13. g_main_ctx.arg_kv_size = 0;
  14. SAFE_ALLOC(g_main_ctx.arg_kv, sizeof(char*) * maxsize);
  15. }
  16. static void save_arg_kv(const char* key, int key_len, const char* val, int val_len) {
  17. if (key_len <= 0) key_len = strlen(key);
  18. if (val_len <= 0) val_len = strlen(val);
  19. char* arg = NULL;
  20. SAFE_ALLOC(arg, key_len + val_len + 2);
  21. memcpy(arg, key, key_len);
  22. arg[key_len] = '=';
  23. memcpy(arg + key_len + 1, val, val_len);
  24. // printf("save_arg_kv: %s\n", arg);
  25. g_main_ctx.arg_kv[g_main_ctx.arg_kv_size++] = arg;
  26. }
  27. static void init_arg_list(int maxsize) {
  28. g_main_ctx.arg_list_size = 0;
  29. SAFE_ALLOC(g_main_ctx.arg_list, sizeof(char*) * maxsize);
  30. }
  31. static void save_arg_list(const char* arg) {
  32. // printf("save_arg_list: %s\n", arg);
  33. g_main_ctx.arg_list[g_main_ctx.arg_list_size++] = strdup(arg);
  34. }
  35. static const char* get_val(char** kvs, const char* key) {
  36. if (kvs == NULL) return NULL;
  37. int key_len = strlen(key);
  38. char* kv = NULL;
  39. int kv_len = 0;
  40. for (int i = 0; kvs[i]; ++i) {
  41. kv = kvs[i];
  42. kv_len = strlen(kv);
  43. if (kv_len <= key_len) continue;
  44. // key=val
  45. if (memcmp(kv, key, key_len) == 0 && kv[key_len] == '=') {
  46. return kv + key_len + 1;
  47. }
  48. }
  49. return NULL;
  50. }
  51. const char* get_arg(const char* key) {
  52. return get_val(g_main_ctx.arg_kv, key);
  53. }
  54. const char* get_env(const char* key) {
  55. return get_val(g_main_ctx.save_envp, key);
  56. }
  57. int main_ctx_init(int argc, char** argv) {
  58. if (argc == 0 || argv == NULL) {
  59. argc = 1;
  60. SAFE_ALLOC(argv, 2 * sizeof(char*));
  61. SAFE_ALLOC(argv[0], MAX_PATH);
  62. get_executable_path(argv[0], MAX_PATH);
  63. }
  64. get_run_dir(g_main_ctx.run_dir, sizeof(g_main_ctx.run_dir));
  65. //printf("run_dir=%s\n", g_main_ctx.run_dir);
  66. strncpy(g_main_ctx.program_name, hv_basename(argv[0]), sizeof(g_main_ctx.program_name));
  67. #ifdef OS_WIN
  68. if (strcmp(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4, ".exe") == 0) {
  69. *(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4) = '\0';
  70. }
  71. #endif
  72. //printf("program_name=%s\n", g_main_ctx.program_name);
  73. char logdir[MAX_PATH] = {0};
  74. snprintf(logdir, sizeof(logdir), "%s/logs", g_main_ctx.run_dir);
  75. hv_mkdir(logdir);
  76. snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_dir, g_main_ctx.program_name);
  77. snprintf(g_main_ctx.pidfile, sizeof(g_main_ctx.pidfile), "%s/logs/%s.pid", g_main_ctx.run_dir, g_main_ctx.program_name);
  78. snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.confile), "%s/logs/%s.log", g_main_ctx.run_dir, g_main_ctx.program_name);
  79. hlog_set_file(g_main_ctx.logfile);
  80. g_main_ctx.pid = getpid();
  81. g_main_ctx.oldpid = getpid_from_pidfile();
  82. #ifdef OS_UNIX
  83. if (kill(g_main_ctx.oldpid, 0) == -1 && errno == ESRCH) {
  84. g_main_ctx.oldpid = -1;
  85. }
  86. #else
  87. HANDLE hproc = OpenProcess(PROCESS_TERMINATE, FALSE, g_main_ctx.oldpid);
  88. if (hproc == NULL) {
  89. g_main_ctx.oldpid = -1;
  90. }
  91. else {
  92. CloseHandle(hproc);
  93. }
  94. #endif
  95. // save arg
  96. int i = 0;
  97. g_main_ctx.os_argv = argv;
  98. g_main_ctx.argc = 0;
  99. g_main_ctx.arg_len = 0;
  100. for (i = 0; argv[i]; ++i) {
  101. g_main_ctx.arg_len += strlen(argv[i]) + 1;
  102. }
  103. g_main_ctx.argc = i;
  104. char* argp = NULL;
  105. SAFE_ALLOC(argp, g_main_ctx.arg_len);
  106. SAFE_ALLOC(g_main_ctx.save_argv, (g_main_ctx.argc + 1) * sizeof(char*));
  107. char* cmdline = NULL;
  108. SAFE_ALLOC(cmdline, g_main_ctx.arg_len);
  109. g_main_ctx.cmdline = cmdline;
  110. for (i = 0; argv[i]; ++i) {
  111. strcpy(argp, argv[i]);
  112. g_main_ctx.save_argv[i] = argp;
  113. argp += strlen(argv[i]) + 1;
  114. strcpy(cmdline, argv[i]);
  115. cmdline += strlen(argv[i]);
  116. *cmdline = ' ';
  117. ++cmdline;
  118. }
  119. g_main_ctx.save_argv[g_main_ctx.argc] = NULL;
  120. g_main_ctx.cmdline[g_main_ctx.arg_len-1] = '\0';
  121. #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_DARWIN)
  122. // save env
  123. g_main_ctx.os_envp = environ;
  124. g_main_ctx.envc = 0;
  125. g_main_ctx.env_len = 0;
  126. for (i = 0; environ[i]; ++i) {
  127. g_main_ctx.env_len += strlen(environ[i]) + 1;
  128. }
  129. g_main_ctx.envc = i;
  130. char* envp = NULL;
  131. SAFE_ALLOC(envp, g_main_ctx.env_len);
  132. SAFE_ALLOC(g_main_ctx.save_envp, (g_main_ctx.envc + 1) * sizeof(char*));
  133. for (i = 0; environ[i]; ++i) {
  134. g_main_ctx.save_envp[i] = envp;
  135. strcpy(g_main_ctx.save_envp[i], environ[i]);
  136. envp += strlen(environ[i]) + 1;
  137. }
  138. g_main_ctx.save_envp[g_main_ctx.envc] = NULL;
  139. #endif
  140. // signals
  141. g_main_ctx.reload_fn = NULL;
  142. g_main_ctx.reload_userdata = NULL;
  143. // master workers
  144. g_main_ctx.worker_processes = 0;
  145. g_main_ctx.worker_threads = 0;
  146. g_main_ctx.worker_fn = 0;
  147. g_main_ctx.worker_userdata = 0;
  148. g_main_ctx.proc_ctxs = NULL;
  149. return 0;
  150. }
  151. #define UNDEFINED_OPTION -1
  152. static int get_arg_type(int short_opt, const char* options) {
  153. if (options == NULL) return UNDEFINED_OPTION;
  154. const char* p = options;
  155. while (*p && *p != short_opt) ++p;
  156. if (*p == '\0') return UNDEFINED_OPTION;
  157. if (*(p+1) == ':') return REQUIRED_ARGUMENT;
  158. return NO_ARGUMENT;
  159. }
  160. int parse_opt(int argc, char** argv, const char* options) {
  161. if (argc < 1) return 0;
  162. init_arg_kv(strlen(options) + 1);
  163. init_arg_list(argc);
  164. for (int i = 1; argv[i]; ++i) {
  165. char* p = argv[i];
  166. if (*p != '-') {
  167. save_arg_list(argv[i]);
  168. continue;
  169. }
  170. while (*++p) {
  171. int arg_type = get_arg_type(*p, options);
  172. if (arg_type == UNDEFINED_OPTION) {
  173. printf("Invalid option '%c'\n", *p);
  174. return -20;
  175. } else if (arg_type == NO_ARGUMENT) {
  176. save_arg_kv(p, 1, OPTION_ENABLE, 0);
  177. continue;
  178. } else if (arg_type == REQUIRED_ARGUMENT) {
  179. if (*(p+1) != '\0') {
  180. save_arg_kv(p, 1, p+1, 0);
  181. break;
  182. } else if (argv[i+1] != NULL) {
  183. save_arg_kv(p, 1, argv[++i], 0);
  184. break;
  185. } else {
  186. printf("Option '%c' requires param\n", *p);
  187. return -30;
  188. }
  189. }
  190. }
  191. }
  192. return 0;
  193. }
  194. static const option_t* get_option(const char* opt, const option_t* long_options, int size) {
  195. if (opt == NULL || long_options == NULL) return NULL;
  196. int len = strlen(opt);
  197. if (len == 0) return NULL;
  198. if (len == 1) {
  199. for (int i = 0; i < size; ++i) {
  200. if (long_options[i].short_opt == *opt) {
  201. return &long_options[i];
  202. }
  203. }
  204. } else {
  205. for (int i = 0; i < size; ++i) {
  206. if (strcmp(long_options[i].long_opt, opt) == 0) {
  207. return &long_options[i];
  208. }
  209. }
  210. }
  211. return NULL;
  212. }
  213. #define MAX_OPTION 32
  214. // opt type
  215. #define NOPREFIX_OPTION 0
  216. #define SHORT_OPTION -1
  217. #define LONG_OPTION -2
  218. int parse_opt_long(int argc, char** argv, const option_t* long_options, int size) {
  219. if (argc < 1) return 0;
  220. init_arg_kv(size + 1);
  221. init_arg_list(argc);
  222. char opt[MAX_OPTION+1] = {0};
  223. for (int i = 1; argv[i]; ++i) {
  224. char* arg = argv[i];
  225. int opt_type = NOPREFIX_OPTION;
  226. // prefix
  227. if (*arg == OPTION_PREFIX) {
  228. ++arg;
  229. opt_type = SHORT_OPTION;
  230. if (*arg == OPTION_PREFIX) {
  231. ++arg;
  232. opt_type = LONG_OPTION;
  233. }
  234. }
  235. int arg_len = strlen(arg);
  236. // delim
  237. char* delim = strchr(arg, OPTION_DELIM);
  238. if (delim) {
  239. if (delim == arg || delim == arg+arg_len-1 || delim-arg > MAX_OPTION) {
  240. printf("Invalid option '%s'\n", argv[i]);
  241. return -10;
  242. }
  243. memcpy(opt, arg, delim-arg);
  244. opt[delim-arg] = '\0';
  245. } else {
  246. if (opt_type == SHORT_OPTION) {
  247. *opt = *arg;
  248. opt[1] = '\0';
  249. } else {
  250. strncpy(opt, arg, MAX_OPTION);
  251. }
  252. }
  253. // get_option
  254. const option_t* pOption = get_option(opt, long_options, size);
  255. if (pOption == NULL) {
  256. if (delim == NULL && opt_type == NOPREFIX_OPTION) {
  257. save_arg_list(arg);
  258. continue;
  259. } else {
  260. printf("Invalid option: '%s'\n", argv[i]);
  261. return -10;
  262. }
  263. }
  264. const char* value = NULL;
  265. if (pOption->arg_type == NO_ARGUMENT) {
  266. // -h
  267. value = OPTION_ENABLE;
  268. } else if (pOption->arg_type == REQUIRED_ARGUMENT) {
  269. if (delim) {
  270. // --port=80
  271. value = delim+1;
  272. } else {
  273. if (opt_type == SHORT_OPTION && *(arg+1) != '\0') {
  274. // p80
  275. value = arg+1;
  276. } else if (argv[i+1] != NULL) {
  277. // --port 80
  278. value = argv[++i];
  279. } else {
  280. printf("Option '%s' requires parament\n", opt);
  281. return -20;
  282. }
  283. }
  284. }
  285. // preferred to use short_opt as key
  286. if (pOption->short_opt > 0) {
  287. save_arg_kv(&pOption->short_opt, 1, value, 0);
  288. } else if (pOption->long_opt) {
  289. save_arg_kv(pOption->long_opt, 0, value, 0);
  290. }
  291. }
  292. return 0;
  293. }
  294. #ifdef OS_UNIX
  295. /*
  296. * memory layout
  297. * argv[0]\0argv[1]\0argv[n]\0env[0]\0env[1]\0env[n]\0
  298. */
  299. void hv_setproctitle(const char* fmt, ...) {
  300. char buf[256] = {0};
  301. va_list ap;
  302. va_start(ap, fmt);
  303. vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
  304. va_end(ap);
  305. int len = g_main_ctx.arg_len + g_main_ctx.env_len;
  306. if (g_main_ctx.os_argv && len) {
  307. strncpy(g_main_ctx.os_argv[0], buf, len-1);
  308. }
  309. }
  310. #endif
  311. int create_pidfile() {
  312. FILE* fp = fopen(g_main_ctx.pidfile, "w");
  313. if (fp == NULL) {
  314. hloge("fopen('%s') error: %d", g_main_ctx.pidfile, errno);
  315. return -1;
  316. }
  317. g_main_ctx.pid = hv_getpid();
  318. fprintf(fp, "%d\n", (int)g_main_ctx.pid);
  319. fclose(fp);
  320. hlogi("create_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
  321. atexit(delete_pidfile);
  322. return 0;
  323. }
  324. void delete_pidfile() {
  325. hlogi("delete_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
  326. remove(g_main_ctx.pidfile);
  327. }
  328. pid_t getpid_from_pidfile() {
  329. FILE* fp = fopen(g_main_ctx.pidfile, "r");
  330. if (fp == NULL) {
  331. // hloge("fopen('%s') error: %d", g_main_ctx.pidfile, errno);
  332. return -1;
  333. }
  334. int pid = -1;
  335. fscanf(fp, "%d", &pid);
  336. fclose(fp);
  337. return pid;
  338. }
  339. #ifdef OS_UNIX
  340. // unix use signal
  341. #include <sys/wait.h>
  342. void signal_handler(int signo) {
  343. hlogi("pid=%d recv signo=%d", getpid(), signo);
  344. switch (signo) {
  345. case SIGINT:
  346. case SIGNAL_TERMINATE:
  347. hlogi("killall processes");
  348. signal(SIGCHLD, SIG_IGN);
  349. // master send SIGKILL => workers
  350. for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
  351. if (g_main_ctx.proc_ctxs[i].pid <= 0) break;
  352. kill(g_main_ctx.proc_ctxs[i].pid, SIGKILL);
  353. g_main_ctx.proc_ctxs[i].pid = -1;
  354. }
  355. exit(0);
  356. break;
  357. case SIGCHLD:
  358. {
  359. pid_t pid = 0;
  360. int status = 0;
  361. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  362. hlogw("proc stop/waiting, pid=%d status=%d", pid, status);
  363. for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
  364. proc_ctx_t* ctx = g_main_ctx.proc_ctxs + i;
  365. if (ctx->pid == pid) {
  366. ctx->pid = -1;
  367. // NOTE: avoid frequent crash and restart
  368. time_t run_time = time(NULL) - ctx->start_time;
  369. if (ctx->spawn_cnt < 3 || run_time > 3600) {
  370. hproc_spawn(ctx);
  371. }
  372. else {
  373. hloge("proc crash, pid=%d spawn_cnt=%d run_time=%us",
  374. pid, ctx->spawn_cnt, (unsigned int)run_time);
  375. bool have_worker = false;
  376. for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
  377. if (g_main_ctx.proc_ctxs[i].pid > 0) {
  378. have_worker = true;
  379. break;
  380. }
  381. }
  382. if (!have_worker) {
  383. hlogw("No alive worker process, exit master process!");
  384. exit(0);
  385. }
  386. }
  387. break;
  388. }
  389. }
  390. }
  391. }
  392. break;
  393. case SIGNAL_RELOAD:
  394. if (g_main_ctx.reload_fn) {
  395. g_main_ctx.reload_fn(g_main_ctx.reload_userdata);
  396. if (getpid_from_pidfile() == getpid()) {
  397. // master send SIGNAL_RELOAD => workers
  398. for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
  399. if (g_main_ctx.proc_ctxs[i].pid <= 0) break;
  400. kill(g_main_ctx.proc_ctxs[i].pid, SIGNAL_RELOAD);
  401. }
  402. }
  403. }
  404. break;
  405. default:
  406. break;
  407. }
  408. }
  409. int signal_init(procedure_t reload_fn, void* reload_userdata) {
  410. g_main_ctx.reload_fn = reload_fn;
  411. g_main_ctx.reload_userdata = reload_userdata;
  412. signal(SIGINT, signal_handler);
  413. signal(SIGCHLD, signal_handler);
  414. signal(SIGNAL_TERMINATE, signal_handler);
  415. signal(SIGNAL_RELOAD, signal_handler);
  416. return 0;
  417. }
  418. #elif defined(OS_WIN)
  419. #include <mmsystem.h> // for timeSetEvent
  420. // win32 use Event
  421. //static HANDLE s_hEventTerm = NULL;
  422. static HANDLE s_hEventReload = NULL;
  423. static void WINAPI on_timer(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) {
  424. DWORD ret;
  425. /*
  426. ret = WaitForSingleObject(s_hEventTerm, 0);
  427. if (ret == WAIT_OBJECT_0) {
  428. hlogi("pid=%d recv event [TERM]", getpid());
  429. if (getpid_from_pidfile() == getpid()) {
  430. timeKillEvent(uTimerID);
  431. exit(0);
  432. }
  433. }
  434. */
  435. ret = WaitForSingleObject(s_hEventReload, 0);
  436. if (ret == WAIT_OBJECT_0) {
  437. hlogi("pid=%d recv event [RELOAD]", getpid());
  438. if (g_main_ctx.reload_fn) {
  439. g_main_ctx.reload_fn(g_main_ctx.reload_userdata);
  440. }
  441. }
  442. }
  443. static void signal_cleanup() {
  444. //CloseHandle(s_hEventTerm);
  445. //s_hEventTerm = NULL;
  446. CloseHandle(s_hEventReload);
  447. s_hEventReload = NULL;
  448. }
  449. int signal_init(procedure_t reload_fn, void* reload_userdata) {
  450. g_main_ctx.reload_fn = reload_fn;
  451. g_main_ctx.reload_userdata = reload_userdata;
  452. char eventname[MAX_PATH] = {0};
  453. //snprintf(eventname, sizeof(eventname), "%s_term_event", g_main_ctx.program_name);
  454. //s_hEventTerm = CreateEvent(NULL, FALSE, FALSE, eventname);
  455. //s_hEventTerm = OpenEvent(EVENT_ALL_ACCESS, FALSE, eventname);
  456. snprintf(eventname, sizeof(eventname), "%s_reload_event", g_main_ctx.program_name);
  457. s_hEventReload = CreateEvent(NULL, FALSE, FALSE, eventname);
  458. timeSetEvent(1000, 1000, on_timer, 0, TIME_PERIODIC);
  459. atexit(signal_cleanup);
  460. return 0;
  461. }
  462. #endif
  463. static void kill_proc(int pid) {
  464. #ifdef OS_UNIX
  465. kill(pid, SIGNAL_TERMINATE);
  466. #else
  467. //SetEvent(s_hEventTerm);
  468. //hv_sleep(1);
  469. HANDLE hproc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
  470. if (hproc) {
  471. TerminateProcess(hproc, 0);
  472. CloseHandle(hproc);
  473. }
  474. #endif
  475. }
  476. void signal_handle(const char* signal) {
  477. if (strcmp(signal, "start") == 0) {
  478. if (g_main_ctx.oldpid > 0) {
  479. printf("%s is already running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
  480. exit(0);
  481. }
  482. } else if (strcmp(signal, "stop") == 0) {
  483. if (g_main_ctx.oldpid > 0) {
  484. kill_proc(g_main_ctx.oldpid);
  485. printf("%s stop/waiting\n", g_main_ctx.program_name);
  486. } else {
  487. printf("%s is already stopped\n", g_main_ctx.program_name);
  488. }
  489. exit(0);
  490. } else if (strcmp(signal, "restart") == 0) {
  491. if (g_main_ctx.oldpid > 0) {
  492. kill_proc(g_main_ctx.oldpid);
  493. printf("%s stop/waiting\n", g_main_ctx.program_name);
  494. hv_sleep(1);
  495. }
  496. } else if (strcmp(signal, "status") == 0) {
  497. if (g_main_ctx.oldpid > 0) {
  498. printf("%s start/running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
  499. } else {
  500. printf("%s stop/waiting\n", g_main_ctx.program_name);
  501. }
  502. exit(0);
  503. } else if (strcmp(signal, "reload") == 0) {
  504. if (g_main_ctx.oldpid > 0) {
  505. printf("reload confile [%s]\n", g_main_ctx.confile);
  506. #ifdef OS_UNIX
  507. kill(g_main_ctx.oldpid, SIGNAL_RELOAD);
  508. #else
  509. SetEvent(s_hEventReload);
  510. #endif
  511. }
  512. hv_sleep(1);
  513. exit(0);
  514. } else {
  515. printf("Invalid signal: '%s'\n", signal);
  516. exit(0);
  517. }
  518. printf("%s start/running\n", g_main_ctx.program_name);
  519. }
  520. // master-workers processes
  521. static HTHREAD_ROUTINE(worker_thread) {
  522. hlogi("worker_thread pid=%ld tid=%ld", hv_getpid(), hv_gettid());
  523. if (g_main_ctx.worker_fn) {
  524. g_main_ctx.worker_fn(g_main_ctx.worker_userdata);
  525. }
  526. return 0;
  527. }
  528. static void worker_init(void* userdata) {
  529. #ifdef OS_UNIX
  530. hv_setproctitle("%s: worker process", g_main_ctx.program_name);
  531. signal(SIGNAL_RELOAD, signal_handler);
  532. #endif
  533. }
  534. static void worker_proc(void* userdata) {
  535. for (int i = 1; i < g_main_ctx.worker_threads; ++i) {
  536. hthread_create(worker_thread, NULL);
  537. }
  538. worker_thread(NULL);
  539. }
  540. int master_workers_run(procedure_t worker_fn, void* worker_userdata,
  541. int worker_processes, int worker_threads, bool wait) {
  542. #ifdef OS_WIN
  543. // NOTE: Windows not provide MultiProcesses
  544. if (worker_threads == 0) {
  545. // MultiProcesses => MultiThreads
  546. worker_threads = worker_processes;
  547. }
  548. worker_processes = 0;
  549. #endif
  550. if (worker_threads == 0) worker_threads = 1;
  551. g_main_ctx.worker_threads = worker_threads;
  552. g_main_ctx.worker_fn = worker_fn;
  553. g_main_ctx.worker_userdata = worker_userdata;
  554. if (worker_processes == 0) {
  555. // single process
  556. if (wait) {
  557. for (int i = 1; i < worker_threads; ++i) {
  558. hthread_create(worker_thread, NULL);
  559. }
  560. worker_thread(NULL);
  561. }
  562. else {
  563. for (int i = 0; i < worker_threads; ++i) {
  564. hthread_create(worker_thread, NULL);
  565. }
  566. }
  567. }
  568. else {
  569. if (g_main_ctx.worker_processes != 0) {
  570. return ERR_OVER_LIMIT;
  571. }
  572. // master-workers processes
  573. #ifdef OS_UNIX
  574. hv_setproctitle("%s: master process", g_main_ctx.program_name);
  575. signal(SIGNAL_RELOAD, signal_handler);
  576. #endif
  577. g_main_ctx.worker_processes = worker_processes;
  578. int bytes = g_main_ctx.worker_processes * sizeof(proc_ctx_t);
  579. SAFE_ALLOC(g_main_ctx.proc_ctxs, bytes);
  580. proc_ctx_t* ctx = g_main_ctx.proc_ctxs;
  581. for (int i = 0; i < g_main_ctx.worker_processes; ++i, ++ctx) {
  582. ctx->init = worker_init;
  583. ctx->proc = worker_proc;
  584. hproc_spawn(ctx);
  585. hlogi("workers[%d] start/running, pid=%d", i, ctx->pid);
  586. }
  587. g_main_ctx.pid = getpid();
  588. hlogi("master start/running, pid=%d", g_main_ctx.pid);
  589. if (wait) {
  590. while (1) hv_sleep (1);
  591. }
  592. }
  593. return 0;
  594. }