Chromium Code Reviews| Index: native_client_sdk/src/libraries/nacl_io/event_emitter_pipe.h |
| diff --git a/native_client_sdk/src/libraries/nacl_io/event_emitter_pipe.h b/native_client_sdk/src/libraries/nacl_io/event_emitter_pipe.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0aa4587c61427855528e1bea36449d3fd2d75025 |
| --- /dev/null |
| +++ b/native_client_sdk/src/libraries/nacl_io/event_emitter_pipe.h |
| @@ -0,0 +1,85 @@ |
| +// 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_PIPE_H_ |
| +#define LIBRARIES_NACL_IO_EVENT_EMITTER_PIPE_H_ |
| + |
| +#include <poll.h> |
| +#include <stdint.h> |
| +#include <stdlib.h> |
| + |
| +#include "nacl_io/event_emitter.h" |
| +#include "nacl_io/fifo_char.h" |
| + |
| +#include "sdk_util/auto_lock.h" |
| + |
| +namespace nacl_io { |
| + |
| +class EventEmitterPipe; |
| +typedef sdk_util::ScopedRef<EventEmitterPipe> ScopedEmitterPipe; |
| + |
| +class EventEmitterPipe : public EventEmitter { |
| + public: |
| + EventEmitterPipe(size_t size) |
| + : fifo_(NULL), |
| + event_status_(0) { |
| + size = std::max<size_t>(1, size); |
| + fifo_ = new FIFOChar(size); |
| + |
| + UpdateStatusLocked(); |
| + } |
| + |
| + ~EventEmitterPipe() { |
| + delete fifo_; |
| + } |
| + |
| + virtual uint32_t GetEventStatus() { |
| + return event_status_; |
| + } |
| + |
| + size_t Read(char* data, size_t len) { |
|
binji
2013/09/12 01:47:56
Be consistent with:
MountNode::Read(void* buf, si
|
| + AUTO_LOCK(emitter_lock_); |
| + size_t out_len = fifo_->Read(data, len); |
| + |
| + UpdateStatusLocked(); |
| + return out_len; |
| + } |
| + |
| + size_t Write(const char* data, size_t len) { |
| + AUTO_LOCK(emitter_lock_); |
| + size_t out_len = fifo_->Write(data, len); |
| + |
| + UpdateStatusLocked(); |
| + return out_len; |
| + } |
| + |
| + protected: |
| + void UpdateStatusLocked() { |
| + uint32_t old_status = event_status_; |
| + |
| + if (!fifo_->IsEmpty()) { |
| + event_status_ |= POLLIN; |
| + } else { |
| + event_status_ &= ~POLLIN; |
| + } |
| + |
| + if (!fifo_->IsFull()) { |
| + event_status_ |= POLLOUT; |
| + } else { |
| + event_status_ &= ~POLLOUT; |
| + } |
| + |
| + uint32_t raise_status = event_status_ & ~old_status; |
| + if (raise_status) |
| + RaiseEvents_Locked(raise_status); |
| + } |
| + |
| + FIFOChar* fifo_; |
| + uint32_t event_status_; |
| +}; |
| + |
| +} // namespace nacl_io |
| + |
| +#endif // LIBRARIES_NACL_IO_EVENT_EMITTER_PIPE_H_ |
| + |