reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 8 | #include "SkGraphics.h" |
| 9 | #include "SkPaint.h" |
| 10 | #include "SkTLS.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 11 | #include "Test.h" |
Mike Klein | 03141d2 | 2017-10-30 11:57:15 -0400 | [diff] [blame] | 12 | #include <atomic> |
| 13 | #include <thread> |
reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 14 | |
Mike Klein | 03141d2 | 2017-10-30 11:57:15 -0400 | [diff] [blame] | 15 | static void thread_main() { |
reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 16 | SkGraphics::SetTLSFontCacheLimit(1 * 1024 * 1024); |
| 17 | |
| 18 | const char text[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 19 | size_t len = strlen(text); |
| 20 | |
| 21 | SkPaint paint; |
| 22 | |
| 23 | for (int j = 0; j < 10; ++j) { |
| 24 | for (int i = 9; i <= 48; ++i) { |
| 25 | paint.setTextSize(SkIntToScalar(i)); |
| 26 | paint.setAntiAlias(false); |
| 27 | paint.measureText(text, len); |
| 28 | paint.setAntiAlias(true); |
| 29 | paint.measureText(text, len); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
Mike Klein | 03141d2 | 2017-10-30 11:57:15 -0400 | [diff] [blame] | 34 | template <typename Fn> |
| 35 | static void test_threads(Fn fn) { |
| 36 | std::thread threads[8]; |
reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 37 | |
Mike Klein | 03141d2 | 2017-10-30 11:57:15 -0400 | [diff] [blame] | 38 | for (auto& thread : threads) { |
| 39 | thread = std::thread(fn); |
reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 40 | } |
Mike Klein | 03141d2 | 2017-10-30 11:57:15 -0400 | [diff] [blame] | 41 | for (auto& thread : threads) { |
| 42 | thread.join(); |
reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Mike Klein | 03141d2 | 2017-10-30 11:57:15 -0400 | [diff] [blame] | 46 | static std::atomic<int> gCounter{0}; |
scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 47 | |
Mike Klein | 03141d2 | 2017-10-30 11:57:15 -0400 | [diff] [blame] | 48 | static void* fake_create_TLS() { |
| 49 | gCounter++; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 50 | return nullptr; |
scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 51 | } |
Mike Klein | 03141d2 | 2017-10-30 11:57:15 -0400 | [diff] [blame] | 52 | static void fake_delete_TLS(void*) { |
| 53 | gCounter--; |
scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 54 | } |
| 55 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 56 | DEF_TEST(TLS, reporter) { |
scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 57 | // TODO: Disabled for now to work around |
| 58 | // http://code.google.com/p/skia/issues/detail?id=619 |
| 59 | // ('flaky segfault in TLS test on Shuttle_Ubuntu12 buildbots') |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 60 | if( false ) test_threads(&thread_main); |
scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 61 | |
| 62 | // Test to ensure that at thread destruction, TLS destructors |
| 63 | // have been called. |
Mike Klein | 03141d2 | 2017-10-30 11:57:15 -0400 | [diff] [blame] | 64 | test_threads([] { |
| 65 | SkTLS::Get(fake_create_TLS, fake_delete_TLS); |
| 66 | }); |
| 67 | REPORTER_ASSERT(reporter, 0 == gCounter.load()); |
reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 68 | } |