这次来看worker子进程。
ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL,
"worker process", type);
在创建子进程的同时,注册了一个子进程要执行的函数:ngx_worker_process_cycle。
下面来看这个函数:
- static void
- ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
- {
- ngx_uint_t i;
- ngx_connection_t *c;
- ngx_process = NGX_PROCESS_WORKER;
- ngx_worker_process_init(cycle, 1);
- ngx_setproctitle("worker process");
- #if (NGX_THREADS)
- {
- ngx_int_t n;
- ngx_err_t err;
- ngx_core_conf_t *ccf;
- ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
- if (ngx_threads_n) {
- if (ngx_init_threads(ngx_threads_n, ccf->thread_stack_size, cycle)
- == NGX_ERROR)
- {
- /* fatal */
- exit(2);
- }
- err = ngx_thread_key_create(&ngx_core_tls_key);
- if (err != 0) {
- ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
- ngx_thread_key_create_n " failed");
- /* fatal */
- exit(2);
- }
- for (n = 0; n < ngx_threads_n; n++) {
- ngx_threads[n].cv = ngx_cond_init(cycle->log);
- if (ngx_threads[n].cv == NULL) {
- /* fatal */
- exit(2);
- }
- if (ngx_create_thread((ngx_tid_t *) &ngx_threads[n].tid,
- ngx_worker_thread_cycle,
- (void *) &ngx_threads[n], cycle->log)
- != 0)
- {
- /* fatal */
- exit(2);
- }
- }
- }
- }
- #endif
- for ( ;; ) {
- if (ngx_exiting) {
- c = cycle->connections;
- for (i = 0; i < cycle->connection_n; i++) {
- /* THREAD: lock */
- if (c[i].fd != -1 && c[i].idle) {
- c[i].close = 1;
- c[i].read->handler(c[i].read);
- }
- }
- if (ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel)
- {
- ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
- ngx_worker_process_exit(cycle);
- }
- }
- ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");
- ngx_process_events_and_timers(cycle);
- if (ngx_terminate) {
- ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
- ngx_worker_process_exit(cycle);
- }
- if (ngx_quit) {
- ngx_quit = 0;
- ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
- "gracefully shutting down");
- ngx_setproctitle("worker process is shutting down");
- if (!ngx_exiting) {
- ngx_close_listening_sockets(cycle);
- ngx_exiting = 1;
- }
- }
- if (ngx_reopen) {
- ngx_reopen = 0;
- ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
- ngx_reopen_files(cycle, -1);
- }
- }
- }
(1)函数首先执行了一个ngx_worker_process_init。这里不进去看了,说说大概的作用:设好进程环境,屏蔽一些信号,还有设好跟其他进程的通信(上次提到过的Unix域用于进程间通信)等等。
(2)接下来涉及线程:首先初始化线程属性、创建“线程标识号”(key)、每一个线程的pthread_cond_t的初始化、创建线程。线程执行的是ngx_worker_thread_cycle这个操作,我们来看看:
- static ngx_thread_value_t
- ngx_worker_thread_cycle(void *data)
- {
- ngx_thread_t *thr = data;
- sigset_t set;
- ngx_err_t err;
- ngx_core_tls_t *tls;
- ngx_cycle_t *cycle;
- cycle = (ngx_cycle_t *) ngx_cycle;
- sigemptyset(&set);
- sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL));
- sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL));
- sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL));
- err = ngx_thread_sigmask(SIG_BLOCK, &set, NULL);
- if (err) {
- ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
- ngx_thread_sigmask_n " failed");
- return (ngx_thread_value_t) 1;
- }
- ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
- "thread " NGX_TID_T_FMT " started", ngx_thread_self());
- ngx_setthrtitle("worker thread");
- tls = ngx_calloc(sizeof(ngx_core_tls_t), cycle->log);
- if (tls == NULL) {
- return (ngx_thread_value_t) 1;
- }
- err = ngx_thread_set_tls(ngx_core_tls_key, tls);
- if (err != 0) {
- ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
- ngx_thread_set_tls_n " failed");
- return (ngx_thread_value_t) 1;
- }
- ngx_mutex_lock(ngx_posted_events_mutex);
- for ( ;; ) {
- thr->state = NGX_THREAD_FREE;
- if (ngx_cond_wait(thr->cv, ngx_posted_events_mutex) == NGX_ERROR) {
- return (ngx_thread_value_t) 1;
- }
- if (ngx_terminate) {
- thr->state = NGX_THREAD_EXIT;
- ngx_mutex_unlock(ngx_posted_events_mutex);
- ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
- "thread " NGX_TID_T_FMT " is done",
- ngx_thread_self());
- return (ngx_thread_value_t) 0;
- }
- thr->state = NGX_THREAD_BUSY;
- if (ngx_event_thread_process_posted(cycle) == NGX_ERROR) {
- return (ngx_thread_value_t) 1;
- }
- if (ngx_event_thread_process_posted(cycle) == NGX_ERROR) {
- return (ngx_thread_value_t) 1;
- }
- if (ngx_process_changes) {
- if (ngx_process_changes(cycle, 1) == NGX_ERROR) {
- return (ngx_thread_value_t) 1;
- }
- }
- }
- }
这个函数的流程大概为:首先是屏蔽一些信号,接着上一个关于有没有事件的锁,接着进入一个for死循环,进入之后线程为free状态,等待事件到来也就是事件锁被打开,最后由ngx_event_thread_process_posted处理被post的事件(估计有一个全局的事件链表,暂未考证,待下面分析再看回来),注意每个线程每一次得到事件锁后处理两个事件。
(3)最后进程(其实应该是主线程,创建的线程都在忙着上面的事)进入死循环,等待一些事情的发生。
阅读(1827) | 评论(0) | 转发(0) |