]> git.ipfire.org Git - thirdparty/squid.git/blob - src/DiskIO/DiskThreads/async_io.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / DiskIO / DiskThreads / async_io.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 32 Asynchronous Disk I/O
6 * AUTHOR: Pete Bentley <pete@demon.net>
7 * AUTHOR: Stewart Forster <slf@connect.com.au>
8 *
9 * SQUID Web Proxy Cache http://www.squid-cache.org/
10 * ----------------------------------------------------------
11 *
12 * Squid is the result of efforts by numerous individuals from
13 * the Internet community; see the CONTRIBUTORS file for full
14 * details. Many organizations have provided support for Squid's
15 * development; see the SPONSORS file for full details. Squid is
16 * Copyrighted (C) 2001 by the Regents of the University of
17 * California; see the COPYRIGHT file for full details. Squid
18 * incorporates software developed and/or copyrighted by other
19 * sources; see the CREDITS file for full details.
20 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 *
35 */
36
37 #include "squid.h"
38 #include "DiskThreads.h"
39 #include "Store.h"
40 #include "fde.h"
41 #include "DiskThreadsIOStrategy.h"
42 #include "Generic.h"
43
44 AIOCounts squidaio_counts;
45
46 typedef struct squidaio_unlinkq_t {
47 char *path;
48
49 struct squidaio_unlinkq_t *next;
50 } squidaio_unlinkq_t;
51
52 dlink_list used_list;
53
54 void
55 aioOpen(const char *path, int oflag, mode_t mode, AIOCB * callback, void *callback_data)
56 {
57 squidaio_ctrl_t *ctrlp;
58
59 assert(DiskThreadsIOStrategy::Instance.initialised);
60 ++squidaio_counts.open_start;
61 ctrlp = (squidaio_ctrl_t *)DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->alloc();
62 ctrlp->fd = -2;
63 ctrlp->done_handler = callback;
64 ctrlp->done_handler_data = cbdataReference(callback_data);
65 ctrlp->operation = _AIO_OPEN;
66 ctrlp->result.data = ctrlp;
67 squidaio_open(path, oflag, mode, &ctrlp->result);
68 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
69 return;
70 }
71
72 void
73 aioClose(int fd)
74 {
75 squidaio_ctrl_t *ctrlp;
76
77 assert(DiskThreadsIOStrategy::Instance.initialised);
78 ++squidaio_counts.close_start;
79 aioCancel(fd);
80 ctrlp = (squidaio_ctrl_t *)DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->alloc();
81 ctrlp->fd = fd;
82 ctrlp->done_handler = NULL;
83 ctrlp->done_handler_data = NULL;
84 ctrlp->operation = _AIO_CLOSE;
85 ctrlp->result.data = ctrlp;
86 squidaio_close(fd, &ctrlp->result);
87 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
88 return;
89 }
90
91 void
92 aioCancel(int fd)
93 {
94 squidaio_ctrl_t *ctrlp;
95 dlink_node *m, *next;
96
97 assert(DiskThreadsIOStrategy::Instance.initialised);
98 ++squidaio_counts.cancel;
99
100 for (m = used_list.head; m; m = next) {
101 next = m->next;
102 ctrlp = (squidaio_ctrl_t *)m->data;
103
104 if (ctrlp->fd != fd)
105 continue;
106
107 squidaio_cancel(&ctrlp->result);
108
109 if (ctrlp->done_handler) {
110 AIOCB *callback = ctrlp->done_handler;
111 void *cbdata;
112 ctrlp->done_handler = NULL;
113 debugs(32, DBG_IMPORTANT, "this be aioCancel. Danger ahead!");
114
115 if (cbdataReferenceValidDone(ctrlp->done_handler_data, &cbdata))
116 callback(fd, cbdata, NULL, -2, -2);
117
118 /* free data if requested to aioWrite() */
119 if (ctrlp->free_func)
120 ctrlp->free_func(ctrlp->bufp);
121
122 /* free temporary read buffer */
123 if (ctrlp->operation == _AIO_READ)
124 squidaio_xfree(ctrlp->bufp, ctrlp->len);
125 }
126
127 dlinkDelete(m, &used_list);
128 DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->freeOne(ctrlp);
129 }
130 }
131
132 void
133 aioWrite(int fd, off_t offset, char *bufp, size_t len, AIOCB * callback, void *callback_data, FREE * free_func)
134 {
135 squidaio_ctrl_t *ctrlp;
136 int seekmode;
137
138 assert(DiskThreadsIOStrategy::Instance.initialised);
139 ++squidaio_counts.write_start;
140 ctrlp = (squidaio_ctrl_t *)DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->alloc();
141 ctrlp->fd = fd;
142 ctrlp->done_handler = callback;
143 ctrlp->done_handler_data = cbdataReference(callback_data);
144 ctrlp->operation = _AIO_WRITE;
145 ctrlp->bufp = bufp;
146 ctrlp->free_func = free_func;
147
148 if (offset >= 0)
149 seekmode = SEEK_SET;
150 else {
151 seekmode = SEEK_END;
152 offset = 0;
153 }
154
155 ctrlp->result.data = ctrlp;
156 squidaio_write(fd, bufp, len, offset, seekmode, &ctrlp->result);
157 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
158 } /* aioWrite */
159
160 void
161 aioRead(int fd, off_t offset, size_t len, AIOCB * callback, void *callback_data)
162 {
163 squidaio_ctrl_t *ctrlp;
164 int seekmode;
165
166 assert(DiskThreadsIOStrategy::Instance.initialised);
167 ++squidaio_counts.read_start;
168 ctrlp = (squidaio_ctrl_t *)DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->alloc();
169 ctrlp->fd = fd;
170 ctrlp->done_handler = callback;
171 ctrlp->done_handler_data = cbdataReference(callback_data);
172 ctrlp->operation = _AIO_READ;
173 ctrlp->len = len;
174 ctrlp->bufp = (char *)squidaio_xmalloc(len);
175
176 if (offset >= 0)
177 seekmode = SEEK_SET;
178 else {
179 seekmode = SEEK_CUR;
180 offset = 0;
181 }
182
183 ctrlp->result.data = ctrlp;
184 squidaio_read(fd, ctrlp->bufp, len, offset, seekmode, &ctrlp->result);
185 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
186 return;
187 } /* aioRead */
188
189 void
190
191 aioStat(char *path, struct stat *sb, AIOCB * callback, void *callback_data)
192 {
193 squidaio_ctrl_t *ctrlp;
194
195 assert(DiskThreadsIOStrategy::Instance.initialised);
196 ++squidaio_counts.stat_start;
197 ctrlp = (squidaio_ctrl_t *)DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->alloc();
198 ctrlp->fd = -2;
199 ctrlp->done_handler = callback;
200 ctrlp->done_handler_data = cbdataReference(callback_data);
201 ctrlp->operation = _AIO_STAT;
202 ctrlp->result.data = ctrlp;
203 squidaio_stat(path, sb, &ctrlp->result);
204 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
205 return;
206 } /* aioStat */
207
208 void
209 aioUnlink(const char *path, AIOCB * callback, void *callback_data)
210 {
211 squidaio_ctrl_t *ctrlp;
212 assert(DiskThreadsIOStrategy::Instance.initialised);
213 ++squidaio_counts.unlink_start;
214 ctrlp = (squidaio_ctrl_t *)DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->alloc();
215 ctrlp->fd = -2;
216 ctrlp->done_handler = callback;
217 ctrlp->done_handler_data = cbdataReference(callback_data);
218 ctrlp->operation = _AIO_UNLINK;
219 ctrlp->result.data = ctrlp;
220 squidaio_unlink(path, &ctrlp->result);
221 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
222 } /* aioUnlink */
223
224 int
225 aioQueueSize(void)
226 {
227 return DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->inUseCount();
228 }