inputs.h 14.2 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 87 88
    // 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 122
  virtual bool OnHasCycle(ClockListener * node) {
    return listener_->OnHasCycle(node);
  }
123 124
  bool IsFullyConnected() {
    return this->template Get<0>().IsConnected();
125
  }
126
 private:
127
  embb::base::Atomic<int> * count_;
128
  ClockListener * listener_;
129
  int slices_;
130 131
};

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

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

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

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

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

#endif // EMBB_DATAFLOW_INTERNAL_INPUTS_H_