]> git.ipfire.org Git - thirdparty/squid.git/blob - src/DiskIO/DiskThreads/async_io.cc
The --enable-truncate and USE_TRUNCATE code has been removed.
[thirdparty/squid.git] / src / DiskIO / DiskThreads / async_io.cc
1
2 /*
3 * $Id: async_io.cc,v 1.3 2007/04/12 23:51:57 wessels Exp $
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 {
48 char *path;
49
50 struct squidaio_unlinkq_t *next;
51 }
52
53 squidaio_unlinkq_t;
54
55 dlink_list used_list;
56
57 void
58 aioOpen(const char *path, int oflag, mode_t mode, AIOCB * callback, void *callback_data)
59 {
60 squidaio_ctrl_t *ctrlp;
61
62 assert(DiskThreadsIOStrategy::Instance.initialised);
63 squidaio_counts.open_start++;
64 ctrlp = (squidaio_ctrl_t *)DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->alloc();
65 ctrlp->fd = -2;
66 ctrlp->done_handler = callback;
67 ctrlp->done_handler_data = cbdataReference(callback_data);
68 ctrlp->operation = _AIO_OPEN;
69 ctrlp->result.data = ctrlp;
70 squidaio_open(path, oflag, mode, &ctrlp->result);
71 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
72 return;
73 }
74
75 void
76 aioClose(int fd)
77 {
78 squidaio_ctrl_t *ctrlp;
79
80 assert(DiskThreadsIOStrategy::Instance.initialised);
81 squidaio_counts.close_start++;
82 aioCancel(fd);
83 ctrlp = (squidaio_ctrl_t *)DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->alloc();
84 ctrlp->fd = fd;
85 ctrlp->done_handler = NULL;
86 ctrlp->done_handler_data = NULL;
87 ctrlp->operation = _AIO_CLOSE;
88 ctrlp->result.data = ctrlp;
89 squidaio_close(fd, &ctrlp->result);
90 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
91 return;
92 }
93
94 void
95 aioCancel(int fd)
96 {
97 squidaio_ctrl_t *ctrlp;
98 dlink_node *m, *next;
99
100 assert(DiskThreadsIOStrategy::Instance.initialised);
101 squidaio_counts.cancel++;
102
103 for (m = used_list.head; m; m = next) {
104 next = m->next;
105 ctrlp = (squidaio_ctrl_t *)m->data;
106
107 if (ctrlp->fd != fd)
108 continue;
109
110 squidaio_cancel(&ctrlp->result);
111
112 if (ctrlp->done_handler) {
113 AIOCB *callback = ctrlp->done_handler;
114 void *cbdata;
115 ctrlp->done_handler = NULL;
116 debug(32, 1) ("this be aioCancel. Danger ahead!\n");
117
118 if (cbdataReferenceValidDone(ctrlp->done_handler_data, &cbdata))
119 callback(fd, cbdata, NULL, -2, -2);
120
121 /* free data if requested to aioWrite() */
122 if (ctrlp->free_func)
123 ctrlp->free_func(ctrlp->bufp);
124
125 /* free temporary read buffer */
126 if (ctrlp->operation == _AIO_READ)
127 squidaio_xfree(ctrlp->bufp, ctrlp->len);
128 }
129
130 dlinkDelete(m, &used_list);
131 DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->free(ctrlp);
132 }
133 }
134
135
136 void
137 aioWrite(int fd, int offset, char *bufp, int len, AIOCB * callback, void *callback_data, FREE * free_func)
138 {
139 squidaio_ctrl_t *ctrlp;
140 int seekmode;
141
142 assert(DiskThreadsIOStrategy::Instance.initialised);
143 squidaio_counts.write_start++;
144 ctrlp = (squidaio_ctrl_t *)DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->alloc();
145 ctrlp->fd = fd;
146 ctrlp->done_handler = callback;
147 ctrlp->done_handler_data = cbdataReference(callback_data);
148 ctrlp->operation = _AIO_WRITE;
149 ctrlp->bufp = bufp;
150 ctrlp->free_func = free_func;
151
152 if (offset >= 0)
153 seekmode = SEEK_SET;
154 else {
155 seekmode = SEEK_END;
156 offset = 0;
157 }
158
159 ctrlp->result.data = ctrlp;
160 squidaio_write(fd, bufp, len, offset, seekmode, &ctrlp->result);
161 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
162 } /* aioWrite */
163
164
165 void
166 aioRead(int fd, int offset, int len, AIOCB * callback, void *callback_data)
167 {
168 squidaio_ctrl_t *ctrlp;
169 int seekmode;
170
171 assert(DiskThreadsIOStrategy::Instance.initialised);
172 squidaio_counts.read_start++;
173 ctrlp = (squidaio_ctrl_t *)DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->alloc();
174 ctrlp->fd = fd;
175 ctrlp->done_handler = callback;
176 ctrlp->done_handler_data = cbdataReference(callback_data);
177 ctrlp->operation = _AIO_READ;
178 ctrlp->len = len;
179 ctrlp->bufp = (char *)squidaio_xmalloc(len);
180
181 if (offset >= 0)
182 seekmode = SEEK_SET;
183 else {
184 seekmode = SEEK_CUR;
185 offset = 0;
186 }
187
188 ctrlp->result.data = ctrlp;
189 squidaio_read(fd, ctrlp->bufp, len, offset, seekmode, &ctrlp->result);
190 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
191 return;
192 } /* aioRead */
193
194 void
195
196 aioStat(char *path, struct stat *sb, AIOCB * callback, void *callback_data)
197 {
198 squidaio_ctrl_t *ctrlp;
199
200 assert(DiskThreadsIOStrategy::Instance.initialised);
201 squidaio_counts.stat_start++;
202 ctrlp = (squidaio_ctrl_t *)DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->alloc();
203 ctrlp->fd = -2;
204 ctrlp->done_handler = callback;
205 ctrlp->done_handler_data = cbdataReference(callback_data);
206 ctrlp->operation = _AIO_STAT;
207 ctrlp->result.data = ctrlp;
208 squidaio_stat(path, sb, &ctrlp->result);
209 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
210 return;
211 } /* aioStat */
212
213 void
214 aioUnlink(const char *path, AIOCB * callback, void *callback_data)
215 {
216 squidaio_ctrl_t *ctrlp;
217 assert(DiskThreadsIOStrategy::Instance.initialised);
218 squidaio_counts.unlink_start++;
219 ctrlp = (squidaio_ctrl_t *)DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->alloc();
220 ctrlp->fd = -2;
221 ctrlp->done_handler = callback;
222 ctrlp->done_handler_data = cbdataReference(callback_data);
223 ctrlp->operation = _AIO_UNLINK;
224 ctrlp->result.data = ctrlp;
225 squidaio_unlink(path, &ctrlp->result);
226 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
227 } /* aioUnlink */
228
229 int
230 aioQueueSize(void)
231 {
232 return DiskThreadsIOStrategy::Instance.squidaio_ctrl_pool->inUseCount();
233 }