v / vlib / coroutines / sp_corrector.c
32 lines · 31 sloc · 1.12 KB · 5a532ead435add329f6b53902379d90317a80a69
Raw
1// TODO: convert this to v code? handle all platforms
2// currently if it's not macos or win it defaults to the linux impl
3
4static void sp_corrector(void** sp_ptr, void* tid) {
5 size_t stack_size;
6 char* stack_addr;
7#ifdef __APPLE__
8 stack_size = pthread_get_stacksize_np((pthread_t)tid);
9 stack_addr = (char*) pthread_get_stackaddr_np((pthread_t)tid);
10#elif defined(_WIN64)
11 ULONG_PTR stack_low, stack_high;
12 GetCurrentThreadStackLimits(&stack_low, &stack_high);
13 stack_size = stack_high - stack_low;
14 stack_addr = (char*)stack_low;
15// #elif defined(__linux__)
16// pthread_attr_t gattr;
17// pthread_getattr_np((pthread_t)tid, &gattr);
18// pthread_attr_getstack(&gattr, (void**)&stack_addr, &stack_size);
19// pthread_attr_destroy(&gattr);
20// #else
21// assert("unsupported platform");
22#else
23 pthread_attr_t gattr;
24 pthread_getattr_np((pthread_t)tid, &gattr);
25 pthread_attr_getstack(&gattr, (void**)&stack_addr, &stack_size);
26 pthread_attr_destroy(&gattr);
27#endif
28 char *sp = (char*)*sp_ptr;
29 if(sp <= stack_addr || sp >= stack_addr+stack_size) {
30 *sp_ptr = (void*)stack_addr;
31 }
32}
33