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.
4
5 #include "nacl_io/pipe/pipe_node.h"
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <pthread.h>
10 #include <string.h>
11
12 #include "nacl_io/ioctl.h"
13 #include "nacl_io/kernel_handle.h"
14 #include "nacl_io/pipe/pipe_event_emitter.h"
15
16 namespace {
17 const size_t kDefaultPipeSize = 512 * 1024;
18 }
19
20 namespace nacl_io {
21
PipeNode(Filesystem * fs)22 PipeNode::PipeNode(Filesystem* fs)
23 : StreamNode(fs), pipe_(new PipeEventEmitter(kDefaultPipeSize)) {
24 }
25
GetEventEmitter()26 PipeEventEmitter* PipeNode::GetEventEmitter() {
27 return pipe_.get();
28 }
29
Read(const HandleAttr & attr,void * buf,size_t count,int * out_bytes)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;
35
36 EventListenerLock wait(GetEventEmitter());
37 Error err = wait.WaitOnEvent(POLLIN, ms);
38 if (err == ETIMEDOUT)
39 err = EWOULDBLOCK;
40 if (err)
41 return err;
42
43 return GetEventEmitter()->Read_Locked(static_cast<char*>(buf), count,
44 out_bytes);
45 }
46
Write(const HandleAttr & attr,const void * buf,size_t count,int * 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;
52
53 EventListenerLock wait(GetEventEmitter());
54 Error err = wait.WaitOnEvent(POLLOUT, ms);
55 if (err == ETIMEDOUT)
56 err = EWOULDBLOCK;
57 if (err)
58 return err;
59
60 return GetEventEmitter()->Write_Locked(static_cast<const char*>(buf),
61 count, out_bytes);
62 }
63
64 } // namespace nacl_io
65