aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/OnceTest.cpp
blob: 31c6a85a44cbd50213129f3c265ae8c71773cf55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkOnce.h"
#include "SkThreadPool.h"
#include "Test.h"
#include "TestClassDef.h"

static void add_five(int* x) {
    *x += 5;
}

DEF_TEST(SkOnce_Singlethreaded, r) {
    int x = 0;

    SK_DECLARE_STATIC_ONCE(once);
    // No matter how many times we do this, x will be 5.
    SkOnce(&once, add_five, &x);
    SkOnce(&once, add_five, &x);
    SkOnce(&once, add_five, &x);
    SkOnce(&once, add_five, &x);
    SkOnce(&once, add_five, &x);

    REPORTER_ASSERT(r, 5 == x);
}

static void add_six(int* x) {
    *x += 6;
}

class Racer : public SkRunnable {
public:
    SkOnceFlag* once;
    int* ptr;

    virtual void run() SK_OVERRIDE {
        SkOnce(once, add_six, ptr);
    }
};

DEF_TEST(SkOnce_Multithreaded, r) {
    const int kTasks = 16, kThreads = 4;

    // Make a bunch of tasks that will race to be the first to add six to x.
    Racer racers[kTasks];
    SK_DECLARE_STATIC_ONCE(once);
    int x = 0;
    for (int i = 0; i < kTasks; i++) {
        racers[i].once = &once;
        racers[i].ptr = &x;
    }

    // Let them race.
    SkThreadPool pool(kThreads);
    for (int i = 0; i < kTasks; i++) {
        pool.add(&racers[i]);
    }
    pool.wait();

    // Only one should have done the +=.
    REPORTER_ASSERT(r, 6 == x);
}