コード例 #1
0
ファイル: xdripback.c プロジェクト: gijsbers/snet-rts
/* DripBack process a record. */
void SNetNodeDripBack(snet_stream_desc_t *desc, snet_record_t *rec)
{
  landing_t             *land = desc->landing;
  dripback_arg_t        *darg = LAND_NODE_SPEC(land, dripback);

  trace(__func__);

  if (land->type == LAND_dripback1) {
    landing_dripback1_t *db1 = LAND_SPEC(land, dripback1);
    landing_dripback2_t *db2 = LAND_SPEC(db1->dripback2, dripback2);
    SNetFifoPut(db1->recfifo, rec);
    if (FAA(&db2->queued, 1) == 0) {
      if (db1->controldesc == NULL) {
        SNetPushLanding(desc, db1->dripback2);
        db1->controldesc = SNetStreamOpen(darg->selfref, desc);
      }
      rec = SNetRecCreate(REC_wakeup);
      SNetWrite(&db1->controldesc, rec, true);
    }
  }
  else if (land->type == LAND_dripback2) {
    landing_dripback2_t *db2 = LAND_SPEC(land, dripback2);
    bool via_access = (DESC_STREAM(desc) == darg->selfref);

    assert(via_access || DESC_STREAM(desc) == darg->dripback);

    while (rec) {
      switch (REC_DESCR( rec)) {
        case REC_data:
          /* Test if record should go to the instance. */
          if (SNetFeedbackMatch( rec, darg->back_patterns, darg->guards)) {
            if (via_access) {
              /* Because record came from outside add a detref counter. */
              assert(db2->detfifo.head->next == NULL);
              SNetRecDetrefAdd(rec, ++(db2->entered), land, &db2->detfifo);
            } else {
              /* Record came from the instance. */
              assert(DATA_REC(rec, detref));
            }
            if (db2->instdesc == NULL) {
              /* Instance should come back to this landing. */
              if (SNetTopLanding(desc) != land) {
                SNetPushLanding(desc, land);
              }
              db2->instdesc = SNetStreamOpen(darg->instance, desc);
              if (SNetTopLanding(desc) == land) {
                SNetPopLanding(desc);
                SNetLandingDone(land);
              }
            }
            SNetWrite(&db2->instdesc, rec, false);
          } else {
            if (!via_access) {
              /* Record leaves the dripback loop. */
              assert(DATA_REC(rec, detref));
              SNetFeedbackLeave(rec, land, &db2->detfifo);
            }
            if (db2->outdesc == NULL) {
              db2->outdesc = SNetStreamOpen(darg->output, desc);
            }
            SNetWrite(&db2->outdesc, rec, false);
          }
          break;

        case REC_wakeup:
          assert(via_access);
          SNetRecDestroy(rec);
          break;

        case REC_terminate:
          assert(via_access);
          assert(db2->terminate == DripBackInitial);
          db2->terminate = DripBackDraining;
          SNetRecDestroy(rec);
          break;

        case REC_detref:
          if (DETREF_REC( rec, leave) == land &&
              DETREF_REC( rec, location) == SNetDistribGetNodeId())
          {
            assert(!via_access);
            SNetDetLeaveCheckDetref(rec, &db2->detfifo);
            if (DETREF_REC( rec, detref) == SNetFifoPeekFirst(&db2->detfifo)) {
              DripBackCheckBusy(db2);
            }
            SNetRecDestroy(rec);
          } else {
            assert(via_access);
            if (db2->outdesc == NULL) {
              db2->outdesc = SNetStreamOpen(darg->output, desc);
            }
            SNetWrite(&db2->outdesc, rec, false);
          }
          break;

        case REC_sync:
          SNetRecDestroy(rec);
          break;

        default:
          SNetRecUnknownEnt(__func__, rec, darg->entity);
      }
      rec = NULL;
      if (db2->state == DripBackBusy) {
        if (DripBackCheckBusy(db2) == false) {
          assert(db2->queued > 0);
          if (SAF(&db2->queued, 1) == 0) {
            db2->state = DripBackIdle;
          } else {
            rec = SNetFifoGet(&db2->recfifo);
            assert(rec);
            via_access = true;
          }
        }
      } else {
        assert(db2->state == DripBackIdle);
        assert(SNetFifoPeekFirst(&db2->detfifo) == NULL);
        if (db2->queued > 0) {
          rec = SNetFifoGet(&db2->recfifo);
          assert(rec);
          via_access = true;
          db2->state = DripBackBusy;
        }
      }
    }
    if (db2->terminate == DripBackDraining) {
      if (db2->state == DripBackIdle) {
        assert(db2->queued == 0 && DripBackCheckBusy(db2) == false);
        db2->terminate = DripBackTerminated;
      }
    }
    if (db2->terminate == DripBackTerminated) {
      if (db2->instdesc) {
        snet_stream_desc_t *desc = db2->instdesc;
        db2->instdesc = NULL;
        SNetDescDone(desc);
        SNetLandingDone(land);
      }
    }
  }
  else {
    assert(0);
  }
}
コード例 #2
0
ファイル: xfeedback.c プロジェクト: ra-one/snet-rts
/* Feedback forward a record. */
void SNetNodeFeedback(snet_stream_desc_t *desc, snet_record_t *rec)
{
  landing_t             *land = desc->landing;
  feedback_arg_t        *farg = LAND_NODE_SPEC(land, feedback);

  trace(__func__);

  if (land->type == LAND_feedback1) {
    landing_feedback1_t *fb1 = LAND_SPEC(land, feedback1);

    switch (REC_DESCR( rec)) {
      case REC_data:
        /* Test if record should go into the feedback loop. */
        if (SNetFeedbackMatch( rec, farg->back_patterns, farg->guards)) {
          /* Because record came from outside add a detref counter. */
          SNetRecDetrefAdd(rec, ++(fb1->counter), fb1->feedback3, fb1->detfifo);
          if (fb1->instdesc == NULL) {
            SNetPushLanding(desc, fb1->feedback2);
            fb1->instdesc = SNetStreamOpen(farg->selfref2, desc);
          }
          SNetWrite(&fb1->instdesc, rec, true);
        } else {
          if (fb1->outdesc == NULL) {
            SNetPushLanding(desc, fb1->feedback4);
            fb1->outdesc = SNetStreamOpen(farg->selfref4, desc);
          }
          SNetWrite(&fb1->outdesc, rec, true);
        }
        break;

      case REC_detref:
        if (fb1->outdesc == NULL) {
          SNetPushLanding(desc, fb1->feedback4);
          fb1->outdesc = SNetStreamOpen(farg->selfref4, desc);
        }
        SNetWrite(&fb1->outdesc, rec, true);
        break;

      case REC_sync:
        SNetRecDestroy(rec);
        break;

      default:
        SNetRecUnknownEnt(__func__, rec, farg->entity);
    }
  }
  else if (land->type == LAND_feedback2) {
    landing_feedback2_t *fb2 = LAND_SPEC(land, feedback2);
    if (fb2->instdesc == NULL) {
      SNetPushLanding(desc, fb2->feedback3);
      fb2->instdesc = SNetStreamOpen(farg->instance, desc);
    }
    switch (REC_DESCR( rec)) {
      case REC_data:
      case REC_detref:
        SNetWrite(&fb2->instdesc, rec, true);
        break;

      default:
        SNetRecUnknownEnt(__func__, rec, farg->entity);
    }
  }
  else if (land->type == LAND_feedback4) {
    landing_feedback4_t *fb4 = LAND_SPEC(land, feedback4);
    if (fb4->outdesc == NULL) {
      fb4->outdesc = SNetStreamOpen(farg->output, desc);
    }
    switch (REC_DESCR( rec)) {
      case REC_data:
      case REC_detref:
        SNetWrite(&fb4->outdesc, rec, true);
        break;

      default:
        SNetRecUnknownEnt(__func__, rec, farg->entity);
    }
  }
  else if (land->type == LAND_feedback3) {
    landing_feedback3_t *fb3 = LAND_SPEC(land, feedback3);
    if (fb3->terminate == FeedbackTerminated) {
      assert(REC_DESCR( rec) != REC_data);
      SNetRecDestroy(rec);
    } else {
      switch (REC_DESCR( rec)) {
        case REC_data:
          /* Test if record should go into the feedback loop. */
          if (SNetFeedbackMatch( rec, farg->back_patterns, farg->guards)) {
            if (fb3->instdesc == NULL) {
              SNetPushLanding(desc, fb3->feedback2);
              fb3->instdesc = SNetStreamOpen(farg->selfref2, desc);
            }
            /* send the record to the instance */
            SNetWrite(&fb3->instdesc, rec, false);
          } else {
            /* record leaves the feedback loop. */
            SNetFeedbackLeave(rec, land, &fb3->detfifo);
            if (fb3->outdesc == NULL) {
              SNetPushLanding(desc, fb3->feedback4);
              fb3->outdesc = SNetStreamOpen(farg->selfref4, desc);
            }
            /* forward record outwards */
            SNetWrite(&fb3->outdesc, rec, false);
            if (fb3->terminate) {
              assert(fb3->terminate == FeedbackDraining);
              FeedbackCheckBusy(fb3);
            }
          }
          break;

        case REC_detref:
          if (DETREF_REC( rec, leave) == land) {
            if (DETREF_REC( rec, seqnr) == -1L) {
              assert(fb3->terminate == FeedbackInitial);
              fb3->terminate = FeedbackDraining;
            }
            SNetRecDestroy(rec);
            FeedbackCheckBusy(fb3);
          }
          break;

        case REC_sync:
          SNetRecDestroy(rec);
          break;

        default:
          SNetRecUnknownEnt(__func__, rec, farg->entity);
      }
      if (fb3->terminate == FeedbackTerminating) {
        if (fb3->outdesc) {
          SNetDescDone(fb3->outdesc);
        }
        if (fb3->instdesc) {
          SNetDescDone(fb3->instdesc);
        }
        SNetLandingDone(fb3->feedback2);
        fb3->terminate = FeedbackTerminated;
      }
    }
  }
  else {
    assert(0);
  }
}