]> git.kaiwu.me - nginx.git/commitdiff
Threads: task pointer stored in ngx_file_t.
authorMaxim Dounin <mdounin@mdounin.ru>
Fri, 18 Mar 2016 03:43:52 +0000 (06:43 +0300)
committerMaxim Dounin <mdounin@mdounin.ru>
Fri, 18 Mar 2016 03:43:52 +0000 (06:43 +0300)
This simplifies the interface of the ngx_thread_read() function.

Additionally, most of the thread operations now explicitly set
file->thread_task, file->thread_handler and file->thread_ctx,
to facilitate use of thread operations in other places.

(Potential problems remain with sendfile in threads though - it uses
file->thread_handler as set in ngx_output_chain(), and it should not
be overwritten to an incompatible one.)

In collaboration with Valentin Bartenev.

src/core/ngx_file.h
src/core/ngx_output_chain.c
src/http/ngx_http_file_cache.c
src/os/unix/ngx_files.c
src/os/unix/ngx_files.h

index 301b1918b7c063a0766a50e6e673bd703a413fda..aeb6c0cb285c4819a53543fe05babd1ca9e0dd32 100644 (file)
@@ -27,6 +27,7 @@ struct ngx_file_s {
     ngx_int_t                (*thread_handler)(ngx_thread_task_t *task,
                                                ngx_file_t *file);
     void                      *thread_ctx;
+    ngx_thread_task_t         *thread_task;
 #endif
 
 #if (NGX_HAVE_FILE_AIO)
index b8361dcd6461a79393e0c2ef09476b293f843cc1..f7845787b6af08b0317be3ae40132d908851640d 100644 (file)
@@ -577,10 +577,15 @@ ngx_output_chain_copy_buf(ngx_output_chain_ctx_t *ctx)
         } else
 #endif
 #if (NGX_THREADS)
-        if (src->file->thread_handler) {
-            n = ngx_thread_read(&ctx->thread_task, src->file, dst->pos,
-                                (size_t) size, src->file_pos, ctx->pool);
+        if (ctx->thread_handler) {
+            src->file->thread_task = ctx->thread_task;
+            src->file->thread_handler = ctx->thread_handler;
+            src->file->thread_ctx = ctx->filter_ctx;
+
+            n = ngx_thread_read(src->file, dst->pos, (size_t) size,
+                                src->file_pos, ctx->pool);
             if (n == NGX_AGAIN) {
+                ctx->thread_task = src->file->thread_task;
                 return NGX_AGAIN;
             }
 
index 66339182f625d69f9a60391bdac402a04d17f94f..37c4754098d90eee6fabd9c083562e49a791f3df 100644 (file)
@@ -691,12 +691,13 @@ ngx_http_file_cache_aio_read(ngx_http_request_t *r, ngx_http_cache_t *c)
 #if (NGX_THREADS)
 
     if (clcf->aio == NGX_HTTP_AIO_THREADS) {
+        c->file.thread_task = c->thread_task;
         c->file.thread_handler = ngx_http_cache_thread_handler;
         c->file.thread_ctx = r;
 
-        n = ngx_thread_read(&c->thread_task, &c->file, c->buf->pos,
-                            c->body_start, 0, r->pool);
+        n = ngx_thread_read(&c->file, c->buf->pos, c->body_start, 0, r->pool);
 
+        c->thread_task = c->file.thread_task;
         c->reading = (n == NGX_AGAIN);
 
         return n;
index 77ed0c0a42ed9f64ad5be727360c8601a3754c28..13b9e3f0b3cfc033c16ed5e5288bd20db7bb7471 100644 (file)
@@ -88,8 +88,8 @@ typedef struct {
 
 
 ssize_t
-ngx_thread_read(ngx_thread_task_t **taskp, ngx_file_t *file, u_char *buf,
-    size_t size, off_t offset, ngx_pool_t *pool)
+ngx_thread_read(ngx_file_t *file, u_char *buf, size_t size, off_t offset,
+    ngx_pool_t *pool)
 {
     ngx_thread_task_t      *task;
     ngx_thread_read_ctx_t  *ctx;
@@ -98,7 +98,7 @@ ngx_thread_read(ngx_thread_task_t **taskp, ngx_file_t *file, u_char *buf,
                    "thread read: %d, %p, %uz, %O",
                    file->fd, buf, size, offset);
 
-    task = *taskp;
+    task = file->thread_task;
 
     if (task == NULL) {
         task = ngx_thread_task_alloc(pool, sizeof(ngx_thread_read_ctx_t));
@@ -108,7 +108,7 @@ ngx_thread_read(ngx_thread_task_t **taskp, ngx_file_t *file, u_char *buf,
 
         task->handler = ngx_thread_read_handler;
 
-        *taskp = task;
+        file->thread_task = task;
     }
 
     ctx = task->ctx;
index 6081b003f9f72bd09f8d8a90c13972e23a4067f7..88b2f81cc7ad2ce593132311ddc0d70c3e9374f9 100644 (file)
@@ -385,8 +385,8 @@ extern ngx_uint_t  ngx_file_aio;
 #endif
 
 #if (NGX_THREADS)
-ssize_t ngx_thread_read(ngx_thread_task_t **taskp, ngx_file_t *file,
-    u_char *buf, size_t size, off_t offset, ngx_pool_t *pool);
+ssize_t ngx_thread_read(ngx_file_t *file, u_char *buf, size_t size,
+    off_t offset, ngx_pool_t *pool);
 #endif