summaryrefslogtreecommitdiff
path: root/cpp/test/IceUtil/condvar/Match.cpp
blob: 749249106580e4e3364fec5073319d016b684dea (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// **********************************************************************
//
// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceUtil/Thread.h>
#include <IceUtil/Time.h>
#include <IceUtil/Cond.h>
#include <IceUtil/Options.h>
#include <iostream>

using namespace std;
using namespace IceUtil;
using namespace IceUtilInternal;

enum State
{
    StateStart,
    StatePlayerA,
    StatePlayerB,
    StateGameOver
};

struct Game
{
    IceUtil::Mutex mutex;
    IceUtil::Cond cond;
    State state;
    int hits;
    bool broadcast;
    bool noise;
};

class Player : public Thread
{
public:

    Player(Game* game, State which) :
        _game(game), _which(which)
    {
    }

    virtual void
    run()
    {
        Mutex::Lock sync(_game->mutex);
        while(_game->state < StateGameOver)
        {
            ++_game->hits;
            _game->state = _which;
            if(_game->broadcast)
            {
                _game->cond.broadcast();
            }
            else
            {
                _game->cond.signal();
            }
            do
            {
                _game->cond.wait(sync);
                if(_game->state == _which && !_game->noise)
                {
                    cout << endl << "PLAYER-" << ((_which == StatePlayerA) ? "B" : "A")
                         << ": Supurious wakeup" << endl;
                }
            }
            while(_game->state == _which);
        }
    }

private:

    Game* _game;
    State _which;
};
typedef Handle<Player> PlayerPtr;

int
main(int argc, char** argv)
{
    Options opts;
    opts.addOpt("t", "timeout", Options::NeedArg, "5");
    opts.addOpt("b", "broadcast");
    try
    {
        opts.parse(argc, (const char**)argv);
    }
    catch(const IceUtilInternal::BadOptException& e)
    {
        cerr << argv[0] << ": " << e.reason << endl;
        return EXIT_FAILURE;
    }

    Game game;
    game.hits = 0;
    game.state = StateStart;
    game.broadcast = opts.isSet("b");
    game.noise = false;

    PlayerPtr playerA = new Player(&game, StatePlayerA);
    playerA->start();
    PlayerPtr playerB = new Player(&game, StatePlayerB);
    playerB->start();

    cout << "running spurious wake test (" << ((game.broadcast) ? "broadcast" : "signal") << ")... " << flush;

    ThreadControl::sleep(Time::seconds(atoi(opts.optArg("t").c_str())));

    if(game.broadcast)
    {
        {
            Mutex::Lock sync(game.mutex);
            game.noise = true;
        }
        for(int i =0 ; i < 100000; ++i)
        {
            game.cond.broadcast();
        }
        {
            Mutex::Lock sync(game.mutex);
            game.noise = false;
        }
    }
    
    {
        Mutex::Lock sync(game.mutex);
        game.state = StateGameOver;
        game.cond.broadcast();
    }

    playerA->getThreadControl().join();
    playerB->getThreadControl().join();

    cout << game.hits << " ok" << endl;

    return 0;
}