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