Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/libc/crt1_proxy_main.c
6162 views
1
/*
2
* Copyright 2021 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
8
#include <pthread.h>
9
#include <stdlib.h>
10
11
#include <emscripten.h>
12
#include <emscripten/stack.h>
13
#include <emscripten/threading.h>
14
#include <emscripten/eventloop.h>
15
16
#include "threading_internal.h"
17
18
static int _main_argc;
19
static char** _main_argv;
20
21
int __main_argc_argv(int argc, char *argv[]);
22
23
weak int __main_void(void) {
24
return __main_argc_argv(_main_argc, _main_argv);
25
}
26
27
static void* _main_thread(void* param) {
28
// This is the main runtime thread for the application.
29
emscripten_set_thread_name(pthread_self(), "Application main thread");
30
// Will either call user's __main_void or weak version above.
31
int rtn = __main_void();
32
if (!emscripten_runtime_keepalive_check()) {
33
exit(rtn);
34
}
35
return NULL;
36
}
37
38
EMSCRIPTEN_KEEPALIVE int _emscripten_proxy_main(int argc, char** argv) {
39
pthread_attr_t attr;
40
pthread_attr_init(&attr);
41
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
42
// Use the size of the current stack, which is the normal size of the stack
43
// that main() would have without PROXY_TO_PTHREAD.
44
pthread_attr_setstacksize(&attr, emscripten_stack_get_base() - emscripten_stack_get_end());
45
// Pass special ID -1 to the list of transferred canvases to denote that the
46
// thread creation should instead take a list of canvases that are specified
47
// from the command line with -sOFFSCREENCANVASES_TO_PTHREAD linker flag.
48
emscripten_pthread_attr_settransferredcanvases(&attr, (const char*)-1);
49
_main_argc = argc;
50
_main_argv = argv;
51
pthread_t thread;
52
int rc = pthread_create(&thread, &attr, _main_thread, NULL);
53
pthread_attr_destroy(&attr);
54
if (rc == 0) {
55
// Mark the thread as strongly referenced, so that Node.js doesn't exit
56
// while the pthread is running.
57
_emscripten_thread_set_strongref(thread);
58
}
59
return rc;
60
}
61
62