xrootd
Main Page
Namespaces
Classes
Files
File List
File Members
src
XrdSys
XrdSysIOEvents.hh
Go to the documentation of this file.
1
#ifndef __XRDSYSIOEVENTS_HH__
2
#define __XRDSYSIOEVENTS_HH__
3
/******************************************************************************/
4
/* */
5
/* X r d S y s I O E v e n t s . h h */
6
/* */
7
/* (c) 2012 by the Board of Trustees of the Leland Stanford, Jr., University */
8
/* All Rights Reserved */
9
/* Produced by Andrew Hanushevsky for Stanford University under contract */
10
/* DE-AC02-76-SFO0515 with the Department of Energy */
11
/* */
12
/* This file is part of the XRootD software suite. */
13
/* */
14
/* XRootD is free software: you can redistribute it and/or modify it under */
15
/* the terms of the GNU Lesser General Public License as published by the */
16
/* Free Software Foundation, either version 3 of the License, or (at your */
17
/* option) any later version. */
18
/* */
19
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
20
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
21
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
22
/* License for more details. */
23
/* */
24
/* You should have received a copy of the GNU Lesser General Public License */
25
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
26
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
27
/* */
28
/* The copyright holder's institutional names and contributor's names may not */
29
/* be used to endorse or promote products derived from this software without */
30
/* specific prior written permission of the institution or contributor. */
31
/******************************************************************************/
32
33
#include <poll.h>
34
#include <time.h>
35
#include <sys/types.h>
36
37
#include "
XrdSys/XrdSysPthread.hh
"
38
#include "
XrdSys/XrdSysAtomics.hh
"
39
40
//-----------------------------------------------------------------------------
54
//-----------------------------------------------------------------------------
55
56
namespace
XrdSys
57
{
58
namespace
IOEvents
59
{
60
61
/******************************************************************************/
62
/* C l a s s C a l l B a c k */
63
/******************************************************************************/
64
65
//-----------------------------------------------------------------------------
75
//-----------------------------------------------------------------------------
76
77
class
Channel
;
78
class
CallBack
79
{
80
public
:
81
82
//-----------------------------------------------------------------------------
84
//-----------------------------------------------------------------------------
85
86
enum
EventType
87
{
88
ReadyToRead
= 0x01,
89
ReadTimeOut
= 0x02,
90
ReadyToWrite
= 0x04,
91
WriteTimeOut
= 0x08,
92
ValidEvents
= 0x0f
93
};
94
95
//-----------------------------------------------------------------------------
114
//-----------------------------------------------------------------------------
115
116
virtual
bool
Event
(
Channel
*chP,
void
*cbArg,
int
evFlags) = 0;
117
118
//-----------------------------------------------------------------------------
129
//-----------------------------------------------------------------------------
130
131
virtual
void
Fatal
(
Channel
*chP,
void
*cbArg,
int
eNum,
const
char
*eTxt)
132
{
133
(void)chP; (void)cbArg; (void)eNum; (void)eTxt;
134
};
135
136
//-----------------------------------------------------------------------------
144
//-----------------------------------------------------------------------------
145
146
virtual
void
Stop
(
Channel
*chP,
void
*cbArg) { (void)chP; (void)cbArg;}
147
148
//-----------------------------------------------------------------------------
150
//-----------------------------------------------------------------------------
151
152
CallBack
() {}
153
154
//-----------------------------------------------------------------------------
156
//-----------------------------------------------------------------------------
157
158
virtual
~CallBack
() {}
159
};
160
161
/******************************************************************************/
162
/* C l a s s C h a n n e l */
163
/******************************************************************************/
164
165
//-----------------------------------------------------------------------------
169
//-----------------------------------------------------------------------------
170
171
class
ChannelWait;
172
class
Poller;
173
class
Channel
174
{
175
friend
class
Poller
;
176
public
:
177
178
//-----------------------------------------------------------------------------
184
//-----------------------------------------------------------------------------
185
186
void
Delete
();
187
188
//-----------------------------------------------------------------------------
190
//-----------------------------------------------------------------------------
191
192
enum
EventCode
{
readEvents
= 0x01,
193
writeEvents
= 0x04,
194
rwEvents
= 0x05,
195
errorEvents
= 0x10,
196
stopEvent
= 0x20,
197
allEvents
= 0x35
198
};
199
200
//-----------------------------------------------------------------------------
210
//-----------------------------------------------------------------------------
211
212
bool
Disable
(
int
events,
const
char
**eText=0);
213
214
//-----------------------------------------------------------------------------
246
//-----------------------------------------------------------------------------
247
248
bool
Enable
(
int
events,
int
timeout=0,
const
char
**eText=0);
249
250
//-----------------------------------------------------------------------------
255
//-----------------------------------------------------------------------------
256
257
void
GetCallBack
(
CallBack
**cbP,
void
**cbArg);
258
259
//-----------------------------------------------------------------------------
265
//-----------------------------------------------------------------------------
266
267
inline
int
GetEvents
() {
return
(
chPoller
? static_cast<int>(
chEvents
) : -1);}
268
269
//-----------------------------------------------------------------------------
274
//-----------------------------------------------------------------------------
275
276
inline
int
GetFD
() {
return
chFD
;}
277
278
//-----------------------------------------------------------------------------
285
//-----------------------------------------------------------------------------
286
287
void
SetCallBack
(
CallBack
*cbP,
void
*cbArg=0);
288
289
//-----------------------------------------------------------------------------
298
//-----------------------------------------------------------------------------
299
300
void
SetFD
(
int
fd);
301
302
//-----------------------------------------------------------------------------
317
//-----------------------------------------------------------------------------
318
319
Channel
(
Poller
*pollP,
int
fd,
CallBack
*cbP=0,
void
*cbArg=0);
320
321
private
:
322
323
//-----------------------------------------------------------------------------
325
//-----------------------------------------------------------------------------
326
327
~Channel
() {}
328
329
struct
dlQ
{
Channel
*
next
;
Channel
*
prev
;};
330
331
XrdSysRecMutex
chMutex
;
332
333
dlQ
attList
;
// List of attached channels
334
dlQ
tmoList
;
// List of channels in the timeout queue
335
336
Poller
*
chPoller
;
// The effective poller
337
Poller
*
chPollXQ
;
// The real poller
338
CallBack
*
chCB
;
// CallBack function
339
void
*
chCBA
;
// CallBack argument
340
int
chFD
;
// Associated file descriptor
341
int
pollEnt
;
// Used only for poll() type pollers
342
int
chRTO
;
// Read timeout value (0 means none)
343
int
chWTO
;
// Write timeout value (0 means none)
344
time_t
rdDL
;
// Read deadline
345
time_t
wrDL
;
// Write deadline
346
time_t
deadLine
;
// The deadline in effect (read or write)
347
char
dlType
;
// The deadline type in deadLine as CallBack type
348
char
chEvents
;
// Enabled events as Channel type
349
char
chStat
;
// Channel status below (!0 -> in callback mode)
350
enum
Status
{
isClear
= 0,
isCBMode
,
isDead
};
351
char
inTOQ
;
// True if the channel is in the timeout queue
352
char
inPSet
;
// FD is in the actual poll set
353
char
reMod
;
// Modify issued while defered, re-issue needed
354
short
chFault
;
// Defered error, 0 if all is well
355
356
void
Reset
(
Poller
*thePoller,
int
fd,
int
eNum=0);
357
};
358
359
/******************************************************************************/
360
/* C l a s s P o l l e r */
361
/******************************************************************************/
362
363
//-----------------------------------------------------------------------------
369
//-----------------------------------------------------------------------------
370
371
class
Poller
372
{
373
friend
class
BootStrap
;
374
friend
class
Channel
;
375
public
:
376
377
//-----------------------------------------------------------------------------
396
//-----------------------------------------------------------------------------
397
398
enum
CreateOpts
{
optTOM
};
399
400
static
Poller
*
Create
(
int
&eNum,
const
char
**eTxt=0,
int
crOpts=0);
401
402
//-----------------------------------------------------------------------------
413
//-----------------------------------------------------------------------------
414
415
void
Stop
();
416
417
//-----------------------------------------------------------------------------
422
//-----------------------------------------------------------------------------
423
424
Poller
(
int
cFD,
int
rFD);
425
426
//-----------------------------------------------------------------------------
428
//-----------------------------------------------------------------------------
429
430
virtual
~Poller
() {}
431
432
protected
:
433
struct
PipeData;
434
435
void
CbkTMO
();
436
bool
CbkXeq
(
Channel
*cP,
int
events,
int
eNum,
const
char
*eTxt);
437
inline
int
GetFault
(
Channel
*cP) {
return
cP->
chFault
;}
438
inline
int
GetPollEnt
(
Channel
*cP) {
return
cP->
pollEnt
;}
439
int
GetRequest
();
440
bool
Init
(
Channel
*cP,
int
&eNum,
const
char
**eTxt,
bool
&isLockd);
441
inline
void
LockChannel
(
Channel
*cP) {cP->
chMutex
.
Lock
();}
442
int
Poll2Enum
(
short
events);
443
int
SendCmd
(PipeData &cmd);
444
void
SetPollEnt
(
Channel
*cP,
int
ptEnt);
445
bool
TmoAdd
(
Channel
*cP,
int
tmoSet);
446
void
TmoDel
(
Channel
*cP);
447
int
TmoGet
();
448
inline
void
UnLockChannel
(
Channel
*cP) {cP->
chMutex
.
UnLock
();}
449
453
virtual
void
Begin
(
XrdSysSemaphore
*syncp,
int
&rc,
const
char
**eTxt) = 0;
454
459
virtual
void
Exclude
(
Channel
*cP,
bool
&isLocked,
bool
dover=1) = 0;
460
465
virtual
bool
Include
(
Channel
*cP,
466
int
&eNum,
467
const
char
**eTxt,
468
bool
&isLocked) = 0;
469
474
virtual
bool
Modify
(
Channel
*cP,
475
int
&eNum,
476
const
char
**eTxt,
477
bool
&isLocked) = 0;
478
483
//
484
virtual
void
Shutdown
() = 0;
485
486
// The following is common to all implementations
487
//
488
Channel
*
attBase
;
// -> First channel in attach queue or 0
489
Channel
*
tmoBase
;
// -> First channel in timeout queue or 0
490
491
pthread_t
pollTid
;
// Poller's thread ID
492
493
struct
pollfd
pipePoll
;
// Stucture to wait for pipe events
494
int
cmdFD
;
// FD to send PipeData commands
495
int
reqFD
;
// FD to recv PipeData requests
496
struct
PipeData
{
char
req
;
char
evt
;
short
ent
;
int
fd
;
497
XrdSysSemaphore
*
theSem
;
498
enum
cmd
{
NoOp
= 0,
MdFD
= 1,
Post
= 2,
499
MiFD
= 3,
RmFD
= 4,
Stop
= 5};
500
PipeData
(
char
reQ=0,
char
evT=0,
short
enT=0,
501
int
fD =0,
XrdSysSemaphore
*sP=0)
502
:
req
(reQ),
evt
(evT),
ent
(enT),
fd
(fD),
503
theSem
(sP) {}
504
~PipeData
() {}
505
};
506
PipeData
reqBuff
;
// Buffer used by poller thread to recv data
507
char
*
pipeBuff
;
// Read resumption point in buffer
508
int
pipeBlen
;
// Number of outstanding bytes
509
char
tmoMask
;
// Timeout mask
510
CPP_ATOMIC_TYPE
(
bool
) wakePend;
// Wakeup is effectively pending (don't send)
511
bool
chDead
;
// True if channel deleted by callback
512
513
static time_t
maxTime
;
// Maximum time allowed
514
515
private:
516
517
void
Attach
(
Channel
*cP);
518
void
Detach
(
Channel
*cP,
bool
&isLocked,
bool
keep=true);
519
void
WakeUp
();
520
521
// newPoller() called to get a specialized new poll object at in response to
522
// Create(). A specialized implementation must be supplied.
523
//
524
static
Poller
*
newPoller
(
int
pFD[2],
int
&eNum, const
char
**eTxt);
525
526
XrdSysMutex
adMutex
;
// Mutex for adding & detaching channels
527
XrdSysMutex
toMutex
;
// Mutex for handling the timeout list
528
};
529
};
530
};
531
#endif
Generated by
1.8.3.1