nginx实现
运行./auto/configure
之后就会生成一个目录objs
,里面有一堆公用的文件,记录着和系统的有关的特性,比如头文件里面都是宏,决定了需要编译哪些代码。
有趣的实现
- 错误信息拷贝
注释里提到,strerror()
函数不是信号安全的,所以开始就先将所有的错误字符串都拷一份缓存在内存中。下面是注释。
/*
* The strerror() messages are copied because:
*
* 1) strerror() and strerror_r() functions are not Async-Signal-Safe,
* therefore, they cannot be used in signal handlers;
*
* 2) a direct sys_errlist[] array may be used instead of these functions,
* but Linux linker warns about its usage:
*
* warning: `sys_errlist' is deprecated; use `strerror' or `strerror_r' instead
* warning: `sys_nerr' is deprecated; use `strerror' or `strerror_r' instead
*
* causing false bug reports.
*/
- 时间更新机制
nginx是支持多线程的,考虑到可能频繁读取当前时间,所以nginx使用了64个slot的数组来循环存放当前时间,更新下一个slot的时候加锁,读的时候无需加锁。这可能存在的问题是,某线程正在更新的时候,被抢占且超过64秒没有再调度它,其他线程会读到这块残缺的内存,这种情况一般不出现。
/*
* The time may be updated by signal handler or by several threads.
* The time update operations are rare and require to hold the ngx_time_lock.
* The time read operations are frequent, so they are lock-free and get time
* values and strings from the current slot. Thus thread may get the corrupted
* values only if it is preempted while copying and then it is not scheduled
* to run more than NGX_TIME_SLOTS seconds.
*/
- 其他
ngx_process
表示了当前进程是master或是slave