2013-10-09 16:12:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkOnce.h"
|
|
|
|
#include "src/core/SkTaskGroup.h"
|
|
|
|
#include "tests/Test.h"
|
2013-10-09 16:12:23 +00:00
|
|
|
|
2013-10-23 14:44:08 +00:00
|
|
|
static void add_five(int* x) {
|
2013-10-09 16:12:23 +00:00
|
|
|
*x += 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkOnce_Singlethreaded, r) {
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
// No matter how many times we do this, x will be 5.
|
2016-04-18 15:09:11 +00:00
|
|
|
SkOnce once;
|
|
|
|
once(add_five, &x);
|
|
|
|
once(add_five, &x);
|
|
|
|
once(add_five, &x);
|
|
|
|
once(add_five, &x);
|
|
|
|
once(add_five, &x);
|
2013-10-09 16:12:23 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(r, 5 == x);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkOnce_Multithreaded, r) {
|
|
|
|
int x = 0;
|
2016-04-18 15:09:11 +00:00
|
|
|
|
2015-06-17 22:26:15 +00:00
|
|
|
// Run a bunch of tasks to be the first to add six to x.
|
2016-04-18 15:09:11 +00:00
|
|
|
SkOnce once;
|
2016-01-05 03:13:19 +00:00
|
|
|
SkTaskGroup().batch(1021, [&](int) {
|
2016-04-18 15:09:11 +00:00
|
|
|
once([&] { x += 6; });
|
2015-06-17 22:26:15 +00:00
|
|
|
});
|
2013-10-09 16:12:23 +00:00
|
|
|
|
|
|
|
// Only one should have done the +=.
|
|
|
|
REPORTER_ASSERT(r, 6 == x);
|
|
|
|
}
|
2014-01-24 22:38:39 +00:00
|
|
|
|
2014-06-02 18:26:59 +00:00
|
|
|
static int gX = 0;
|
|
|
|
static void inc_gX() { gX++; }
|
2014-01-24 22:38:39 +00:00
|
|
|
|
2014-06-02 18:26:59 +00:00
|
|
|
DEF_TEST(SkOnce_NoArg, r) {
|
2016-04-18 15:09:11 +00:00
|
|
|
SkOnce once;
|
|
|
|
once(inc_gX);
|
|
|
|
once(inc_gX);
|
|
|
|
once(inc_gX);
|
2014-06-02 18:26:59 +00:00
|
|
|
REPORTER_ASSERT(r, 1 == gX);
|
2014-01-24 22:38:39 +00:00
|
|
|
}
|