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 
8 #include "SkGraphics.h"
9 #include "SkPaint.h"
10 #include "SkTLS.h"
11 #include "Test.h"
12 #include <atomic>
13 #include <thread>
14 
thread_main()15 static void thread_main() {
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 
34 template <typename Fn>
test_threads(Fn fn)35 static void test_threads(Fn fn) {
36     std::thread threads[8];
37 
38     for (auto& thread : threads) {
39         thread = std::thread(fn);
40     }
41     for (auto& thread : threads) {
42         thread.join();
43     }
44 }
45 
46 static std::atomic<int> gCounter{0};
47 
fake_create_TLS()48 static void* fake_create_TLS() {
49     gCounter++;
50     return nullptr;
51 }
fake_delete_TLS(void *)52 static void fake_delete_TLS(void*) {
53     gCounter--;
54 }
55 
DEF_TEST(TLS,reporter)56 DEF_TEST(TLS, reporter) {
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')
60     if( false ) test_threads(&thread_main);
61 
62     // Test to ensure that at thread destruction, TLS destructors
63     // have been called.
64     test_threads([] {
65         SkTLS::Get(fake_create_TLS, fake_delete_TLS);
66     });
67     REPORTER_ASSERT(reporter, 0 == gCounter.load());
68 }
69