in.h 3.74 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
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
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_DATAFLOW_INTERNAL_IN_H_
#define EMBB_DATAFLOW_INTERNAL_IN_H_

#if EMBB_DATAFLOW_TRACE_SIGNAL_HISTORY
#include <vector>
32
#include <embb/base/mutex.h>
33 34 35 36 37 38 39 40 41
#endif

#include <embb/dataflow/internal/signal.h>
#include <embb/dataflow/internal/clock_listener.h>

namespace embb {
namespace dataflow {
namespace internal {

42 43
class Scheduler;

44
template <typename>
45 46
class Out;

47
template <typename Type>
48 49 50 51
class In {
 public:
  typedef Signal<Type> SignalType;

52
  In() : values_(NULL), connected_(false), slices_(0) {}
53

54 55
  ~In() {
    if (NULL != values_) {
56 57 58
      for (int ii = 0; ii < slices_; ii++) {
        values_[ii].~SignalType();
      }
59 60 61 62
      embb::base::Allocation::Free(values_);
    }
  }

63
  SignalType const & GetSignal(int clock) const {
64
    return values_[clock % slices_];
65 66 67 68
  }

  Type GetValue(int clock) const {
    SignalType const & signal = GetSignal(clock);
69
    assert(!signal.IsBlank());
70 71 72 73 74 75
    return signal.GetValue();
  }

  bool IsConnected() const { return connected_; }
  void SetConnected() { connected_ = true; }

76 77 78 79
  bool HasCycle(ClockListener * node) {
    return listener_->OnHasCycle(node);
  }

80
  void SetSlices(int slices) {
81 82 83 84 85 86 87
    if (0 < slices_) {
      for (int ii = 0; ii < slices_; ii++) {
        values_[ii].~SignalType();
      }
      embb::base::Allocation::Free(values_);
      values_ = NULL;
    }
88
    slices_ = slices;
89 90 91 92 93 94 95
    if (0 < slices_) {
      values_ = reinterpret_cast<SignalType*>(
        embb::base::Allocation::Allocate(
          sizeof(SignalType)*slices_));
      for (int ii = 0; ii < slices_; ii++) {
        new (&values_[ii]) SignalType();
      }
96 97 98
    }
  }

99 100 101
  void SetListener(ClockListener * listener) { listener_ = listener; }

  void Clear(int clock) {
102
    const int idx = clock % slices_;
103 104 105
    values_[idx].Clear();
  }

106
  friend class Out<Type>;
107 108

 private:
109
  SignalType * values_;
110 111
  ClockListener * listener_;
  bool connected_;
112
  int slices_;
113
#if EMBB_DATAFLOW_TRACE_SIGNAL_HISTORY
114
  embb::base::Spinlock lock_;
115 116 117 118
  std::vector<SignalType> history_;
#endif

  void Receive(SignalType const & value) {
119
    const int idx = value.GetClock() % slices_;
120
    assert(values_[idx].GetClock() < value.GetClock());
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    values_[idx] = value;
    listener_->OnClock(value.GetClock());
#if EMBB_DATAFLOW_TRACE_SIGNAL_HISTORY
    lock_.Lock();
    history_.push_back(value);
    lock_.Unlock();
#endif
  }
};

} // namespace internal
} // namespace dataflow
} // namespace embb

#endif // EMBB_DATAFLOW_INTERNAL_IN_H_