Path: blob/main/system/lib/libc/crt1_proxy_main.c
6162 views
/*1* Copyright 2021 The Emscripten Authors. All rights reserved.2* Emscripten is available under two separate licenses, the MIT license and the3* University of Illinois/NCSA Open Source License. Both these licenses can be4* found in the LICENSE file.5*/67#include <pthread.h>8#include <stdlib.h>910#include <emscripten.h>11#include <emscripten/stack.h>12#include <emscripten/threading.h>13#include <emscripten/eventloop.h>1415#include "threading_internal.h"1617static int _main_argc;18static char** _main_argv;1920int __main_argc_argv(int argc, char *argv[]);2122weak int __main_void(void) {23return __main_argc_argv(_main_argc, _main_argv);24}2526static void* _main_thread(void* param) {27// This is the main runtime thread for the application.28emscripten_set_thread_name(pthread_self(), "Application main thread");29// Will either call user's __main_void or weak version above.30int rtn = __main_void();31if (!emscripten_runtime_keepalive_check()) {32exit(rtn);33}34return NULL;35}3637EMSCRIPTEN_KEEPALIVE int _emscripten_proxy_main(int argc, char** argv) {38pthread_attr_t attr;39pthread_attr_init(&attr);40pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);41// Use the size of the current stack, which is the normal size of the stack42// that main() would have without PROXY_TO_PTHREAD.43pthread_attr_setstacksize(&attr, emscripten_stack_get_base() - emscripten_stack_get_end());44// Pass special ID -1 to the list of transferred canvases to denote that the45// thread creation should instead take a list of canvases that are specified46// from the command line with -sOFFSCREENCANVASES_TO_PTHREAD linker flag.47emscripten_pthread_attr_settransferredcanvases(&attr, (const char*)-1);48_main_argc = argc;49_main_argv = argv;50pthread_t thread;51int rc = pthread_create(&thread, &attr, _main_thread, NULL);52pthread_attr_destroy(&attr);53if (rc == 0) {54// Mark the thread as strongly referenced, so that Node.js doesn't exit55// while the pthread is running.56_emscripten_thread_set_strongref(thread);57}58return rc;59}606162