|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +// Copyright 2017 The Fuchsia Authors. All rights reserved. |
| 21 | +// Use of this source code is governed by a BSD-style license that can be |
| 22 | +// found in the LICENSE file. |
| 23 | + |
| 24 | +#pragma once |
| 25 | + |
| 26 | +// This header defines thread annotation macros to be used everywhere in Zircon |
| 27 | +// outside of publicly exposed headers. See system/public/zircon/compiler.h for |
| 28 | +// the publicly exported macros. |
| 29 | + |
| 30 | +// The thread safety analysis system is documented at |
| 31 | +// http://clang.llvm.org/docs/ThreadSafetyAnalysis.html and its use in Zircon is documented at |
| 32 | +// docs/thread_annotations.md. The macros we use are: |
| 33 | +// |
| 34 | +// TA_CAP(x) |x| is the capability this type represents, e.g. "mutex". |
| 35 | +// TA_GUARDED(x) the annotated variable is guarded by the capability (e.g. lock) |x| |
| 36 | +// TA_ACQ(x) function acquires the mutex |x| |
| 37 | +// TA_ACQ_SHARED(x) function acquires the mutex |x| for shared reading |
| 38 | +// TA_ACQ_BEFORE(x) Indicates that if both this mutex and muxex |x| are to be acquired, |
| 39 | +// that this mutex must be acquired before mutex |x|. |
| 40 | +// TA_ACQ_AFTER(x) Indicates that if both this mutex and muxex |x| are to be acquired, |
| 41 | +// that this mutex must be acquired after mutex |x|. |
| 42 | +// TA_TRY_ACQ(bool, x) function acquires the mutex |x| if the function returns |bool| |
| 43 | +// TA_TRY_ACQ_SHARED(bool, x) function acquires the mutex |x| for shared reading if the function |
| 44 | +// returns |bool| |
| 45 | +// TA_REL(x) function releases the mutex |x| |
| 46 | +// TA_REL_SHARED(x) function releases the shared for reading mutex |x| |
| 47 | +// TA_ASSERT(x) function asserts that |x| is held |
| 48 | +// TA_ASSERT_SHARED(x) function asserts that |x| is held for shared reading |
| 49 | +// TA_REQ(x) function requires that the caller hold the mutex |x| |
| 50 | +// TA_REQ_SHARED(x) function requires that the caller hold the mutex |x| for shared |
| 51 | +// reading TA_EXCL(x) function requires that the caller not be holding the mutex |
| 52 | +// |x| TA_RET_CAP(x) function returns a reference to the mutex |x| TA_SCOPED_CAP type |
| 53 | +// represents a scoped or RAII-style wrapper around a capability TA_NO_THREAD_SAFETY_ANALYSIS |
| 54 | +// function is excluded entirely from thread safety analysis |
| 55 | + |
| 56 | +#ifdef __clang__ |
| 57 | +#define TA_SUPPRESS _Pragma("clang diagnostic ignored \"-Wthread-safety-analysis\"") |
| 58 | +#else |
| 59 | +#define TA_SUPPRESS |
| 60 | +#endif |
| 61 | + |
| 62 | +#ifdef __clang__ |
| 63 | +#define THREAD_ANNOTATION(x) __attribute__((x)) |
| 64 | +#else |
| 65 | +#define THREAD_ANNOTATION(x) |
| 66 | +#endif |
| 67 | + |
| 68 | +#define TA_CAP(x) THREAD_ANNOTATION(capability(x)) |
| 69 | +#define TA_GUARDED(x) THREAD_ANNOTATION(guarded_by(x)) |
| 70 | +#define TA_ACQ(...) THREAD_ANNOTATION(acquire_capability(__VA_ARGS__)) |
| 71 | +#define TA_ACQ_SHARED(...) THREAD_ANNOTATION(acquire_shared_capability(__VA_ARGS__)) |
| 72 | +#define TA_ACQ_BEFORE(...) THREAD_ANNOTATION(acquired_before(__VA_ARGS__)) |
| 73 | +#define TA_ACQ_AFTER(...) THREAD_ANNOTATION(acquired_after(__VA_ARGS__)) |
| 74 | +#define TA_TRY_ACQ(...) THREAD_ANNOTATION(try_acquire_capability(__VA_ARGS__)) |
| 75 | +#define TA_TRY_ACQ_SHARED(...) THREAD_ANNOTATION(try_acquire_shared_capability(__VA_ARGS__)) |
| 76 | +#define TA_REL(...) THREAD_ANNOTATION(release_capability(__VA_ARGS__)) |
| 77 | +#define TA_REL_SHARED(...) THREAD_ANNOTATION(release_shared_capability(__VA_ARGS__)) |
| 78 | +#define TA_REL_GENERIC(...) THREAD_ANNOTATION(release_generic_capability(__VA_ARGS__)) |
| 79 | +#define TA_ASSERT(...) THREAD_ANNOTATION(assert_capability(__VA_ARGS__)) |
| 80 | +#define TA_ASSERT_SHARED(...) THREAD_ANNOTATION(assert_shared_capability(__VA_ARGS__)) |
| 81 | +#define TA_REQ(...) THREAD_ANNOTATION(requires_capability(__VA_ARGS__)) |
| 82 | +#define TA_REQ_SHARED(...) THREAD_ANNOTATION(requires_shared_capability(__VA_ARGS__)) |
| 83 | +#define TA_EXCL(...) THREAD_ANNOTATION(locks_excluded(__VA_ARGS__)) |
| 84 | +#define TA_RET_CAP(x) THREAD_ANNOTATION(lock_returned(x)) |
| 85 | +#define TA_SCOPED_CAP THREAD_ANNOTATION(scoped_lockable) |
| 86 | +#define TA_NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION(no_thread_safety_analysis) |
0 commit comments