| 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 UpdateStatus_Locked(); |
| 31 } |
| 32 |
| 33 ~EventEmitterPipe() { |
| 34 delete fifo_; |
| 35 } |
| 36 |
| 37 virtual uint32_t GetEventStatus() { |
| 38 return event_status_; |
| 39 } |
| 40 |
| 41 size_t Read_Locked(char* data, size_t len) { |
| 42 size_t out_len = fifo_->Read(data, len); |
| 43 |
| 44 UpdateStatus_Locked(); |
| 45 return out_len; |
| 46 } |
| 47 |
| 48 size_t Write_Locked(const char* data, size_t len) { |
| 49 size_t out_len = fifo_->Write(data, len); |
| 50 |
| 51 UpdateStatus_Locked(); |
| 52 return out_len; |
| 53 } |
| 54 |
| 55 protected: |
| 56 void UpdateStatus_Locked() { |
| 57 uint32_t old_status = event_status_; |
| 58 |
| 59 if (!fifo_->IsEmpty()) { |
| 60 event_status_ |= POLLIN; |
| 61 } else { |
| 62 event_status_ &= ~POLLIN; |
| 63 } |
| 64 |
| 65 if (!fifo_->IsFull()) { |
| 66 event_status_ |= POLLOUT; |
| 67 } else { |
| 68 event_status_ &= ~POLLOUT; |
| 69 } |
| 70 |
| 71 uint32_t raise_status = event_status_ & ~old_status; |
| 72 if (raise_status) |
| 73 RaiseEvents_Locked(raise_status); |
| 74 } |
| 75 |
| 76 FIFOChar* fifo_; |
| 77 uint32_t event_status_; |
| 78 }; |
| 79 |
| 80 } // namespace nacl_io |
| 81 |
| 82 #endif // LIBRARIES_NACL_IO_EVENT_EMITTER_PIPE_H_ |
| 83 |
| OLD | NEW |