3 * $Id: async_io.cc,v 1.3 2007/04/12 23:51:57 wessels Exp $
5 * DEBUG: section 32 Asynchronous Disk I/O
6 * AUTHOR: Pete Bentley <pete@demon.net>
7 * AUTHOR: Stewart Forster <slf@connect.com.au>
9 * SQUID Web Proxy Cache http://www.squid-cache.org/
10 * ----------------------------------------------------------
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.
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.
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.
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.
38 #include "DiskThreads.h"
41 #include "DiskThreadsIOStrategy.h"
44 AIOCounts squidaio_counts
;
46 typedef struct squidaio_unlinkq_t
50 struct squidaio_unlinkq_t
*next
;
58 aioOpen(const char *path
, int oflag
, mode_t mode
, AIOCB
* callback
, void *callback_data
)
60 squidaio_ctrl_t
*ctrlp
;
62 assert(DiskThreadsIOStrategy::Instance
.initialised
);
63 squidaio_counts
.open_start
++;
64 ctrlp
= (squidaio_ctrl_t
*)DiskThreadsIOStrategy::Instance
.squidaio_ctrl_pool
->alloc();
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
);
78 squidaio_ctrl_t
*ctrlp
;
80 assert(DiskThreadsIOStrategy::Instance
.initialised
);
81 squidaio_counts
.close_start
++;
83 ctrlp
= (squidaio_ctrl_t
*)DiskThreadsIOStrategy::Instance
.squidaio_ctrl_pool
->alloc();
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
);
97 squidaio_ctrl_t
*ctrlp
;
100 assert(DiskThreadsIOStrategy::Instance
.initialised
);
101 squidaio_counts
.cancel
++;
103 for (m
= used_list
.head
; m
; m
= next
) {
105 ctrlp
= (squidaio_ctrl_t
*)m
->data
;
110 squidaio_cancel(&ctrlp
->result
);
112 if (ctrlp
->done_handler
) {
113 AIOCB
*callback
= ctrlp
->done_handler
;
115 ctrlp
->done_handler
= NULL
;
116 debug(32, 1) ("this be aioCancel. Danger ahead!\n");
118 if (cbdataReferenceValidDone(ctrlp
->done_handler_data
, &cbdata
))
119 callback(fd
, cbdata
, NULL
, -2, -2);
121 /* free data if requested to aioWrite() */
122 if (ctrlp
->free_func
)
123 ctrlp
->free_func(ctrlp
->bufp
);
125 /* free temporary read buffer */
126 if (ctrlp
->operation
== _AIO_READ
)
127 squidaio_xfree(ctrlp
->bufp
, ctrlp
->len
);
130 dlinkDelete(m
, &used_list
);
131 DiskThreadsIOStrategy::Instance
.squidaio_ctrl_pool
->free(ctrlp
);
137 aioWrite(int fd
, int offset
, char *bufp
, int len
, AIOCB
* callback
, void *callback_data
, FREE
* free_func
)
139 squidaio_ctrl_t
*ctrlp
;
142 assert(DiskThreadsIOStrategy::Instance
.initialised
);
143 squidaio_counts
.write_start
++;
144 ctrlp
= (squidaio_ctrl_t
*)DiskThreadsIOStrategy::Instance
.squidaio_ctrl_pool
->alloc();
146 ctrlp
->done_handler
= callback
;
147 ctrlp
->done_handler_data
= cbdataReference(callback_data
);
148 ctrlp
->operation
= _AIO_WRITE
;
150 ctrlp
->free_func
= free_func
;
159 ctrlp
->result
.data
= ctrlp
;
160 squidaio_write(fd
, bufp
, len
, offset
, seekmode
, &ctrlp
->result
);
161 dlinkAdd(ctrlp
, &ctrlp
->node
, &used_list
);
166 aioRead(int fd
, int offset
, int len
, AIOCB
* callback
, void *callback_data
)
168 squidaio_ctrl_t
*ctrlp
;
171 assert(DiskThreadsIOStrategy::Instance
.initialised
);
172 squidaio_counts
.read_start
++;
173 ctrlp
= (squidaio_ctrl_t
*)DiskThreadsIOStrategy::Instance
.squidaio_ctrl_pool
->alloc();
175 ctrlp
->done_handler
= callback
;
176 ctrlp
->done_handler_data
= cbdataReference(callback_data
);
177 ctrlp
->operation
= _AIO_READ
;
179 ctrlp
->bufp
= (char *)squidaio_xmalloc(len
);
188 ctrlp
->result
.data
= ctrlp
;
189 squidaio_read(fd
, ctrlp
->bufp
, len
, offset
, seekmode
, &ctrlp
->result
);
190 dlinkAdd(ctrlp
, &ctrlp
->node
, &used_list
);
196 aioStat(char *path
, struct stat
*sb
, AIOCB
* callback
, void *callback_data
)
198 squidaio_ctrl_t
*ctrlp
;
200 assert(DiskThreadsIOStrategy::Instance
.initialised
);
201 squidaio_counts
.stat_start
++;
202 ctrlp
= (squidaio_ctrl_t
*)DiskThreadsIOStrategy::Instance
.squidaio_ctrl_pool
->alloc();
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
);
214 aioUnlink(const char *path
, AIOCB
* callback
, void *callback_data
)
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();
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
);
232 return DiskThreadsIOStrategy::Instance
.squidaio_ctrl_pool
->inUseCount();