async_req: Fix the S4 build
[ira/wip.git] / lib / async_req / async_req.h
1 /*
2    Unix SMB/CIFS implementation.
3    Infrastructure for async requests
4    Copyright (C) Volker Lendecke 2008
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef __ASYNC_REQ_H__
21 #define __ASYNC_REQ_H__
22
23 #include "includes.h"
24
25 /**
26  * An async request moves between the following 4 states:
27  */
28
29 enum async_req_state {
30         /**
31          * we are creating the request
32          */
33         ASYNC_REQ_INIT,
34         /**
35          * we are waiting the request to complete
36          */
37         ASYNC_REQ_IN_PROGRESS,
38         /**
39          * the request is finished
40          */
41         ASYNC_REQ_DONE,
42         /**
43          * an error has occured
44          */
45         ASYNC_REQ_ERROR
46 };
47
48 /**
49  * @brief An async request
50  *
51  * This represents an async request being processed by callbacks via an event
52  * context. A user can issue for example a write request to a socket, giving
53  * an implementation function the fd, the buffer and the number of bytes to
54  * transfer. The function issuing the request will immediately return without
55  * blocking most likely without having sent anything. The API user then fills
56  * in req->async.fn and req->async.priv, functions that are called when the
57  * request is finished.
58  *
59  * It is up to the user of the async request to talloc_free it after it has
60  * finished. This can happen while the completion function is called.
61  */
62
63 struct async_req {
64         /**
65          * @brief The external state - will be queried by the caller
66          *
67          * While the async request is being processed, state will remain in
68          * ASYNC_REQ_IN_PROGRESS. A request is finished if
69          * req->state>=ASYNC_REQ_DONE.
70          */
71         enum async_req_state state;
72
73         /**
74          * @brief Private pointer for the actual implementation
75          *
76          * The implementation doing the work for the async request needs a
77          * current state like for example a fd event. The user of an async
78          * request should not touch this.
79          */
80         void *private_data;
81
82         /**
83          * @brief Print yourself, for debugging purposes
84          *
85          * Async requests are opaque data structures. The implementation of an
86          * async request can define a custom function to print more debug
87          * info.
88          */
89         char *(*print)(TALLOC_CTX *mem_ctx, struct async_req *);
90
91         /**
92          * @brief status code when finished
93          *
94          * This status can be queried in the async completion function. It
95          * will be set to NT_STATUS_OK when everything went fine.
96          **/
97         NTSTATUS status;
98
99         /**
100          * @brief What to do on completion
101          *
102          * This is used for the user of an async request, fn is called when
103          * the request completes, either successfully or with an error.
104          */
105         struct {
106                 /**
107                  * @brief Completion function
108                  * Completion function, to be filled by the API user
109                  */
110                 void (*fn)(struct async_req *);
111                 /**
112                  * @brief Private data for the completion function
113                  */
114                 void *priv;
115         } async;
116 };
117
118 struct async_req *async_req_new(TALLOC_CTX *mem_ctx);
119
120 char *async_req_print(TALLOC_CTX *mem_ctx, struct async_req *req);
121
122 void async_req_done(struct async_req *req);
123
124 void async_req_error(struct async_req *req, NTSTATUS status);
125
126 bool async_post_status(struct async_req *req, struct tevent_context *ev,
127                        NTSTATUS status);
128
129 bool async_req_nomem(const void *p, struct async_req *req);
130
131 bool async_req_is_error(struct async_req *req, NTSTATUS *status);
132
133 NTSTATUS async_req_simple_recv(struct async_req *req);
134
135 bool async_req_set_timeout(struct async_req *req, struct tevent_context *ev,
136                            struct timeval to);
137
138 struct async_req *async_wait_send(TALLOC_CTX *mem_ctx,
139                                   struct tevent_context *ev,
140                                   struct timeval to);
141
142 NTSTATUS async_wait_recv(struct async_req *req);
143
144 struct async_req_queue;
145
146 struct async_req_queue *async_req_queue_init(TALLOC_CTX *mem_ctx);
147
148 bool async_req_enqueue(struct async_req_queue *queue,
149                        struct tevent_context *ev,
150                        struct async_req *req,
151                        void (*trigger)(struct async_req *req));
152
153 bool _async_req_setup(TALLOC_CTX *mem_ctx, struct async_req **preq,
154                       void *pstate, size_t state_size, const char *typename);
155
156 #define async_req_setup(_mem_ctx, _preq, _pstate, type) \
157         _async_req_setup((_mem_ctx), (_preq), (_pstate), sizeof(type), #type)
158
159
160 #endif