blob: 2702d9c0002f8021328e898bc5e003e95e4e1f9b [file] [log] [blame]
reed@google.comb6a4b732012-05-21 15:27:23 +00001/*
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
bungeman2c4bd072016-04-08 06:58:51 -07008#include "SkAtomics.h"
reed@google.comb6a4b732012-05-21 15:27:23 +00009#include "SkGraphics.h"
10#include "SkPaint.h"
11#include "SkTLS.h"
12#include "SkThreadUtils.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000013#include "Test.h"
reed@google.comb6a4b732012-05-21 15:27:23 +000014
15static 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.com93897632012-10-25 19:12:40 +000034static void test_threads(SkThread::entryPointProc proc) {
reed@google.comb6a4b732012-05-21 15:27:23 +000035 SkThread* threads[8];
36 int N = SK_ARRAY_COUNT(threads);
37 int i;
38
39 for (i = 0; i < N; ++i) {
scroggo@google.com93897632012-10-25 19:12:40 +000040 threads[i] = new SkThread(proc);
reed@google.comb6a4b732012-05-21 15:27:23 +000041 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000042
reed@google.comb6a4b732012-05-21 15:27:23 +000043 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.com93897632012-10-25 19:12:40 +000056static int32_t gCounter;
57
58static void* FakeCreateTLS() {
59 sk_atomic_inc(&gCounter);
halcanary96fcdcc2015-08-27 07:41:13 -070060 return nullptr;
scroggo@google.com93897632012-10-25 19:12:40 +000061}
62
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000063static void FakeDeleteTLS(void*) {
scroggo@google.com93897632012-10-25 19:12:40 +000064 sk_atomic_dec(&gCounter);
65}
66
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000067static void testTLSDestructor(void*) {
scroggo@google.com93897632012-10-25 19:12:40 +000068 SkTLS::Get(FakeCreateTLS, FakeDeleteTLS);
69}
70
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000071DEF_TEST(TLS, reporter) {
scroggo@google.com93897632012-10-25 19:12:40 +000072 // 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.com05af1af2013-01-07 16:47:43 +000075 if( false ) test_threads(&thread_main);
scroggo@google.com93897632012-10-25 19:12:40 +000076
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.comb6a4b732012-05-21 15:27:23 +000081}