Chromium Code Reviews| Index: native_client_sdk/src/libraries/nacl_io/event_emitter_packet.h |
| diff --git a/native_client_sdk/src/libraries/nacl_io/event_emitter_packet.h b/native_client_sdk/src/libraries/nacl_io/event_emitter_packet.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..432a1ef68479b6239d025bfccd1f2fb0b1eac4ca |
| --- /dev/null |
| +++ b/native_client_sdk/src/libraries/nacl_io/event_emitter_packet.h |
| @@ -0,0 +1,90 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef LIBRARIES_NACL_IO_EVENT_EMITTER_PACKET_H_ |
| +#define LIBRARIES_NACL_IO_EVENT_EMITTER_PACKET_H_ |
| + |
| +#include <poll.h> |
| +#include <stdint.h> |
| +#include <stdlib.h> |
| + |
| +#include "nacl_io/event_emitter.h" |
| +#include "nacl_io/fifo_packet.h" |
| + |
| +#include "sdk_util/auto_lock.h" |
| + |
| +namespace nacl_io { |
| + |
| +class EventEmitterPacket; |
| +typedef sdk_util::ScopedRef<EventEmitterPacket> ScopedEmitterPacket; |
| + |
| +class EventEmitterPacket : public EventEmitter { |
| + public: |
| + EventEmitterPacket(size_t rsize, size_t wsize) |
| + : rfifo_(NULL), |
| + wfifo_(NULL), |
| + event_status_(0) { |
| + |
| + rsize = std::max<size_t>(1, rsize); |
| + fifo_ = new FIFOPacket(rsize); |
| + |
| + wsize = std::max<size_t>(1, wsize); |
| + fifo_ = new FIFOPacket(wsize); |
| + |
| + UpdateStatusLocked(); |
| + } |
| + |
| + ~EventEmitterPipe() { |
| + delete rfifo_; |
| + delete wfifo_; |
| + } |
| + |
| + virtual uint32_t GetEventStatus() { |
| + return event_status_; |
| + } |
| + |
| + Packet* ReadTXPacket_Locked() { |
|
binji
2013/09/12 01:47:57
why TXPacket? ReadPacket is easier to read...
noelallen1
2013/09/12 23:19:03
There are two FIFOs, an incoming and and outgoing.
|
| + Packet* packet = rfifo_->Read(); |
| + |
| + UpdateStatusLocked(); |
|
binji
2013/09/12 01:47:57
_Locked?
noelallen1
2013/09/12 23:19:03
Done.
|
| + return packet; |
| + } |
| + |
| + uint32_t WriteTXPacket_Locked(Packet* packet) { |
| + uint32_t result = rfifo_->Write(packet); |
| + |
| + UpdateStatusLocked(); |
| + return result; |
| + } |
| + |
| + protected: |
| + void UpdateStatus_Locked() { |
| + uint32_t old_status = event_status_; |
| + |
| + if (!rfifo_->IsEmpty()) { |
| + event_status_ |= POLLIN; |
| + } else { |
| + event_status_ &= ~POLLIN; |
| + } |
| + |
| + if (!wfifo_->IsFull()) { |
| + event_status_ |= POLLOUT; |
| + } else { |
| + event_status_ &= ~POLLOUT; |
| + } |
| + |
| + uint32_t raise_status = event_status_ & ~old_status; |
| + if (raise_status) |
| + RaiseEvents_Locked(raise_status); |
| + } |
| + |
| + FIFOPacket* rfifo_; |
| + FIFOPacket* wfifo_; |
| + uint32_t event_status_; |
| +}; |
| + |
| +} // namespace nacl_io |
| + |
| +#endif // LIBRARIES_NACL_IO_EVENT_EMITTER_PIPE_H_ |
| + |