OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Native Client 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 // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 6 // |
| 7 // This is an early draft of background thread support. |
| 8 // Until it is complete, we assume that all functions proxy functions |
| 9 // (but CallOnMainThread) are called on the main thread. |
| 10 // |
| 11 // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 12 // |
| 13 // Support for "upcalls" -- RPCs to the browser that are done from other than |
| 14 // the main thread. These calls are synchronized by the ppapi_proxy library |
| 15 // at the plugin end. |
| 16 |
| 17 #include "native_client/src/shared/ppapi_proxy/browser_upcall.h" |
| 18 |
| 19 #include <new> |
| 20 |
| 21 #include "native_client/src/include/portability.h" |
| 22 #include "native_client/src/include/nacl_macros.h" |
| 23 #include "native_client/src/include/nacl_scoped_ptr.h" |
| 24 #include "native_client/src/shared/platform/nacl_threads.h" |
| 25 #include "native_client/src/shared/ppapi_proxy/browser_callback.h" |
| 26 #include "native_client/src/shared/ppapi_proxy/browser_globals.h" |
| 27 #include "native_client/src/shared/ppapi_proxy/utility.h" |
| 28 #include "native_client/src/shared/srpc/nacl_srpc.h" |
| 29 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" |
| 30 #include "ppapi/c/pp_completion_callback.h" |
| 31 #include "ppapi/c/pp_errors.h" |
| 32 #include "ppapi/c/ppb_core.h" |
| 33 #include "srpcgen/upcall.h" |
| 34 |
| 35 using nacl::DescWrapper; |
| 36 using nacl::DescWrapperFactory; |
| 37 |
| 38 namespace { |
| 39 |
| 40 // Structure for passing information to the thread. Shares ownership of |
| 41 // the descriptor with the creating routine. This allows passing ownership |
| 42 // to the upcall thread. |
| 43 struct UpcallInfo { |
| 44 nacl::scoped_ptr<DescWrapper> wrapper; |
| 45 NaClSrpcChannel* channel; |
| 46 }; |
| 47 |
| 48 void WINAPI UpcallThread(void* arg) { |
| 49 // The memory for info was allocated on the creating (browser UI) thread, |
| 50 // but ownership was conferred to the upcall thread for deletion. |
| 51 nacl::scoped_ptr<UpcallInfo> info(reinterpret_cast<UpcallInfo*>(arg)); |
| 52 NaClSrpcServerLoop(info->wrapper->desc(), |
| 53 PpbUpcalls::srpc_methods, |
| 54 info->channel); |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 59 namespace ppapi_proxy { |
| 60 |
| 61 DescWrapper* BrowserUpcall::Start(struct NaClThread* nacl_thread, |
| 62 NaClSrpcChannel* upcall_channel) { |
| 63 // Create a socket pair for the upcall server. |
| 64 DescWrapperFactory factory; |
| 65 DescWrapper* pair[2] = { NULL, NULL }; |
| 66 if (factory.MakeSocketPair(pair)) { |
| 67 return NULL; |
| 68 } |
| 69 nacl::scoped_ptr<DescWrapper> browser_end(pair[0]); |
| 70 nacl::scoped_ptr<DescWrapper> plugin_end(pair[1]); |
| 71 // Create an info node to pass to the thread. |
| 72 nacl::scoped_ptr<UpcallInfo> info(new UpcallInfo); |
| 73 info->wrapper.reset(browser_end.get()); |
| 74 info->channel = upcall_channel; |
| 75 // On success, info took ownership of browser_end. |
| 76 browser_end.release(); |
| 77 // Create a thread and an SRPC "upcall" server. |
| 78 const int kThreadStackSize = 128 * 1024; |
| 79 if (!NaClThreadCreateJoinable(nacl_thread, |
| 80 UpcallThread, |
| 81 info.get(), |
| 82 kThreadStackSize)) { |
| 83 return NULL; |
| 84 } |
| 85 // On successful thread creation, ownership of info passes to the thread. |
| 86 info.release(); |
| 87 // On successful return, the caller gets the plugin_end of the socketpair. |
| 88 return plugin_end.release(); |
| 89 } |
| 90 |
| 91 } // namespace ppapi_proxy |
OLD | NEW |