• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2008-2009, Google Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Neither the name of Google Inc. nor the names of its
11  * contributors may be used to endorse or promote products derived from
12  * this software without specific prior written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * ---
27  * Author: Kostya Serebryany
28  */
29 
30 /* This file defines dynamic annotations for use with dynamic analysis
31    tool such as valgrind, PIN, etc.
32 
33    Dynamic annotation is a source code annotation that affects
34    the generated code (that is, the annotation is not a comment).
35    Each such annotation is attached to a particular
36    instruction and/or to a particular object (address) in the program.
37 
38    The annotations that should be used by users are macros in all upper-case
39    (e.g., ANNOTATE_NEW_MEMORY).
40 
41    Actual implementation of these macros may differ depending on the
42    dynamic analysis tool being used.
43 
44    See http://code.google.com/p/data-race-test/  for more information.
45 
46    This file supports the following dynamic analysis tools:
47    - None (DYNAMIC_ANNOTATIONS_ENABLED is not defined or zero).
48       Macros are defined empty.
49    - ThreadSanitizer, Helgrind, DRD (DYNAMIC_ANNOTATIONS_ENABLED is 1).
50       Macros are defined as calls to non-inlinable empty functions
51       that are intercepted by Valgrind. */
52 
53 #ifndef __DYNAMIC_ANNOTATIONS_H__
54 #define __DYNAMIC_ANNOTATIONS_H__
55 
56 #ifndef DYNAMIC_ANNOTATIONS_PREFIX
57 # define DYNAMIC_ANNOTATIONS_PREFIX
58 #endif
59 
60 #ifndef DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND
61 # define DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND 1
62 #endif
63 
64 #ifdef DYNAMIC_ANNOTATIONS_WANT_ATTRIBUTE_WEAK
65 # ifdef __GNUC__
66 #  define DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK __attribute__((weak))
67 # else
68 /* TODO(glider): for Windows support we may want to change this macro in order
69    to prepend __declspec(selectany) to the annotations' declarations. */
70 #  error weak annotations are not supported for your compiler
71 # endif
72 #else
73 # define DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK
74 #endif
75 
76 /* The following preprocessor magic prepends the value of
77    DYNAMIC_ANNOTATIONS_PREFIX to annotation function names. */
78 #define DYNAMIC_ANNOTATIONS_GLUE0(A, B) A##B
79 #define DYNAMIC_ANNOTATIONS_GLUE(A, B) DYNAMIC_ANNOTATIONS_GLUE0(A, B)
80 #define DYNAMIC_ANNOTATIONS_NAME(name) \
81   DYNAMIC_ANNOTATIONS_GLUE(DYNAMIC_ANNOTATIONS_PREFIX, name)
82 
83 #ifndef DYNAMIC_ANNOTATIONS_ENABLED
84 # define DYNAMIC_ANNOTATIONS_ENABLED 0
85 #endif
86 
87 #if DYNAMIC_ANNOTATIONS_ENABLED != 0
88 
89   /* -------------------------------------------------------------
90      Annotations useful when implementing condition variables such as CondVar,
91      using conditional critical sections (Await/LockWhen) and when constructing
92      user-defined synchronization mechanisms.
93 
94      The annotations ANNOTATE_HAPPENS_BEFORE() and ANNOTATE_HAPPENS_AFTER() can
95      be used to define happens-before arcs in user-defined synchronization
96      mechanisms:  the race detector will infer an arc from the former to the
97      latter when they share the same argument pointer.
98 
99      Example 1 (reference counting):
100 
101      void Unref() {
102        ANNOTATE_HAPPENS_BEFORE(&refcount_);
103        if (AtomicDecrementByOne(&refcount_) == 0) {
104          ANNOTATE_HAPPENS_AFTER(&refcount_);
105          delete this;
106        }
107      }
108 
109      Example 2 (message queue):
110 
111      void MyQueue::Put(Type *e) {
112        MutexLock lock(&mu_);
113        ANNOTATE_HAPPENS_BEFORE(e);
114        PutElementIntoMyQueue(e);
115      }
116 
117      Type *MyQueue::Get() {
118        MutexLock lock(&mu_);
119        Type *e = GetElementFromMyQueue();
120        ANNOTATE_HAPPENS_AFTER(e);
121        return e;
122      }
123 
124      Note: when possible, please use the existing reference counting and message
125      queue implementations instead of inventing new ones. */
126 
127   /* Report that wait on the condition variable at address "cv" has succeeded
128      and the lock at address "lock" is held. */
129   #define ANNOTATE_CONDVAR_LOCK_WAIT(cv, lock) \
130     DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarWait)(__FILE__, __LINE__, cv, lock)
131 
132   /* Report that wait on the condition variable at "cv" has succeeded.  Variant
133      w/o lock. */
134   #define ANNOTATE_CONDVAR_WAIT(cv) \
135     DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarWait)(__FILE__, __LINE__, cv, NULL)
136 
137   /* Report that we are about to signal on the condition variable at address
138      "cv". */
139   #define ANNOTATE_CONDVAR_SIGNAL(cv) \
140     DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignal)(__FILE__, __LINE__, cv)
141 
142   /* Report that we are about to signal_all on the condition variable at address
143      "cv". */
144   #define ANNOTATE_CONDVAR_SIGNAL_ALL(cv) \
145     DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignalAll)(__FILE__, __LINE__, cv)
146 
147   /* Annotations for user-defined synchronization mechanisms. */
148   #define ANNOTATE_HAPPENS_BEFORE(obj) ANNOTATE_CONDVAR_SIGNAL(obj)
149   #define ANNOTATE_HAPPENS_AFTER(obj)  ANNOTATE_CONDVAR_WAIT(obj)
150 
151   /* DEPRECATED. Don't use it. */
152   #define ANNOTATE_PUBLISH_MEMORY_RANGE(pointer, size) \
153     DYNAMIC_ANNOTATIONS_NAME(AnnotatePublishMemoryRange)(__FILE__, __LINE__, \
154         pointer, size)
155 
156   /* DEPRECATED. Don't use it. */
157   #define ANNOTATE_UNPUBLISH_MEMORY_RANGE(pointer, size) \
158     DYNAMIC_ANNOTATIONS_NAME(AnnotateUnpublishMemoryRange)(__FILE__, __LINE__, \
159         pointer, size)
160 
161   /* DEPRECATED. Don't use it. */
162   #define ANNOTATE_SWAP_MEMORY_RANGE(pointer, size)   \
163     do {                                              \
164       ANNOTATE_UNPUBLISH_MEMORY_RANGE(pointer, size); \
165       ANNOTATE_PUBLISH_MEMORY_RANGE(pointer, size);   \
166     } while (0)
167 
168   /* Instruct the tool to create a happens-before arc between mu->Unlock() and
169      mu->Lock(). This annotation may slow down the race detector and hide real
170      races. Normally it is used only when it would be difficult to annotate each
171      of the mutex's critical sections individually using the annotations above.
172      This annotation makes sense only for hybrid race detectors. For pure
173      happens-before detectors this is a no-op. For more details see
174      http://code.google.com/p/data-race-test/wiki/PureHappensBeforeVsHybrid . */
175   #define ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(mu) \
176     DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsUsedAsCondVar)(__FILE__, __LINE__, \
177         mu)
178 
179   /* Opposite to ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX.
180      Instruct the tool to NOT create h-b arcs between Unlock and Lock, even in
181      pure happens-before mode. For a hybrid mode this is a no-op. */
182   #define ANNOTATE_NOT_HAPPENS_BEFORE_MUTEX(mu) \
183     DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsNotPHB)(__FILE__, __LINE__, mu)
184 
185   /* Deprecated. Use ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX. */
186   #define ANNOTATE_MUTEX_IS_USED_AS_CONDVAR(mu) \
187     DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsUsedAsCondVar)(__FILE__, __LINE__, \
188         mu)
189 
190   /* -------------------------------------------------------------
191      Annotations useful when defining memory allocators, or when memory that
192      was protected in one way starts to be protected in another. */
193 
194   /* Report that a new memory at "address" of size "size" has been allocated.
195      This might be used when the memory has been retrieved from a free list and
196      is about to be reused, or when a the locking discipline for a variable
197      changes. */
198   #define ANNOTATE_NEW_MEMORY(address, size) \
199     DYNAMIC_ANNOTATIONS_NAME(AnnotateNewMemory)(__FILE__, __LINE__, address, \
200         size)
201 
202   /* -------------------------------------------------------------
203      Annotations useful when defining FIFO queues that transfer data between
204      threads. */
205 
206   /* Report that the producer-consumer queue (such as ProducerConsumerQueue) at
207      address "pcq" has been created.  The ANNOTATE_PCQ_* annotations
208      should be used only for FIFO queues.  For non-FIFO queues use
209      ANNOTATE_HAPPENS_BEFORE (for put) and ANNOTATE_HAPPENS_AFTER (for get). */
210   #define ANNOTATE_PCQ_CREATE(pcq) \
211     DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQCreate)(__FILE__, __LINE__, pcq)
212 
213   /* Report that the queue at address "pcq" is about to be destroyed. */
214   #define ANNOTATE_PCQ_DESTROY(pcq) \
215     DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQDestroy)(__FILE__, __LINE__, pcq)
216 
217   /* Report that we are about to put an element into a FIFO queue at address
218      "pcq". */
219   #define ANNOTATE_PCQ_PUT(pcq) \
220     DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQPut)(__FILE__, __LINE__, pcq)
221 
222   /* Report that we've just got an element from a FIFO queue at address
223      "pcq". */
224   #define ANNOTATE_PCQ_GET(pcq) \
225     DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQGet)(__FILE__, __LINE__, pcq)
226 
227   /* -------------------------------------------------------------
228      Annotations that suppress errors.  It is usually better to express the
229      program's synchronization using the other annotations, but these can
230      be used when all else fails. */
231 
232   /* Report that we may have a benign race at "pointer", with size
233      "sizeof(*(pointer))". "pointer" must be a non-void* pointer.  Insert at the
234      point where "pointer" has been allocated, preferably close to the point
235      where the race happens.  See also ANNOTATE_BENIGN_RACE_STATIC. */
236   #define ANNOTATE_BENIGN_RACE(pointer, description) \
237     DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRaceSized)(__FILE__, __LINE__, \
238         pointer, sizeof(*(pointer)), description)
239 
240   /* Same as ANNOTATE_BENIGN_RACE(address, description), but applies to
241      the memory range [address, address+size). */
242   #define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \
243     DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRaceSized)(__FILE__, __LINE__, \
244         address, size, description)
245 
246   /* Request the analysis tool to ignore all reads in the current thread
247      until ANNOTATE_IGNORE_READS_END is called.
248      Useful to ignore intentional racey reads, while still checking
249      other reads and all writes.
250      See also ANNOTATE_UNPROTECTED_READ. */
251   #define ANNOTATE_IGNORE_READS_BEGIN() \
252     DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsBegin)(__FILE__, __LINE__)
253 
254   /* Stop ignoring reads. */
255   #define ANNOTATE_IGNORE_READS_END() \
256     DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsEnd)(__FILE__, __LINE__)
257 
258   /* Similar to ANNOTATE_IGNORE_READS_BEGIN, but ignore writes. */
259   #define ANNOTATE_IGNORE_WRITES_BEGIN() \
260     DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesBegin)(__FILE__, __LINE__)
261 
262   /* Stop ignoring writes. */
263   #define ANNOTATE_IGNORE_WRITES_END() \
264     DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesEnd)(__FILE__, __LINE__)
265 
266   /* Start ignoring all memory accesses (reads and writes). */
267   #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
268     do {\
269       ANNOTATE_IGNORE_READS_BEGIN();\
270       ANNOTATE_IGNORE_WRITES_BEGIN();\
271     }while(0)\
272 
273   /* Stop ignoring all memory accesses. */
274   #define ANNOTATE_IGNORE_READS_AND_WRITES_END() \
275     do {\
276       ANNOTATE_IGNORE_WRITES_END();\
277       ANNOTATE_IGNORE_READS_END();\
278     }while(0)\
279 
280   /* Similar to ANNOTATE_IGNORE_READS_BEGIN, but ignore synchronization events:
281      RWLOCK* and CONDVAR*. */
282   #define ANNOTATE_IGNORE_SYNC_BEGIN() \
283     DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncBegin)(__FILE__, __LINE__)
284 
285   /* Stop ignoring sync events. */
286   #define ANNOTATE_IGNORE_SYNC_END() \
287     DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncEnd)(__FILE__, __LINE__)
288 
289 
290   /* Enable (enable!=0) or disable (enable==0) race detection for all threads.
291      This annotation could be useful if you want to skip expensive race analysis
292      during some period of program execution, e.g. during initialization. */
293   #define ANNOTATE_ENABLE_RACE_DETECTION(enable) \
294     DYNAMIC_ANNOTATIONS_NAME(AnnotateEnableRaceDetection)(__FILE__, __LINE__, \
295         enable)
296 
297   /* -------------------------------------------------------------
298      Annotations useful for debugging. */
299 
300   /* Request to trace every access to "address". */
301   #define ANNOTATE_TRACE_MEMORY(address) \
302     DYNAMIC_ANNOTATIONS_NAME(AnnotateTraceMemory)(__FILE__, __LINE__, address)
303 
304   /* Report the current thread name to a race detector. */
305   #define ANNOTATE_THREAD_NAME(name) \
306     DYNAMIC_ANNOTATIONS_NAME(AnnotateThreadName)(__FILE__, __LINE__, name)
307 
308   /* -------------------------------------------------------------
309      Annotations useful when implementing locks.  They are not
310      normally needed by modules that merely use locks.
311      The "lock" argument is a pointer to the lock object. */
312 
313   /* Report that a lock has been created at address "lock". */
314   #define ANNOTATE_RWLOCK_CREATE(lock) \
315     DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockCreate)(__FILE__, __LINE__, lock)
316 
317   /* Report that the lock at address "lock" is about to be destroyed. */
318   #define ANNOTATE_RWLOCK_DESTROY(lock) \
319     DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockDestroy)(__FILE__, __LINE__, lock)
320 
321   /* Report that the lock at address "lock" has been acquired.
322      is_w=1 for writer lock, is_w=0 for reader lock. */
323   #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
324     DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockAcquired)(__FILE__, __LINE__, lock, \
325         is_w)
326 
327   /* Report that the lock at address "lock" is about to be released. */
328   #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
329     DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockReleased)(__FILE__, __LINE__, lock, \
330         is_w)
331 
332   /* -------------------------------------------------------------
333      Annotations useful when implementing barriers.  They are not
334      normally needed by modules that merely use barriers.
335      The "barrier" argument is a pointer to the barrier object. */
336 
337   /* Report that the "barrier" has been initialized with initial "count".
338    If 'reinitialization_allowed' is true, initialization is allowed to happen
339    multiple times w/o calling barrier_destroy() */
340   #define ANNOTATE_BARRIER_INIT(barrier, count, reinitialization_allowed) \
341     DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierInit)(__FILE__, __LINE__, barrier, \
342         count, reinitialization_allowed)
343 
344   /* Report that we are about to enter barrier_wait("barrier"). */
345   #define ANNOTATE_BARRIER_WAIT_BEFORE(barrier) \
346     DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitBefore)(__FILE__, __LINE__, \
347         barrier)
348 
349   /* Report that we just exited barrier_wait("barrier"). */
350   #define ANNOTATE_BARRIER_WAIT_AFTER(barrier) \
351     DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitAfter)(__FILE__, __LINE__, \
352         barrier)
353 
354   /* Report that the "barrier" has been destroyed. */
355   #define ANNOTATE_BARRIER_DESTROY(barrier) \
356     DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierDestroy)(__FILE__, __LINE__, \
357         barrier)
358 
359   /* -------------------------------------------------------------
360      Annotations useful for testing race detectors. */
361 
362   /* Report that we expect a race on the variable at "address".
363      Use only in unit tests for a race detector. */
364   #define ANNOTATE_EXPECT_RACE(address, description) \
365     DYNAMIC_ANNOTATIONS_NAME(AnnotateExpectRace)(__FILE__, __LINE__, address, \
366         description)
367 
368   #define ANNOTATE_FLUSH_EXPECTED_RACES() \
369     DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushExpectedRaces)(__FILE__, __LINE__)
370 
371   /* A no-op. Insert where you like to test the interceptors. */
372   #define ANNOTATE_NO_OP(arg) \
373     DYNAMIC_ANNOTATIONS_NAME(AnnotateNoOp)(__FILE__, __LINE__, arg)
374 
375   /* Force the race detector to flush its state. The actual effect depends on
376    * the implementation of the detector. */
377   #define ANNOTATE_FLUSH_STATE() \
378     DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushState)(__FILE__, __LINE__)
379 
380 
381 #else  /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */
382 
383   #define ANNOTATE_RWLOCK_CREATE(lock) /* empty */
384   #define ANNOTATE_RWLOCK_DESTROY(lock) /* empty */
385   #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) /* empty */
386   #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) /* empty */
387   #define ANNOTATE_BARRIER_INIT(barrier, count, reinitialization_allowed) /* */
388   #define ANNOTATE_BARRIER_WAIT_BEFORE(barrier) /* empty */
389   #define ANNOTATE_BARRIER_WAIT_AFTER(barrier) /* empty */
390   #define ANNOTATE_BARRIER_DESTROY(barrier) /* empty */
391   #define ANNOTATE_CONDVAR_LOCK_WAIT(cv, lock) /* empty */
392   #define ANNOTATE_CONDVAR_WAIT(cv) /* empty */
393   #define ANNOTATE_CONDVAR_SIGNAL(cv) /* empty */
394   #define ANNOTATE_CONDVAR_SIGNAL_ALL(cv) /* empty */
395   #define ANNOTATE_HAPPENS_BEFORE(obj) /* empty */
396   #define ANNOTATE_HAPPENS_AFTER(obj) /* empty */
397   #define ANNOTATE_PUBLISH_MEMORY_RANGE(address, size) /* empty */
398   #define ANNOTATE_UNPUBLISH_MEMORY_RANGE(address, size)  /* empty */
399   #define ANNOTATE_SWAP_MEMORY_RANGE(address, size)  /* empty */
400   #define ANNOTATE_PCQ_CREATE(pcq) /* empty */
401   #define ANNOTATE_PCQ_DESTROY(pcq) /* empty */
402   #define ANNOTATE_PCQ_PUT(pcq) /* empty */
403   #define ANNOTATE_PCQ_GET(pcq) /* empty */
404   #define ANNOTATE_NEW_MEMORY(address, size) /* empty */
405   #define ANNOTATE_EXPECT_RACE(address, description) /* empty */
406   #define ANNOTATE_FLUSH_EXPECTED_RACES(address, description) /* empty */
407   #define ANNOTATE_BENIGN_RACE(address, description) /* empty */
408   #define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) /* empty */
409   #define ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(mu) /* empty */
410   #define ANNOTATE_MUTEX_IS_USED_AS_CONDVAR(mu) /* empty */
411   #define ANNOTATE_TRACE_MEMORY(arg) /* empty */
412   #define ANNOTATE_THREAD_NAME(name) /* empty */
413   #define ANNOTATE_IGNORE_READS_BEGIN() /* empty */
414   #define ANNOTATE_IGNORE_READS_END() /* empty */
415   #define ANNOTATE_IGNORE_WRITES_BEGIN() /* empty */
416   #define ANNOTATE_IGNORE_WRITES_END() /* empty */
417   #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() /* empty */
418   #define ANNOTATE_IGNORE_READS_AND_WRITES_END() /* empty */
419   #define ANNOTATE_IGNORE_SYNC_BEGIN() /* empty */
420   #define ANNOTATE_IGNORE_SYNC_END() /* empty */
421   #define ANNOTATE_ENABLE_RACE_DETECTION(enable) /* empty */
422   #define ANNOTATE_NO_OP(arg) /* empty */
423   #define ANNOTATE_FLUSH_STATE() /* empty */
424 
425 #endif  /* DYNAMIC_ANNOTATIONS_ENABLED */
426 
427 /* Use the macros above rather than using these functions directly. */
428 #ifdef __cplusplus
429 extern "C" {
430 #endif
431 
432 
433 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockCreate)(
434     const char *file, int line,
435     const volatile void *lock) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
436 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockDestroy)(
437     const char *file, int line,
438     const volatile void *lock) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
439 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockAcquired)(
440     const char *file, int line,
441     const volatile void *lock, long is_w) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
442 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockReleased)(
443     const char *file, int line,
444     const volatile void *lock, long is_w) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
445 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierInit)(
446     const char *file, int line, const volatile void *barrier, long count,
447     long reinitialization_allowed) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
448 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitBefore)(
449     const char *file, int line,
450     const volatile void *barrier) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
451 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitAfter)(
452     const char *file, int line,
453     const volatile void *barrier) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
454 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierDestroy)(
455     const char *file, int line,
456     const volatile void *barrier) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
457 void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarWait)(
458     const char *file, int line, const volatile void *cv,
459     const volatile void *lock) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
460 void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignal)(
461     const char *file, int line,
462     const volatile void *cv) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
463 void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignalAll)(
464     const char *file, int line,
465     const volatile void *cv) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
466 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePublishMemoryRange)(
467     const char *file, int line,
468     const volatile void *address, long size) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
469 void DYNAMIC_ANNOTATIONS_NAME(AnnotateUnpublishMemoryRange)(
470     const char *file, int line,
471     const volatile void *address, long size) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
472 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQCreate)(
473     const char *file, int line,
474     const volatile void *pcq) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
475 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQDestroy)(
476     const char *file, int line,
477     const volatile void *pcq) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
478 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQPut)(
479     const char *file, int line,
480     const volatile void *pcq) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
481 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQGet)(
482     const char *file, int line,
483     const volatile void *pcq) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
484 void DYNAMIC_ANNOTATIONS_NAME(AnnotateNewMemory)(
485     const char *file, int line,
486     const volatile void *mem, long size) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
487 void DYNAMIC_ANNOTATIONS_NAME(AnnotateExpectRace)(
488     const char *file, int line, const volatile void *mem,
489     const char *description) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
490 void DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushExpectedRaces)(
491     const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
492 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRace)(
493     const char *file, int line, const volatile void *mem,
494     const char *description) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
495 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRaceSized)(
496     const char *file, int line, const volatile void *mem, long size,
497     const char *description) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
498 void DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsUsedAsCondVar)(
499     const char *file, int line,
500     const volatile void *mu) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
501 void DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsNotPHB)(
502     const char *file, int line,
503     const volatile void *mu) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
504 void DYNAMIC_ANNOTATIONS_NAME(AnnotateTraceMemory)(
505     const char *file, int line,
506     const volatile void *arg) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
507 void DYNAMIC_ANNOTATIONS_NAME(AnnotateThreadName)(
508     const char *file, int line,
509     const char *name) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
510 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsBegin)(
511     const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
512 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsEnd)(
513     const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
514 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesBegin)(
515     const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
516 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesEnd)(
517     const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
518 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncBegin)(
519     const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
520 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncEnd)(
521     const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
522 void DYNAMIC_ANNOTATIONS_NAME(AnnotateEnableRaceDetection)(
523     const char *file, int line, int enable) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
524 void DYNAMIC_ANNOTATIONS_NAME(AnnotateNoOp)(
525     const char *file, int line,
526     const volatile void *arg) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
527 void DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushState)(
528     const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
529 
530 #if DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND == 1
531 /* Return non-zero value if running under valgrind.
532 
533   If "valgrind.h" is included into dynamic_annotations.c,
534   the regular valgrind mechanism will be used.
535   See http://valgrind.org/docs/manual/manual-core-adv.html about
536   RUNNING_ON_VALGRIND and other valgrind "client requests".
537   The file "valgrind.h" may be obtained by doing
538      svn co svn://svn.valgrind.org/valgrind/trunk/include
539 
540   If for some reason you can't use "valgrind.h" or want to fake valgrind,
541   there are two ways to make this function return non-zero:
542     - Use environment variable: export RUNNING_ON_VALGRIND=1
543     - Make your tool intercept the function RunningOnValgrind() and
544       change its return value.
545  */
546 int RunningOnValgrind(void);
547 #endif /* DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND == 1 */
548 
549 #ifdef __cplusplus
550 }
551 #endif
552 
553 #if DYNAMIC_ANNOTATIONS_ENABLED != 0 && defined(__cplusplus)
554 
555   /* ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
556 
557      Instead of doing
558         ANNOTATE_IGNORE_READS_BEGIN();
559         ... = x;
560         ANNOTATE_IGNORE_READS_END();
561      one can use
562         ... = ANNOTATE_UNPROTECTED_READ(x); */
563   template <class T>
ANNOTATE_UNPROTECTED_READ(const volatile T & x)564   inline T ANNOTATE_UNPROTECTED_READ(const volatile T &x) {
565     ANNOTATE_IGNORE_READS_BEGIN();
566     T res = x;
567     ANNOTATE_IGNORE_READS_END();
568     return res;
569   }
570   /* Apply ANNOTATE_BENIGN_RACE_SIZED to a static variable. */
571   #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description)        \
572     namespace {                                                       \
573       class static_var ## _annotator {                                \
574        public:                                                        \
575         static_var ## _annotator() {                                  \
576           ANNOTATE_BENIGN_RACE_SIZED(&static_var,                     \
577                                       sizeof(static_var),             \
578             # static_var ": " description);                           \
579         }                                                             \
580       };                                                              \
581       static static_var ## _annotator the ## static_var ## _annotator;\
582     }
583 #else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */
584 
585   #define ANNOTATE_UNPROTECTED_READ(x) (x)
586   #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description)  /* empty */
587 
588 #endif /* DYNAMIC_ANNOTATIONS_ENABLED */
589 
590 #endif  /* __DYNAMIC_ANNOTATIONS_H__ */
591