annotate src/os/unix/ngx_thread_cond.c @ 6422:768e287a6f36

Fixed sendfile in threads (or with aio preload) and subrequests. If sendfile in threads is used, it is possible that multiple subrequests will trigger multiple ngx_linux_sendfile_thread() calls, as operations are only serialized in output chain based on r->aio, that is, on subrequest level. This resulted in "task #N already active" alerts, in particular, when running proxy_store.t with "aio threads; sendfile on;". Fix is to tolerate duplicate calls, with an additional safety check that the file is the same as previously used. The same problem also affects "aio on; sendfile on;" on FreeBSD (previously known as "aio sendfile;"), where aio->preload_handler() could be called multiple times due to similar reasons, resulting in "second aio post" alerts. Fix is the same as well. It is also believed that similar problems can arise if a filter calls the next body filter multiple times for some reason. These are mostly theoretical though.
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 03 Mar 2016 21:14:12 +0300
parents 466bd63b63d1
children 022ea0d17177
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6018
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
1
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
2 /*
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
3 * Copyright (C) Igor Sysoev
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
4 * Copyright (C) Nginx, Inc.
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
5 */
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
6
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
7
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
8 #include <ngx_config.h>
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
9 #include <ngx_core.h>
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
10
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
11
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
12 ngx_int_t
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
13 ngx_thread_cond_create(ngx_thread_cond_t *cond, ngx_log_t *log)
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
14 {
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
15 ngx_err_t err;
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
16
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
17 err = pthread_cond_init(cond, NULL);
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
18 if (err == 0) {
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
19 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
20 "pthread_cond_init(%p)", cond);
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
21 return NGX_OK;
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
22 }
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
23
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
24 ngx_log_error(NGX_LOG_EMERG, log, err, "pthread_cond_init() failed");
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
25 return NGX_ERROR;
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
26 }
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
27
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
28
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
29 ngx_int_t
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
30 ngx_thread_cond_destroy(ngx_thread_cond_t *cond, ngx_log_t *log)
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
31 {
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
32 ngx_err_t err;
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
33
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
34 err = pthread_cond_destroy(cond);
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
35 if (err == 0) {
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
36 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
37 "pthread_cond_destroy(%p)", cond);
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
38 return NGX_OK;
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
39 }
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
40
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
41 ngx_log_error(NGX_LOG_EMERG, log, err, "pthread_cond_destroy() failed");
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
42 return NGX_ERROR;
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
43 }
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
44
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
45
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
46 ngx_int_t
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
47 ngx_thread_cond_signal(ngx_thread_cond_t *cond, ngx_log_t *log)
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
48 {
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
49 ngx_err_t err;
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
50
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
51 err = pthread_cond_signal(cond);
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
52 if (err == 0) {
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
53 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
54 "pthread_cond_signal(%p)", cond);
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
55 return NGX_OK;
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
56 }
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
57
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
58 ngx_log_error(NGX_LOG_EMERG, log, err, "pthread_cond_signal() failed");
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
59 return NGX_ERROR;
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
60 }
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
61
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
62
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
63 ngx_int_t
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
64 ngx_thread_cond_wait(ngx_thread_cond_t *cond, ngx_thread_mutex_t *mtx,
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
65 ngx_log_t *log)
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
66 {
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
67 ngx_err_t err;
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
68
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
69 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
70 "pthread_cond_wait(%p) enter", cond);
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
71
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
72 err = pthread_cond_wait(cond, mtx);
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
73
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
74 #if 0
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
75 ngx_time_update();
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
76 #endif
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
77
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
78 if (err == 0) {
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
79 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
80 "pthread_cond_wait(%p) exit", cond);
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
81 return NGX_OK;
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
82 }
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
83
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
84 ngx_log_error(NGX_LOG_ALERT, log, err, "pthread_cond_wait() failed");
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
85
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
86 return NGX_ERROR;
466bd63b63d1 Thread pools implementation.
Valentin Bartenev <vbart@nginx.com>
parents:
diff changeset
87 }