1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker // This file provides a macro ONLY for use in testing. 6*635a8641SAndroid Build Coastguard Worker // DO NOT USE IN PRODUCTION CODE. There are much better ways to wait. 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker // This code is very helpful in testing multi-threaded code, without depending 9*635a8641SAndroid Build Coastguard Worker // on almost any primitives. This is especially helpful if you are testing 10*635a8641SAndroid Build Coastguard Worker // those primitive multi-threaded constructs. 11*635a8641SAndroid Build Coastguard Worker 12*635a8641SAndroid Build Coastguard Worker // We provide a simple one argument spin wait (for 1 second), and a generic 13*635a8641SAndroid Build Coastguard Worker // spin wait (for longer periods of time). 14*635a8641SAndroid Build Coastguard Worker 15*635a8641SAndroid Build Coastguard Worker #ifndef BASE_SYNCHRONIZATION_SPIN_WAIT_H_ 16*635a8641SAndroid Build Coastguard Worker #define BASE_SYNCHRONIZATION_SPIN_WAIT_H_ 17*635a8641SAndroid Build Coastguard Worker 18*635a8641SAndroid Build Coastguard Worker #include "base/threading/platform_thread.h" 19*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h" 20*635a8641SAndroid Build Coastguard Worker 21*635a8641SAndroid Build Coastguard Worker // Provide a macro that will wait no longer than 1 second for an asynchronous 22*635a8641SAndroid Build Coastguard Worker // change is the value of an expression. 23*635a8641SAndroid Build Coastguard Worker // A typical use would be: 24*635a8641SAndroid Build Coastguard Worker // 25*635a8641SAndroid Build Coastguard Worker // SPIN_FOR_1_SECOND_OR_UNTIL_TRUE(0 == f(x)); 26*635a8641SAndroid Build Coastguard Worker // 27*635a8641SAndroid Build Coastguard Worker // The expression will be evaluated repeatedly until it is true, or until 28*635a8641SAndroid Build Coastguard Worker // the time (1 second) expires. 29*635a8641SAndroid Build Coastguard Worker // Since tests generally have a 5 second watch dog timer, this spin loop is 30*635a8641SAndroid Build Coastguard Worker // typically used to get the padding needed on a given test platform to assure 31*635a8641SAndroid Build Coastguard Worker // that the test passes, even if load varies, and external events vary. 32*635a8641SAndroid Build Coastguard Worker 33*635a8641SAndroid Build Coastguard Worker #define SPIN_FOR_1_SECOND_OR_UNTIL_TRUE(expression) \ 34*635a8641SAndroid Build Coastguard Worker SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(base::TimeDelta::FromSeconds(1), \ 35*635a8641SAndroid Build Coastguard Worker (expression)) 36*635a8641SAndroid Build Coastguard Worker 37*635a8641SAndroid Build Coastguard Worker #define SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(delta, expression) do { \ 38*635a8641SAndroid Build Coastguard Worker base::TimeTicks start = base::TimeTicks::Now(); \ 39*635a8641SAndroid Build Coastguard Worker const base::TimeDelta kTimeout = delta; \ 40*635a8641SAndroid Build Coastguard Worker while (!(expression)) { \ 41*635a8641SAndroid Build Coastguard Worker if (kTimeout < base::TimeTicks::Now() - start) { \ 42*635a8641SAndroid Build Coastguard Worker EXPECT_LE((base::TimeTicks::Now() - start).InMilliseconds(), \ 43*635a8641SAndroid Build Coastguard Worker kTimeout.InMilliseconds()) << "Timed out"; \ 44*635a8641SAndroid Build Coastguard Worker break; \ 45*635a8641SAndroid Build Coastguard Worker } \ 46*635a8641SAndroid Build Coastguard Worker base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50)); \ 47*635a8641SAndroid Build Coastguard Worker } \ 48*635a8641SAndroid Build Coastguard Worker } while (0) 49*635a8641SAndroid Build Coastguard Worker 50*635a8641SAndroid Build Coastguard Worker #endif // BASE_SYNCHRONIZATION_SPIN_WAIT_H_ 51