inputs.h 13.8 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
 *
 * 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_INPUTS_H_
#define EMBB_DATAFLOW_INTERNAL_INPUTS_H_

30
#include <embb/base/atomic.h>
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <embb/dataflow/internal/tuple.h>
#include <embb/dataflow/internal/in.h>

namespace embb {
namespace dataflow {
namespace internal {

template <
  typename = embb::base::internal::Nil,
  typename = embb::base::internal::Nil,
  typename = embb::base::internal::Nil,
  typename = embb::base::internal::Nil,
  typename = embb::base::internal::Nil>
class Inputs;

46 47
template <>
class Inputs<embb::base::internal::Nil, embb::base::internal::Nil,
48 49 50 51 52 53 54 55 56 57 58
  embb::base::internal::Nil, embb::base::internal::Nil,
  embb::base::internal::Nil>
  : public Tuple<embb::base::internal::Nil, embb::base::internal::Nil,
    embb::base::internal::Nil, embb::base::internal::Nil,
    embb::base::internal::Nil>
  , public ClockListener {
 public:
  void SetListener(ClockListener * /*notify*/) {}
  bool AreNoneBlank(int /*clock*/) { return false; }
  bool AreAtClock(int /*clock*/) { return true; }
  virtual void OnClock(int /*clock*/) {}
59 60 61
  bool IsFullyConnected() {
    return true;
  }
62
  void SetSlices(int /*slices*/) {}
63 64
};

65 66
template <typename T1>
class Inputs<T1, embb::base::internal::Nil, embb::base::internal::Nil,
67
  embb::base::internal::Nil, embb::base::internal::Nil>
68
  : public Tuple<In<T1>, embb::base::internal::Nil,
69 70 71 72
    embb::base::internal::Nil, embb::base::internal::Nil,
    embb::base::internal::Nil>
  , public ClockListener {
 public:
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  explicit Inputs() : count_(NULL), slices_(0) {
    // empty
  }
  void SetSlices(int slices) {
    if (0 < slices_) {
      embb::base::Allocation::Free(count_);
      count_ = NULL;
    }
    slices_ = slices;
    if (0 < slices_) {
      count_ = reinterpret_cast<embb::base::Atomic<int>*>(
        embb::base::Allocation::Allocate(
          sizeof(embb::base::Atomic<int>)*slices_));
      for (int ii = 0; ii < slices_; ii++) {
        count_[ii] = 1;
      }
89 90
    }
    this->template Get<0>().SetSlices(slices_);
91
  }
92 93 94 95 96
  ~Inputs() {
    if (NULL != count_) {
      embb::base::Allocation::Free(count_);
    }
  }
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  void SetListener(ClockListener * listener) {
    listener_ = listener;
    this->template Get<0>().SetListener(this);
  }
  bool AreNoneBlank(int clock) {
    return !(
      this->template Get<0>().GetSignal(clock).IsBlank());
  }
  bool AreAtClock(int clock) {
    return
      (this->template Get<0>().GetSignal(clock).GetClock() == clock);
  }
  void Clear(int clock) {
    this->template Get<0>().Clear(clock);
  }
  virtual void OnClock(int clock) {
113
    const int idx = clock % slices_;
114
    assert(count_[idx] > 0);
115 116 117 118 119
    if (--count_[idx] == 0) {
      count_[idx] = 1;
      listener_->OnClock(clock);
    }
  }
120 121
  bool IsFullyConnected() {
    return this->template Get<0>().IsConnected();
122
  }
123
 private:
124
  embb::base::Atomic<int> * count_;
125
  ClockListener * listener_;
126
  int slices_;
127 128
};

129 130
template <typename T1, typename T2>
class Inputs<T1, T2, embb::base::internal::Nil,
131
  embb::base::internal::Nil, embb::base::internal::Nil>
132
  : public Tuple<In<T1>, In<T2>, embb::base::internal::Nil,
133 134 135
    embb::base::internal::Nil, embb::base::internal::Nil>
  , public ClockListener {
 public:
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  explicit Inputs() : count_(NULL), slices_(0) {
    // empty
  }
  void SetSlices(int slices) {
    if (0 < slices_) {
      embb::base::Allocation::Free(count_);
      count_ = NULL;
    }
    slices_ = slices;
    if (0 < slices_) {
      count_ = reinterpret_cast<embb::base::Atomic<int>*>(
        embb::base::Allocation::Allocate(
          sizeof(embb::base::Atomic<int>)*slices_));
      for (int ii = 0; ii < slices_; ii++) {
        count_[ii] = 2;
      }
152 153 154
    }
    this->template Get<0>().SetSlices(slices_);
    this->template Get<1>().SetSlices(slices_);
155
  }
156 157 158 159 160
  ~Inputs() {
    if (NULL != count_) {
      embb::base::Allocation::Free(count_);
    }
  }
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  void SetListener(ClockListener * listener) {
    listener_ = listener;
    this->template Get<0>().SetListener(this);
    this->template Get<1>().SetListener(this);
  }
  bool AreNoneBlank(int clock) {
    return !(
      this->template Get<0>().GetSignal(clock).IsBlank() ||
      this->template Get<1>().GetSignal(clock).IsBlank());
  }
  bool AreAtClock(int clock) {
    return
      (this->template Get<0>().GetSignal(clock).GetClock() == clock) &&
      (this->template Get<1>().GetSignal(clock).GetClock() == clock);
  }
  void Clear(int clock) {
    this->template Get<0>().Clear(clock);
    this->template Get<1>().Clear(clock);
  }
  virtual void OnClock(int clock) {
181
    const int idx = clock % slices_;
182
    assert(count_[idx] > 0);
183 184 185 186 187
    if (--count_[idx] == 0) {
      count_[idx] = 2;
      listener_->OnClock(clock);
    }
  }
188 189 190
  bool IsFullyConnected() {
    return this->template Get<0>().IsConnected() &
      this->template Get<1>().IsConnected();
191
  }
192
 private:
193
  embb::base::Atomic<int> * count_;
194
  ClockListener * listener_;
195
  int slices_;
196 197
};

198 199
template <typename T1, typename T2, typename T3>
class Inputs<T1, T2, T3, embb::base::internal::Nil,
200
  embb::base::internal::Nil>
201
  : public Tuple<In<T1>, In<T2>, In<T3>,
202 203 204
    embb::base::internal::Nil, embb::base::internal::Nil>
  , public ClockListener {
 public:
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
  explicit Inputs() : count_(NULL), slices_(0) {
    // empty
  }
  void SetSlices(int slices) {
    if (0 < slices_) {
      embb::base::Allocation::Free(count_);
      count_ = NULL;
    }
    slices_ = slices;
    if (0 < slices_) {
      count_ = reinterpret_cast<embb::base::Atomic<int>*>(
        embb::base::Allocation::Allocate(
          sizeof(embb::base::Atomic<int>)*slices_));
      for (int ii = 0; ii < slices_; ii++) {
        count_[ii] = 3;
      }
221 222 223 224
    }
    this->template Get<0>().SetSlices(slices_);
    this->template Get<1>().SetSlices(slices_);
    this->template Get<2>().SetSlices(slices_);
225
  }
226 227 228 229 230
  ~Inputs() {
    if (NULL != count_) {
      embb::base::Allocation::Free(count_);
    }
  }
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  void SetListener(ClockListener * listener) {
    listener_ = listener;
    this->template Get<0>().SetListener(this);
    this->template Get<1>().SetListener(this);
    this->template Get<2>().SetListener(this);
  }
  bool AreNoneBlank(int clock) {
    return !(
      this->template Get<0>().GetSignal(clock).IsBlank() ||
      this->template Get<1>().GetSignal(clock).IsBlank() ||
      this->template Get<2>().GetSignal(clock).IsBlank());
  }
  bool AreAtClock(int clock) {
    return
      (this->template Get<0>().GetSignal(clock).GetClock() == clock) &&
      (this->template Get<1>().GetSignal(clock).GetClock() == clock) &&
      (this->template Get<2>().GetSignal(clock).GetClock() == clock);
  }
  void Clear(int clock) {
    this->template Get<0>().Clear(clock);
    this->template Get<1>().Clear(clock);
    this->template Get<2>().Clear(clock);
  }
  virtual void OnClock(int clock) {
255
    const int idx = clock % slices_;
256
    assert(count_[idx] > 0);
257
    if (--count_[idx] == 0) {
258 259 260 261
      count_[idx] = 3;
      listener_->OnClock(clock);
    }
  }
262 263 264 265
  bool IsFullyConnected() {
    return this->template Get<0>().IsConnected() &
      this->template Get<1>().IsConnected() &
      this->template Get<2>().IsConnected();
266
  }
267
 private:
268
  embb::base::Atomic<int> * count_;
269
  ClockListener * listener_;
270
  int slices_;
271 272
};

273 274 275 276
template <typename T1, typename T2, typename T3, typename T4>
class Inputs<T1, T2, T3, T4, embb::base::internal::Nil>
  : public Tuple<In<T1>, In<T2>, In<T3>,
      In<T4>, embb::base::internal::Nil>
277 278
  , public ClockListener {
 public:
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
  explicit Inputs() : count_(NULL), slices_(0) {
    // empty
  }
  void SetSlices(int slices) {
    if (0 < slices_) {
      embb::base::Allocation::Free(count_);
      count_ = NULL;
    }
    slices_ = slices;
    if (0 < slices_) {
      count_ = reinterpret_cast<embb::base::Atomic<int>*>(
        embb::base::Allocation::Allocate(
          sizeof(embb::base::Atomic<int>)*slices_));
      for (int ii = 0; ii < slices_; ii++) {
        count_[ii] = 4;
      }
295 296 297 298 299
    }
    this->template Get<0>().SetSlices(slices_);
    this->template Get<1>().SetSlices(slices_);
    this->template Get<2>().SetSlices(slices_);
    this->template Get<3>().SetSlices(slices_);
300
  }
301 302 303 304 305
  ~Inputs() {
    if (NULL != count_) {
      embb::base::Allocation::Free(count_);
    }
  }
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
  void SetListener(ClockListener * listener) {
    listener_ = listener;
    this->template Get<0>().SetListener(this);
    this->template Get<1>().SetListener(this);
    this->template Get<2>().SetListener(this);
    this->template Get<3>().SetListener(this);
  }
  bool AreNoneBlank(int clock) {
    return !(
      this->template Get<0>().GetSignal(clock).IsBlank() ||
      this->template Get<1>().GetSignal(clock).IsBlank() ||
      this->template Get<2>().GetSignal(clock).IsBlank() ||
      this->template Get<3>().GetSignal(clock).IsBlank());
  }
  bool AreAtClock(int clock) {
    return
      (this->template Get<0>().GetSignal(clock).GetClock() == clock) &&
      (this->template Get<1>().GetSignal(clock).GetClock() == clock) &&
      (this->template Get<2>().GetSignal(clock).GetClock() == clock) &&
      (this->template Get<3>().GetSignal(clock).GetClock() == clock);
  }
  void Clear(int clock) {
    this->template Get<0>().Clear(clock);
    this->template Get<1>().Clear(clock);
    this->template Get<2>().Clear(clock);
    this->template Get<3>().Clear(clock);
  }
  virtual void OnClock(int clock) {
334
    const int idx = clock % slices_;
335
    assert(count_[idx] > 0);
336 337 338 339 340
    if (--count_[idx] == 0) {
      count_[idx] = 4;
      listener_->OnClock(clock);
    }
  }
341 342 343 344 345
  bool IsFullyConnected() {
    return this->template Get<0>().IsConnected() &
      this->template Get<1>().IsConnected() &
      this->template Get<2>().IsConnected() &
      this->template Get<3>().IsConnected();
346
  }
347
 private:
348
  embb::base::Atomic<int> * count_;
349
  ClockListener * listener_;
350
  int slices_;
351 352
};

353
template <typename T1, typename T2, typename T3, typename T4,
354 355
  typename T5>
class Inputs
356 357
  : public Tuple<In<T1>, In<T2>, In<T3>,
      In<T4>, In<T5> >
358 359
  , public ClockListener {
 public:
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
  explicit Inputs() : count_(NULL), slices_(0) {
    // empty
  }
  void SetSlices(int slices) {
    if (0 < slices_) {
      embb::base::Allocation::Free(count_);
      count_ = NULL;
    }
    slices_ = slices;
    if (0 < slices_) {
      count_ = reinterpret_cast<embb::base::Atomic<int>*>(
        embb::base::Allocation::Allocate(
          sizeof(embb::base::Atomic<int>)*slices_));
      for (int ii = 0; ii < slices_; ii++) {
        count_[ii] = 5;
      }
376 377 378 379 380 381
    }
    this->template Get<0>().SetSlices(slices_);
    this->template Get<1>().SetSlices(slices_);
    this->template Get<2>().SetSlices(slices_);
    this->template Get<3>().SetSlices(slices_);
    this->template Get<4>().SetSlices(slices_);
382
  }
383 384 385 386 387
  ~Inputs() {
    if (NULL != count_) {
      embb::base::Allocation::Free(count_);
    }
  }
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
  void SetListener(ClockListener * listener) {
    listener_ = listener;
    this->template Get<0>().SetListener(this);
    this->template Get<1>().SetListener(this);
    this->template Get<2>().SetListener(this);
    this->template Get<3>().SetListener(this);
    this->template Get<4>().SetListener(this);
  }
  bool AreNoneBlank(int clock) {
    return !(
      this->template Get<0>().GetSignal(clock).IsBlank() ||
      this->template Get<1>().GetSignal(clock).IsBlank() ||
      this->template Get<2>().GetSignal(clock).IsBlank() ||
      this->template Get<3>().GetSignal(clock).IsBlank() ||
      this->template Get<4>().GetSignal(clock).IsBlank());
  }
  bool AreAtClock(int clock) {
    return
      (this->template Get<0>().GetSignal(clock).GetClock() == clock) &&
      (this->template Get<1>().GetSignal(clock).GetClock() == clock) &&
      (this->template Get<2>().GetSignal(clock).GetClock() == clock) &&
      (this->template Get<3>().GetSignal(clock).GetClock() == clock) &&
      (this->template Get<4>().GetSignal(clock).GetClock() == clock);
  }
  void Clear(int clock) {
    this->template Get<0>().Clear(clock);
    this->template Get<1>().Clear(clock);
    this->template Get<2>().Clear(clock);
    this->template Get<3>().Clear(clock);
    this->template Get<4>().Clear(clock);
  }
  virtual void OnClock(int clock) {
420
    const int idx = clock % slices_;
421
    assert(count_[idx] > 0);
422 423 424 425 426
    if (--count_[idx] == 0) {
      count_[idx] = 5;
      listener_->OnClock(clock);
    }
  }
427 428 429 430 431 432
  bool IsFullyConnected() {
    return this->template Get<0>().IsConnected() &&
      this->template Get<1>().IsConnected() &
      this->template Get<2>().IsConnected() &
      this->template Get<3>().IsConnected() &
      this->template Get<4>().IsConnected();
433
  }
434
 private:
435
  embb::base::Atomic<int> * count_;
436
  ClockListener * listener_;
437
  int slices_;
438 439 440 441 442 443 444
};

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

#endif // EMBB_DATAFLOW_INTERNAL_INPUTS_H_