blob: 0fadd7ed9ccef653885c9bea720401ace8c4a647 [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
reed@google.comb6a4b732012-05-21 15:27:23 +00008#include "SkGraphics.h"
9#include "SkPaint.h"
10#include "SkTLS.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "Test.h"
Mike Klein03141d22017-10-30 11:57:15 -040012#include <atomic>
13#include <thread>
reed@google.comb6a4b732012-05-21 15:27:23 +000014
Mike Klein03141d22017-10-30 11:57:15 -040015static void thread_main() {
reed@google.comb6a4b732012-05-21 15:27:23 +000016 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 Klein03141d22017-10-30 11:57:15 -040034template <typename Fn>
35static void test_threads(Fn fn) {
36 std::thread threads[8];
reed@google.comb6a4b732012-05-21 15:27:23 +000037
Mike Klein03141d22017-10-30 11:57:15 -040038 for (auto& thread : threads) {
39 thread = std::thread(fn);
reed@google.comb6a4b732012-05-21 15:27:23 +000040 }
Mike Klein03141d22017-10-30 11:57:15 -040041 for (auto& thread : threads) {
42 thread.join();
reed@google.comb6a4b732012-05-21 15:27:23 +000043 }
44}
45
Mike Klein03141d22017-10-30 11:57:15 -040046static std::atomic<int> gCounter{0};
scroggo@google.com93897632012-10-25 19:12:40 +000047
Mike Klein03141d22017-10-30 11:57:15 -040048static void* fake_create_TLS() {
49 gCounter++;
halcanary96fcdcc2015-08-27 07:41:13 -070050 return nullptr;
scroggo@google.com93897632012-10-25 19:12:40 +000051}
Mike Klein03141d22017-10-30 11:57:15 -040052static void fake_delete_TLS(void*) {
53 gCounter--;
scroggo@google.com93897632012-10-25 19:12:40 +000054}
55
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000056DEF_TEST(TLS, reporter) {
scroggo@google.com93897632012-10-25 19:12:40 +000057 // 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.com05af1af2013-01-07 16:47:43 +000060 if( false ) test_threads(&thread_main);
scroggo@google.com93897632012-10-25 19:12:40 +000061
62 // Test to ensure that at thread destruction, TLS destructors
63 // have been called.
Mike Klein03141d22017-10-30 11:57:15 -040064 test_threads([] {
65 SkTLS::Get(fake_create_TLS, fake_delete_TLS);
66 });
67 REPORTER_ASSERT(reporter, 0 == gCounter.load());
reed@google.comb6a4b732012-05-21 15:27:23 +000068}