blob: de0c5c90da7918196265da16b89e41614d87633d [file] [log] [blame]
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +00001//
2// Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3//
4// Use of this source code is governed by a BSD-style license
5// that can be found in the LICENSE file in the root of the source
6// tree. An additional intellectual property rights grant can be found
7// in the file PATENTS. All contributing project authors may
8// be found in the AUTHORS file in the root of the source tree.
9//
10// Borrowed from
11// https://code.google.com/p/gperftools/source/browse/src/base/thread_annotations.h
12// but adapted for clang attributes instead of the gcc.
13//
14// This header file contains the macro definitions for thread safety
15// annotations that allow the developers to document the locking policies
16// of their multi-threaded code. The annotations can also help program
17// analysis tools to identify potential thread safety issues.
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#ifndef RTC_BASE_THREAD_ANNOTATIONS_H_
20#define RTC_BASE_THREAD_ANNOTATIONS_H_
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000021
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022#if defined(__clang__) && (!defined(SWIG))
danilchap5301e3c2017-09-06 04:10:21 -070023#define RTC_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024#else
danilchap5301e3c2017-09-06 04:10:21 -070025#define RTC_THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026#endif
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000027
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020028// Document if a shared variable/field needs to be protected by a lock.
29// GUARDED_BY allows the user to specify a particular lock that should be
30// held when accessing the annotated variable, while GUARDED_VAR only
31// indicates a shared variable should be guarded (by any lock). GUARDED_VAR
32// is primarily used when the client cannot express the name of the lock.
danilchap5301e3c2017-09-06 04:10:21 -070033#define RTC_GUARDED_BY(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
danilchap5301e3c2017-09-06 04:10:21 -070034#define RTC_GUARDED_VAR RTC_THREAD_ANNOTATION_ATTRIBUTE__(guarded_var)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035
36// Document if the memory location pointed to by a pointer should be guarded
37// by a lock when dereferencing the pointer. Similar to GUARDED_VAR,
38// PT_GUARDED_VAR is primarily used when the client cannot express the name
39// of the lock. Note that a pointer variable to a shared memory location
40// could itself be a shared variable. For example, if a shared global pointer
41// q, which is guarded by mu1, points to a shared memory location that is
42// guarded by mu2, q should be annotated as follows:
43// int *q GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
danilchap5301e3c2017-09-06 04:10:21 -070044#define RTC_PT_GUARDED_BY(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
danilchap5301e3c2017-09-06 04:10:21 -070045#define RTC_PT_GUARDED_VAR RTC_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_var)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020046
47// Document the acquisition order between locks that can be held
48// simultaneously by a thread. For any two locks that need to be annotated
49// to establish an acquisition order, only one of them needs the annotation.
50// (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER
51// and ACQUIRED_BEFORE.)
danilchap5301e3c2017-09-06 04:10:21 -070052#define RTC_ACQUIRED_AFTER(x) \
53 RTC_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x))
danilchap5301e3c2017-09-06 04:10:21 -070054#define RTC_ACQUIRED_BEFORE(x) \
55 RTC_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056
57// The following three annotations document the lock requirements for
58// functions/methods.
59
60// Document if a function expects certain locks to be held before it is called
danilchap5301e3c2017-09-06 04:10:21 -070061#define RTC_EXCLUSIVE_LOCKS_REQUIRED(...) \
62 RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__))
danilchap5301e3c2017-09-06 04:10:21 -070063#define RTC_SHARED_LOCKS_REQUIRED(...) \
64 RTC_THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065
66// Document the locks acquired in the body of the function. These locks
67// cannot be held when calling this function (as google3's Mutex locks are
68// non-reentrant).
danilchap5301e3c2017-09-06 04:10:21 -070069#define RTC_LOCKS_EXCLUDED(...) \
70 RTC_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020071
72// Document the lock the annotated function returns without acquiring it.
danilchap5301e3c2017-09-06 04:10:21 -070073#define RTC_LOCK_RETURNED(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074
75// Document if a class/type is a lockable type (such as the Mutex class).
danilchap5301e3c2017-09-06 04:10:21 -070076#define RTC_LOCKABLE RTC_THREAD_ANNOTATION_ATTRIBUTE__(lockable)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020077
78// Document if a class is a scoped lockable type (such as the MutexLock class).
danilchap5301e3c2017-09-06 04:10:21 -070079#define RTC_SCOPED_LOCKABLE RTC_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020080
81// The following annotations specify lock and unlock primitives.
danilchap5301e3c2017-09-06 04:10:21 -070082#define RTC_EXCLUSIVE_LOCK_FUNCTION(...) \
83 RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084
danilchap5301e3c2017-09-06 04:10:21 -070085#define RTC_SHARED_LOCK_FUNCTION(...) \
86 RTC_THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087
danilchap5301e3c2017-09-06 04:10:21 -070088#define RTC_EXCLUSIVE_TRYLOCK_FUNCTION(...) \
89 RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020090
danilchap5301e3c2017-09-06 04:10:21 -070091#define RTC_SHARED_TRYLOCK_FUNCTION(...) \
92 RTC_THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093
danilchap5301e3c2017-09-06 04:10:21 -070094#define RTC_UNLOCK_FUNCTION(...) \
95 RTC_THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096
97// An escape hatch for thread safety analysis to ignore the annotated function.
danilchap5301e3c2017-09-06 04:10:21 -070098#define RTC_NO_THREAD_SAFETY_ANALYSIS \
99 RTC_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +0000100
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200101#endif // RTC_BASE_THREAD_ANNOTATIONS_H_