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/stream/stream_fs.h"
9 #include "nacl_io/ossocket.h"
10 #include "nacl_io/pepper_interface.h"
14 void DispatchStart(void* work_ptr
, int32_t val
) {
15 StreamFs::Work
* work
= static_cast<StreamFs::Work
*>(work_ptr
);
17 // Delete if it fails to Start, Run will never get called.
18 if (!work
->Start(val
))
22 void DispatchRun(void* work_ptr
, int32_t val
) {
23 StreamFs::Work
* work
= static_cast<StreamFs::Work
*>(work_ptr
);
29 void* StreamFs::StreamThreadThunk(void* fs_ptr
) {
30 StreamFs
* filesystem
= static_cast<StreamFs
*>(fs_ptr
);
31 filesystem
->StreamThread();
35 // All work is done via completions callbacks from posted work.
36 void StreamFs::StreamThread() {
38 AUTO_LOCK(message_lock_
)
40 ppapi_
->GetMessageLoopInterface()->Create(ppapi()->GetInstance());
41 ppapi_
->GetMessageLoopInterface()->AttachToCurrentThread(message_loop_
);
42 pthread_cond_broadcast(&message_cond_
);
45 // Run loop until Quit is posted.
46 ppapi_
->GetMessageLoopInterface()->Run(message_loop_
);
49 PP_CompletionCallback
StreamFs::GetStartCompletion(Work
* work
) {
50 return PP_MakeCompletionCallback(DispatchStart
, work
);
53 PP_CompletionCallback
StreamFs::GetRunCompletion(Work
* work
) {
54 return PP_MakeCompletionCallback(DispatchRun
, work
);
57 // Place enqueue onto the socket thread.
58 void StreamFs::EnqueueWork(Work
* work
) {
59 if (message_loop_
== 0) {
60 AUTO_LOCK(message_lock_
);
62 if (message_loop_
== 0) {
64 pthread_create(&thread
, NULL
, StreamThreadThunk
, this);
67 while (message_loop_
== 0)
68 pthread_cond_wait(&message_cond_
, message_lock_
.mutex());
71 PP_CompletionCallback cb
= PP_MakeCompletionCallback(DispatchStart
, work
);
72 ppapi_
->GetMessageLoopInterface()->PostWork(message_loop_
, cb
, 0);
75 StreamFs::StreamFs() : message_loop_(0) {
76 pthread_cond_init(&message_cond_
, NULL
);
79 StreamFs::~StreamFs() {
81 ppapi_
->GetMessageLoopInterface()->PostQuit(message_loop_
, PP_TRUE
);
82 ppapi_
->ReleaseResource(message_loop_
);
84 pthread_cond_destroy(&message_cond_
);
87 Error
StreamFs::OpenWithMode(const Path
& path
, int o_flags
, mode_t mode
,
88 ScopedNode
* out_node
) {
92 Error
StreamFs::Unlink(const Path
& path
) {
96 Error
StreamFs::Mkdir(const Path
& path
, int permissions
) {
100 Error
StreamFs::Rmdir(const Path
& path
) {
104 Error
StreamFs::Remove(const Path
& path
) {
108 Error
StreamFs::Rename(const Path
& path
, const Path
& newpath
) {
112 } // namespace nacl_io