OLD | NEW |
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 #include <dlfcn.h> | 5 #include <dlfcn.h> |
6 #include <pthread.h> | 6 #include <pthread.h> |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <sys/mount.h> | 10 #include <sys/mount.h> |
(...skipping 23 matching lines...) Expand all Loading... |
34 #error "Unknown arch" | 34 #error "Unknown arch" |
35 #endif | 35 #endif |
36 | 36 |
37 class DlOpenInstance : public pp::Instance { | 37 class DlOpenInstance : public pp::Instance { |
38 public: | 38 public: |
39 explicit DlOpenInstance(PP_Instance instance) | 39 explicit DlOpenInstance(PP_Instance instance) |
40 : pp::Instance(instance), | 40 : pp::Instance(instance), |
41 eightball_so_(NULL), | 41 eightball_so_(NULL), |
42 reverse_so_(NULL), | 42 reverse_so_(NULL), |
43 eightball_(NULL), | 43 eightball_(NULL), |
44 reverse_(NULL), | 44 reverse_(NULL) {} |
45 tid_(NULL) {} | |
46 | 45 |
47 virtual ~DlOpenInstance() {} | 46 virtual ~DlOpenInstance() {} |
48 | 47 |
49 // Helper function to post a message back to the JS and stdout functions. | 48 // Helper function to post a message back to the JS and stdout functions. |
50 void logmsg(const char* pStr) { | 49 void logmsg(const char* pStr) { |
51 PostMessage(pp::Var(std::string("log:") + pStr)); | 50 PostMessage(pp::Var(std::string("log:") + pStr)); |
52 fprintf(stdout, pStr); | 51 fprintf(stdout, pStr); |
53 fprintf(stdout, "\n"); | 52 fprintf(stdout, "\n"); |
54 } | 53 } |
55 | 54 |
56 // Initialize the module, staring a worker thread to load the shared object. | 55 // Initialize the module, staring a worker thread to load the shared object. |
57 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { | 56 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
58 nacl_io_init_ppapi(pp_instance(), | 57 nacl_io_init_ppapi(pp_instance(), |
59 pp::Module::Get()->get_browser_interface()); | 58 pp::Module::Get()->get_browser_interface()); |
60 // Mount a HTTP mount at /http. All reads from /http/* will read from the | 59 // Mount a HTTP mount at /http. All reads from /http/* will read from the |
61 // server. | 60 // server. |
62 mount("", "/http", "httpfs", 0, ""); | 61 mount("", "/http", "httpfs", 0, ""); |
63 | 62 |
| 63 pthread_t thread; |
64 logmsg("Spawning thread to cache .so files..."); | 64 logmsg("Spawning thread to cache .so files..."); |
65 if (pthread_create(&tid_, NULL, LoadLibrariesOnWorker, this)) { | 65 int rtn = pthread_create(&thread, NULL, LoadLibrariesOnWorker, this); |
| 66 if (rtn != 0) { |
66 logmsg("ERROR; pthread_create() failed."); | 67 logmsg("ERROR; pthread_create() failed."); |
67 return false; | 68 return false; |
68 } | 69 } |
| 70 rtn = pthread_detach(thread); |
| 71 if (rtn != 0) { |
| 72 logmsg("ERROR; pthread_detach() failed."); |
| 73 return false; |
| 74 } |
69 return true; | 75 return true; |
70 } | 76 } |
71 | 77 |
72 // This function is called on a worker thread, and will call dlopen to load | 78 // This function is called on a worker thread, and will call dlopen to load |
73 // the shared object. In addition, note that this function does NOT call | 79 // the shared object. In addition, note that this function does NOT call |
74 // dlclose, which would close the shared object and unload it from memory. | 80 // dlclose, which would close the shared object and unload it from memory. |
75 void LoadLibrary() { | 81 void LoadLibrary() { |
76 eightball_so_ = dlopen("libeightball.so", RTLD_LAZY); | 82 eightball_so_ = dlopen("libeightball.so", RTLD_LAZY); |
77 if (eightball_so_ != NULL) { | 83 if (eightball_so_ != NULL) { |
78 intptr_t offset = (intptr_t) dlsym(eightball_so_, "Magic8Ball"); | 84 intptr_t offset = (intptr_t) dlsym(eightball_so_, "Magic8Ball"); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 DlOpenInstance* inst = static_cast<DlOpenInstance*>(pInst); | 159 DlOpenInstance* inst = static_cast<DlOpenInstance*>(pInst); |
154 inst->LoadLibrary(); | 160 inst->LoadLibrary(); |
155 return NULL; | 161 return NULL; |
156 } | 162 } |
157 | 163 |
158 private: | 164 private: |
159 void* eightball_so_; | 165 void* eightball_so_; |
160 void* reverse_so_; | 166 void* reverse_so_; |
161 TYPE_eightball eightball_; | 167 TYPE_eightball eightball_; |
162 TYPE_reverse reverse_; | 168 TYPE_reverse reverse_; |
163 pthread_t tid_; | |
164 }; | 169 }; |
165 | 170 |
166 class DlOpenModule : public pp::Module { | 171 class DlOpenModule : public pp::Module { |
167 public: | 172 public: |
168 DlOpenModule() : pp::Module() {} | 173 DlOpenModule() : pp::Module() {} |
169 virtual ~DlOpenModule() {} | 174 virtual ~DlOpenModule() {} |
170 | 175 |
171 // Create and return a DlOpenInstance object. | 176 // Create and return a DlOpenInstance object. |
172 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 177 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
173 return new DlOpenInstance(instance); | 178 return new DlOpenInstance(instance); |
174 } | 179 } |
175 }; | 180 }; |
176 | 181 |
177 namespace pp { | 182 namespace pp { |
178 Module* CreateModule() { return new DlOpenModule(); } | 183 Module* CreateModule() { return new DlOpenModule(); } |
179 } // namespace pp | 184 } // namespace pp |
OLD | NEW |