blob: 0c67fd966674ec124e359683de641ac779d90868 [file] [log] [blame]
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001// Copyright 2014 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.concurrent;
15
16import java.lang.annotation.Documented;
17import java.lang.annotation.ElementType;
18import java.lang.annotation.Retention;
19import java.lang.annotation.RetentionPolicy;
20import java.lang.annotation.Target;
21
22/**
23 * Define some standard attributes for documenting thread safety properties.
24 *<p>
25 * The names used here are adapted from those used in Joshua Bloch's book
26 * "Effective Java", which are also described at
27 * <http://www-128.ibm.com/developerworks/java/library/j-jtp09263.html>.
28 *<p>
29 * These attributes are just documentation. They don't have any run-time
30 * effect. The main aim is mainly just to standardize the terminology.
31 * (However, if this catches on, I can also imagine in the future having
32 * a presubmit check that checks that all new classes have thread safety
33 * annotations :)
34 *<p>
35 * See ThreadSafetyTest for examples of how these attributes should be used.
36 */
37public class ThreadSafety {
38 /**
39 * The Immutable attribute indicates that instances of the class are
40 * immutable, or at least appear that way are far as their external API
41 * is concerned. Immutable classes are usually also ThreadSafe,
42 * but can be ThreadHostile if they perform unsynchronized access to
43 * mutable static data. (We deviate from Bloch's nomenclature by
44 * not assuming that Immutable implies ThreadSafe; developers should
45 * explicitly annotate classes as both Immutable and ThreadSafe when
46 * appropriate.)
47 */
48 @Documented
49 @Target(value = {ElementType.TYPE})
50 @Retention(RetentionPolicy.RUNTIME)
51 public @interface Immutable {}
52
53 /**
54 * The ThreadSafe attribute marks a class or method which can safely be used
55 * from multiple threads without any need for external synchronization.
56 *
57 * When applied to a class, this attribute indicates that instances
58 * of the class can safely be used concurrently from multiple threads
59 * without any need for external synchronization, i.e. that all non-static methods
60 * are thread-safe (except any private methods that are explicitly
61 * annotated with a different thread safety annotation). In addition it
62 * also indicates that all non-static nested classes are thread-safe (except any private
63 * nested classes that are explicitly annotated with a different thread
64 * safety annotation). Note that no guarantees are made about static class methods or static
65 * nested classes - they should be annotated separately.
66 *
67 * When applied to a method, this attribute indicates that the
68 * method can safely be called concurrently from multiple threads.
69 * The implementation must synchronize any accesses to mutable data.
70 */
71 @Documented
72 @Target(value = {ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.TYPE})
73 @Retention(RetentionPolicy.SOURCE)
74 public @interface ThreadSafe {}
75
76 /**
77 * The ThreadCompatible attribute marks a class or method that
78 * is thread-safe provided that only one thread attempts to
79 * access each object at a time.
80 *
81 * The implementation of such a class must synchronize accesses
82 * to mutable static data, but can assume that each instance will
83 * only be accessed from one thread at a time.
84 *
85 * The client must obtain an appropriate lock before calling ThreadCompatible
86 * methods, or must otherwise ensure that only one thread calls such methods.
87 * Unless otherwise specified, an appropriate lock means synchronizing on the
88 * instance.
89 *
90 * A ThreadCompatible class may contain private methods or private nested
91 * classes that are not ThreadCompatible provided that they are explicitly
92 * annotated with a different thread safety annotation.
93 */
94 @Documented
95 @Target(value = {ElementType.METHOD, ElementType.TYPE})
96 @Retention(RetentionPolicy.SOURCE)
97 public @interface ThreadCompatible {}
98
99 /**
100 * The ThreadHostile attribute marks a class or method that
101 * can't safely be used by multiple threads, for example because
102 * it performs unsynchronized access to mutable static objects.
103 */
104 @Documented
105 @Target(value = {ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.TYPE})
106 @Retention(RetentionPolicy.SOURCE)
107 public @interface ThreadHostile {}
108
109 /**
110 * The ConditionallyThreadSafe attribute marks a class that contains
111 * some methods (or nested classes) which are ThreadSafe but others which are
112 * only ThreadCompatible or ThreadHostile.
113 *
114 * The methods (and nested classes) of a ConditionallyThreadSafe class should
115 * each have their thread safety marked.
116 */
117 @Documented
118 @Target(value = {ElementType.METHOD, ElementType.TYPE})
119 @Retention(RetentionPolicy.SOURCE)
120 public @interface ConditionallyThreadSafe {}
121
122 /**
123 * The ConditionallyThreadCompatible attribute marks a class that contains
124 * some methods (or nested classes) which are ThreadCompatible but others
125 * which are ThreadHostile.
126 *
127 * The methods (and nested classes) of a ConditionallyThreadCompatible class
128 * should each have their thread safety marked.
129 */
130 @Documented
131 @Target(value = {ElementType.METHOD, ElementType.TYPE})
132 @Retention(RetentionPolicy.SOURCE)
133 public @interface ConditionallyThreadCompatible {}
134
135}