|
| 1 | +#define _GNU_SOURCE |
| 2 | +#include <stdio.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <sys/types.h> |
| 5 | +#include <sys/mman.h> |
| 6 | +#include <sys/stat.h> |
| 7 | +#include <fcntl.h> |
| 8 | +#include <errno.h> |
| 9 | +#include <string.h> |
| 10 | +#include <stdlib.h> |
| 11 | +#include <dlfcn.h> |
| 12 | +#include <elf.h> |
| 13 | +#include <pthread.h> |
| 14 | +#include "app_lib.h" |
| 15 | + |
| 16 | +static __thread int tls_dont_care; /* just to avoid zero offsets everywhere else */ |
| 17 | + |
| 18 | +__thread int tls_exec; |
| 19 | +extern __thread int tls_shared; |
| 20 | + |
| 21 | +static __thread int tls_local_exec; |
| 22 | + |
| 23 | +int __attribute__((weak)) get_tls_exec(void) |
| 24 | +{ |
| 25 | + return tls_exec; |
| 26 | +} |
| 27 | + |
| 28 | +int __attribute__((weak)) get_tls_shared(void) |
| 29 | +{ |
| 30 | + return tls_shared; |
| 31 | +} |
| 32 | + |
| 33 | +int __attribute__((weak)) get_tls_local_exec(void) |
| 34 | +{ |
| 35 | + return tls_local_exec; |
| 36 | +} |
| 37 | + |
| 38 | +/* Forward declarations for recursive functions */ |
| 39 | +void func_a(int depth); |
| 40 | +void func_b(int depth); |
| 41 | +void func_c(int depth); |
| 42 | + |
| 43 | +static __always_inline void func_mux(int depth) |
| 44 | +{ |
| 45 | + if (depth <= 0) |
| 46 | + return; |
| 47 | + |
| 48 | + switch (rand() % 3) { |
| 49 | + case 0: func_a(depth - 1); break; |
| 50 | + case 1: func_b(depth - 1); break; |
| 51 | + case 2: func_c(depth - 1); break; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +void func_a(int depth) |
| 56 | +{ |
| 57 | + volatile char stack_space[120]; |
| 58 | + stack_space[119] = 'a'; |
| 59 | + stack_space[0] += 1; |
| 60 | + |
| 61 | + if (depth <= 0) |
| 62 | + return; |
| 63 | + |
| 64 | + func_mux(depth - 1); |
| 65 | +} |
| 66 | + |
| 67 | +void func_b(int depth) |
| 68 | +{ |
| 69 | + volatile char stack_space[350]; |
| 70 | + stack_space[349] = 'b'; |
| 71 | + stack_space[0] += 1; |
| 72 | + |
| 73 | + if (depth <= 0) |
| 74 | + return; |
| 75 | + |
| 76 | + func_mux(depth - 1); |
| 77 | +} |
| 78 | + |
| 79 | +void func_c(int depth) |
| 80 | +{ |
| 81 | + volatile char stack_space[800]; |
| 82 | + stack_space[799] = 'c'; |
| 83 | + stack_space[0] += 1; |
| 84 | + |
| 85 | + if (depth <= 0) |
| 86 | + return; |
| 87 | + |
| 88 | + func_mux(depth - 1); |
| 89 | +} |
| 90 | + |
| 91 | +static void *thread_func(void *arg) |
| 92 | +{ |
| 93 | + time_t last_print = 0; |
| 94 | + (void)arg; |
| 95 | + |
| 96 | + pthread_setname_np(pthread_self(), "app_thread"); |
| 97 | + |
| 98 | + while (1) { |
| 99 | + time_t now; |
| 100 | + |
| 101 | + func_mux(10); |
| 102 | + |
| 103 | + now = time(NULL); |
| 104 | + if (now > last_print) { |
| 105 | + tls_exec += 4; |
| 106 | + tls_shared += 8; |
| 107 | + tls_local_exec += 16; |
| 108 | + bump_tls_local_shared(); |
| 109 | + bump_tls_local_shared(); |
| 110 | + |
| 111 | + printf("Hello from thread (exec=%d, shared=%d, local_exec=%d, local_shared=%d)!\n", |
| 112 | + get_tls_exec(), get_tls_shared(), get_tls_local_exec(), get_tls_local_shared()); |
| 113 | + last_print = now; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return NULL; |
| 118 | +} |
| 119 | + |
| 120 | +int main() { |
| 121 | + pthread_t thread; |
| 122 | + |
| 123 | + pthread_create(&thread, NULL, thread_func, NULL); |
| 124 | + |
| 125 | + while (1) { |
| 126 | + tls_dont_care += 1; |
| 127 | + tls_exec += 2; |
| 128 | + tls_shared += 4; |
| 129 | + tls_local_exec += 8; |
| 130 | + bump_tls_local_shared(); |
| 131 | + |
| 132 | + printf("Hello from app (exec=%d, shared=%d, local_exec=%d, local_shared=%d)!\n", |
| 133 | + get_tls_exec(), get_tls_shared(), get_tls_local_exec(), get_tls_local_shared()); |
| 134 | + sleep(1); |
| 135 | + } |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
0 commit comments