} Motion;
typedef struct Method Method;
+typedef struct Change Change;
+
+typedef struct MovPosChange {
+ Segment *move;
+ /* everything beyond here is private for indep */
+ MovPosComb actual, target;
+ int refcount;
+ int n_changes;
+ Change *changes[];
+} Indep;
+
+static void method_update_feature(Method*, MovPosChange*, const Motion *mo);
+ /* Called from methods' execution logic when an individual feature
+ * has been moved. This is used by the method-independent code to
+ * compute the correct delta set of movements from the current
+ * actual position, when thinking about new plans. It is also sent
+ * to clients and ultimately used by the UI.
+ */
+
+static void method_change_done(Method *m, Change *chg);
+ /* Called from methods' execution logic when a whole change
+ * has been done. The method-independent code will take care of
+ * updating move->movposcomb. etc.
+ *
+ * REENTRANCY: May be called from within a call to the method's
+ * execute(). Of course cannot legally be called from within
+ * prepare, consider, check or dispose.
+ */
+
/* Kind-independent code is responsible for determining
* the method, doing a bit of cleanup, and adjusting the flow
*/
/*
* The following states exist for each Method
- * I Initialising corresponds to global states other than Sta_Run
+ * S Starting corresponds to global states other than Sta_Run
* T Tentative changes have been made but may yet be undone
* Y Yes proposed changes have been checked and are OK
* E Executing the method is executing
* at points when control flow passes between kind and indep:
* U Unallocated no memory allocated (Change does not exist)
* P Prepared memory allocation done, basics filled in
- * Q Queued reservation or confirmation successful
+ * I Installed reservation or confirmation successful
* D Done motion is complete and callback is being entered
* G Garbage motion is complete and callback is being exited
*
* No Changes remain Prepared while the Method is Executing.
*/
-typedef struct Change Change;
-
struct Change { /* valid in: filled in by and when: */
- Method *meth; /* PQ indep after prepare() */
- MovPosChange *indep; /* PQ indep after prepare() */
+ Method *meth; /* PIDG indep after prepare() */
+ MovPosChange *indep; /* PID indep after prepare() */
+ unsigned installed:1; /* private for indep */
/* kind-specific data follows */ /* varies kind-specific code, varies */
-} Change;
+};
struct Method {
const char *pname;
- MovFeatKind kind;
+
ErrorCode (*prepare)(Method *m, /* TYE->T */
- Segment *move,
+ const Segment *move,
int n_motions, const Motion *motions,
- int ms, int confirming,
- Change *chg_r, /* 0->P; err: 0->0; may be 0 */
+ int ms, int confirmation,
+ Change **chg_r, /* 0->P; err: 0->0; may be 0 */
int *cost_r /* may be 0 */);
- ErrorCode (*consider)(Method *m, /* TYE->T */
- Change *remove, /* Q->P; err: Q->Q */
- Change *install); /* P->Q; err: P->P */
- /* error: ET->T, no change to Changes */
void (*dispose)(Method *m, /* TY->TY */
Change*); /* P->U */
+ ErrorCode (*install)(Method *m, /* TYE->T; err: TYE->TYY */
+ Change *inst); /* P->I; err: P->P */
+ /* error: ET->T, no change to Changes */
+ void (*remove)(Method *m, /* TYE->TYY */
+ Change *remove); /* I->P */
+ /* error: ET->T, no change to Changes */
+
ErrorCode (*check)(Method *m); /* TYE->Y; err: TYE->TYE */
void (*execute)(Method *m); /* EY->E */
- void (*all_abandon)(Method *m); /* I */
+ void (*all_abandon)(Method *m); /* TYE->S */
+
+ unsigned needcheck, needexec; /* used by indep to track T/Y/E */
};
/*========== general utility functions ==========*/
-const char *movpos_pname(Segment *move, MovPosComb poscomb) {
- return poscomb<0 ? "?" : move->i->poscombs[poscomb].pname;
+const char *movpos_pname(const Segment *move, MovPosComb poscomb) {
+ return !SOMEP(poscomb) ? "?" : move->i->poscombs[poscomb].pname;
}
-static void ouposn_moving(Change *chg) {
- Segment *move= chg->move;
+static void ouposn_moving(const MovPosChange *indep) {
+ Segment *move= indep->move;
ouprintf("movpos %s position %s moving\n",
- move->i->pname, movpos_pname(move, chg->actual));
+ move->i->pname, movpos_pname(move, indep->actual));
+}
+static void ouposn_stable(const Segment *move) {
+ ouprintf("movpos %s position %s stable\n",
+ move->i->pname, movpos_pname(move, move->movposcomb));
+}
+static void ouposn_feat(const Segment *move, const MovFeatInfo *feati,
+ MovPosComb posn, const Method *m) {
+ ouprintf("movpos %s feat %s %d %s\n", move->i->pname,
+ feati->pname, posn, m->pname);
}
MovPosComb movposcomb_update_feature(MovPosComb startpoint,
return above*above_weight + featpos*mfi->weight + below;
}
-MovPosComb movpos_poscomb_actual(Segment *seg) {
+MovPosComb movpos_poscomb_actual(const Segment *seg) {
return seg->moving ? seg->motion->actual : seg->movposcomb;
}
typedef unsigned FsqSlot;
typedef int FsqSlotSigned;
-/* We think there are three states: Allocated, Reserved and Confirmed.
- * (plus of course Unallocated where we don't have a request at all).
- * These correspond to the indep code as follows:
- *
- * indep state pt state queues checked and plan viable
- * Unallocated n/a yes
- * Allocated Allocated yes
- * Reserved Reserved yes
- * Confirmed Confirmed yes
- * Erroneous A/R/C no
- *
- * Erroneous exists only after a failed reserve() or confirm() so it's
- * not that confusing to have this slightly malleable terminology.
- */
-
#define FSQDN (~(FsqSlot)0)
-typedef struct { /* PR QR PC QC */
- /* in queue? absent reserved absent confirmed */
- Change h;
- FsqSlot deadline; /* relative relative absolute absolute */
- int n_motions; /* 1 1 alloc'd num undone */
- Motion motions[];
- /* [0].i: 0 0 non-0 non-0 */
- /* .posn: undef undef defined defined */
+typedef struct { /* state Prep Resv Inst Resv Prep Conf Inst Conf */
+ Change h; /* in queue? absent reserved absent confirmed */
+ FsqSlot deadline; /* relative relative absolute absolute */
+ int n_motions; /* 1 1 num undone num undone */
+ Motion motions[]; /* [0].i: 0 0 non-0 non-0 */
+ /* .posn: undef undef defined defined */
} FsqReq;
- /* We can determine the the state by looking at the two
- * `statedet' fields, marked <- above.
- * There are also intermediate states where the req's
- * statedet fields do not agree with the queue it's on.
- * We write these as, for example,
- * AR to mean statedet says Allocated, but queued on fsq_reserved
- * A? to mean statedet says Allocated, but may be queued
- * etc. They are only allowed while we are in a fsq_... method function.
- */
#define FSQ_MAX_QUEUE 15
future++; DPRINTF2("-");
continue;
}
- DPRINTF2("%s/%s[%d@t+%d]", whichr->h.move->i->pname,
- movpos_pname(whichr->h.move, whichr->h.intent),
+ DPRINTF2("%s/%s[%d@t+%d]", whichr->h.indep->move->i->pname,
+ movpos_pname(whichr->h.indep->move, whichr->h.indep->target),
whichr->n_motions, whichwhen);
if (future > whichwhen) {
DPRINTF2("!...bad\n");
q->l[insat]= q->l[insat-1];
q->l[insat]= r;
q->n++;
+
+ return 0;
}
-static void fsq_item_debug(FsqMethod *m, FsqReq *r,
- const char *pfx, Segment *move) {
- DPRINTF1(movpos,fsq, "%s%s", pfx, move.pname);
+static void fsq_item_debug(FsqMethod *m, FsqReq *r, const char *opwhat) {
+ int DP;
+ Segment *move= r->h.indep->move;
+ DPRINTF1(movpos,fsq, "%s %s %s", m->m.pname, opwhat, move->i->pname);
if (r->motions[0].i) {
- for (int i=0, Motion *mo=r->motions; i<r->n_motions; i++, mo++)
- DPRINTF1(movpos,fsq, "/%s%d", mo->i->pname, (int)mo->posn);
+ int i;
+ Motion *mo;
+ for (i=0, mo=r->motions; i<r->n_motions; i++, mo++)
+ DPRINTF2("/%s%d", mo->i->pname, (int)mo->posn);
+ DPRINTF2("\n");
} else {
- DPRINTF1(movpos,fsq, "(%d)", r->n_motions);
+ DPRINTF2("(%d)\n", r->n_motions);
}
}
-static ErrorCode fsq_enqueue(FsqMethod *m, FsqReq *r) { /* P->Q; err: P->P */
+static ErrorCode fsq_enqueue(FsqMethod *m, FsqReq *r) { /* P->I; err: P->P */
if (!r) return 0;
- fsq_item_debug(m,r,"enqueue:",r->h.indep.move);
- return fsq_queue_insert_item(fsq_item_queue(m,r), r);
+ fsq_item_debug(m,r,"enqueue");
+ return fsq_queue_insert_item(m, fsq_item_queue(m,r), r);
}
-static void fsq_dequeue(FsqMethod *m, FsqReq *r) { /* Q->P */
- if (!r) return 0;
- fsq_item_debug(m,r,"dequeue:",r->h.indep.move);
- fsq_remove_item(fsq_item_queue(m,r), r);
+static void fsq_dequeue(FsqMethod *m, FsqReq *r) { /* I->P */
+ if (!r) return;
+ fsq_item_debug(m,r,"dequeue");
+ fsq_queue_remove_item(fsq_item_queue(m,r), r);
}
/*---------- method entrypoints ----------*/
-static ErrorCode fsq_prepare(Method *mm, Segment *move,
+static ErrorCode fsq_prepare(Method *mm, const Segment *move,
int n_motions, const Motion *motions,
int ms, int confirmation,
- Change *chg_r, int *cost_r) {
+ Change **chg_r, int *cost_r) {
FsqMethod *m= (void*)mm;
assert(n_motions > 0);
- FsqSlotSigned reldeadline= fsq_maxdelay_reldeadline(m, ms, r->n_motions);
+ FsqSlotSigned reldeadline= fsq_maxdelay_reldeadline(m, ms, n_motions);
if (reldeadline <= 0) return EC_MovFeatTooLate;
if (chg_r) {
r->n_motions= n_motions;
if (confirmation) {
r->deadline= reldeadline + m->f.cslot;
- memcpy(r->motions, motions, sizeof(*r->motions)*motions);
+ memcpy(r->motions, motions, sizeof(*r->motions)*n_motions);
} else {
r->deadline= reldeadline;
r->motions[0].i= 0;
*chg_r= &r->h;
}
if (cost_r) {
- *cost_r= n_motions * m->slot_ms;
+ *cost_r= n_motions * m->f.slot_ms;
}
return 0;
}
-static ErrorCode fsq_consider(Method *mm, Change *remove, Change *install) {
- FsqMethod *m= (void*)mm;
- FsqReq *remv= (FsqReq*)remove;
- FsqReq *inst= (FsqReq*)install;
+static void fsq_dispose(Method *mm, Change *chg) {
+ FsqReq *r= (FsqReq*)chg;
+ free(r);
+}
- DPRINTF1(movpos,fsq, "%s consider ", m->m.pname);
+static void fsq_remove(Method *mm, Change *remvchg) {
+ FsqMethod *m= (void*)mm;
+ FsqReq *remv= (FsqReq*)remvchg;
fsq_dequeue(m, remv);
- ErrorCode ec= fsq_enqueue(m, inst);
- if (ec) {
- ErrorCode ec2= fsq_enqueue(m, remv);
- assert(!ec2);
- }
- DPRINTF1(movpos,fsq, " %s\n", ec2str(ec));
- return ec;
+}
+
+static ErrorCode fsq_install(Method *mm, Change *instchg) {
+ FsqMethod *m= (void*)mm;
+ FsqReq *inst= (FsqReq*)instchg;
+
+ return fsq_enqueue(m, inst);
}
static ErrorCode fsq_check(Method *mm) {
FsqMethod *m= (void*)mm;
ErrorCode ec= fsq_check_plan(m);
- DPRINTF(movpos,fsq, "%s check=%s\n", m->m.pname, ec2str(ec));
return ec;
}
static void fsq_execute(Method *mm) {
FsqMethod *m= (void*)mm;
- DPRINTF(movpos,fsq, "%s execute\n", m->m.pname);
fsq_check_action(m);
}
-static void fsq_dispose(Method *mm, Change *chg) {
- FsqMethod *m= (void*)mm;
- FsqReq *r= (FsqReq*)chg;
- free(r);
-}
-
/*---------- something to do ? ----------*/
static void fsq_check_action(FsqMethod *m) {
if (!r->n_motions) {
fsq_queue_remove_index(&m->f.confirmed, 0);
- indep_change_done(&m->m, &r->h);
- m->h.dispose(&m->m, &r->h);
+ method_change_done(&m->m, &r->h);
+ m->m.dispose(&m->m, &r->h);
ec= fsq_check_plan(m); assert(!ec);
fsq_check_action(m);
#define CDU_RECHARGE 350 /*ms*/
#define POINT_MOVEMENT 50 /*ms*/
-static ErrorCode point_prepare(Method *mm, Segment *move,
+static ErrorCode point_prepare(Method *mm, const Segment *move,
int n_motions, const Motion *motions,
int ms, int confirmation,
- Change *chg_r, int *cost_r) {
+ Change **chg_r, int *cost_r) {
FsqMethod *m= (void*)mm;
assert(m->f.ready>=0);
return fsq_prepare(mm,move, n_motions,motions,
}
static FsqMethod points_method= {
- { "point", mfk_point,
- point_prepare, fsq_consider, fsq_dispose,
+ { "point",
+ point_prepare, fsq_dispose,
+ fsq_install, fsq_remove,
fsq_check, fsq_execute, points_all_abandon },
{ .lag_ms= POINT_MOVEMENT, .slot_ms= CDU_RECHARGE, .move= point_move }
};
waggle.f.ready= 1;
waggle_settle_check();
}
-
+
void on_pic_waggled(const PicInsnInfo *pii, const PicInsn *pi, int objnum) {
if (sta_state == Sta_Settling) {
waggle.f.ready= 1;
}
static FsqMethod waggle= {
- { "relay", mfk_relay,
- fsq_prepare, fsq_consider, fsq_dispose,
+ { "relay",
+ fsq_prepare, fsq_dispose,
+ fsq_install, fsq_remove,
fsq_check, fsq_execute, ignore_all_abandon },
{ .lag_ms= 5, .slot_ms= 50, .move= waggle_do }
};
/*========== dummy `nomove' kind ==========*/
-static ErrorCode nomove_prepare(Method *m, Segment *move,
+typedef struct NomoveChange {
+ Change h;
+ DLIST_NODE(struct NomoveChange) inqueue;
+ int n_motions; /* 0 for reservations */
+ Motion motions[];
+} NomoveChange;
+
+typedef struct {
+ Method m;
+ NomoveChange *queuehead; /* contains confirmations only */
+} NomoveMethod;
+
+static ErrorCode nomove_prepare(Method *meth_in, const Segment *move,
int n_motions, const Motion *motions,
int ms, int confirming,
- Change *chg_r, int *cost_r) {
+ Change **chg_r, int *cost_r) {
if (chg_r) {
- *chg= mmalloc(sizeof(Change));
- fixme
+ NomoveChange *chg;
+ assert(n_motions>0);
+ if (!confirming) n_motions= 0;
+ chg= mmalloc(sizeof(*chg) + sizeof(Motion)*n_motions);
+ chg->n_motions= n_motions;
+ memcpy(chg->motions, motions, sizeof(Motion)*n_motions);
}
if (cost_r) {
*cost_r= 0;
}
return 0;
}
+static void nomove_dispose(Method *mm, Change *remvchg) {
+ NomoveChange *remv= (void*)remvchg;
+ free(remv);
+}
+
+static ErrorCode nomove_install(Method *mm, Change *instchg) {
+ NomoveMethod *meth= (void*)mm;
+ NomoveChange *inst= (void*)instchg;
+ if (inst->n_motions)
+ DLIST1_PREPEND(meth->queuehead, inst, inqueue);
+ return 0;
+}
+static void nomove_remove(Method *mm, Change *remvchg) {
+ NomoveMethod *meth= (void*)mm;
+ NomoveChange *remv= (void*)remvchg;
+ if (remv->n_motions)
+ DLIST1_REMOVE(meth->queuehead, remv, inqueue);
+}
+
+static ErrorCode nomove_check(Method *mm) { return 0; }
-static ErrorCode nomove_consider(Method *m, Change *remove, Change *install)
- { return 0; }
+static void nomove_execute(Method *mm) {
+ NomoveMethod *meth= (void*)mm;
+ NomoveChange *done;
-static void nomove_dispose(Method *m, Change *chg) { }
-static ErrorCode nomove_check(Method *m) { return 0; }
-static ErrorCode nomove_execute(Method *m) {
- fixme
+ while ((done= meth->queuehead)) {
+ assert(done->n_motions);
+ int i;
+ for (i=0; i<done->n_motions; i++)
+ method_update_feature(&meth->m, done->h.indep, &done->motions[i]);
+ method_change_done(&meth->m, &done->h);
+ DLIST1_REMOVE(meth->queuehead, done, inqueue);
+ nomove_dispose(&meth->m, &done->h);
+ }
}
-
+
static Method nomove_method= {
- "nomove", mfk_none,
- nomove_prepare, nomove_consider, nomove_dispose,
+ "nomove",
+ nomove_prepare, nomove_dispose,
+ nomove_install, nomove_remove,
nomove_check, nomove_execute, ignore_all_abandon
};
/*========== method-independent machinery ==========*/
-struct MovPosChange {
- Segment *move;
- MovPosComb actual, target;
- int n_changes;
- Change *changes[];
-};
+#define INDEP_DBG_FMT "<%p:%s/%s[%d]>"
+#define INDEP_DBG_ARGS(in) (in), \
+ ((in)->move->i->pname), (movpos_pname((in)->move, (in)->target)), \
+ ((in)->n_changes)
+
+#define INDEP_DPFX_FMT "movpos " INDEP_DBG_FMT " "
+#define INDEP_DPFX_ARGS(in) INDEP_DBG_ARGS((in))
+#define METH_DPFX_FMT "%s " INDEP_DBG_FMT " "
+#define METH_DPFX_ARGS(indep, meth) ((meth).pname), INDEP_DBG_ARGS((indep))
+
+static void indep_indep_done(Indep *indep);
static Method *methods[]= {
- &nomove_method,
- (Method*)&points_method,
- (Method*)&waggle,
+ [mfk_none] = (Method*)&nomove_method,
+ [mfk_point] = (Method*)&points_method,
+ [mfk_relay] = (Method*)&waggle,
0
};
/*---------- entrypoints from methods ----------*/
static void method_update_feature(Method *m, MovPosChange *indep,
- const Motion *mo) {
- /* Called from methods' execution logic when an individual feature
- * has been moved. This is used by the method-independent code to
- * compute the correct delta set of movements from the current
- * actual position, when thinking about new plans. It is also sent
- * to clients and ultimately used by the UI.
- */
- ouprintf("movpos %s feat %s %d %s\n", indep->move->i->pname,
- mo->i->pname, mo->posn, m.pname);
+ const Motion *mo) {
+ ouposn_feat(indep->move, mo->i, mo->posn, m);
if (SOMEP(indep->actual))
indep->actual=
movposcomb_update_feature(indep->actual, mo->i, mo->posn);
+ ouposn_moving(indep);
}
static void method_change_done(Method *m, Change *chg) {
- /* Called from methods' execution logic when a whole change
- * has been done. The method-independent code will take care of
- * updating move->movposcomb. etc.
- *
- * REENTRANCY: must NOT be called from within a call to the method's
- * execute() (and of course cannot legally be called from within
- * prepare, consider, check or dispose).
- */
- MovPosComb *indep= chg->indep;
+ Indep *indep= chg->indep;
Change **search;
+ DPRINTF(movpos,meth, METH_DPFX_FMT "method_change_done...\n",
+ METH_DPFX_ARGS(indep,*m));
+
for (search=indep->changes; *search; search++)
if ((*search) == chg) goto found;
assert(!"change in indep");
found:
indep->n_changes--;
if (indep->n_changes) {
- *search= indep[n_changes];
+ *search= indep->changes[indep->n_changes];
return;
}
+ if (!indep->refcount)
+ indep_indep_done(indep);
+}
+
+static void indep_indep_done(Indep *indep) {
/* all done */
+ Segment *move= indep->move;
move->moving= 0;
move->motion= 0;
move->movposcomb= indep->target;
- ouprintf("movpos %s position %s stable\n",
- move->i->pname, movpos_pname(move, move->movposcomb));
+ ouposn_stable(move);
free(indep);
}
/*---------- internal core machinery ----------*/
+static Method *feature_method(const MovFeatInfo *feati) {
+ assert(feati->kind >= 0);
+ assert(feati->kind < ARRAY_SIZE(methods));
+ Method *meth= methods[feati->kind];
+ assert(meth);
+ return meth;
+}
+
static int change_needed(const MovFeatInfo *feati,
MovPosComb startpoint, MovPosComb target) {
int r;
r= !SOMEP(startpoint) ||
- (target - startpoint) / feati->weight % feati->posns;
- DPRINTF(movpos,changeneeded, "%s:%s(%d*%d) %d..%d => %d\n",
- methods[feati->kind]->pname, feati->pname,
- feati->posns, feati->weight,
- startpoint, target, r);
+ (target / feati->weight) % feati->posns -
+ (startpoint / feati->weight) % feati->posns;
+ if (DEBUGP(movpos,eval))
+ DPRINTFA(" { %s:%s(%d*%d) %d..%d => %d }",
+ feature_method(feati)->pname, feati->pname,
+ feati->posns, feati->weight,
+ startpoint, target, r);
return r;
-}
+}
+
+static void indep_dispose(MovPosChange *indep) {
+ if (!indep) return;
+
+ DPRINTF(movpos,intern, INDEP_DPFX_FMT "dispose...\n",
+ INDEP_DPFX_ARGS(indep));
+
+ int changei;
+ for (changei=0; changei<indep->n_changes; changei++) {
+ Change *chg= indep->changes[changei];
+ if (!chg) continue;
+
+ Method *meth= chg->meth;
+ DPRINTF(movpos,meth, METH_DPFX_FMT "dispose...\n",
+ METH_DPFX_ARGS(indep,*meth));
+ meth->dispose(meth, chg);
+ }
+ free(indep);
+}
#define EVAL_MAX_METHODS 2
#define EVAL_MAX_MOTIONS 2
static ErrorCode indep_prepare(Segment *move, MovPosComb target,
MovPosComb startpoint,
int ms, int confirming,
- MovPosChange *indep_r /* 0 ok */,
+ MovPosChange **indep_r /* 0 ok */,
int *cost_r /* 0 ok */) {
static int n_meths;
static Method *meths[EVAL_MAX_METHODS];
static int n_motions[EVAL_MAX_METHODS];
- static Motion meths[EVAL_MAX_METHODS][EVAL_MAX_MOTIONS];
+ static Motion motions[EVAL_MAX_METHODS][EVAL_MAX_MOTIONS];
const SegmentInfo *movei= move->i;
- int feat;
- const MovFeatInfo *feati;
+ int feat, DP;
MovPosChange *indep=0;
- DPRINTF(movpos,eval, "%s/%s <-%s", move->i->pname,
- movpos_pname(move,target), movpos_pname(move,startpoint));
+ DPRINTF1(movpos,eval, "movpos prepare %s/%s <-%s", move->i->pname,
+ movpos_pname(move,target), movpos_pname(move,startpoint));
if (!SOMEP(startpoint)) {
startpoint= movpos_poscomb_actual(move);
- DPRINTF(movpos,eval, " actual <-%s\n",
- movpos_pname(move,startpoint));
+ DPRINTF2(" actual <-%s", movpos_pname(move,startpoint));
}
n_meths= 0;
- for (feat=0, feati=movei->movfeats, tchanges=0, kind= mfk_none;
- feat<movei->n_movfeats;
- feat++, feati++) {
+ for (feat=0; feat<movei->n_movfeats; feat++) {
+ const MovFeatInfo *feati= &movei->movfeats[feat];
if (!change_needed(feati,startpoint,target)) continue;
MovPosComb posn= target / feati->weight % feati->posns;
- Method *meth= methods[kind];
+ Method *meth= feature_method(feati);
int methi;
for (methi=0; methi<n_meths; methi++)
if (meths[methi] == meth) goto found_method;
/* need a new method */
- methi= ++n_meths;
- if (methi >= EVAL_MAX_METHODS) return EC_MovFeatTooManyMethods;
+ methi= n_meths++;
+ if (methi >= EVAL_MAX_METHODS) {
+ DPRINTF2(" MovFeatTooManyMethods methi=%d\n",methi);
+ return EC_MovFeatTooManyMethods;
+ }
meths[methi]= meth;
n_motions[methi]= 0;
- DPRINTF(movpos,eval, " meths[%d]=%s", methi,meth->pname);
+ DPRINTF2(" meths[%d]=%s", methi,meth->pname);
- found_method:
- int motioni= ++n_motions[methi];
- if (motioni >= EVAL_MAX_MOTIONS) return EC_MovFeatTooManyMotions;
- DPRINTF(movpos,eval, " motion[%d][%d]=%s%d",
- methi,motioni,feati->pname,posn);
+ found_method:;
+ int motioni= n_motions[methi]++;
+ if (motioni >= EVAL_MAX_MOTIONS) {
+ DPRINTF2(" MovFeatTooManyMotions motioni=%d\n",motioni);
+ return EC_MovFeatTooManyMotions;
+ }
+ DPRINTF2(" motion[%d][%d]=%s%d", methi, motioni, feati->pname,posn);
motions[methi][motioni].i= feati;
motions[methi][motioni].posn= posn;
}
if (indep_r) {
- DPRINTF(movpos,eval, " alloc");
+ DPRINTF2(" alloc");
indep= mmalloc(sizeof(*indep) + sizeof(Change*) * n_meths);
- memset(indep->changes, 0, sizeof(Change*) * n_meths);
indep->move= move;
- indep->start= startpoint;
+ indep->actual= startpoint;
indep->target= target;
+ indep->n_changes= n_meths;
+ indep->refcount= 0;
+ memset(indep->changes, 0, sizeof(Change*) * n_meths);
}
- DPRINTF(movpos,eval, "\n");
+ DPRINTF2("\n");
int totalcost= 0;
+ int changei;
+ ErrorCode ec;
- for (int changei=0; changei<n_meths; changei++) {
+ for (changei=0; changei<n_meths; changei++) {
Method *meth= meths[changei];
int thiscost= 0;
+ meth->needcheck= 1;
+ meth->needexec= 1;
+
+ if (indep_r)
+ DPRINTF(movpos,meth, METH_DPFX_FMT "prepare n_motions=%d...\n",
+ METH_DPFX_ARGS(indep,*meth), n_motions[changei]);
+ else
+ DPRINTF(movpos,meth, "%s prepare (costing) n_motions=%d...\n",
+ meth->pname, n_motions[changei]);
+
ec= meth->prepare(meth,move,
n_motions[changei],motions[changei],
ms, confirming,
indep ? &indep->changes[changei] : 0,
- thiscost);
+ &thiscost);
if (ec) goto x;
if (indep) {
Change *chg= indep->changes[changei];
chg->meth= meth;
chg->indep= indep;
+ chg->installed= 0;
}
totalcost += thiscost;
}
if (indep_r) *indep_r= indep;
if (cost_r) *cost_r= totalcost;
- DPRINTF(movpos,eval, "%s/%s ok cost=%d\n", move->i->pname,
- movpos_pname(move,target), totalcost);
+
+ if (indep_r)
+ DPRINTF(movpos,eval, INDEP_DPFX_FMT "prepare cost=%d ok\n",
+ INDEP_DPFX_ARGS(indep), totalcost);
+ else
+ DPRINTF(movpos,eval, "movpos prepare %s/%s cost=%d ok\n",
+ move->i->pname, movpos_pname(move,target), totalcost);
return 0;
x:
- DPRINTF(movpos,eval, "%s/%s abandon %s\n", move->i->pname,
- movpos_pname(move,target), totalcost, ec2str(ec));
indep_dispose(indep);
+ DPRINTF(movpos,entry, "movpos prepare %s/%s err=%s\n", move->i->pname,
+ movpos_pname(move,target), ec2str(ec));
return ec;
}
-static ErrorCode
-indep_consider(MovPosChange *remv, MovPosChange *inst) {
- /* does consider and check; on failure, it does consider on remv
- * and check (which is guaranteed to always succeed) */
- assert(inst->n_changes <= 32);
- assert(remv->n_changes <= 32);
- uint32_t inst_done, remv_done;
- int inst2remv[inst->n_changes]; /* valid only if inst_done; -ve => none */
-
- int ii, jj;
+static void indep_remove(MovPosChange *remv) {
+ if (!remv) return;
- for (ii=0; ii<inst->n_changes; ii++) {
- Method *meth= inst->changes[ii].meth;
- for (jj=remv->n_changes-1; jj>=0; jj++)
- if (remv->changes[jj].meth == meth) break;
- ec= meth->consider(meth,
- jj>=0 ? remv->changes[jj] : 0,
- inst->changes[ii]);
- if (ec) goto x;
+ DPRINTF(movpos,intern, INDEP_DPFX_FMT "remove...\n",
+ INDEP_DPFX_ARGS(remv));
+
+ int i;
+ for (i=0; i<remv->n_changes; i++) {
+ Change *chg= remv->changes[i];
+ if (!chg->installed) continue;
+ Method *meth= chg->meth;
+ meth->needexec= 1;
+
+ DPRINTF(movpos,meth, METH_DPFX_FMT "remove...\n",
+ METH_DPFX_ARGS(remv,*meth));
+ meth->remove(meth, chg);
+ chg->installed= 0;
+ }
+}
- inst_done |= (1UL << ii);
- inst2remv[ii]= jj;
- if (jj>=0) remv_done |= (1UL << jj);
+static ErrorCode
+indep_install(MovPosChange *inst, int checknow) {
+ /* if this fails, inst may be left partially installed */
+ if (!inst) return 0;
+
+ DPRINTF(movpos,intern, INDEP_DPFX_FMT "install checknow=%d...\n",
+ INDEP_DPFX_ARGS(inst), checknow);
- ec= meth->check(meth);
+ int i;
+ ErrorCode ec;
+ for (i=0; i<inst->n_changes; i++) {
+ Change *chg= inst->changes[i];
+ assert(!chg->installed);
+ Method *meth= chg->meth;
+
+ meth->needexec= 1;
+ ec= meth->install(meth, chg);
+ DPRINTF(movpos,meth, METH_DPFX_FMT "install=%s\n",
+ METH_DPFX_ARGS(inst,*meth), ec2str(ec));
if (ec) goto x;
- }
- for (jj=0; jj<remv->n_changes; jj++) {
- if (remv_done & (1UL << jj)) continue;
- ec= meth->consider(meth, remv->changes[jj], 0);
- assert(!ec);
- ec= meth->check(meth);
- assert(!ec);
- /* remv_done |= (1UL << jj);
- not needed because we never roll back from here */
+ chg->installed= 1;
+ meth->needcheck= 1;
+
+ if (checknow) {
+ ec= meth->check(meth);
+ DPRINTF(movpos,meth, METH_DPFX_FMT "check=%s\n",
+ METH_DPFX_ARGS(inst,*meth), ec2str(ec));
+ if (ec) goto x;
+ meth->needcheck= 0;
+ }
}
return 0;
x:
- for (ii=0; ii<inst->n_changes; ii++) {
- if (!(inst_done & (1UL << ii))) continue;
- Method *meth= inst->changes[ii].meth;
- jj= inst2remv[ii];
- ec= meth->consider(meth,
- inst->changes[ii],
- jj>=0 ? remv->changes[jj] : 0);
- assert(!ec);
- ec= meth->check(meth);
- assert(!ec);
- }
- /* we do not need to unwind methods for which we are only removing
- * since we never roll back after starting those */
return ec;
}
-static void indep_execute(void) {
- for (Method **methwalk= methods;
- (meth= *methwalk);
- methwalk++) {
- meth->execute(meth);
- }
-}
-
-static void indep_dispose(MovPosChange *indep) {
- if (!indep) return;
+static void indep_check_execute(void) {
+ DPRINTF(movpos,intern, "movpos indep_check_execute\n");
- for (int changei=0; changei<indep->n_changes; changei++) {
- Change *chg= indep->changes[changei];
- Method *meth= chg->meth;
- if (chg)
- meth->dispose(meth, chg);
+ Method **methwalk, *meth;
+ for (methwalk= methods; (meth= *methwalk); methwalk++) {
+ if (meth->needcheck) {
+ ErrorCode ec= meth->check(meth);
+ DPRINTF(movpos,meth, "%s check=%s\n", meth->pname, ec2str(ec));
+ assert(!ec);
+ meth->needcheck= 0;
+ }
+ if (meth->needexec) {
+ meth->needexec= 0;
+ DPRINTF(movpos,meth, "%s execute...\n", meth->pname);
+ meth->execute(meth);
+ }
}
- free(indep);
}
/*---------- entrypoints from rest of program ----------*/
ErrorCode ec;
MovPosChange *indep= 0;
- DPRINTF(movpos,reserve, "%s/%s maxdelay=%dms startpoint=%s\n",
+ DPRINTF(movpos,entry, "movpos reserve %s/%s maxdelay=%dms startpoint=%s\n",
move->i->pname, movpos_pname(move,target),
maxdelay_ms, movpos_pname(move,startpoint));
ec= indep_prepare(move,target, startpoint,
- ms,0,
- &indep, 0);
+ maxdelay_ms,0,
+ &indep, 0);
if (ec) return ec;
- ec= indep_consider(0, indep);
+ ec= indep_install(indep, 1);
if (ec) goto x;
- indep_execute();
+ indep_check_execute();
+
+ DPRINTF(movpos,entry, "movpos reserve %s/%s ok\n",
+ move->i->pname, movpos_pname(move,target));
+ *res_r= indep;
return 0;
x:
+ indep_remove(indep);
indep_dispose(indep);
- indep_execute();
+ indep_check_execute();
+
+ DPRINTF(movpos,entry, "movpos reserve %s/%s err=%s\n",
+ move->i->pname, movpos_pname(move,target), ec2str(ec));
return ec;
}
int tcost, bestcost=INT_MAX;
const SegPosCombInfo *pci;
+ DPRINTF(movpos,eval, "movpos_findcomb_bysegs %s-%s-%s <-%s\n",
+ back ? back->i->pname : "*", move->i->pname,
+ fwd ? fwd ->i->pname : "*", movpos_pname(move, startpoint));
+
for (tcomb=0, pci=movei->poscombs;
tcomb<movei->n_poscombs;
tcomb++, pci++) {
+ /* these next assignments may generate segments[-1] but we don't
+ * care because that won't compare equal to back or fwd */
Segment *tback= &segments[pci->link[1].next];
Segment *tfwd= &segments[pci->link[0].next];
+
+ DPRINTF(movpos,intern, "movpos_findcomb_bysegs ... %s : %s-%s-%s\n",
+ movpos_pname(move, tcomb),
+ SOMEP(pci->link[1].next) ? tback->i->pname : "#",
+ move->i->pname,
+ SOMEP(pci->link[0].next) ? tfwd->i->pname : "#");
+
if (back && !(back==tback || back==tfwd)) continue;
if (fwd && !(fwd ==tback || fwd ==tfwd)) continue;
/* we have to search for the one which is least effort */
- ec= indep_prepare(move,tcomb,startpoint, -1,0, 0,&tcost);
+ ErrorCode ec= indep_prepare(move,tcomb,startpoint, -1,0, 0,&tcost);
if (ec) return ec;
if (tcost >= bestcost) /* prefer low-numbered movposcombs */
bestcomb= tcomb;
bestcost= tcost;
}
+ DPRINTF(movpos,entry, "movpos_findcomb_bysegs %s..%s..%s <-%s => %s/%s\n",
+ back ? back->i->pname : "-", move->i->pname,
+ fwd ? fwd ->i->pname : "-", movpos_pname(move, startpoint),
+ move->i->pname, movpos_pname(move,bestcomb));
+
if (chosen_r) *chosen_r= bestcomb;
return
bestcost==INT_MAX ? EC_MovFeatRouteNotFound :
ErrorCode movpos_change(Segment *move, MovPosComb target,
int maxdelay_ms, MovPosChange *resv) {
- const SegmentInfo *movei= move->i;
- const MovFeatInfo *feati;
- int feat;
+ int DP;
MovPosComb actual;
ErrorCode ec;
actual= move->motion->actual;
}
- DPRINTF(movpos,change, "%s/%s maxdelay=%dms actual=%s\n",
- move->i->pname, movpos_pname(move,target),
- maxdelay_ms, movpos_pname(move, actual));
- if (resv) DPRINTF(movpos,change, " chg=%s:%s/%s\n",
- chg->meth->pname, chg->move->i->pname,
- movpos_pname(chg->move, chg->intent));
+ DPRINTF1(movpos,entry, "movpos change %s/%s maxdelay=%dms actual=%s",
+ move->i->pname, movpos_pname(move, target),
+ maxdelay_ms, movpos_pname(move, actual));
+ if (resv) DPRINTF2(" resv=%s/%s",
+ resv->move->i->pname,
+ movpos_pname(resv->move, resv->target));
+ if (move->motion) DPRINTF2(" oldmotion=/%s",
+ movpos_pname(move, move->motion->target));
+ DPRINTF2("\n");
- MovPosChange *indep= 0;
+ MovPosChange *inst= 0;
ec= indep_prepare(move,target, actual,
maxdelay_ms,1,
- &indep, 0);
+ &inst, 0);
if (ec) goto x;
- ec= indep_consider(resv, indep);
+ indep_remove(resv);
+ indep_remove(move->motion);;
+
+ ec= indep_install(inst, 1);
if (ec) goto x;
indep_dispose(resv);
- indep_execute();
+ indep_dispose(move->motion);
+
+ move->motion= inst;
+ move->moving= 1;
+
+ inst->refcount++; /* prevents method_change_done from destroying it */
+
+ ouposn_moving(inst);
+ indep_check_execute();
+
+ inst->refcount--;
+ if (!inst->n_changes)
+ /* oh! */
+ indep_indep_done(inst);
+
+ DPRINTF(movpos,entry, "movpos change %s/%s ok\n",
+ move->i->pname, movpos_pname(move, target));
return 0;
x:
- indep_dispose(indep);
- indep_execute();
+ indep_remove(inst);
+ indep_dispose(inst);
+ ec= indep_install(move->motion, 0); assert(!ec);
+ ec= indep_install(resv, 0); assert(!ec);
+ indep_check_execute();
+
+ DPRINTF(movpos,entry, "movpos change %s/%s err=%s\n",
+ move->i->pname, movpos_pname(move, target), ec2str(ec));
return ec;
}
void movpos_unreserve(MovPosChange *resv) {
- if (!res) return;
- DPRINTF(movpos,unreserve, "%s:%s/%s\n",
- res->meth->pname, res->move->i->pname,
- movpos_pname(res->move, res->intent));
- ErrorCode ec= indep_consider(resv, 0);
- assert(!ec);
+ if (!resv) return;
+ DPRINTF(movpos,entry, "movpos unreserve %s/%s...\n",
+ resv->move->i->pname, movpos_pname(resv->move, resv->target));
+ indep_remove(resv);
indep_dispose(resv);
- indep_execute();
+ indep_check_execute();
}
MovPosComb movpos_change_intent(MovPosChange *indep) {
- return indep->intent;
+ return indep->target;
}
void motions_all_abandon(void) {
Method **meth;
+ SEG_IV;
+ DPRINTF(movpos,entry, "movpos motions_all_abandon...\n");
+ FOR_SEG {
+ if (!seg->moving) continue;
+
+ MovPosChange *abandon= seg->motion;
+ indep_remove(abandon);
+ seg->movposcomb= abandon->actual;
+ seg->moving= 0;
+ seg->motion= 0;
+ indep_dispose(abandon);
+ }
for (meth=methods; *meth; meth++)
(*meth)->all_abandon(*meth);
}
-
-
-
-
-
-
-
- STUFF BEYOND HERE IS FUNCTIONALITY WHICH NEEDS MOVING TO INDEP
- AND/OR CHECKING THAT WE STILL HAVE IT
-
-
-static ErrorCode fsq_confirm(Method *mm, Change *chg, Segment *move,
- int n_motions, const Motion *motions,
- int maxdelay_ms) {
- FsqMethod *m= (void*)mm;
- FsqReq *r= (FsqReq*)chg;
- FsqSlotSigned reldeadline;
- int allow_failure, DP;
- ErrorCode ec;
-
- DPRINTF1(movpos,fsq, "%s confirm %s n=%d maxdelay=%dms",
- m->m.pname, move->i->pname, n_motions, maxdelay_ms);
-
- assert(!r->motions[0].i); /* no confirming things already confirmed */
- if (r->deadline==FSQDN)
- DPRINTF2(" (alloc'd: %d)\n", r->n_motions);
- else
- DPRINTF2(" (res: %s/%s[%d@t+%d])\n",
- r->h.move->i->pname, movpos_pname(r->h.move, r->h.intent),
- r->n_motions, r->deadline);
-
- /* If the segment is moving, these motions are already based on the
- * actual physical position which is stored in the existing request.
- * So we try removing the existing request from the queue and put
- * it back if it doesn't work.
- */
-
- if (n_motions > r->n_motions)
- return EC_MovFeatReservationInapplicable;
- assert(n_motions <= r->n_motions);
- if (maxdelay_ms == -1) {
- reldeadline= r->deadline;
- if (reldeadline==FSQDN)
- reldeadline= fsq_maxdelay_reldeadline(m, -1, n_motions);
- } else {
- reldeadline= fsq_maxdelay_reldeadline(m, maxdelay_ms, n_motions);
- }
- allow_failure= reldeadline < (FsqSlotSigned)r->deadline;
- DPRINTF(movpos,fsq, "%s reldeadline=[%d@t+%d] allow_failure=%d\n",
- m->m.pname, n_motions, reldeadline, allow_failure);
-
- /* state A or R */
- fsq_dequeue(m, r);
- /* states of existing: */
- FsqReq *existing=
- move->moving ? (FsqReq*)move->motion : 0; /* U or C */
- if (existing) {
- DPRINTF(movpos,fsq,
- "%s existing %s n=%d deadline=t+%d\n",
- m->m.pname,
- existing->h.move->i->pname,
- existing->n_motions,
- existing->deadline - m->f.cslot);
- fsq_dequeue(m, existing); /* U or CA */
- }
-
- /* state A or RA */
- memcpy(r->motions, motions, sizeof(r->motions[0])*n_motions);
- if (!n_motions) r->motions[0].i= move->i->movfeats;
- assert(r->motions[0].i);
- r->n_motions= n_motions;
- r->deadline= reldeadline + m->f.cslot;
-
- if (n_motions == move->i->n_movfeats)
- r->actual= 0;
- else
- r->actual= chg->actual;
- assert(r->actual >= 0);
-
- /* state CA */
- ec= fsq_enqueue(m, &m->f.confirmed, r);
- DPRINTF(movpos,fsq, "%s fsq_enqueue=%s\n", m->m.pname, ec2str(ec));
- assert(allow_failure || !ec);
-
- if (existing) { /* CA */
- if (ec) { /* state C but bad */
- fsq_dequeue(m,r); /* state CA */
- fsq_mark_as_allocated(r); /* state A */
- ErrorCode ec_putback= fsq_enqueue(m,&m->f.confirmed, existing);
- assert(!ec_putback); /* C */
- } else { /* state C and good */
- free(existing); /* U */
- }
- }
- /* either ec=0 state C U
- * or ec!=0 state A C
- * or ec!=0 state C but bad C
- */
-
- if (ec) return ec;
-
- move->moving= 1;
- move->motion= chg;
- move->movposcomb= -1;
- ouposn_moving(chg);
- fsq_check_action(m);
- return 0;
-}
-
-
-
- r->actual= movposcomb_update_feature(r->actual,mo->i,mo->posn);
- if (r->h.actual >= 0 || !r->n_motions)
- r->h.actual= r->actual;
- ouposn_moving(&r->h);
-
-
-
-
- /* look for something to report
- * we can get things here other than from the above
- * eg if we are asked to move the
- */
- Segment *move= r->h.move;
- assert(move->moving && move->motion == (Change*)r);
- fsq_mark_as_allocated(r); /* now state A aka Done */
- motion_done(move,r->h.actual);
- free(r);
-
-/*---------- entrypoints from rest of program ----------*/
-
-static void fsq_all_abandon(Method *mm) {
- FsqMethod *m= (void*)mm;
- int i;
-
- assert(!m->f.reserved.n);
-
- for (i=0; i<m->f.confirmed.n; i++) {
- indep_
- FsqReq *r= m->f.confirmed.l[i];
- Segment *move= r->h.move;
- assert(move->motion == (Change*)r);
- motion_done(move,r->h.actual);
- free(r);
- }
- m->f.confirmed.n= 0;
-}
-
-in points_all_abandon
- fsq_all_abandon(mm);
-
-
- *
-
- * seg->moving and ->motion is in one of the states UC
-
-static Change *mp_allocate(Method *meth, Segment *move,
- int alloc_motions, MovPosComb target) {
- assert(sta_state >= Sta_Resolving || sta_state == Sta_Manual);
- Change *chg= meth->allocate(meth, alloc_motions);
- chg->meth= meth;
- chg->move= move;
- chg->intent= target;
- return chg;
-}
-