void WaitToJoin() { int threadsRunning = 0; // Join all threads. for(int i = 0; i < NUM_THREADS; ++i) { if (thread[i]) { void *status; int rc = pthread_join(thread[i], &status); if (rc == 0) { thread[i] = 0; if (threadNum < numThreadsToCreateTotal) { CreateThread(i, threadNum++); ++threadsRunning; } } else ++threadsRunning; } } if (!threadsRunning) { if (counter == numThreadsToCreateTotal) EM_ASM(console.log('All threads finished. Counter = ' + $0 + ' as expected.'), counter); else EM_ASM(console.error('All threads finished, but counter = ' + $0 + ' != ' + $1 + '!'), counter, numThreadsToCreateTotal); #ifdef REPORT_RESULT REPORT_RESULT(counter); #endif emscripten_cancel_main_loop(); } }
EMSCRIPTEN_RESULT emscripten_fetch_wait(emscripten_fetch_t *fetch, double timeoutMsecs) { #if __EMSCRIPTEN_PTHREADS__ if (!fetch) return EMSCRIPTEN_RESULT_INVALID_PARAM; uint32_t proxyState = emscripten_atomic_load_u32(&fetch->__proxyState); if (proxyState == 2) return EMSCRIPTEN_RESULT_SUCCESS; // already finished. if (proxyState != 1) return EMSCRIPTEN_RESULT_INVALID_PARAM; // the fetch should be ongoing? // #ifdef FETCH_DEBUG EM_ASM({ console.log('fetch: emscripten_fetch_wait..') }); // #endif // TODO: timeoutMsecs is currently ignored. Return EMSCRIPTEN_RESULT_TIMED_OUT on timeout. while(proxyState == 1/*sent to proxy worker*/) { emscripten_futex_wait(&fetch->__proxyState, proxyState, 100 /*TODO HACK:Sleep sometimes doesn't wake up?*/);//timeoutMsecs); proxyState = emscripten_atomic_load_u32(&fetch->__proxyState); } // #ifdef FETCH_DEBUG EM_ASM({ console.log('fetch: emscripten_fetch_wait done..') }); // #endif if (proxyState == 2) return EMSCRIPTEN_RESULT_SUCCESS; else return EMSCRIPTEN_RESULT_FAILED; #else EM_ASM({ console.error('fetch: emscripten_fetch_wait is not available when building without pthreads!') }); return EMSCRIPTEN_RESULT_FAILED; #endif }
void *thread_main(void *arg) { EM_ASM(Module.print('hello from pthread 1: ' + $0), globalData); assert(globalData == 10); globalData = 20; EM_ASM(Module.print('hello from pthread 2: ' + $0), globalData); assert(globalData == 20); return 0; }
// Check attributes support in the WebGL implementation (see test_webgl_context_attributes function in test_browser.py) // Tests will succeed if they are not. static void checkContextAttributesSupport() { if (!webglAntialiasSupported()) { resultAA = 1; EM_ASM(alert('warning: no antialiasing\n')); } if (!webglDepthSupported()) { resultDepth = 1; EM_ASM(alert('warning: no depth\n')); } if (!webglStencilSupported()) { resultStencil = 1; EM_ASM(alert('warning: no stencil\n')); } }
void *ThreadMain(void *arg) { assert(pthread_self() != 0); assert(globalDouble == 5.0); assert(globalU64 == 5); struct Test *t = (struct Test*)arg; EM_ASM(out('Thread ' + $0 + ' for test ' + $1 + ': starting computation.'), t->threadId, t->op); for(int i = 0; i < 99999; ++i) for(int j = 0; j < N; ++j) { switch(t->op) { case 0: emscripten_atomic_add_u64(&sharedData[j], 1); break; case 1: emscripten_atomic_sub_u64(&sharedData[j], 1); break; case 2: emscripten_atomic_and_u64(&sharedData[j], ~(1UL << t->threadId)); break; case 3: emscripten_atomic_or_u64(&sharedData[j], 1UL << t->threadId); break; case 4: emscripten_atomic_xor_u64(&sharedData[j], 1UL << t->threadId); break; case 5: { // Atomically load and store data, and test that each individual u8 is the same. uint64_t data = emscripten_atomic_load_u64(&sharedData[j]); uint8_t dataU8[8]; memcpy(dataU8, &data, 8); assert(dataU8[0] >= 10 && dataU8[0] < 10+NUM_THREADS); assert(dataU8[0] == dataU8[1] && dataU8[0] == dataU8[2] && dataU8[0] == dataU8[3]); assert(dataU8[0] == dataU8[4] && dataU8[0] == dataU8[5] && dataU8[0] == dataU8[6] && dataU8[0] == dataU8[7]); dataU8[0] = dataU8[1] = dataU8[2] = dataU8[3] = dataU8[4] = dataU8[5] = dataU8[6] = dataU8[7] = 10 + t->threadId; memcpy(&data, dataU8, 8); emscripten_atomic_store_u64(&sharedData[j], data); } break; case 6: { uint64_t newData = rand_60(); uint64_t data; uint64_t prevData; do { data = emscripten_atomic_load_u64(&sharedData[j]); prevData = emscripten_atomic_cas_u64(&sharedData[j], data, newData); } while(prevData != data); threadCasAccumulatedReadData[t->threadId] += data; threadCasAccumulatedWrittenData[t->threadId] += newData; } break; } } EM_ASM(out('Thread ' + $0 + ' for test ' + $1 + ': finished, exit()ing.'), t->threadId, t->op); pthread_exit(0); }
extern "C" int main(int argc, char** argv) { printf("Hello, world! Hello LabLua, this is a example of code with emscripten and SDL2\n Sincerely, Geovane Pacheco \n\n"); SDL_Init(SDL_INIT_VIDEO); SDL_Surface *screen = SDL_SetVideoMode(256, 256, 32, SDL_SWSURFACE); #ifdef TEST_SDL_LOCK_OPTS EM_ASM("SDL.defaults.copyOnLock = false; SDL.defaults.discardOnLock = true; SDL.defaults.opaqueFrontBuffer = false;"); #endif if (SDL_MUSTLOCK(screen)) SDL_LockSurface(screen); for (int i = 0; i < 256; i++) { for (int j = 0; j < 256; j++) { #ifdef TEST_SDL_LOCK_OPTS // Alpha behaves like in the browser, so write proper opaque pixels. int alpha = 255; #else // To emulate native behavior with blitting to screen, alpha component is ignored. Test that it is so by outputting // data (and testing that it does get discarded) int alpha = (i+j) % 255; #endif *((Uint32*)screen->pixels + i * 256 + j) = SDL_MapRGBA(screen->format, i, j, 255-i, alpha); } } if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); SDL_Flip(screen); SDL_Flip("ball.bmp"); printf("The result of SDL code is above from this console text. In this case is a colored square"); SDL_Quit(); return 0; }
extern "C" int main(int argc, char** argv) { printf("hello, world!\n"); SDL_Init(SDL_INIT_VIDEO); SDL_Surface *screen = SDL_SetVideoMode(256, 256, 32, SDL_SWSURFACE); #ifdef TEST_SDL_LOCK_OPTS EM_ASM("SDL.defaults.copyOnLock = false; SDL.defaults.discardOnLock = true; SDL.defaults.opaqueFrontBuffer = false;"); #endif if (SDL_MUSTLOCK(screen)) SDL_LockSurface(screen); for (int i = 0; i < 256; i++) { for (int j = 0; j < 256; j++) { #ifdef TEST_SDL_LOCK_OPTS // Alpha behaves like in the browser, so write proper opaque pixels. int alpha = 255; #else // To emulate native behavior with blitting to screen, alpha component is ignored. Test that it is so by outputting // data (and testing that it does get discarded) int alpha = (i+j) % 255; #endif *((Uint32*)screen->pixels + i * 256 + j) = SDL_MapRGBA(screen->format, i, j, 255-i, alpha); } } if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); SDL_Flip(screen); printf("you should see a smoothly-colored square - no sharp lines but the square borders!\n"); printf("and here is some text that should be HTML-friendly: amp: |&| double-quote: |\"| quote: |'| less-than, greater-than, html-like tags: |<cheez></cheez>|\nanother line.\n"); SDL_Quit(); return 0; }
int main(int argc, char **argv) { // For testing purposes, rename the canvas on the page to some arbitrary ID. EM_ASM(document.getElementById('canvas').id = 'myCanvasId'); // Accessing #canvas should resize Module['canvas'] EMSCRIPTEN_RESULT r = emscripten_set_canvas_element_size("#canvas", 100, 200); assert(r == EMSCRIPTEN_RESULT_SUCCESS); int w, h; r = emscripten_get_canvas_element_size("#canvas", &w, &h); assert(r == EMSCRIPTEN_RESULT_SUCCESS); assert(w == 100); assert(h == 200); w = h = 0; // Check that we see the change via 'NULL' r = emscripten_get_canvas_element_size(NULL, &w, &h); assert(r == EMSCRIPTEN_RESULT_SUCCESS); assert(w == 100); assert(h == 200); w = h = 0; // Check that we see the change via 'mycanvasId' r = emscripten_get_canvas_element_size("myCanvasId", &w, &h); assert(r == EMSCRIPTEN_RESULT_SUCCESS); assert(w == 100); assert(h == 200); // The following line will not work with OffscreenCanvas, that is covered in another test int jsAgreesWithSize = EM_ASM_INT({return Module['canvas'].width == 100 && Module['canvas'].height == 200});
int main() { volatile int i = 1; volatile int j = 0; EM_ASM({ Module.print('|' + $0 + '|') }, i / j); }
int PDC_curs_set(int visibility) { int ret_vis; PDC_LOG(("PDC_curs_set() - called: visibility=%d\n", visibility)); ret_vis = SP->visibility; SP->visibility = visibility; if (visibility) EM_ASM(term.cursorOn()); else EM_ASM(term.cursorOff()); return ret_vis; }
void *thread_main(void *arg) { EM_ASM(out('hello from thread!')); #ifdef REPORT_RESULT REPORT_RESULT(1); #endif return 0; }
void RunTest(int test) { pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); pthread_attr_setstacksize(&attr, 4*1024); printf("Main thread has thread ID %d\n", (int)pthread_self()); assert(pthread_self() != 0); switch(test) { case 2: memset(sharedData, 0xFF, sizeof(sharedData)); break; case 5: memset(sharedData, 0x10, sizeof(sharedData)); break; default: memset(sharedData, 0, sizeof(sharedData)); break; } EM_ASM(out('Main: Starting test ' + $0), test); for(int i = 0; i < NUM_THREADS; ++i) { t[i].op = test; t[i].threadId = i; int rc = pthread_create(&thread[i], &attr, ThreadMain, &t[i]); assert(rc == 0); } pthread_attr_destroy(&attr); for(int i = 0; i < NUM_THREADS; ++i) { int status = 1; int rc = pthread_join(thread[i], (void**)&status); assert(rc == 0); assert(status == 0); } int val = sharedData[0]; EM_ASM(out('Main: Test ' + $0 + ' finished. Result: ' + $1), test, val); if (test != 6) { for(int i = 1; i < N; ++i) assert(sharedData[i] == sharedData[0]); } }
int main() { EM_ASM(Module.print('hello from main 1: ' + $0), globalData); assert(globalData == 1); globalData = 10; EM_ASM(Module.print('hello from main 2: ' + $0), globalData); assert(globalData == 10); pthread_t thread; pthread_create(&thread, NULL, thread_main, NULL); pthread_join(thread, 0); EM_ASM(Module.print('hello from main 3: ' + $0), globalData); assert(globalData == 20); #ifdef REPORT_RESULT REPORT_RESULT(globalData); #endif }
int main() { globalDouble = 5.0; globalU64 = 4; uint64_t prevU64 = emscripten_atomic_add_u64((void*)&globalU64, 1); assert(prevU64 == 4); if (!emscripten_has_threading_support()) { #ifdef REPORT_RESULT REPORT_RESULT(0); #endif printf("Skipped: Threading is not supported.\n"); return 0; } for(int i = 0; i < 7; ++i) RunTest(i); uint64_t totalRead = 0; uint64_t totalWritten = 0; for(int i = 0; i < NUM_THREADS; ++i) { totalRead += threadCasAccumulatedReadData[i]; totalWritten += threadCasAccumulatedWrittenData[i]; } for(int i = 0; i < N; ++i) totalRead += sharedData[i]; if (totalRead == totalWritten) printf("totalRead: %llu, totalWritten: %llu\n", totalRead, totalWritten); else printf("64-bit CAS test failed! totalRead != totalWritten (%llu != %llu)\n", totalRead, totalWritten); #ifdef REPORT_RESULT int result = (totalRead != totalWritten) ? 1 : 0; REPORT_RESULT(result); #else EM_ASM(out('Main: Test successfully finished.')); #endif }
int main() { for(int i = 0; i < NUM_KEYS; ++i) pthread_key_create(&keys[i], NULL); // Create initial threads. for(int i = 0; i < NUM_THREADS; ++i) CreateThread(i); // Join all threads and create more. if (emscripten_has_threading_support()) { for(int i = 0; i < NUM_THREADS; ++i) { if (thread[i]) { int status; int rc = pthread_join(thread[i], (void**)&status); assert(rc == 0); EM_ASM(Module['printErr']('Main: Joined thread idx ' + $0 + ' with status ' + $1), i, (int)status); assert(status == 0); thread[i] = 0; if (numThreadsToCreate > 0) { --numThreadsToCreate; CreateThread(i); } } } } #ifdef REPORT_RESULT REPORT_RESULT(0); #endif for(int i = 0; i < NUM_KEYS; ++i) pthread_key_delete(keys[i]); }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int argc, char** argv) { if (argc < 2) { PrintUsageAndExit(); } // init the variables AP4_ByteStream* input = NULL; const char* filename = NULL; AP4_ProtectionKeyMap key_map; AP4_Array<AP4_Ordinal> tracks_to_dump; AP4_Ordinal verbosity = 0; bool json_format = false; // parse the command line argv++; char* arg; while ((arg = *argv++)) { if (!strcmp(arg, "--track")) { arg = *argv++; if (arg == NULL) { fprintf(stderr, "ERROR: missing argument after --track option\n"); return 1; } char* track_ascii = arg; char* key_ascii = NULL; char* delimiter = strchr(arg, ':'); if (delimiter != NULL) { *delimiter = '\0'; key_ascii = delimiter+1; } // this track will be dumped AP4_Ordinal track_id = (AP4_Ordinal) strtoul(track_ascii, NULL, 10); tracks_to_dump.Append(track_id); // see if we have a key for this track if (key_ascii != NULL) { unsigned char key[16]; if (AP4_ParseHex(key_ascii, key, 16)) { fprintf(stderr, "ERROR: invalid hex format for key\n"); return 1; } // set the key in the map key_map.SetKey(track_id, key, 16); } } else if (!strcmp(arg, "--verbosity")) { arg = *argv++; if (arg == NULL) { fprintf(stderr, "ERROR: missing argument after --verbosity option\n"); return 1; } verbosity = strtoul(arg, NULL, 10); } else if (!strcmp(arg, "--format")) { arg = *argv++; if (arg == NULL) { fprintf(stderr, "ERROR: missing argument after --format option\n"); return 1; } if (strcmp(arg, "json") == 0) { json_format = true; } else if (strcmp(arg, "text")) { fprintf(stderr, "ERROR: unknown output format\n"); return 1; } } else { #ifdef __EMSCRIPTEN__ // Emscripten has to "mount" the filesystem to a virtual directory. EM_ASM(FS.mkdir('/working')); EM_ASM(FS.mount(NODEFS, { root: '.' }, '/working')); const char* const mount_name = "/working/%s"; const int filename_size = snprintf(NULL, 0, mount_name, arg); char* mounted_filename = (char*) malloc(filename_size + 1); sprintf(mounted_filename, mount_name, arg); filename = mounted_filename; #else filename = arg; #endif AP4_Result result = AP4_FileByteStream::Create(filename, AP4_FileByteStream::STREAM_MODE_READ, input); #ifdef __EMSCRIPTEN__ free(mounted_filename); mounted_filename = NULL; #endif if (AP4_FAILED(result)) { fprintf(stderr, "ERROR: cannot open input (%d)\n", result); return 1; } } }
int main() { pthread_t thread; pthread_create(&thread, NULL, thread_main, NULL); EM_ASM(Module['noExitRuntime']=true); }
int main() { EM_ASM( out("hello world…") ); }
int main() { printf("EM_ASM: Simple expression without trailing semicolon\n"); EM_ASM(console.log('1. expression without trailing semicolon')); EM_ASM("console.log('2. expression without trailing semicolon')"); EM_ASM({"console.log('3. expression without trailing semicolon')"}); EM_ASM({console.log('4. expression without trailing semicolon')}); EM_ASM("{console.log('5. expression without trailing semicolon')}"); printf("\nEM_ASM: Double quotes\n"); EM_ASM(console.log("1. string in double quotes")); EM_ASM("console.log(\"2. string in double quotes\")"); EM_ASM({"console.log(\"3. string in double quotes\")"}); EM_ASM({console.log("4. string in double quotes")}); EM_ASM("{console.log(\"5. string in double quotes\")}"); printf("\nEM_ASM: Double quotes inside a string\n"); EM_ASM(console.log('1. this is \"double\" \"quotes\"')); EM_ASM(console.log('2. this is "double" "quotes" without escaping')); EM_ASM("console.log('3. this is \"double\" \"quotes\"')"); EM_ASM({"console.log('4. this is \"double\" \"quotes\"')"}); EM_ASM({console.log('5. this is \"double\" \"quotes\"')}); EM_ASM({console.log('6. this is "double" "quotes" without esacping')}); EM_ASM("{console.log('7. this is \"double\" \"quotes\"')}"); printf("\nEM_ASM: Pass a string\n"); EM_ASM(console.log('1. hello ' + UTF8ToString($0)), "world!"); EM_ASM("console.log('2. hello ' + UTF8ToString($0))", "world!"); EM_ASM({"console.log('3. hello ' + UTF8ToString($0))"}, "world!"); EM_ASM({console.log('4. hello ' + UTF8ToString($0))}, "world!"); EM_ASM("{console.log('5. hello ' + UTF8ToString($0))}", "world!"); printf("\nEM_ASM: Simple expression without trailing semicolon, wrap code block in extra parentheses\n"); EM_ASM((console.log('1. expression without trailing semicolon, in parentheses'))); EM_ASM(("console.log('2. expression without trailing semicolon, in parentheses')")); EM_ASM(({"console.log('3. expression without trailing semicolon, in parentheses')"}));
void never() { EM_ASM({ alert('this should never be reached! runtime must not be shut down!') }); assert(0); while (1) {} }
char* EMSCRIPTEN_KEEPALIVE note(int n) { EM_ASM({ Module.noted = $0 }, (int)¬ed); EM_ASM({ out([$0, $1]) }, n, noted);