[ASan] Update sanitizer_common and asan test_util headers to support building on...
[blocksruntime.git] / lib / sanitizer_common / tests / sanitizer_stoptheworld_testlib.cc
blobd8be2afb19e9c173454b5e62907b7b78f8b14e5d
1 //===-- sanitizer_stoptheworld_testlib.cc ---------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // Dynamic library to test StopTheWorld functionality.
10 // When loaded with LD_PRELOAD, it will periodically suspend all threads.
11 //===----------------------------------------------------------------------===//
12 /* Usage:
13 clang++ -fno-exceptions -g -fPIC -I. \
14 sanitizer_common/tests/sanitizer_stoptheworld_testlib.cc \
15 sanitizer_common/sanitizer_*.cc -shared -lpthread -o teststoptheworld.so
16 LD_PRELOAD=`pwd`/teststoptheworld.so /your/app
19 #include "sanitizer_common/sanitizer_platform.h"
20 #if SANITIZER_LINUX
22 #include <dlfcn.h>
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <pthread.h>
26 #include <unistd.h>
28 #include "sanitizer_common/sanitizer_stoptheworld.h"
30 namespace {
31 const uptr kSuspendDuration = 3;
32 const uptr kRunDuration = 3;
34 void Callback(const SuspendedThreadsList &suspended_threads_list,
35 void *argument) {
36 sleep(kSuspendDuration);
39 void *SuspenderThread(void *argument) {
40 while (true) {
41 sleep(kRunDuration);
42 StopTheWorld(Callback, NULL);
44 return NULL;
47 __attribute__((constructor)) void StopTheWorldTestLibConstructor(void) {
48 pthread_t thread_id;
49 pthread_create(&thread_id, NULL, SuspenderThread, NULL);
51 } // namespace
53 #endif // SANITIZER_LINUX