| 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 |  | 
| bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 8 | #include "SkAtomics.h" | 
| reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 9 | #include "SkGraphics.h" | 
|  | 10 | #include "SkPaint.h" | 
|  | 11 | #include "SkTLS.h" | 
|  | 12 | #include "SkThreadUtils.h" | 
| tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "Test.h" | 
| reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 14 |  | 
|  | 15 | static void thread_main(void*) { | 
|  | 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 |  | 
| scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 34 | static void test_threads(SkThread::entryPointProc proc) { | 
| reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 35 | SkThread* threads[8]; | 
|  | 36 | int N = SK_ARRAY_COUNT(threads); | 
|  | 37 | int i; | 
|  | 38 |  | 
|  | 39 | for (i = 0; i < N; ++i) { | 
| scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 40 | threads[i] = new SkThread(proc); | 
| reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 41 | } | 
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 42 |  | 
| reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 43 | for (i = 0; i < N; ++i) { | 
|  | 44 | threads[i]->start(); | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | for (i = 0; i < N; ++i) { | 
|  | 48 | threads[i]->join(); | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | for (i = 0; i < N; ++i) { | 
|  | 52 | delete threads[i]; | 
|  | 53 | } | 
|  | 54 | } | 
|  | 55 |  | 
| scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 56 | static int32_t gCounter; | 
|  | 57 |  | 
|  | 58 | static void* FakeCreateTLS() { | 
|  | 59 | sk_atomic_inc(&gCounter); | 
| halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 60 | return nullptr; | 
| scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 61 | } | 
|  | 62 |  | 
| sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 63 | static void FakeDeleteTLS(void*) { | 
| scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 64 | sk_atomic_dec(&gCounter); | 
|  | 65 | } | 
|  | 66 |  | 
| sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 67 | static void testTLSDestructor(void*) { | 
| scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 68 | SkTLS::Get(FakeCreateTLS, FakeDeleteTLS); | 
|  | 69 | } | 
|  | 70 |  | 
| tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 71 | DEF_TEST(TLS, reporter) { | 
| scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 72 | // TODO: Disabled for now to work around | 
|  | 73 | // http://code.google.com/p/skia/issues/detail?id=619 | 
|  | 74 | // ('flaky segfault in TLS test on Shuttle_Ubuntu12 buildbots') | 
| humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 75 | if( false ) test_threads(&thread_main); | 
| scroggo@google.com | 9389763 | 2012-10-25 19:12:40 +0000 | [diff] [blame] | 76 |  | 
|  | 77 | // Test to ensure that at thread destruction, TLS destructors | 
|  | 78 | // have been called. | 
|  | 79 | test_threads(&testTLSDestructor); | 
|  | 80 | REPORTER_ASSERT(reporter, 0 == gCounter); | 
| reed@google.com | b6a4b73 | 2012-05-21 15:27:23 +0000 | [diff] [blame] | 81 | } |