Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef LIBRARIES_NACL_IO_EVENT_EMITTER_PIPE_H_ | |
| 6 #define LIBRARIES_NACL_IO_EVENT_EMITTER_PIPE_H_ | |
| 7 | |
| 8 #include <poll.h> | |
| 9 #include <stdint.h> | |
| 10 #include <stdlib.h> | |
| 11 | |
| 12 #include "nacl_io/event_emitter.h" | |
| 13 #include "nacl_io/fifo_char.h" | |
| 14 | |
| 15 #include "sdk_util/auto_lock.h" | |
| 16 | |
| 17 namespace nacl_io { | |
| 18 | |
| 19 class EventEmitterPipe; | |
| 20 typedef sdk_util::ScopedRef<EventEmitterPipe> ScopedEmitterPipe; | |
| 21 | |
| 22 class EventEmitterPipe : public EventEmitter { | |
| 23 public: | |
| 24 EventEmitterPipe(size_t size) | |
| 25 : fifo_(NULL), | |
| 26 event_status_(0) { | |
| 27 size = std::max<size_t>(1, size); | |
| 28 fifo_ = new FIFOChar(size); | |
| 29 | |
| 30 UpdateStatusLocked(); | |
| 31 } | |
| 32 | |
| 33 ~EventEmitterPipe() { | |
| 34 delete fifo_; | |
| 35 } | |
| 36 | |
| 37 virtual uint32_t GetEventStatus() { | |
| 38 return event_status_; | |
| 39 } | |
| 40 | |
| 41 size_t Read(char* data, size_t len) { | |
|
binji
2013/09/12 01:47:56
Be consistent with:
MountNode::Read(void* buf, si
| |
| 42 AUTO_LOCK(emitter_lock_); | |
| 43 size_t out_len = fifo_->Read(data, len); | |
| 44 | |
| 45 UpdateStatusLocked(); | |
| 46 return out_len; | |
| 47 } | |
| 48 | |
| 49 size_t Write(const char* data, size_t len) { | |
| 50 AUTO_LOCK(emitter_lock_); | |
| 51 size_t out_len = fifo_->Write(data, len); | |
| 52 | |
| 53 UpdateStatusLocked(); | |
| 54 return out_len; | |
| 55 } | |
| 56 | |
| 57 protected: | |
| 58 void UpdateStatusLocked() { | |
| 59 uint32_t old_status = event_status_; | |
| 60 | |
| 61 if (!fifo_->IsEmpty()) { | |
| 62 event_status_ |= POLLIN; | |
| 63 } else { | |
| 64 event_status_ &= ~POLLIN; | |
| 65 } | |
| 66 | |
| 67 if (!fifo_->IsFull()) { | |
| 68 event_status_ |= POLLOUT; | |
| 69 } else { | |
| 70 event_status_ &= ~POLLOUT; | |
| 71 } | |
| 72 | |
| 73 uint32_t raise_status = event_status_ & ~old_status; | |
| 74 if (raise_status) | |
| 75 RaiseEvents_Locked(raise_status); | |
| 76 } | |
| 77 | |
| 78 FIFOChar* fifo_; | |
| 79 uint32_t event_status_; | |
| 80 }; | |
| 81 | |
| 82 } // namespace nacl_io | |
| 83 | |
| 84 #endif // LIBRARIES_NACL_IO_EVENT_EMITTER_PIPE_H_ | |
| 85 | |
| OLD | NEW |