IoCallback.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 #include "squid.h"
10 #include "ClientInfo.h"
11 #include "comm/Connection.h"
12 #include "comm/IoCallback.h"
13 #include "comm/Loops.h"
14 #include "comm/Write.h"
15 #include "CommCalls.h"
16 #include "fde.h"
17 #include "globals.h"
18 
20 
21 void
23 {
24  // XXX: convert this to a std::map<> ?
25  iocb_table = static_cast<CbEntry*>(xcalloc(Squid_MaxFD, sizeof(CbEntry)));
26  for (int pos = 0; pos < Squid_MaxFD; ++pos) {
27  iocb_table[pos].fd = pos;
30  }
31 }
32 
33 void
35 {
36  // release any Comm::Connections being held.
37  for (int pos = 0; pos < Squid_MaxFD; ++pos) {
38  iocb_table[pos].readcb.conn = nullptr;
39  iocb_table[pos].writecb.conn = nullptr;
40  }
42 }
43 
54 void
56 {
57  assert(!active());
58  assert(type == t);
59  assert(cb != nullptr);
60 
61  callback = cb;
62  buf = b;
63  freefunc = f;
64  size = sz;
65  offset = 0;
66 }
67 
68 void
70 {
71 #if USE_DELAY_POOLS
72  if (BandwidthBucket *bucket = BandwidthBucket::SelectBucket(&fd_table[conn->fd])) {
73  bucket->scheduleWrite(this);
74  return;
75  }
76 #endif
77 
78  SetSelect(conn->fd, COMM_SELECT_WRITE, Comm::HandleWrite, this, 0);
79 }
80 
81 void
82 Comm::IoCallback::cancel(const char *reason)
83 {
84  if (!active())
85  return;
86 
87  callback->cancel(reason);
88  callback = nullptr;
89  reset();
90 }
91 
92 void
94 {
95  conn = nullptr;
96  if (freefunc) {
97  freefunc(buf);
98  buf = nullptr;
99  freefunc = nullptr;
100  }
101  xerrno = 0;
102 
103 #if USE_DELAY_POOLS
104  quotaQueueReserv = 0;
105 #endif
106 }
107 
108 // Schedule the callback call and clear the callback
109 void
111 {
112  debugs(5, 3, "called for " << conn << " (" << code << ", " << xerrn << ")");
113  assert(active());
114 
115  /* free data */
116  if (freefunc && buf) {
117  freefunc(buf);
118  buf = nullptr;
119  freefunc = nullptr;
120  }
121 
122  if (callback != nullptr) {
123  typedef CommIoCbParams Params;
124  Params &params = GetCommParams<Params>(callback);
125  if (conn != nullptr) params.fd = conn->fd; // for legacy write handlers...
126  params.conn = conn;
127  params.buf = buf;
128  params.size = offset;
129  params.flag = code;
130  params.xerrno = xerrn;
131  ScheduleCallHere(callback);
132  callback = nullptr;
133  }
134 
135  /* Reset for next round. */
136  reset();
137 }
138 
void * xcalloc(size_t n, size_t sz)
Definition: xalloc.cc:71
IoCallback readcb
Definition: IoCallback.h:67
void FREE(void *)
Definition: forward.h:37
@ IOCB_READ
Definition: IoCallback.h:24
bool active() const
Definition: IoCallback.h:45
#define ScheduleCallHere(call)
Definition: AsyncCall.h:166
void setCallback(iocb_type type, AsyncCall::Pointer &cb, char *buf, FREE *func, int sz)
Definition: IoCallback.cc:55
IoCallback writecb
Definition: IoCallback.h:68
AsyncCall::Pointer callback
Definition: IoCallback.h:34
Comm::ConnectionPointer conn
Definition: IoCallback.h:33
@ IOCB_WRITE
Definition: IoCallback.h:25
#define safe_free(x)
Definition: xalloc.h:73
Base class for Squid-to-client bandwidth limiting.
#define assert(EX)
Definition: assert.h:17
static BandwidthBucket * SelectBucket(fde *f)
void cancel(const char *reason)
Actively cancel the given callback.
Definition: IoCallback.cc:82
iocb_type
Type of IO callbacks the Comm layer deals with.
Definition: IoCallback.h:22
void selectOrQueueWrite()
called when fd needs to write but may need to wait in line for its quota
Definition: IoCallback.cc:69
int Squid_MaxFD
int code
Definition: smb-errors.c:145
Flag
Definition: Flag.h:15
#define fd_table
Definition: fde.h:189
void finish(Comm::Flag code, int xerrn)
finish the IO operation immediately and schedule the callback with the current state.
Definition: IoCallback.cc:110
iocb_type type
Definition: IoCallback.h:32
void SetSelect(int, unsigned int, PF *, void *, time_t)
Mark an FD to be watched for its IO status.
Definition: ModDevPoll.cc:220
void CallbackTableDestruct()
Definition: IoCallback.cc:34
PF HandleWrite
Definition: forward.h:33
void CallbackTableInit()
Definition: IoCallback.cc:22
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
#define COMM_SELECT_WRITE
Definition: defines.h:25
CbEntry * iocb_table
Definition: IoCallback.cc:19

 

Introduction

Documentation

Support

Miscellaneous