1 // Copyright 2019 The Emscripten Authors.  All rights reserved.
2 // Emscripten is available under two separate licenses, the MIT license and the
3 // University of Illinois/NCSA Open Source License.  Both these licenses can be
4 // found in the LICENSE file.
5 
6 #include <pthread.h>
7 #include <sys/types.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <emscripten.h>
12 #include <emscripten/threading.h>
13 
thread_start(void * arg)14 static void *thread_start(void *arg)
15 {
16   // allocate more memory than we currently have, forcing a growth
17   printf("thread_start\n");
18   char* buffer = (char*)malloc(64 * 1024 * 1024);
19   assert(buffer);
20   *buffer = 42;
21   pthread_exit((void*)buffer);
22 }
23 
main()24 int main()
25 {
26   printf("prep\n");
27   if (!emscripten_has_threading_support())
28   {
29 #ifdef REPORT_RESULT
30     REPORT_RESULT(6765);
31 #endif
32     printf("Skipped: Threading is not supported.\n");
33     return 0;
34   }
35 
36   pthread_t thr;
37 
38   printf("start\n");
39   EM_ASM({ assert(HEAP8.length === 32 * 1024 * 1024, "start at 32MB") });
40 
41   printf("create\n");
42   int s = pthread_create(&thr, NULL, thread_start, (void*)NULL);
43   assert(s == 0);
44   void* result = NULL;
45 
46   printf("join\n");
47   s = pthread_join(thr, &result);
48   assert(result != 0); // allocation should have succeeded
49   char* buffer = (char*)result;
50   assert(*buffer == 42); // should see the value the thread wrote
51   EM_ASM({ assert(HEAP8.length > 64 * 1024 * 1024, "end with >64MB") });
52 #ifdef REPORT_RESULT
53   REPORT_RESULT(1);
54 #endif
55 }
56 
57