xref: /unit/src/nxt_job.h (revision 2084:7d479274f334)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_JOB_H_INCLUDED_
8 #define _NXT_JOB_H_INCLUDED_
9 
10 
11 /*
12  * A job may run by separate thread, so each job should have its
13  * its own mem_pool.  A job itself is allocated from this mem_pool.
14  * On job completion a job initiator can destroy the job at once
15  * with nxt_job_destroy() or can postpone the destruction with
16  * nxt_job_cleanup_add(), if the initiator uses data from the job's
17  * mem_pool.
18  *
19  * Several child jobs may run in context of another job in the same
20  * thread.  In this case the child job may use a mem_pool of the
21  * parent job and the child job is allocated using the mem_pool's cache.
22  * nxt_job_destroy() just returns the job to the cache.  All job
23  * allocations however still remain in the parent mem_pool.
24  *
25  * The first thread in job thread pool is created on demand.  If this
26  * operation fails the job abort handler is called.  It also is called
27  * if the job is canceled.  To avoid race condition the abort handler
28  * always runs in context of a thread initiated the job.  The abort
29  * handler may be as simple as nxt_job_destroy().
30  */
31 
32 
33 typedef struct {
34     void                *data;
35 
36     nxt_task_t          *task;
37 
38     nxt_work_handler_t  abort_handler;
39 
40     uint16_t            cache_size;
41     uint8_t             cancel;          /* 1 bit */
42 
43     nxt_mp_t            *mem_pool;
44     nxt_queue_link_t    link;
45 
46     nxt_thread_pool_t   *thread_pool;
47     nxt_event_engine_t  *engine;
48     nxt_log_t           *log;
49 
50     nxt_work_t          work;
51 
52 #if (NXT_DEBUG)
53     const char          *name;
54 #endif
55 
56 } nxt_job_t;
57 
58 
59 NXT_EXPORT void *nxt_job_create(nxt_mp_t *mp, size_t size);
60 NXT_EXPORT void nxt_job_init(nxt_job_t *job, size_t size);
61 NXT_EXPORT void nxt_job_destroy(nxt_task_t *task, void *data);
62 NXT_EXPORT nxt_int_t nxt_job_cleanup_add(nxt_mp_t *mp, nxt_job_t *job);
63 
64 NXT_EXPORT void nxt_job_start(nxt_task_t *task, nxt_job_t *job,
65     nxt_work_handler_t handler);
66 NXT_EXPORT void nxt_job_return(nxt_task_t *task, nxt_job_t *job,
67     nxt_work_handler_t handler);
68 
69 
70 #define nxt_job_cancel(job)                                                   \
71     (job)->cancel = 1
72 
73 
74 #if (NXT_DEBUG)
75 
76 #define nxt_job_set_name(job, text)                                           \
77     (job)->name = text
78 
79 #else
80 
81 #define nxt_job_set_name(job, text)
82 
83 #endif
84 
85 
86 #endif /* _NXT_JOB_H_INCLUDED_ */
87