Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(504)

Side by Side Diff: native_client_sdk/src/examples/tutorial/dlopen/dlopen.cc

Issue 14607005: [NaCl SDK] Cleanup examples. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: feedback Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /// @file
6 /// This example demonstrates building a dynamic library which is loaded by the
7 /// NaCl module. To load the NaCl module, the browser first looks for the
8 /// CreateModule() factory method (at the end of this file). It calls
9 /// CreateModule() once to load the module code from your .nexe. After the
10 /// .nexe code is loaded, CreateModule() is not called again.
11 ///
12 /// Once the .nexe code is loaded, the browser then calls the CreateInstance()
13 /// method on the object returned by CreateModule(). If the CreateInstance
14 /// returns successfully, then Init function is called, which will load the
15 /// shared object on a worker thread. We use a worker because dlopen is
16 /// a blocking call, which is not allowed on the main thread.
17
18 #include <dlfcn.h> 5 #include <dlfcn.h>
19 #include <pthread.h> 6 #include <pthread.h>
20 #include <stdio.h> 7 #include <stdio.h>
21 #include <stdlib.h> 8 #include <stdlib.h>
22 #include <string.h> 9 #include <string.h>
23 10
24 #include <ppapi/cpp/completion_callback.h> 11 #include <ppapi/cpp/completion_callback.h>
25 #include <ppapi/cpp/instance.h> 12 #include <ppapi/cpp/instance.h>
26 #include <ppapi/cpp/module.h> 13 #include <ppapi/cpp/module.h>
27 #include <ppapi/cpp/var.h> 14 #include <ppapi/cpp/var.h>
28 15
29 #include "eightball.h" 16 #include "eightball.h"
30 #include "nacl_io/nacl_io.h" 17 #include "nacl_io/nacl_io.h"
31 #include "reverse.h" 18 #include "reverse.h"
32 19
33 #if defined(NACL_SDK_DEBUG) 20 #if defined(NACL_SDK_DEBUG)
34 #define CONFIG_NAME "Debug" 21 #define CONFIG_NAME "Debug"
35 #else 22 #else
36 #define CONFIG_NAME "Release" 23 #define CONFIG_NAME "Release"
37 #endif 24 #endif
38 25
39 #define XSTRINGIFY(x) STRINGIFY(x) 26 #define XSTRINGIFY(x) STRINGIFY(x)
40 #define STRINGIFY(x) #x 27 #define STRINGIFY(x) #x
41 #define NACL_ARCH_STRING XSTRINGIFY(NACL_ARCH) 28 #define NACL_ARCH_STRING XSTRINGIFY(NACL_ARCH)
42 29
43 class DlopenInstance : public pp::Instance { 30 class DlOpenInstance : public pp::Instance {
44 public: 31 public:
45 explicit DlopenInstance(PP_Instance instance) 32 explicit DlOpenInstance(PP_Instance instance)
46 : pp::Instance(instance), 33 : pp::Instance(instance),
47 eightball_so_(NULL), 34 eightball_so_(NULL),
48 reverse_so_(NULL), 35 reverse_so_(NULL),
49 eightball_(NULL), 36 eightball_(NULL),
50 reverse_(NULL), 37 reverse_(NULL),
51 tid_(NULL) {} 38 tid_(NULL) {}
52 39
53 virtual ~DlopenInstance() {} 40 virtual ~DlOpenInstance() {}
54 ;
55 41
56 // Helper function to post a message back to the JS and stdout functions. 42 // Helper function to post a message back to the JS and stdout functions.
57 void logmsg(const char* pStr) { 43 void logmsg(const char* pStr) {
58 PostMessage(pp::Var(std::string("log:") + pStr)); 44 PostMessage(pp::Var(std::string("log:") + pStr));
59 fprintf(stdout, pStr); 45 fprintf(stdout, pStr);
60 } 46 }
61 47
62 // Initialize the module, staring a worker thread to load the shared object. 48 // Initialize the module, staring a worker thread to load the shared object.
63 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { 49 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
64 nacl_io_init_ppapi(pp_instance(), 50 nacl_io_init_ppapi(pp_instance(),
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 146
161 logmsg(message.c_str()); 147 logmsg(message.c_str());
162 } else { 148 } else {
163 std::string errormsg = "Unexpected message: "; 149 std::string errormsg = "Unexpected message: ";
164 errormsg += message + "\n"; 150 errormsg += message + "\n";
165 logmsg(errormsg.c_str()); 151 logmsg(errormsg.c_str());
166 } 152 }
167 } 153 }
168 154
169 static void* LoadLibrariesOnWorker(void* pInst) { 155 static void* LoadLibrariesOnWorker(void* pInst) {
170 DlopenInstance* inst = static_cast<DlopenInstance*>(pInst); 156 DlOpenInstance* inst = static_cast<DlOpenInstance*>(pInst);
171 inst->LoadLibrary(); 157 inst->LoadLibrary();
172 return NULL; 158 return NULL;
173 } 159 }
174 160
175 static void LoadDoneCB(void* pInst, int32_t result) { 161 static void LoadDoneCB(void* pInst, int32_t result) {
176 DlopenInstance* inst = static_cast<DlopenInstance*>(pInst); 162 DlOpenInstance* inst = static_cast<DlOpenInstance*>(pInst);
177 inst->UseLibrary(); 163 inst->UseLibrary();
178 } 164 }
179 165
180 private: 166 private:
181 void* eightball_so_; 167 void* eightball_so_;
182 void* reverse_so_; 168 void* reverse_so_;
183 TYPE_eightball eightball_; 169 TYPE_eightball eightball_;
184 TYPE_reverse reverse_; 170 TYPE_reverse reverse_;
185 pthread_t tid_; 171 pthread_t tid_;
186 }; 172 };
187 173
188 // The Module class. The browser calls the CreateInstance() method to create 174 class DlOpenModule : public pp::Module {
189 // an instance of your NaCl module on the web page. The browser creates a new
190 // instance for each <embed> tag with type="application/x-nacl".
191 class dlOpenModule : public pp::Module {
192 public: 175 public:
193 dlOpenModule() : pp::Module() {} 176 DlOpenModule() : pp::Module() {}
194 virtual ~dlOpenModule() {} 177 virtual ~DlOpenModule() {}
195 178
196 // Create and return a DlopenInstance object. 179 // Create and return a DlOpenInstance object.
197 virtual pp::Instance* CreateInstance(PP_Instance instance) { 180 virtual pp::Instance* CreateInstance(PP_Instance instance) {
198 return new DlopenInstance(instance); 181 return new DlOpenInstance(instance);
199 } 182 }
200 }; 183 };
201 184
202 // Factory function called by the browser when the module is first loaded.
203 // The browser keeps a singleton of this module. It calls the
204 // CreateInstance() method on the object you return to make instances. There
205 // is one instance per <embed> tag on the page. This is the main binding
206 // point for your NaCl module with the browser.
207 namespace pp { 185 namespace pp {
208 Module* CreateModule() { return new dlOpenModule(); } 186 Module* CreateModule() { return new DlOpenModule(); }
209 } // namespace pp 187 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698