XRootD
Loading...
Searching...
No Matches
XrdPollPoll Class Reference

#include <XrdPollPoll.hh>

+ Inheritance diagram for XrdPollPoll:
+ Collaboration diagram for XrdPollPoll:

Public Member Functions

 XrdPollPoll (struct pollfd *pp, int numfd)
 
 ~XrdPollPoll ()
 
void Disable (XrdPollInfo &pInfo, const char *etxt=0)
 
int Enable (XrdPollInfo &pInfo)
 
void Start (XrdSysSemaphore *syncp, int &rc)
 

Protected Member Functions

void doDetach (int pti)
 
void Exclude (XrdPollInfo &pInfo)
 
int Include (XrdPollInfo &pInfo)
 

Detailed Description

Definition at line 38 of file XrdPollPoll.hh.

Constructor & Destructor Documentation

◆ XrdPollPoll()

XrdPollPoll::XrdPollPoll ( struct pollfd *  pp,
int  numfd 
)

Definition at line 67 of file XrdPollPoll.icc.

68{
69
70// Initialize the standard stuff
71//
72 PollTab = pp;
73 PollTNum= 0;
74 PollQ = 0;
75 maxent = numfd;
76}

◆ ~XrdPollPoll()

XrdPollPoll::~XrdPollPoll ( )

Definition at line 82 of file XrdPollPoll.icc.

83{
84 if (PollTab) free(PollTab);
85}

Member Function Documentation

◆ Disable()

void XrdPollPoll::Disable ( XrdPollInfo pInfo,
const char *  etxt = 0 
)
virtual

Implements XrdPoll.

Definition at line 135 of file XrdPollPoll.icc.

136{
137 XrdSysSemaphore mySem(0);
138 PipeData cmdbuff[2];
139 int myerrno = 0;
140
141// Check if this link is in the pollQ. If so, remove it.
142//
143 if (pInfo.inQ) dqLink(&pInfo);
144
145// Simply return if the link is already disabled
146//
147 if (!pInfo.isEnabled) return;
148
149// Trace this event
150//
151 TRACEI(POLL, "Poller " <<PID <<" async disabling link FD " <<pInfo.FD);
152
153// Send a disable request to the poller thread handling this link. We need to
154// wait until the operation is actually completed before returning.
155//
156 memset(&cmdbuff, 0, sizeof(cmdbuff));
157 cmdbuff[0].req = PipeData::DiFD;
158 cmdbuff[0].Parms.Arg.fd = pInfo.FD;
159 cmdbuff[0].Parms.Arg.ent = pInfo.PollEnt - PollTab;
160 cmdbuff[1].req = PipeData::Post;
161 cmdbuff[1].Parms.theSem = &mySem;
162 PollPipe.Lock();
163 if (write(CmdFD, &cmdbuff, sizeof(cmdbuff)) < 0) myerrno = errno;
165
166// Verify that all went well and if termination wanted, terminate the link
167//
168 if (myerrno) Log.Emsg("Poll", myerrno, "disable link", pInfo.Link.ID);
169 else {mySem.Wait();
170 if (etxt && Finish(pInfo, etxt))
171 Sched.Schedule((XrdJob *)&pInfo.Link);
172 }
173}
#define write(a, b, c)
Definition XrdPosix.hh:115
#define TRACEI(act, x)
Definition XrdTrace.hh:66
struct pollfd * PollEnt
XrdLink & Link
int PID
Definition XrdPoll.hh:82
XrdSysMutex PollPipe
Definition XrdPoll.hh:115
static int Finish(XrdPollInfo &pInfo, const char *etxt=0)
Definition XrdPoll.cc:204
int CmdFD
Definition XrdPoll.hh:117
void Schedule(XrdJob *jp)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
XrdSysError Log
Definition XrdConfig.cc:113
XrdScheduler Sched
Definition XrdLinkCtl.cc:54

References XrdPoll::CmdFD, XrdSysError::Emsg(), XrdPollInfo::FD, XrdPoll::Finish(), XrdLink::ID, XrdPollInfo::inQ, XrdPollInfo::isEnabled, XrdPollInfo::Link, XrdSysMutex::Lock(), XrdPoll::PipeData::Parms, XrdPoll::PID, XrdPollInfo::PollEnt, XrdPoll::PollPipe, XrdPoll::PipeData::req, XrdScheduler::Schedule(), TRACEI, XrdSysMutex::UnLock(), XrdSysSemaphore::Wait(), and write.

Referenced by Exclude().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ doDetach()

void XrdPollPoll::doDetach ( int  pti)
protected

Definition at line 358 of file XrdPollPoll.icc.

359{
360 int lastent;
361
362// Get some starting values
363//
364 if ((lastent = PollTNum-1) < 0)
365 {Log.Emsg("Poll","Underflow during detach"); abort();}
366
367 if (pti == lastent)
368 do {PollTNum--;} while(PollTNum && PollTab[PollTNum-1].fd == -1);
369}

References XrdSysError::Emsg().

+ Here is the call graph for this function:

◆ Enable()

int XrdPollPoll::Enable ( XrdPollInfo pInfo)
virtual

Implements XrdPoll.

Definition at line 179 of file XrdPollPoll.icc.

180{
181 PipeData cmdbuff;
182 int myerrno = 0;
183
184// Simply return if the link is already enabled
185//
186 if (pInfo.isEnabled) return 1;
187
188// Add this link element to the queue
189//
190 PollMutex.Lock();
191 pInfo.Next = PollQ;
192 PollQ = &pInfo;
193 pInfo.inQ = true;
194 PollMutex.UnLock();
195
196// Send an enable request to the poller thread handling this link
197//
198 TRACEI(POLL, "sending poller " <<PID <<" enable for link " <<pInfo.FD);
199 cmdbuff.req = PipeData::EnFD;
200 cmdbuff.Parms.Arg.fd = pInfo.FD;
201 cmdbuff.Parms.Arg.ent = pInfo.PollEnt - PollTab;
202 PollPipe.Lock();
203 if (write(CmdFD, &cmdbuff, sizeof(cmdbuff)) < 0) myerrno = errno;
205
206// Verify that all went well. Note that the link stays in the pollQ.
207//
208 if (myerrno)
209 {Log.Emsg("Poll", myerrno, "enable link", pInfo.Link.ID); return 0;}
210
211// All done
212//
213 return 1;
214}
XrdPollInfo * Next

References XrdPoll::CmdFD, XrdSysError::Emsg(), XrdPollInfo::FD, XrdLink::ID, XrdPollInfo::inQ, XrdPollInfo::isEnabled, XrdPollInfo::Link, XrdSysMutex::Lock(), XrdPollInfo::Next, XrdPoll::PipeData::Parms, XrdPoll::PID, XrdPollInfo::PollEnt, XrdPoll::PollPipe, XrdPoll::PipeData::req, TRACEI, XrdSysMutex::UnLock(), and write.

+ Here is the call graph for this function:

◆ Exclude()

void XrdPollPoll::Exclude ( XrdPollInfo pInfo)
protectedvirtual

Implements XrdPoll.

Definition at line 220 of file XrdPollPoll.icc.

221{
222 XrdSysSemaphore mySem(0);
223 PipeData cmdbuff[2];
224 int myerrno = 0;
225
226// Make sure this link is not enabled
227//
228 if (pInfo.isEnabled)
229 {Log.Emsg("Poll", "Detach of enabled link", pInfo.Link.ID);
230 Disable(pInfo);
231 }
232 else if (pInfo.inQ) dqLink(&pInfo);
233
234// Send a deatch request to the poller thread handling this link
235//
236 TRACEI(POLL, "sending poller " <<PID <<" detach for link " <<pInfo.FD);
237 cmdbuff[0].req = PipeData::RmFD;
238 cmdbuff[0].Parms.Arg.fd = pInfo.FD;
239 cmdbuff[0].Parms.Arg.ent = pInfo.PollEnt - PollTab;
240 cmdbuff[1].req = PipeData::Post;
241 cmdbuff[1].Parms.theSem = &mySem;
242 PollPipe.Lock();
243 if (write(CmdFD, &cmdbuff, sizeof(cmdbuff)) < 0) myerrno = errno;
245
246// Verify that all went well and if termination wanted, terminate the link
247//
248 if (myerrno) Log.Emsg("Poll", myerrno, "detach link", pInfo.Link.ID);
249 else mySem.Wait();
250}
void Disable(XrdPollInfo &pInfo, const char *etxt=0)

References XrdPoll::CmdFD, Disable(), XrdSysError::Emsg(), XrdPollInfo::FD, XrdLink::ID, XrdPollInfo::inQ, XrdPollInfo::isEnabled, XrdPollInfo::Link, XrdSysMutex::Lock(), XrdPoll::PipeData::Parms, XrdPoll::PID, XrdPollInfo::PollEnt, XrdPoll::PollPipe, XrdPoll::PipeData::req, TRACEI, XrdSysMutex::UnLock(), XrdSysSemaphore::Wait(), and write.

+ Here is the call graph for this function:

◆ Include()

int XrdPollPoll::Include ( XrdPollInfo pInfo)
protectedvirtual

Implements XrdPoll.

Definition at line 91 of file XrdPollPoll.icc.

92{
93 struct pollfd *pfd;
94 int ptnum;
95
96// Lock down the poll data structure
97//
98 PollMutex.Lock();
99
100// Get the next entry to be used
101//
102 ptnum = 0;
103 while((ptnum < PollTNum) && (PollTab[ptnum].fd != -1)) ptnum++;
104
105// Make sure we have enough table entries to add this link
106//
107 if (ptnum > maxent)
108 {Log.Emsg("Attach","Attach",pInfo.Link.ID,"failed; poll table overflow.");
109 PollMutex.UnLock();
110 return 0;
111 }
112
113// Initialize the polltable entry
114//
115 pfd = &(PollTab[ptnum]);
116 pfd->fd = -pInfo.FD;
117 pfd->events = POLLIN | POLLRDNORM;
118 pfd->revents = 0;
119
120// Record relevant information in the link
121//
122 pInfo.PollEnt = pfd;
123 if (ptnum == PollTNum) PollTNum++;
124
125// All done
126//
127 PollMutex.UnLock();
128 return 1;
129}

References XrdSysError::Emsg(), XrdPollInfo::FD, XrdLink::ID, XrdPollInfo::Link, XrdSysMutex::Lock(), XrdPollInfo::PollEnt, and XrdSysMutex::UnLock().

+ Here is the call graph for this function:

◆ Start()

void XrdPollPoll::Start ( XrdSysSemaphore syncp,
int &  rc 
)
virtual

Implements XrdPoll.

Definition at line 256 of file XrdPollPoll.icc.

257{
258 int numpolled, num2sched;
259 XrdJob *jfirst, *jlast;
260 XrdPollInfo *plp, *nlp, *pInfo;
261 XrdLink *lp;
262 short pollevents;
263 const short pollOK = POLLIN | POLLRDNORM;
264
265// Set up he first entry in the poll table to be our communications port
266//
267 PollTab[0].fd = ReqFD;
268 PollTab[0].events = pollOK;
269 PollTab[0].revents = 0;
270 PollTNum = 1;
271
272// Signal the caller to continue
273//
274 retcode = 0;
275 syncsem->Post();
276
277// Now do the main poll loop
278//
279 std::vector<struct pollfd> PollTabCopy;
280 do {// Duplicate the polling table so we don't need to hold the PollMutex
281 // while we are sleeping in the poll()
282 PollMutex.Lock();
283 PollTabCopy.resize(PollTNum);
284 memcpy(PollTabCopy.data(), PollTab, sizeof(struct pollfd) * PollTNum);
285 PollMutex.UnLock();
286
287 do {numpolled = poll(PollTabCopy.data(), PollTabCopy.size(), -1);}
288 while(numpolled < 0 && (errno == EAGAIN || errno == EINTR));
289
290 // Check if we had a polling error
291 //
292 if (numpolled < 0)
293 {if (errno != EINTR) Restart(errno);
294 else numInterrupts++;
295 continue;
296 }
297 numEvents += numpolled;
298
299 // Note this thread is the only one that writes directly to the poll
300 // table (everything else is a read). Hence, it's OK to assume that
301 // the table after the poll() is unchanged and we can write back the
302 // revents field.
303 PollMutex.Lock();
304 for (size_t idx=0; idx<PollTabCopy.size(); idx++)
305 PollTab[idx].revents = PollTabCopy[idx].revents;
306
307 // Check out base poll table entry, we can do this without a lock
308 //
309 if (PollTab[0].revents & pollOK)
310 {PollMutex.UnLock();
311 doRequests(numpolled);
312 if (--numpolled <= 0) continue;
313 PollMutex.Lock();
314 }
315
316 // Checkout which links must be dispatched (do this locked)
317 //
318 plp = 0; nlp = PollQ; jfirst = jlast = 0; num2sched = 0;
319 while ((pInfo = nlp) && numpolled > 0)
320 {if ((pollevents = pInfo->PollEnt->revents))
321 {pInfo->PollEnt->fd = -pInfo->PollEnt->fd;
322 if (plp) nlp = plp->Next = pInfo->Next;
323 else nlp = PollQ = pInfo->Next;
324 numpolled--; pInfo->inQ = false;
325 if (!(pollevents & pollOK))
326 Finish(*pInfo, Poll2Text(pollevents));
327 lp = &(pInfo->Link);
328 if (!(pInfo->isEnabled))
329 Log.Emsg("Poll", "Disabled event occurred for", lp->ID);
330 else {pInfo->isEnabled = false;
331 lp->NextJob = jfirst; jfirst = (XrdJob *)lp;
332 if (!jlast) jlast=(XrdJob *)lp;
333 num2sched++;
334 continue;
335 }
336 }
337 plp = pInfo; nlp = pInfo->Next;
338 }
339 if (numpolled) Recover(numpolled);
340 PollMutex.UnLock();
341
342 // Schedule the polled links
343 //
344 if (num2sched == 1) Sched.Schedule(jfirst);
345 else if (num2sched) Sched.Schedule(num2sched, jfirst, jlast);
346 } while(1);
347}
XrdJob * NextJob
Definition XrdJob.hh:46
int numInterrupts
Definition XrdPoll.hh:134
int ReqFD
Definition XrdPoll.hh:118
int numEvents
Definition XrdPoll.hh:133
static char * Poll2Text(short events)
Definition XrdPoll.cc:272

References XrdSysError::Emsg(), XrdPoll::Finish(), XrdLink::ID, XrdPollInfo::inQ, XrdPollInfo::isEnabled, XrdPollInfo::Link, XrdSysMutex::Lock(), XrdPollInfo::Next, XrdJob::NextJob, XrdPoll::numEvents, XrdPoll::numInterrupts, XrdPoll::Poll2Text(), XrdPollInfo::PollEnt, XrdSysSemaphore::Post(), XrdPoll::ReqFD, XrdScheduler::Schedule(), and XrdSysMutex::UnLock().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: