blob: 80e866aebdd106b02f29b48f5951f881a37f7d77 [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
Henrik Kjellanderc0362762017-06-29 08:03:04 +020019#ifndef WEBRTC_RTC_BASE_THREAD_ANNOTATIONS_H_
20#define WEBRTC_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))
danilchap9a2d2dd2017-09-06 01:19:03 -070023#define RTC_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024#else
danilchap9a2d2dd2017-09-06 01:19:03 -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.
Chris Mumford0ed6de42017-08-14 14:33:58 -070033#if !defined(GUARDED_BY)
danilchap9a2d2dd2017-09-06 01:19:03 -070034#define GUARDED_BY(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
Chris Mumford0ed6de42017-08-14 14:33:58 -070035#endif
danilchap9a2d2dd2017-09-06 01:19:03 -070036#define RTC_GUARDED_VAR RTC_THREAD_ANNOTATION_ATTRIBUTE__(guarded_var)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037
38// Document if the memory location pointed to by a pointer should be guarded
39// by a lock when dereferencing the pointer. Similar to GUARDED_VAR,
40// PT_GUARDED_VAR is primarily used when the client cannot express the name
41// of the lock. Note that a pointer variable to a shared memory location
42// could itself be a shared variable. For example, if a shared global pointer
43// q, which is guarded by mu1, points to a shared memory location that is
44// guarded by mu2, q should be annotated as follows:
45// int *q GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
danilchap9a2d2dd2017-09-06 01:19:03 -070046#define RTC_PT_GUARDED_BY(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
47#define RTC_PT_GUARDED_VAR RTC_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_var)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048
49// Document the acquisition order between locks that can be held
50// simultaneously by a thread. For any two locks that need to be annotated
51// to establish an acquisition order, only one of them needs the annotation.
52// (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER
53// and ACQUIRED_BEFORE.)
Chris Mumford0ed6de42017-08-14 14:33:58 -070054#if !defined(ACQUIRED_AFTER)
danilchap9a2d2dd2017-09-06 01:19:03 -070055#define ACQUIRED_AFTER(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x))
Chris Mumford0ed6de42017-08-14 14:33:58 -070056#endif
57#if !defined(ACQUIRED_BEFORE)
danilchap9a2d2dd2017-09-06 01:19:03 -070058#define ACQUIRED_BEFORE(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x))
Chris Mumford0ed6de42017-08-14 14:33:58 -070059#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060
61// The following three annotations document the lock requirements for
62// functions/methods.
63
64// Document if a function expects certain locks to be held before it is called
Chris Mumford0ed6de42017-08-14 14:33:58 -070065#if !defined(EXCLUSIVE_LOCKS_REQUIRED)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066#define EXCLUSIVE_LOCKS_REQUIRED(...) \
danilchap9a2d2dd2017-09-06 01:19:03 -070067 RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__))
Chris Mumford0ed6de42017-08-14 14:33:58 -070068#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069
Chris Mumford0ed6de42017-08-14 14:33:58 -070070#if !defined(SHARED_LOCKS_REQUIRED)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020071#define SHARED_LOCKS_REQUIRED(...) \
danilchap9a2d2dd2017-09-06 01:19:03 -070072 RTC_THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__))
Chris Mumford0ed6de42017-08-14 14:33:58 -070073#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074
75// Document the locks acquired in the body of the function. These locks
76// cannot be held when calling this function (as google3's Mutex locks are
77// non-reentrant).
Chris Mumford0ed6de42017-08-14 14:33:58 -070078#if !defined(LOCKS_EXCLUDED)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079#define LOCKS_EXCLUDED(...) \
danilchap9a2d2dd2017-09-06 01:19:03 -070080 RTC_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
Chris Mumford0ed6de42017-08-14 14:33:58 -070081#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020082
83// Document the lock the annotated function returns without acquiring it.
danilchap9a2d2dd2017-09-06 01:19:03 -070084#define RTC_LOCK_RETURNED(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020085
86// Document if a class/type is a lockable type (such as the Mutex class).
danilchap9a2d2dd2017-09-06 01:19:03 -070087#define RTC_LOCKABLE RTC_THREAD_ANNOTATION_ATTRIBUTE__(lockable)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088
89// Document if a class is a scoped lockable type (such as the MutexLock class).
danilchap9a2d2dd2017-09-06 01:19:03 -070090#define RTC_SCOPED_LOCKABLE RTC_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091
92// The following annotations specify lock and unlock primitives.
danilchap9a2d2dd2017-09-06 01:19:03 -070093#define RTC_EXCLUSIVE_LOCK_FUNCTION(...) \
94 RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020095
danilchap9a2d2dd2017-09-06 01:19:03 -070096#define RTC_SHARED_LOCK_FUNCTION(...) \
97 RTC_THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020098
danilchap9a2d2dd2017-09-06 01:19:03 -070099#define RTC_EXCLUSIVE_TRYLOCK_FUNCTION(...) \
100 RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101
danilchap9a2d2dd2017-09-06 01:19:03 -0700102#define RTC_SHARED_TRYLOCK_FUNCTION(...) \
103 RTC_THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104
danilchap9a2d2dd2017-09-06 01:19:03 -0700105#define RTC_UNLOCK_FUNCTION(...) \
106 RTC_THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107
108// An escape hatch for thread safety analysis to ignore the annotated function.
Chris Mumford0ed6de42017-08-14 14:33:58 -0700109#if !defined(NO_THREAD_SAFETY_ANALYSIS)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110#define NO_THREAD_SAFETY_ANALYSIS \
danilchap9a2d2dd2017-09-06 01:19:03 -0700111 RTC_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
Chris Mumford0ed6de42017-08-14 14:33:58 -0700112#endif
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +0000113
Henrik Kjellanderc0362762017-06-29 08:03:04 +0200114#endif // WEBRTC_RTC_BASE_THREAD_ANNOTATIONS_H_