Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/libfoo.dylib -install_name $RUN_DIR/libfoo.dylib // BUILD: $CXX main.cpp -std=c++11 $BUILD_DIR/libfoo.dylib -o $BUILD_DIR/thread-local-destructors.exe // RUN: ./thread-local-destructors.exe #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <assert.h> #include <unistd.h> #include <TargetConditionals.h> #include "test_support.h" static pthread_t sMainThread; static pthread_t sWorker1; static pthread_t sWorker2; static bool sMainThreadInitialized = false; static bool sWorker1Initialized = false; static bool sWorker2Initialized = false; static bool sMainThreadFinalized = false; static bool sWorker1Finalized = false; static bool sWorker2Finalized = false; struct Struct { Struct() : sInitializer(getInitializer()), sFinalizer(getFinalizer()) { sInitializer = true; } ~Struct() { sFinalizer = true; } bool& getInitializer() { if (pthread_equal(pthread_self(), sMainThread)) { return sMainThreadInitialized; } if (pthread_equal(pthread_self(), sWorker1)) { return sWorker1Initialized; } if (pthread_equal(pthread_self(), sWorker2)) { return sWorker2Initialized; } assert(false); } bool& getFinalizer() { if (pthread_equal(pthread_self(), sMainThread)) { return sMainThreadFinalized; } if (pthread_equal(pthread_self(), sWorker1)) { return sWorker1Finalized; } if (pthread_equal(pthread_self(), sWorker2)) { return sWorker2Finalized; } assert(false); } // TLVs are laxily initialized so have something to do to trigger init void doWork() { } bool& sInitializer; bool& sFinalizer; }; static thread_local Struct s; // Add another thread local so that we test dyld's ability to grow the vector of tlv atexits to run static bool sMainThreadInitialized_Another = false; static bool sWorker1Initialized_Another = false; static bool sWorker2Initialized_Another = false; static bool sMainThreadFinalized_Another = false; static bool sWorker1Finalized_Another = false; static bool sWorker2Finalized_Another = false; struct AnotherStruct { AnotherStruct() : sInitializer(getInitializer()), sFinalizer(getFinalizer()) { sInitializer = true; } ~AnotherStruct() { sFinalizer = true; } bool& getInitializer() { if (pthread_equal(pthread_self(), sMainThread)) { return sMainThreadInitialized_Another; } if (pthread_equal(pthread_self(), sWorker1)) { return sWorker1Initialized_Another; } if (pthread_equal(pthread_self(), sWorker2)) { return sWorker2Initialized_Another; } assert(false); } bool& getFinalizer() { if (pthread_equal(pthread_self(), sMainThread)) { return sMainThreadFinalized_Another; } if (pthread_equal(pthread_self(), sWorker1)) { return sWorker1Finalized_Another; } if (pthread_equal(pthread_self(), sWorker2)) { return sWorker2Finalized_Another; } assert(false); } // TLVs are laxily initialized so have something to do to trigger init void doWork() { } bool& sInitializer; bool& sFinalizer; }; static thread_local AnotherStruct sAnotherStruct; static void* work2(void* arg) { s.doWork(); return NULL; } static void* work1(void* arg) { s.doWork(); if ( pthread_create(&sWorker2, NULL, work2, NULL) != 0 ) { FAIL("pthread_create"); } void* dummy; pthread_join(sWorker2, &dummy); return NULL; } bool passedChecksInMain = false; void checkMainThreadFinalizer() { if ( !passedChecksInMain ) return; bool shouldFinalize = true; if ( sMainThreadFinalized != shouldFinalize ) FAIL("Main thread finalisation not as expected"); else if ( sMainThreadFinalized_Another != shouldFinalize ) FAIL("Main thread other struct finalisation not as expected"); else PASS("Success"); } int main(int argc, const char* argv[], const char* envp[], const char* apple[]) { sMainThread = pthread_self(); s.doWork(); sAnotherStruct.doWork(); // Note, as of writing this is being run after the tlv finalizer for this thread // so this should check that this order continues to be used. atexit(&checkMainThreadFinalizer); if ( pthread_create(&sWorker1, NULL, work1, NULL) != 0 ) { FAIL("pthread_create"); } void* dummy; pthread_join(sWorker1, &dummy); // validate each thread had different addresses for all TLVs if ( !sMainThreadInitialized ) FAIL("Main thread was not initialized"); else if ( !sWorker1Initialized ) FAIL("Thread 1 was not initialized"); else if ( !sWorker2Initialized ) FAIL("Thread 2 was not initialized"); else if ( !sWorker1Finalized ) FAIL("Thread 1 was not finalised"); else if ( !sWorker2Finalized ) FAIL("Thread 2 was not finalised"); else if ( !sMainThreadInitialized_Another ) FAIL("Main thread other variable was not initialized"); else if ( !sWorker1Initialized_Another ) FAIL("Thread 1 other variable was not initialized"); else if ( !sWorker2Initialized_Another ) FAIL("Thread 2 other variable was not initialized"); else if ( !sWorker1Finalized_Another ) FAIL("Thread 1 other variable was not finalised"); else if ( !sWorker2Finalized_Another ) FAIL("Thread 2 other variable was not finalised"); else passedChecksInMain = true; return 0; } |