inputs.h 14.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
  Inputs() : count_(NULL), slices_(0) {
74 75 76 77 78 79 80 81 82 83 84 85 86
    // 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++) {
87
        new(count_+ii) embb::base::Atomic<int>(1);
88
      }
89 90
    }
    this->template Get<0>().SetSlices(slices_);
91
  }
92 93
  ~Inputs() {
    if (NULL != count_) {
94 95 96
      for (int ii = 0; ii < slices_; ii++) {
        count_[ii].~Atomic<int>();
      }
97 98 99
      embb::base::Allocation::Free(count_);
    }
  }
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  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) {
116
    const int idx = clock % slices_;
117
    assert(count_[idx] > 0);
118 119 120 121 122
    if (--count_[idx] == 0) {
      count_[idx] = 1;
      listener_->OnClock(clock);
    }
  }
123 124 125
  virtual bool OnHasCycle(ClockListener * node) {
    return listener_->OnHasCycle(node);
  }
126 127
  bool IsFullyConnected() {
    return this->template Get<0>().IsConnected();
128
  }
129
 private:
130
  embb::base::Atomic<int> * count_;
131
  ClockListener * listener_;
132
  int slices_;
133 134
};

135 136
template <typename T1, typename T2>
class Inputs<T1, T2, embb::base::internal::Nil,
137
  embb::base::internal::Nil, embb::base::internal::Nil>
138
  : public Tuple<In<T1>, In<T2>, embb::base::internal::Nil,
139 140 141
    embb::base::internal::Nil, embb::base::internal::Nil>
  , public ClockListener {
 public:
142
  Inputs() : count_(NULL), slices_(0) {
143 144 145 146 147 148 149 150 151 152 153 154 155
    // 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++) {
156
        new(count_ + ii) embb::base::Atomic<int>(2);
157
      }
158 159 160
    }
    this->template Get<0>().SetSlices(slices_);
    this->template Get<1>().SetSlices(slices_);
161
  }
162 163
  ~Inputs() {
    if (NULL != count_) {
164 165 166
      for (int ii = 0; ii < slices_; ii++) {
        count_[ii].~Atomic<int>();
      }
167 168 169
      embb::base::Allocation::Free(count_);
    }
  }
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  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) {
190
    const int idx = clock % slices_;
191
    assert(count_[idx] > 0);
192 193 194 195 196
    if (--count_[idx] == 0) {
      count_[idx] = 2;
      listener_->OnClock(clock);
    }
  }
197 198 199
  virtual bool OnHasCycle(ClockListener * node) {
    return listener_->OnHasCycle(node);
  }
200 201 202
  bool IsFullyConnected() {
    return this->template Get<0>().IsConnected() &
      this->template Get<1>().IsConnected();
203
  }
204
 private:
205
  embb::base::Atomic<int> * count_;
206
  ClockListener * listener_;
207
  int slices_;
208 209
};

210 211
template <typename T1, typename T2, typename T3>
class Inputs<T1, T2, T3, embb::base::internal::Nil,
212
  embb::base::internal::Nil>
213
  : public Tuple<In<T1>, In<T2>, In<T3>,
214 215 216
    embb::base::internal::Nil, embb::base::internal::Nil>
  , public ClockListener {
 public:
217
  Inputs() : count_(NULL), slices_(0) {
218 219 220 221 222 223 224 225 226 227 228 229 230
    // 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++) {
231
        new(count_ + ii) embb::base::Atomic<int>(3);
232
      }
233 234 235 236
    }
    this->template Get<0>().SetSlices(slices_);
    this->template Get<1>().SetSlices(slices_);
    this->template Get<2>().SetSlices(slices_);
237
  }
238 239
  ~Inputs() {
    if (NULL != count_) {
240 241 242
      for (int ii = 0; ii < slices_; ii++) {
        count_[ii].~Atomic<int>();
      }
243 244 245
      embb::base::Allocation::Free(count_);
    }
  }
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  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) {
270
    const int idx = clock % slices_;
271
    assert(count_[idx] > 0);
272
    if (--count_[idx] == 0) {
273 274 275 276
      count_[idx] = 3;
      listener_->OnClock(clock);
    }
  }
277 278 279
  virtual bool OnHasCycle(ClockListener * node) {
    return listener_->OnHasCycle(node);
  }
280 281 282 283
  bool IsFullyConnected() {
    return this->template Get<0>().IsConnected() &
      this->template Get<1>().IsConnected() &
      this->template Get<2>().IsConnected();
284
  }
285
 private:
286
  embb::base::Atomic<int> * count_;
287
  ClockListener * listener_;
288
  int slices_;
289 290
};

291 292 293 294
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>
295 296
  , public ClockListener {
 public:
297
  Inputs() : count_(NULL), slices_(0) {
298 299 300 301 302 303 304 305 306 307 308 309 310
    // 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++) {
311
        new(count_ + ii) embb::base::Atomic<int>(4);
312
      }
313 314 315 316 317
    }
    this->template Get<0>().SetSlices(slices_);
    this->template Get<1>().SetSlices(slices_);
    this->template Get<2>().SetSlices(slices_);
    this->template Get<3>().SetSlices(slices_);
318
  }
319 320
  ~Inputs() {
    if (NULL != count_) {
321 322 323
      for (int ii = 0; ii < slices_; ii++) {
        count_[ii].~Atomic<int>();
      }
324 325 326
      embb::base::Allocation::Free(count_);
    }
  }
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
  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) {
355
    const int idx = clock % slices_;
356
    assert(count_[idx] > 0);
357 358 359 360 361
    if (--count_[idx] == 0) {
      count_[idx] = 4;
      listener_->OnClock(clock);
    }
  }
362 363 364
  virtual bool OnHasCycle(ClockListener * node) {
    return listener_->OnHasCycle(node);
  }
365 366 367 368 369
  bool IsFullyConnected() {
    return this->template Get<0>().IsConnected() &
      this->template Get<1>().IsConnected() &
      this->template Get<2>().IsConnected() &
      this->template Get<3>().IsConnected();
370
  }
371
 private:
372
  embb::base::Atomic<int> * count_;
373
  ClockListener * listener_;
374
  int slices_;
375 376
};

377
template <typename T1, typename T2, typename T3, typename T4,
378 379
  typename T5>
class Inputs
380 381
  : public Tuple<In<T1>, In<T2>, In<T3>,
      In<T4>, In<T5> >
382 383
  , public ClockListener {
 public:
384
  Inputs() : count_(NULL), slices_(0) {
385 386 387 388 389 390 391 392 393 394 395 396 397
    // 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++) {
398
        new(count_ + ii) embb::base::Atomic<int>(5);
399
      }
400 401 402 403 404 405
    }
    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_);
406
  }
407 408
  ~Inputs() {
    if (NULL != count_) {
409 410 411
      for (int ii = 0; ii < slices_; ii++) {
        count_[ii].~Atomic<int>();
      }
412 413 414
      embb::base::Allocation::Free(count_);
    }
  }
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
  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) {
447
    const int idx = clock % slices_;
448
    assert(count_[idx] > 0);
449 450 451 452 453
    if (--count_[idx] == 0) {
      count_[idx] = 5;
      listener_->OnClock(clock);
    }
  }
454 455 456
  virtual bool OnHasCycle(ClockListener * node) {
    return listener_->OnHasCycle(node);
  }
457 458 459 460 461 462
  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();
463
  }
464
 private:
465
  embb::base::Atomic<int> * count_;
466
  ClockListener * listener_;
467
  int slices_;
468 469 470 471 472 473 474
};

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

#endif // EMBB_DATAFLOW_INTERNAL_INPUTS_H_