]> git.ipfire.org Git - thirdparty/squid.git/blame - src/DiskIO/DiskThreads/async_io.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / DiskIO / DiskThreads / async_io.cc
CommitLineData
cd748f27 1/*
4ac4a490 2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
cd748f27 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
cd748f27 7 */
8
bbc27441
AJ
9/* DEBUG: section 32 Asynchronous Disk I/O */
10
582c2af2 11#include "squid.h"
b9ae18aa 12#include "DiskThreads.h"
b9ae18aa 13#include "DiskThreadsIOStrategy.h"
602d9612 14#include "fde.h"
b9ae18aa 15#include "Generic.h"
602d9612 16#include "Store.h"
cd748f27 17
b9ae18aa 18AIOCounts squidaio_counts;
cd748f27 19
26ac0430 20typedef struct squidaio_unlinkq_t {
cd748f27 21 char *path;
62e76326 22
c04d4f40 23 struct squidaio_unlinkq_t *next;
2fadd50d 24} squidaio_unlinkq_t;
cd748f27 25
b9ae18aa 26dlink_list used_list;
cd748f27 27
28void
29aioOpen(const char *path, int oflag, mode_t mode, AIOCB * callback, void *callback_data)
30{
c04d4f40 31 squidaio_ctrl_t *ctrlp;
cd748f27 32
b9ae18aa 33 assert(DiskThreadsIOStrategy::Instance.initialised);
cb4185f1 34 ++squidaio_counts.open_start;
91ea1911 35 ctrlp = new squidaio_ctrl_t;
cd748f27 36 ctrlp->fd = -2;
37 ctrlp->done_handler = callback;
fa80a8ef 38 ctrlp->done_handler_data = cbdataReference(callback_data);
cd748f27 39 ctrlp->operation = _AIO_OPEN;
55f0e6f7 40 ctrlp->result.data = ctrlp;
c04d4f40 41 squidaio_open(path, oflag, mode, &ctrlp->result);
55f0e6f7 42 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
cd748f27 43 return;
44}
45
46void
47aioClose(int fd)
48{
c04d4f40 49 squidaio_ctrl_t *ctrlp;
cd748f27 50
b9ae18aa 51 assert(DiskThreadsIOStrategy::Instance.initialised);
cb4185f1 52 ++squidaio_counts.close_start;
cd748f27 53 aioCancel(fd);
91ea1911 54 ctrlp = new squidaio_ctrl_t;
cd748f27 55 ctrlp->fd = fd;
56 ctrlp->done_handler = NULL;
57 ctrlp->done_handler_data = NULL;
58 ctrlp->operation = _AIO_CLOSE;
55f0e6f7 59 ctrlp->result.data = ctrlp;
c04d4f40 60 squidaio_close(fd, &ctrlp->result);
55f0e6f7 61 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
cd748f27 62 return;
63}
64
65void
66aioCancel(int fd)
67{
211f1d0b 68 squidaio_ctrl_t *ctrlp;
55f0e6f7 69 dlink_node *m, *next;
cd748f27 70
b9ae18aa 71 assert(DiskThreadsIOStrategy::Instance.initialised);
cb4185f1 72 ++squidaio_counts.cancel;
62e76326 73
55f0e6f7 74 for (m = used_list.head; m; m = next) {
62e76326 75 next = m->next;
76 ctrlp = (squidaio_ctrl_t *)m->data;
77
78 if (ctrlp->fd != fd)
79 continue;
80
81 squidaio_cancel(&ctrlp->result);
82
83 if (ctrlp->done_handler) {
84 AIOCB *callback = ctrlp->done_handler;
85 void *cbdata;
86 ctrlp->done_handler = NULL;
e0236918 87 debugs(32, DBG_IMPORTANT, "this be aioCancel. Danger ahead!");
62e76326 88
89 if (cbdataReferenceValidDone(ctrlp->done_handler_data, &cbdata))
90 callback(fd, cbdata, NULL, -2, -2);
91
92 /* free data if requested to aioWrite() */
93 if (ctrlp->free_func)
94 ctrlp->free_func(ctrlp->bufp);
95
96 /* free temporary read buffer */
97 if (ctrlp->operation == _AIO_READ)
98 squidaio_xfree(ctrlp->bufp, ctrlp->len);
99 }
100
101 dlinkDelete(m, &used_list);
91ea1911 102 delete ctrlp;
cd748f27 103 }
104}
105
cd748f27 106void
ee139403 107aioWrite(int fd, off_t offset, char *bufp, size_t len, AIOCB * callback, void *callback_data, FREE * free_func)
cd748f27 108{
c04d4f40 109 squidaio_ctrl_t *ctrlp;
cd748f27 110 int seekmode;
111
b9ae18aa 112 assert(DiskThreadsIOStrategy::Instance.initialised);
cb4185f1 113 ++squidaio_counts.write_start;
91ea1911 114 ctrlp = new squidaio_ctrl_t;
cd748f27 115 ctrlp->fd = fd;
116 ctrlp->done_handler = callback;
fa80a8ef 117 ctrlp->done_handler_data = cbdataReference(callback_data);
cd748f27 118 ctrlp->operation = _AIO_WRITE;
55f0e6f7 119 ctrlp->bufp = bufp;
120 ctrlp->free_func = free_func;
62e76326 121
cd748f27 122 if (offset >= 0)
62e76326 123 seekmode = SEEK_SET;
cd748f27 124 else {
62e76326 125 seekmode = SEEK_END;
126 offset = 0;
cd748f27 127 }
62e76326 128
55f0e6f7 129 ctrlp->result.data = ctrlp;
c04d4f40 130 squidaio_write(fd, bufp, len, offset, seekmode, &ctrlp->result);
55f0e6f7 131 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
f53969cc 132} /* aioWrite */
cd748f27 133
cd748f27 134void
ee139403 135aioRead(int fd, off_t offset, size_t len, AIOCB * callback, void *callback_data)
cd748f27 136{
c04d4f40 137 squidaio_ctrl_t *ctrlp;
cd748f27 138 int seekmode;
139
b9ae18aa 140 assert(DiskThreadsIOStrategy::Instance.initialised);
cb4185f1 141 ++squidaio_counts.read_start;
91ea1911 142 ctrlp = new squidaio_ctrl_t;
cd748f27 143 ctrlp->fd = fd;
144 ctrlp->done_handler = callback;
fa80a8ef 145 ctrlp->done_handler_data = cbdataReference(callback_data);
cd748f27 146 ctrlp->operation = _AIO_READ;
211f1d0b 147 ctrlp->len = len;
d74b7307 148 ctrlp->bufp = (char *)squidaio_xmalloc(len);
62e76326 149
cd748f27 150 if (offset >= 0)
62e76326 151 seekmode = SEEK_SET;
cd748f27 152 else {
62e76326 153 seekmode = SEEK_CUR;
154 offset = 0;
cd748f27 155 }
62e76326 156
55f0e6f7 157 ctrlp->result.data = ctrlp;
211f1d0b 158 squidaio_read(fd, ctrlp->bufp, len, offset, seekmode, &ctrlp->result);
55f0e6f7 159 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
cd748f27 160 return;
f53969cc 161} /* aioRead */
cd748f27 162
163void
62e76326 164
cd748f27 165aioStat(char *path, struct stat *sb, AIOCB * callback, void *callback_data)
166{
c04d4f40 167 squidaio_ctrl_t *ctrlp;
cd748f27 168
b9ae18aa 169 assert(DiskThreadsIOStrategy::Instance.initialised);
cb4185f1 170 ++squidaio_counts.stat_start;
91ea1911 171 ctrlp = new squidaio_ctrl_t;
cd748f27 172 ctrlp->fd = -2;
173 ctrlp->done_handler = callback;
fa80a8ef 174 ctrlp->done_handler_data = cbdataReference(callback_data);
cd748f27 175 ctrlp->operation = _AIO_STAT;
55f0e6f7 176 ctrlp->result.data = ctrlp;
c04d4f40 177 squidaio_stat(path, sb, &ctrlp->result);
55f0e6f7 178 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
cd748f27 179 return;
f53969cc 180} /* aioStat */
cd748f27 181
15a47d1d 182void
efa3acd1 183aioUnlink(const char *path, AIOCB * callback, void *callback_data)
15a47d1d 184{
c04d4f40 185 squidaio_ctrl_t *ctrlp;
b9ae18aa 186 assert(DiskThreadsIOStrategy::Instance.initialised);
cb4185f1 187 ++squidaio_counts.unlink_start;
91ea1911 188 ctrlp = new squidaio_ctrl_t;
15a47d1d 189 ctrlp->fd = -2;
190 ctrlp->done_handler = callback;
fa80a8ef 191 ctrlp->done_handler_data = cbdataReference(callback_data);
efa3acd1 192 ctrlp->operation = _AIO_UNLINK;
55f0e6f7 193 ctrlp->result.data = ctrlp;
efa3acd1 194 squidaio_unlink(path, &ctrlp->result);
55f0e6f7 195 dlinkAdd(ctrlp, &ctrlp->node, &used_list);
f53969cc 196} /* aioUnlink */
efa3acd1 197
cd748f27 198int
199aioQueueSize(void)
200{
91ea1911 201 return squidaio_ctrl_t::UseCount();
cd748f27 202}
f53969cc 203