Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / libraries / nacl_io / pipe / pipe_node.cc
blob90c6e7586e63cf52eac693831379a394993529d2
1 // Copyright 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.
5 #include "nacl_io/pipe/pipe_node.h"
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <pthread.h>
10 #include <string.h>
12 #include "nacl_io/ioctl.h"
13 #include "nacl_io/kernel_handle.h"
14 #include "nacl_io/pipe/pipe_event_emitter.h"
16 namespace {
17 const size_t kDefaultPipeSize = 512 * 1024;
20 namespace nacl_io {
22 PipeNode::PipeNode(Filesystem* fs)
23 : StreamNode(fs), pipe_(new PipeEventEmitter(kDefaultPipeSize)) {
26 PipeEventEmitter* PipeNode::GetEventEmitter() {
27 return pipe_.get();
30 Error PipeNode::Read(const HandleAttr& attr,
31 void* buf,
32 size_t count,
33 int* out_bytes) {
34 int ms = attr.IsBlocking() ? read_timeout_ : 0;
36 EventListenerLock wait(GetEventEmitter());
37 Error err = wait.WaitOnEvent(POLLIN, ms);
38 if (err == ETIMEDOUT)
39 err = EWOULDBLOCK;
40 if (err)
41 return err;
43 return GetEventEmitter()->Read_Locked(static_cast<char*>(buf), count,
44 out_bytes);
47 Error PipeNode::Write(const HandleAttr& attr,
48 const void* buf,
49 size_t count,
50 int* out_bytes) {
51 int ms = attr.IsBlocking() ? write_timeout_ : 0;
53 EventListenerLock wait(GetEventEmitter());
54 Error err = wait.WaitOnEvent(POLLOUT, ms);
55 if (err == ETIMEDOUT)
56 err = EWOULDBLOCK;
57 if (err)
58 return err;
60 return GetEventEmitter()->Write_Locked(static_cast<const char*>(buf),
61 count, out_bytes);
64 } // namespace nacl_io