1// Copyright 2017 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "dso-ctor.h"
6
7#include <unittest/unittest.h>
8#include <sched.h>
9#include <stdbool.h>
10
11static bool dso_ctor_ran;
12
13static struct Global {
14    Global() { dso_ctor_ran = true; }
15    ~Global() {
16        // This is just some random nonempty thing that the compiler
17        // can definitely never decide to optimize away.  We can't
18        // easily test that the destructor got run, but we can ensure
19        // that using a static destructor compiles and links correctly.
20        sched_yield();
21    }
22} global;
23
24bool check_dso_ctor() {
25    BEGIN_HELPER;
26    EXPECT_TRUE(dso_ctor_ran, "DSO global constuctor didn't run!");
27    END_HELPER;
28}
29
30static bool dso_tlocal_ctor_ran, dso_tlocal_dtor_ran;
31static thread_local ThreadLocal<&dso_tlocal_ctor_ran,
32                                &dso_tlocal_dtor_ran> dso_tlocal;
33
34bool check_dso_tlocal_in_thread() {
35    BEGIN_HELPER;
36    EXPECT_TRUE(decltype(dso_tlocal)::check_before_reference());
37    dso_tlocal.flag = true;
38    EXPECT_TRUE(decltype(dso_tlocal)::check_after_reference());
39    END_HELPER;
40}
41
42bool check_dso_tlocal_after_join() {
43    BEGIN_HELPER;
44    EXPECT_TRUE(decltype(dso_tlocal)::check_after_join());
45    END_HELPER;
46}
47