*) share temporary number between workers
*) randomize collision offset
diff --git a/src/core/ngx_file.c b/src/core/ngx_file.c
index 3a48960..45bb4ca 100644
--- a/src/core/ngx_file.c
+++ b/src/core/ngx_file.c
@@ -8,8 +8,9 @@
#include <ngx_core.h>
-static ngx_atomic_uint_t ngx_temp_number;
-static ngx_atomic_uint_t ngx_random_number;
+static ngx_atomic_t temp_number = 0;
+ngx_atomic_t *ngx_temp_number = &temp_number;
+ngx_atomic_int_t ngx_random_number = 123456;
ssize_t
@@ -205,22 +206,16 @@
}
-void
-ngx_init_temp_number(void)
-{
- ngx_temp_number = 0;
- ngx_random_number = 123456;
-}
-
-
ngx_atomic_uint_t
ngx_next_temp_number(ngx_uint_t collision)
{
- if (collision) {
- ngx_temp_number += ngx_random_number;
- }
+ ngx_atomic_uint_t n, add;
- return ngx_temp_number++;
+ add = collision ? ngx_random_number : 1;
+
+ n = ngx_atomic_fetch_add(ngx_temp_number, add);
+
+ return n + add;
}
diff --git a/src/core/ngx_file.h b/src/core/ngx_file.h
index 0da8646..5631543 100644
--- a/src/core/ngx_file.h
+++ b/src/core/ngx_file.h
@@ -129,7 +129,6 @@
ngx_int_t ngx_copy_file(u_char *from, u_char *to, ngx_copy_file_t *cf);
ngx_int_t ngx_walk_tree(ngx_tree_ctx_t *ctx, ngx_str_t *tree);
-void ngx_init_temp_number(void);
ngx_atomic_uint_t ngx_next_temp_number(ngx_uint_t collision);
char *ngx_conf_set_path_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
@@ -138,4 +137,8 @@
char *ngx_conf_set_access_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
+extern ngx_atomic_t *ngx_temp_number;
+extern ngx_atomic_int_t ngx_random_number;
+
+
#endif /* _NGX_FILE_H_INCLUDED_ */
diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c
index 7fc6901..9c13eac 100644
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -43,7 +43,7 @@
ngx_event_actions_t ngx_event_actions;
-ngx_atomic_t connection_counter = 1;
+static ngx_atomic_t connection_counter = 1;
ngx_atomic_t *ngx_connection_counter = &connection_counter;
@@ -429,6 +429,7 @@
u_char *shared;
size_t size, cl;
ngx_shm_t shm;
+ ngx_time_t *tp;
ngx_core_conf_t *ccf;
ngx_event_conf_t *ecf;
@@ -492,7 +493,8 @@
cl = 128;
size = cl /* ngx_accept_mutex */
- + cl; /* ngx_connection_counter */
+ + cl /* ngx_connection_counter */
+ + cl; /* ngx_temp_number */
#if (NGX_STAT_STUB)
@@ -526,23 +528,29 @@
ngx_connection_counter = (ngx_atomic_t *) (shared + 1 * cl);
-#if (NGX_STAT_STUB)
-
- ngx_stat_accepted = (ngx_atomic_t *) (shared + 2 * cl);
- ngx_stat_handled = (ngx_atomic_t *) (shared + 3 * cl);
- ngx_stat_requests = (ngx_atomic_t *) (shared + 4 * cl);
- ngx_stat_active = (ngx_atomic_t *) (shared + 5 * cl);
- ngx_stat_reading = (ngx_atomic_t *) (shared + 6 * cl);
- ngx_stat_writing = (ngx_atomic_t *) (shared + 7 * cl);
-
-#endif
-
(void) ngx_atomic_cmp_set(ngx_connection_counter, 0, 1);
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"counter: %p, %d",
ngx_connection_counter, *ngx_connection_counter);
+ ngx_temp_number = (ngx_atomic_t *) (shared + 2 * cl);
+
+ tp = ngx_timeofday();
+
+ ngx_random_number = (tp->msec << 16) + ngx_pid;
+
+#if (NGX_STAT_STUB)
+
+ ngx_stat_accepted = (ngx_atomic_t *) (shared + 3 * cl);
+ ngx_stat_handled = (ngx_atomic_t *) (shared + 4 * cl);
+ ngx_stat_requests = (ngx_atomic_t *) (shared + 5 * cl);
+ ngx_stat_active = (ngx_atomic_t *) (shared + 6 * cl);
+ ngx_stat_reading = (ngx_atomic_t *) (shared + 7 * cl);
+ ngx_stat_writing = (ngx_atomic_t *) (shared + 8 * cl);
+
+#endif
+
return NGX_OK;
}
diff --git a/src/os/unix/ngx_process_cycle.c b/src/os/unix/ngx_process_cycle.c
index c923209..69df544 100644
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -277,8 +277,6 @@
{
ngx_uint_t i;
- ngx_init_temp_number();
-
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->init_process) {
if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
@@ -930,8 +928,6 @@
"sigprocmask() failed");
}
- ngx_init_temp_number();
-
/*
* disable deleting previous events for the listening sockets because
* in the worker processes there are no events at all at this point
diff --git a/src/os/win32/ngx_process_cycle.c b/src/os/win32/ngx_process_cycle.c
index 7a4b955..59016b9 100644
--- a/src/os/win32/ngx_process_cycle.c
+++ b/src/os/win32/ngx_process_cycle.c
@@ -793,8 +793,6 @@
cycle = (ngx_cycle_t *) ngx_cycle;
- ngx_init_temp_number();
-
for (n = 0; ngx_modules[n]; n++) {
if (ngx_modules[n]->init_process) {
if (ngx_modules[n]->init_process(cycle) == NGX_ERROR) {
@@ -1024,8 +1022,6 @@
ngx_int_t i;
ngx_tid_t tid;
- ngx_init_temp_number();
-
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->init_process) {
if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {