Add basic infrastructure for general async requests
[samba.git] / source / lib / async_req.c
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 #include "includes.h"
21
22 char *async_req_print(TALLOC_CTX *mem_ctx, struct async_req *req)
23 {
24         return talloc_asprintf(mem_ctx, "async_req: state=%d, status=%s, "
25                                "priv=%s", req->state, nt_errstr(req->status),
26                                talloc_get_name(req->private_data));
27 }
28
29 struct async_req *async_req_new(TALLOC_CTX *mem_ctx, struct event_context *ev)
30 {
31         struct async_req *result;
32
33         result = TALLOC_ZERO_P(mem_ctx, struct async_req);
34         if (result == NULL) {
35                 return NULL;
36         }
37         result->state = ASYNC_REQ_IN_PROGRESS;
38         result->event_ctx = ev;
39         result->print = async_req_print;
40         return result;
41 }
42
43 void async_req_done(struct async_req *req)
44 {
45         req->status = NT_STATUS_OK;
46         req->state = ASYNC_REQ_DONE;
47         if (req->async.fn != NULL) {
48                 req->async.fn(req);
49         }
50 }
51
52 void async_req_error(struct async_req *req, NTSTATUS status)
53 {
54         req->status = status;
55         req->state = ASYNC_REQ_ERROR;
56         if (req->async.fn != NULL) {
57                 req->async.fn(req);
58         }
59 }
60
61 bool async_req_nomem(const void *p, struct async_req *req)
62 {
63         if (p != NULL) {
64                 return false;
65         }
66         async_req_error(req, NT_STATUS_NO_MEMORY);
67         return true;
68 }