]> git.ipfire.org Git - thirdparty/squid.git/blame - src/DiskIO/IpcIo/IpcIoFile.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / DiskIO / IpcIo / IpcIoFile.h
CommitLineData
254912f3
AR
1#ifndef SQUID_IPC_IOFILE_H
2#define SQUID_IPC_IOFILE_H
3
4#include "base/AsyncCall.h"
5#include "cbdata.h"
6#include "DiskIO/DiskFile.h"
7#include "DiskIO/IORequestor.h"
582c2af2 8#include "SquidString.h"
254912f3 9#include "ipc/forward.h"
8ed94021 10#include "ipc/mem/Page.h"
b2aa0934 11#include <list>
254912f3 12#include <map>
f5591061
DK
13#include <memory>
14
9199139f
AR
15namespace Ipc
16{
f5591061
DK
17class FewToFewBiQueue;
18} // Ipc
254912f3
AR
19
20// TODO: expand to all classes
9199139f
AR
21namespace IpcIo
22{
254912f3
AR
23
24/// what kind of I/O the disker needs to do or have done
25typedef enum { cmdNone, cmdOpen, cmdRead, cmdWrite } Command;
26
254912f3
AR
27} // namespace IpcIo
28
9a51593d 29/// converts DiskIO requests to IPC queue messages
9199139f
AR
30class IpcIoMsg
31{
254912f3 32public:
9a51593d 33 IpcIoMsg();
254912f3
AR
34
35public:
9a51593d 36 unsigned int requestId; ///< unique for requestor; matches request w/ response
254912f3 37
254912f3
AR
38 off_t offset;
39 size_t len;
8ed94021 40 Ipc::Mem::PageId page;
254912f3 41
9a51593d 42 IpcIo::Command command; ///< what disker is supposed to do or did
0a11e039 43 struct timeval start; ///< when the I/O request was converted to IpcIoMsg
254912f3
AR
44
45 int xerrno; ///< I/O error code or zero
46};
47
48class IpcIoPendingRequest;
49
50class IpcIoFile: public DiskFile
51{
52
53public:
54 typedef RefCount<IpcIoFile> Pointer;
55
56 IpcIoFile(char const *aDb);
57 virtual ~IpcIoFile();
58
59 /* DiskFile API */
43ebbac3 60 virtual void configure(const Config &cfg);
254912f3
AR
61 virtual void open(int flags, mode_t mode, RefCount<IORequestor> callback);
62 virtual void create(int flags, mode_t mode, RefCount<IORequestor> callback);
63 virtual void read(ReadRequest *);
64 virtual void write(WriteRequest *);
65 virtual void close();
66 virtual bool error() const;
67 virtual int getFD() const;
68 virtual bool canRead() const;
69 virtual bool canWrite() const;
70 virtual bool ioInProgress() const;
71
9a51593d
DK
72 /// handle open response from coordinator
73 static void HandleOpenResponse(const Ipc::StrandSearchResponse &response);
254912f3 74
9a51593d
DK
75 /// handle queue push notifications from worker or disker
76 static void HandleNotification(const Ipc::TypedMsgHdr &msg);
254912f3 77
43ebbac3
AR
78 DiskFile::Config config; ///< supported configuration options
79
caca86d7
AR
80protected:
81 friend class IpcIoPendingRequest;
9a51593d 82 void openCompleted(const Ipc::StrandSearchResponse *const response);
8ed94021 83 void readCompleted(ReadRequest *readRequest, IpcIoMsg *const response);
9a51593d 84 void writeCompleted(WriteRequest *writeRequest, const IpcIoMsg *const response);
0a11e039 85 bool canWait() const;
caca86d7 86
254912f3 87private:
7a907247
DK
88 void trackPendingRequest(IpcIoPendingRequest *const pending);
89 void push(IpcIoPendingRequest *const pending);
9a51593d
DK
90 IpcIoPendingRequest *dequeueRequest(const unsigned int requestId);
91
92 static void Notify(const int peerId);
254912f3 93
b2aa0934 94 static void OpenTimeout(void *const param);
9a51593d
DK
95 static void CheckTimeouts(void *const param);
96 void checkTimeouts();
97 void scheduleTimeoutCheck();
254912f3 98
f5591061 99 static void HandleResponses(const char *const when);
8ed94021 100 void handleResponse(IpcIoMsg &ipcIo);
9a51593d 101
3d31b6a5 102 static void DiskerHandleMoreRequests(void*);
f5591061 103 static void DiskerHandleRequests();
9a51593d 104 static void DiskerHandleRequest(const int workerId, IpcIoMsg &ipcIo);
df881a0f 105 static bool WaitBeforePop();
caca86d7 106
254912f3
AR
107private:
108 const String dbName; ///< the name of the file we are managing
109 int diskId; ///< the process ID of the disker we talk to
110 RefCount<IORequestor> ioRequestor;
111
254912f3
AR
112 bool error_; ///< whether we have seen at least one I/O error (XXX)
113
9a51593d
DK
114 unsigned int lastRequestId; ///< last requestId used
115
254912f3 116 /// maps requestId to the handleResponse callback
caca86d7 117 typedef std::map<unsigned int, IpcIoPendingRequest*> RequestMap;
9a51593d
DK
118 RequestMap requestMap1; ///< older (or newer) pending requests
119 RequestMap requestMap2; ///< newer (or older) pending requests
120 RequestMap *olderRequests; ///< older requests (map1 or map2)
121 RequestMap *newerRequests; ///< newer requests (map2 or map1)
122 bool timeoutCheckScheduled; ///< we expect a CheckTimeouts() call
254912f3 123
b2aa0934
DK
124 static const double Timeout; ///< timeout value in seconds
125
126 typedef std::list<Pointer> IpcIoFileList;
127 static IpcIoFileList WaitingForOpen; ///< pending open requests
128
129 ///< maps diskerId to IpcIoFile, cleared in destructor
130 typedef std::map<int, IpcIoFile*> IpcIoFilesMap;
131 static IpcIoFilesMap IpcIoFiles;
254912f3 132
f5591061
DK
133 typedef Ipc::FewToFewBiQueue Queue;
134 static std::auto_ptr<Queue> queue; ///< IPC queue
135
3d31b6a5
AR
136 /// whether we are waiting for an event to handle still queued I/O requests
137 static bool DiskerHandleMoreRequestsScheduled;
138
254912f3
AR
139 CBDATA_CLASS2(IpcIoFile);
140};
141
254912f3
AR
142/// keeps original I/O request parameters while disker is handling the request
143class IpcIoPendingRequest
144{
145public:
146 IpcIoPendingRequest(const IpcIoFile::Pointer &aFile);
147
caca86d7 148 /// called when response is received and, with a nil response, on timeouts
8ed94021 149 void completeIo(IpcIoMsg *const response);
caca86d7 150
254912f3 151public:
b2aa0934 152 const IpcIoFile::Pointer file; ///< the file object waiting for the response
254912f3
AR
153 ReadRequest *readRequest; ///< set if this is a read requests
154 WriteRequest *writeRequest; ///< set if this is a write request
155
156private:
157 IpcIoPendingRequest(const IpcIoPendingRequest &d); // not implemented
158 IpcIoPendingRequest &operator =(const IpcIoPendingRequest &d); // ditto
159};
160
254912f3 161#endif /* SQUID_IPC_IOFILE_H */