10 #ifndef BIDIRMMAPPIPE_H
11 #define BIDIRMMAPPIPE_H
19 #define BEGIN_NAMESPACE_ROOFIT namespace RooFit {
20 #define END_NAMESPACE_ROOFIT }
27 class BidirMMapPipeException;
76 static void*
dommap(
unsigned len);
78 static void domunmap(
void* p,
unsigned len);
107 return reinterpret_cast<unsigned char*
>(
m_end) -
108 reinterpret_cast<unsigned char*>(m_begin);
117 bool full()
const {
return m_freelist.empty(); }
167 Page*
page(
unsigned pgno)
const;
173 unsigned pageno(Page* p)
const;
382 typedef BidirMMapPipe_impl::BidirMMapPipeException
Exception;
417 BidirMMapPipe(
bool useExceptions =
true,
bool useSocketpair =
false);
446 size_type
read(
void* addr, size_type sz);
459 size_type
write(
const void* addr, size_type sz);
527 pipe(_pipe), events(
None), revents(
None) { }
530 pipe(_pipe), events(_events), revents(
None) { }
673 static int poll(PollVector& pipes,
int timeout);
741 operator bool()
const {
return !
fail() && !
bad(); }
752 #define STREAMOP(TYPE) \
753 BidirMMapPipe& operator<<(const TYPE& val) \
754 { write(&val, sizeof(TYPE)); return *this; } \
755 BidirMMapPipe& operator>>(TYPE& val) \
756 { read(&val, sizeof(TYPE)); return *this; }
813 {
write(&tptr,
sizeof(tptr));
return *
this; }
824 {
read(&tptr,
sizeof(tptr));
return *
this; }
837 {
return manip(*
this); }
850 {
return manip(*
this); }
894 static BidirMMapPipe_impl::PagePool&
pagepool();
927 static size_type
xferraw(
int fd,
void* addr, size_type len,
930 static size_type
xferraw(
int fd,
void* addr,
const size_type len,
934 reinterpret_cast<ssize_t (*)(
977 int doClose(
bool force,
bool holdlock =
false);
979 void doFlush(
bool forcePartialPages =
true);
985 #undef BEGIN_NAMESPACE_ROOFIT
986 #undef END_NAMESPACE_ROOFIT
988 #endif // BIDIRMMAPPIPE_H
pid_t pidOtherEnd() const
return PID of the process on the other end of the pipe
std::size_t size_type
type used to represent sizes
#define BEGIN_NAMESPACE_ROOFIT
PagePool * m_parent
parent page pool
unsigned char m_npages
length in pages
static size_type xferraw(int fd, void *addr, size_type len, ssize_t(*xferfn)(int, void *, std::size_t))
transfer bytes through the pipe (reading, writing, may block)
BidirMMapPipe & operator<<(const T *tptr)
write raw pointer to T to other side
BidirMMapPipe * pipe
pipe of interest
PollEntry(BidirMMapPipe *_pipe, int _events)
poll a pipe for specified events
bool eof() const
true if end-of-file
error reporting with exceptions
unsigned revents
events that happened (or'ed bitmask)
size_type write(const void *addr, size_type sz)
wirte to pipe
read pipe in end-of-file state
bool isChild() const
return if this end of the pipe is the child end
handle class for a number of Pages
PageChunk(const PageChunk &)
forbid copying
impl * m_pimpl
pointer to implementation
unsigned m_refcnt
reference counter
Page * m_freelist
linked list: free pages
bool bad() const
true on I/O error
write pipe in end-of-file state
unsigned npages() const
return number of pages accessible
don't know yet what'll work
BidirMMapPipeException Exception
convenience typedef
namespace for implementation details of BidirMMapPipe
unsigned events
events of interest (or'ed bitmask)
BidirMMapPipe & operator<<(const char *str)
write a C-style string
void doFlush(bool forcePartialPages=true)
perform the flush
PollEntry(BidirMMapPipe *_pipe)
poll a pipe for all events
static MMapVariety s_mmapworks
mmap variety that works on this system
unsigned m_nUsedGrp
number of used page groups
pid_t m_childPid
pid of the child (zero if we're child)
static size_type xferraw(int fd, void *addr, const size_type len, ssize_t(*xferfn)(int, const void *, std::size_t))
transfer bytes through the pipe (reading, writing, may block)
void markPageDirty(Page *p)
put on dirty pages list
~BidirMMapPipe()
destructor
unsigned nPagesPerGroup() const
return number of pages per page group
mmapping a temp file works
Pages & operator=(const Pages &other)
assignment operator
class representing a chunk of pages
Page * m_busylist
linked list: busy pages (data to be read)
write end of pipe invalid
bool good() const
status of stream is good
size_type read(void *addr, size_type sz)
read from pipe
static MMapVariety mmapVariety()
return mmap variety support found
void flush()
flush buffers with unwritten data
BidirMMapPipe_impl::Page Page
convenience typedef for Page
static unsigned pagesize()
return page size
Page * m_dirtylist
linked list: dirty pages (data to be sent)
PollFlags
condition flags for poll
#define END_NAMESPACE_ROOFIT
void sendpages(Page *plist)
send page(s) to the other end (may block)
unsigned len() const
return length of chunk
Page * page(unsigned pgno) const
return page number pageno
Page * busypage()
get a busy page to read data from (may block)
size_type bytesReadableNonBlocking()
number of bytes that can be read without blocking
Pages()
default constructor
int close()
flush buffers, close pipe
Page * operator[](unsigned pgno) const
return page number pageno
void swap(Pages &other)
swap with other's contents
Page * dirtypage()
get a dirty page to write data to (may block)
void purge()
purge buffered data waiting to be read and/or written
std::vector< PollEntry > PollVector
convenience typedef for poll() interface
static int debugflag()
return the current setting of the debug flag
unsigned m_nPgPerGrp
number of pages per group
static unsigned lenPageList(const Page *list)
return length of a page list
BidirMMapPipe & operator=(const BidirMMapPipe &)
assignment forbidden
bool fail() const
logical failure (e.g.
static BidirMMapPipe & purge(BidirMMapPipe &pipe)
for usage a la "pipe << purge;"
Pages pop()
pop a group of pages off the free list
static void setDebugflag(int flag)
set the debug flags
logical failure (e.g. pipe closed)
Double_t length(const TVector2 &v)
static BidirMMapPipe_impl::PagePool & pagepool()
return page pool
int m_outpipe
pipe end to which data may be written
bool contains(const Pages &p) const
return if p is contained in this PageChunk
void * m_begin
pointer to start of mmapped area
static BidirMMapPipe & flush(BidirMMapPipe &pipe)
for usage a la "pipe << flush;"
bool usesSocketpair() const
if BidirMMapPipe uses a socketpair for communications
unsigned recvpages_nonblock()
receive pages from other end (non-blocking)
int doClose(bool force, bool holdlock=false)
close the pipe (no flush if forced)
bool full() const
return true if no free page groups in this chunk
BidirMMapPipe creates a bidirectional channel between the current process and a child it forks...
pid_t m_parentPid
pid of the parent
static void domunmap(void *p, unsigned len)
munmap pages p, len is length of mmapped area in bytes
static pthread_mutex_t s_openpipesmutex
protects s_openpipes
static int s_debugflag
debug flag
size_type bytesWritableNonBlocking()
number of bytes that can be written without blocking
bool isParent() const
return if this end of the pipe is the parent end
static int poll(PollVector &pipes, int timeout)
poll a set of pipes for events (ready to read from, ready to write to, error)
friend class BidirMMapPipe_impl::Page
page is our friend
static BidirMMapPipe_impl::PagePool * s_pagepool
pool of mmapped pages
pages shared (child + parent)
bool empty() const
return true if no used page groups in this chunk
bool closed() const
true if closed
MMapVariety
type of mmap support found
PageChunk & operator=(const PageChunk &)
forbid assignment
void zap(Pages &p)
free all pages except for those pointed to by p
unsigned operator[](Page *p) const
perform page to page number mapping
unsigned recvpages()
receive a pages from the other end (may block), queue them
int rdstate() const
return flags (end of file, BidirMMapPipe closed, ...)
static void * dommap(unsigned len)
mmap pages, len is length of mmapped area in bytes
static unsigned getPageSize()
determine page size at run time
STREAMOP(bool)
C++ style stream operators for bool.
static void teardownall(void)
cleanup routine - at exit, we want our children to get a SIGTERM...
std::list< void * > m_freelist
free pages list
BidirMMapPipe & operator>>(char *(&str))
read a C-style string
void * m_end
pointer one behind end of mmapped area
PageChunk * m_parent
pointer to parent pool
BidirMMapPipe_impl::BidirMMapPipeException Exception
convenience typedef for BidirMMapPipeException
void push(const Pages &p)
push a group of pages onto the free list
int m_inpipe
pipe end from which data may be read
BidirMMapPipe & operator>>(T *&tptr)
read raw pointer to T from other side
void feedPageLists(Page *plist)
"feed" the busy and free lists with a list of pages
BidirMMapPipe & operator<<(BidirMMapPipe &(*manip)(BidirMMapPipe &))
I/O manipulator support.
Page * m_pages
pointer to first page
BidirMMapPipe_impl::Pages m_pages
mmapped pages
static std::list< BidirMMapPipe * > s_openpipes
list of open BidirMMapPipes
BidirMMapPipe(bool useExceptions=true, bool useSocketpair=false)
constructor (forks!)
static unsigned s_pagepoolrefcnt
page pool reference counter
static unsigned s_pagesize
system page size (run-time determined)
pipe has data for reading
nothing special on this pipe
bool usesPipepair() const
if BidirMMapPipe uses a pipe pair for communications
int m_flags
flags (e.g. end of file)
BidirMMapPipe & operator>>(BidirMMapPipe &(*manip)(BidirMMapPipe &))
I/O manipulator support.
mmap doesn't work, have to copy back and forth
static unsigned pagesize()
return the page size of the system
unsigned pageno(Page *p) const
perform page to page number mapping
bool operator!() const
return true if serious error (fail/bad)