void configReader(PortReader& reader) { stateMutex.wait(); readActive = true; readBackground = true; readDelegate = &reader; consume.post(); // just do this once stateMutex.post(); }
void finishReading() { if (!readBackground) { stateMutex.wait(); closed = true; consume.post(); consume.post(); stateMutex.post(); } }
bool reply(PortWriter& writer, bool drop, bool /*interrupted*/) { // send reply even if interrupt has happened in interim if (!replyDue) return false; replyDue = false; dropDue = drop; writeDelegate = &writer; consume.post(); produce.wait(); bool result = readResult; return result; }
virtual ~Private() { Port *closePort = nullptr; stateSema.wait(); if (port!=nullptr) { closePort = port; } stateSema.post(); if (closePort!=nullptr) { closePort->close(); } stateSema.wait(); clear(); //stateSema.post(); // never give back mutex }
bool read(PortReader& reader, bool willReply = false) { // called by user // user claimed they would reply to last read, but then // decided not to. if (replyDue) { Bottle emptyMessage; reply(emptyMessage,false,false); replyDue = false; dropDue = false; } if (willReply) { replyDue = true; } stateMutex.wait(); readActive = true; readDelegate = &reader; writeDelegate = NULL; this->willReply = willReply; consume.post(); // happy consumer stateMutex.post(); produce.wait(); stateMutex.wait(); if (!readBackground) { readDelegate = NULL; } stateMutex.post(); bool result = readResult; return result; }
virtual bool read(ConnectionReader& reader) { // called by comms code readBlock.wait(); if (!reader.isValid()) { // termination stateMutex.wait(); if (readDelegate!=NULL) { readResult = readDelegate->read(reader); } stateMutex.post(); produce.post(); readBlock.post(); return false; } // wait for happy consumer - don't want to miss a packet if (!readBackground) { consume.wait(); } if (closed) { YARP_DEBUG(Logger::get(),"Port::read shutting down"); readBlock.post(); return false; } stateMutex.wait(); readResult = false; if (readDelegate!=NULL) { readResult = readDelegate->read(reader); } else { // read and ignore YARP_DEBUG(Logger::get(),"data received in Port, no reader for it"); Bottle b; b.read(reader); } if (!readBackground) { readDelegate = NULL; writeDelegate = NULL; } bool result = readResult; stateMutex.post(); if (!readBackground) { produce.post(); } if (result&&willReply) { consume.wait(); if (closed) { YARP_DEBUG(Logger::get(),"Port::read shutting down"); readBlock.post(); return false; } if (writeDelegate!=NULL) { stateMutex.wait(); ConnectionWriter *writer = reader.getWriter(); if (writer!=NULL) { result = readResult = writeDelegate->write(*writer); } stateMutex.post(); } if (dropDue) { reader.requestDrop(); } produce.post(); } readBlock.post(); return result; }
void openable() { stateMutex.wait(); closed = false; opened = true; stateMutex.post(); }
void promiseType(const Type& typ) { stateMutex.wait(); this->typ = typ; stateMutex.post(); }
Type getType() { stateMutex.wait(); Type t = typ; stateMutex.post(); return t; }
void resumeFull() { while (produce.check()) {} while (readBlock.check()) {} resume(); readBlock.post(); }