summaryrefslogtreecommitdiffstats
path: root/gfx/angle/src/libANGLE/signal_utils.h
blob: 835f2c602d2a626c6609b9abadbaaf5c70c4309f (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
//
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// signal_utils:
//   Helper classes for tracking dependent state changes between objects.
//   These changes are signaled to the dependent class via channels.

#ifndef LIBANGLE_SIGNAL_UTILS_H_
#define LIBANGLE_SIGNAL_UTILS_H_

#include <set>

#include "common/angleutils.h"

namespace angle
{

// Message token passed to the receiver;
using SignalToken = uint32_t;

// Interface that the depending class inherits from.
class SignalReceiver
{
  public:
    virtual ~SignalReceiver() = default;
    virtual void signal(SignalToken token) = 0;
};

class ChannelBinding;

// The host class owns the channel. It uses the channel to fire signals to the receiver.
class BroadcastChannel final : NonCopyable
{
  public:
    BroadcastChannel();
    ~BroadcastChannel();

    void signal() const;
    void reset();

  private:
    // Only the ChannelBinding class should add or remove receivers.
    friend class ChannelBinding;
    void addReceiver(ChannelBinding *receiver);
    void removeReceiver(ChannelBinding *receiver);

    std::vector<ChannelBinding *> mReceivers;
};

// The dependent class keeps bindings to the host's BroadcastChannel.
class ChannelBinding final
{
  public:
    ChannelBinding(SignalReceiver *receiver, SignalToken token);
    ~ChannelBinding();
    ChannelBinding(const ChannelBinding &other) = default;
    ChannelBinding &operator=(const ChannelBinding &other) = default;

    void bind(BroadcastChannel *channel);
    void reset();
    void signal() const;
    void onChannelClosed();

  private:
    BroadcastChannel *mChannel;
    SignalReceiver *mReceiver;
    SignalToken mToken;
};

}  // namespace angle

#endif  // LIBANGLE_SIGNAL_UTILS_H_