nginx source code reading notes

Source Repository

Run ./auto/configurewill generate after a catalog objs, there are a bunch of public documents and records relating to the characteristics of the system, such as header files which are macros, decide what the code needs to be compiled.

Interestingly realization

  • Error message copy

Notes mentioned, strerror()not a function of signal security, so first of all start on the wrong strings a copy cached in memory. Here is a comment.

 * 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.
  • Time update mechanism

nginx is a multi-threaded, taking into account possible frequently read the current time, so nginx using an array of 64 slot to store the current time cycle, the next update when a lock slot, read the time without locking. This may be a problem that, when a thread is updating, is preempted and no more than 64 seconds and then dispatch it, other threads will read this crippled memory, which generally does not occur.

 * 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.

Guess you like