blob: 1fefdf00c3773fb491b76a5a01f3c760ec116d1c [file] [log] [blame]
Dmitriy Ivanov6b566912014-04-29 08:41:29 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <string>
20
21// use external control number from main test
22static std::string* atexit_sequence = NULL;
23static bool* atexit_valid_this_in_static_dtor = NULL;
24
Dmitriy Ivanov6b566912014-04-29 08:41:29 -070025static class AtExitStaticClass {
Dmitriy Ivanov6396da92014-05-05 19:52:13 -070026 public:
27 AtExitStaticClass() { expected_this = this; }
Dmitriy Ivanov6b566912014-04-29 08:41:29 -070028 ~AtExitStaticClass() {
29 if (atexit_valid_this_in_static_dtor) {
Dmitriy Ivanov6396da92014-05-05 19:52:13 -070030 *atexit_valid_this_in_static_dtor = (expected_this == this);
Dmitriy Ivanov6b566912014-04-29 08:41:29 -070031 }
32 }
Dmitriy Ivanov6396da92014-05-05 19:52:13 -070033 private:
34 static const AtExitStaticClass* expected_this;
35
36} static_obj;
37
38const AtExitStaticClass* AtExitStaticClass::expected_this = NULL;
Dmitriy Ivanov6b566912014-04-29 08:41:29 -070039
40// 4
41static void atexit_handler_from_atexit_from_atexit2() {
42 *atexit_sequence += " on";
43}
44
45// 3
46static void atexit_handler_from_atexit_from_atexit1() {
47 *atexit_sequence += " sat";
48}
49
50// 2
51static void atexit_handler_from_atexit() {
52 *atexit_sequence += " Dumpty";
53 // register 2 others
54 atexit(atexit_handler_from_atexit_from_atexit2);
55 atexit(atexit_handler_from_atexit_from_atexit1);
56}
57
58// 1
59static void atexit_handler_with_atexit() {
60 *atexit_sequence += "Humpty";
61 atexit(atexit_handler_from_atexit);
62}
63
64// last
65static void atexit_handler_regular() {
66 *atexit_sequence += " a wall";
67}
68
69extern "C" void register_atexit(std::string* sequence, bool* valid_this_in_static_dtor) {
70 atexit_sequence = sequence;
71 atexit_valid_this_in_static_dtor = valid_this_in_static_dtor;
72 atexit(atexit_handler_regular);
73 atexit(atexit_handler_with_atexit);
74 atexit(NULL);
75}
76