@@ -80,26 +80,26 @@ bool DefineFunctions(JSContext* cx, JS::Handle<JSObject*> global) {
8080 return true ;
8181}
8282
83- static void * WorkerMain (void * arg) {
84- JSRuntime* parentRuntime = reinterpret_cast <JSRuntime*>(arg);
85-
83+ static void WorkerMain (JSRuntime* parentRuntime) {
8684 JSContext* cx = JS_NewContext (8L * 1024L * 1024L , parentRuntime);
8785
8886 if (!JS::InitSelfHostedCode (cx)) {
89- return nullptr ;
87+ fprintf (stderr, " Error: Failed during JS::InitSelfHostedCode\n " );
88+ return ;
9089 }
9190
9291 {
9392 JS::Rooted<JSObject*> global (cx, boilerplate::CreateGlobal (cx));
9493 if (!global) {
95- return nullptr ;
94+ fprintf (stderr, " Error: Failed during boilerplate::CreateGlobal\n " );
95+ return ;
9696 }
9797
9898 JSAutoRealm ar (cx, global);
9999
100100 if (!DefineFunctions (cx, global)) {
101101 boilerplate::ReportAndClearException (cx);
102- return nullptr ;
102+ return ;
103103 }
104104
105105 if (!ExecuteCode (cx, R"js(
@@ -109,21 +109,13 @@ for (let i = 0; i < 10; i++) {
109109}
110110 )js" )) {
111111 boilerplate::ReportAndClearException (cx);
112- return nullptr ;
112+ return ;
113113 }
114114 }
115115
116116 JS_DestroyContext (cx);
117117
118- return nullptr ;
119- }
120-
121- static bool CreateWorker (JSContext* cx, pthread_t * thread) {
122- if (pthread_create (thread, nullptr , WorkerMain, JS_GetRuntime (cx)) != 0 ) {
123- return false ;
124- }
125-
126- return true ;
118+ return ;
127119}
128120
129121static bool WorkerExample (JSContext* cx) {
@@ -132,15 +124,8 @@ static bool WorkerExample(JSContext* cx) {
132124 return false ;
133125 }
134126
135- pthread_t thread1;
136- pthread_t thread2;
137-
138- if (!CreateWorker (cx, &thread1)) {
139- return false ;
140- }
141- if (!CreateWorker (cx, &thread2)) {
142- return false ;
143- }
127+ std::thread thread1 (WorkerMain, JS_GetRuntime (cx));
128+ std::thread thread2 (WorkerMain, JS_GetRuntime (cx));
144129
145130 JSAutoRealm ar (cx, global);
146131
@@ -159,8 +144,8 @@ for (let i = 0; i < 10; i++) {
159144 return false ;
160145 }
161146
162- pthread_join ( thread1, nullptr );
163- pthread_join ( thread2, nullptr );
147+ thread1. join ( );
148+ thread2. join ( );
164149
165150 return true ;
166151}
0 commit comments