1/*
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package compiler.testlibrary.rtm;
25
26import jdk.test.lib.Asserts;
27
28import java.util.HashMap;
29import java.util.Map;
30
31/**
32 * Type of transactional execution abort.
33 * For more details on different abort types please see
34 * shared/vm/runtime/rtmLocking.hpp
35 */
36public enum AbortType {
37    XABORT(0),
38    RETRIABLE(1),
39    MEM_CONFLICT(2),
40    BUF_OVERFLOW(3),
41    DEBUG_BREAKPOINT(4),
42    NESTED_ABORT(5);
43
44    private final int type;
45    private static final Map<Integer, AbortType> LOOKUP_MAP = new HashMap<>();
46
47    static {
48        for (AbortType abortType : AbortType.values()) {
49            Asserts.assertFalse(LOOKUP_MAP.containsKey(abortType.type),
50                    "Abort type values should be unique.");
51            LOOKUP_MAP.put(abortType.type, abortType);
52        }
53    }
54
55    private AbortType(int type) {
56        this.type = type;
57    }
58
59    /**
60     * Returns AbortProvoker for aborts represented by this abort type.
61     *
62     * @return an AbortProvoker instance
63     */
64    public AbortProvoker provoker() {
65        return AbortType.createNewProvoker(this);
66    }
67
68    public static AbortType lookup(int type) {
69        Asserts.assertLT(type, AbortType.values().length,
70                "Unknown abort type.");
71        return LOOKUP_MAP.get(type);
72    }
73
74    /**
75     * Returns transaction execution abort provoker for specified abortion type.
76     *
77     * @param type a type of abort which will be forced by returned
78     *             AbortProvoker instance.
79     * @return AbortProvoker instance that will force abort of specified type
80     * @throws RuntimeException if there is no provoker for specified type
81     */
82    private static AbortProvoker createNewProvoker(AbortType type) {
83        switch (type) {
84            case XABORT:
85                return new XAbortProvoker();
86            case MEM_CONFLICT:
87                return new MemoryConflictProvoker();
88            case BUF_OVERFLOW:
89                return new BufferOverflowProvoker();
90            case NESTED_ABORT:
91                return new NestedAbortProvoker();
92            default:
93                throw new RuntimeException("No provoker exists for type "
94                        + type.name());
95        }
96    }
97
98    @Override
99    public String toString() {
100        return Integer.toString(type);
101    }
102}
103