summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Sched/SchedEntry.cpp
blob: 86eb472f6258509b18f5c6fce6d2bc3742213082 (plain)
1
2
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
// $Id$

// ============================================================================
//
// = LIBRARY
//    sched
//
// = FILENAME
//    SchedEntry.cpp
//
// = CREATION DATE
//    7 February 1998
//
// = AUTHOR
//    Chris Gill
//
// ============================================================================

#include "SchedEntry.h"

#if ! defined (__ACE_INLINE__)
#include "SchedEntry.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID(Sched, SchedEntry, "$Id$")

Task_Entry::Task_Entry (void)
  : rt_info_ (0),
    effective_period_(0),
    dfs_status_ (NOT_VISITED),
    discovered_ (-1),
    finished_ (-1),
    is_thread_delineator_ (0),
    has_unresolved_remote_dependencies_ (0),
    has_unresolved_local_dependencies_ (0),
    calls_ (),
    callers_ ()
{
}

Task_Entry::~Task_Entry (void)
{
  // Zero out the task entry ACT in the corresponding rt_info
  rt_info_->volatile_token = 0;

  ACE_Unbounded_Set_Iterator <Task_Entry_Link *> iter(calls_);
  Task_Entry_Link **link = 0;

  // Iterate through the "calls" set of Task Entry Links and free each one

  for (iter.first ();
       ! iter.done ();
       iter.advance (), link = 0)
    {
      if (iter.next (link) != 0 && link != 0 && *link != 0)
        {
          // remove the link object pointer from the calling entry's
          // "callers" set and destroy the link object
          (*link)->called ().callers_.remove (*link);
          delete (*link);
        }
    }
}

// Merge dispatches according to info type and type of call, update
// relevant scheduling characteristics for this entry.

Task_Entry::Propagation_Status
Task_Entry::merge_dispatches (ACE_Unbounded_Set <Dispatch_Entry *> &dispatch_entries,
                              ACE_CString &unresolved_locals,
                              ACE_CString &unresolved_remotes)
{
  Task_Entry::Propagation_Status result = SUCCEEDED;
  switch (info_type ())
    {
    case RtecScheduler::DISJUNCTION:

      // Prohibit two-way dispatches of a disjunction group, and
      // disjunctively merge its one-way dispatches.  NOTE: one
      // interpretation of disjunction for two-way calls is that the
      // caller calls one OR the other, but this is problematic: how
      // do we map the dispatches for this ?
      if (prohibit_dispatches (RtecScheduler::TWO_WAY_CALL) < 0)
        result = TWO_WAY_DISJUNCTION;

      if (disjunctive_merge (RtecScheduler::ONE_WAY_CALL,
                             dispatch_entries,
                             unresolved_locals,
                             unresolved_remotes) < 0)
        result = INTERNAL_ERROR;
      break;

    case RtecScheduler::CONJUNCTION:

      // Prohibit two-way dispatches of a conjunction group,
      // and conjunctively merge its one-way dispatches.
      // NOTE: one interpretation of disjunction for two-way calls
      //       is that the caller calls BOTH, so that there is a
      //       disjunctive merge of each two-way, as for the OPERATION
      //       (prohibit for now, as the additional complexity of allowing
      //       conjunctions of two-ways, but not disjunctions does not
      //       buy us anything, anyway).
      if (prohibit_dispatches (RtecScheduler::TWO_WAY_CALL) < 0)
        result = TWO_WAY_CONJUNCTION;
      if (conjunctive_merge (RtecScheduler::ONE_WAY_CALL,
                             dispatch_entries,
                             unresolved_locals,
                             unresolved_remotes) < 0)
        result = INTERNAL_ERROR;
      break;

    case RtecScheduler::OPERATION:
    case RtecScheduler::REMOTE_DEPENDANT:

      // Disjunctively merge the operation's two-way dispatches, and
      // conjunctively merge its one-way dispatches.
      if (disjunctive_merge (RtecScheduler::TWO_WAY_CALL,
                             dispatch_entries,
                             unresolved_locals,
                             unresolved_remotes) < 0)
        result = INTERNAL_ERROR;
      if (conjunctive_merge (RtecScheduler::ONE_WAY_CALL,
                             dispatch_entries,
                             unresolved_locals,
                             unresolved_remotes) < 0)
        result = INTERNAL_ERROR;
      break;

    default:

      // There should not be any other kind of RT_Info, or if there
      // is, the above switch logic is in need of repair.
      result = UNRECOGNIZED_INFO_TYPE;
      break;
    }

  return result;
}

// Prohibit calls of the given type: currently used to enforce the
// notion that two-way calls to disjunctive or conjunctive RT_Infos do
// not have any defined meaning, and thus should be considered
// dependency specification errors: if these constraints are removed
// in the future, this method should be removed as well Returns 0 if
// all is well, or -1 if an error has occurred.

int
Task_Entry::prohibit_dispatches (Dependency_Type dt)
{
  // Iterate over the set of dependencies, ensuring none of them has
  // the given dependency type.
  for (ACE_Unbounded_Set_Iterator <Task_Entry_Link *> iter (callers_);
       ! iter.done ();
      iter.advance ())
    {
      Task_Entry_Link **link;

      if (iter.next (link) == 0
          || link == 0
          || *link == 0
          || (*link)->dependency_type () == dt)
        return -1;
    }

  return 0;
}

// Perform disjunctive merge of arrival times of oneway calls: all
// arrival times of all dependencies are duplicated by the multiplier
// and repetition over the new frame size.

int
Task_Entry::disjunctive_merge (Dependency_Type dt,
                               ACE_Unbounded_Set <Dispatch_Entry *> &dispatch_entries,
                               ACE_CString &unresolved_locals,
                               ACE_CString &unresolved_remotes)
{
  char string_buffer[BUFSIZ];

  // Iterate over the set of dependencies, merging dispatches of the
  // callers over the enclosing frame size.
  for (ACE_Unbounded_Set_Iterator <Task_Entry_Link *> iter (callers_);
       ! iter.done ();
       iter.advance ())
    {
      Task_Entry_Link **link;

      if (iter.next (link) == 0
          || link == 0
          || *link == 0)
        return -1;

      // The link matches the dependency type given
      if ((*link)->dependency_type () == dt)
        {
          // Check for and warn about unresolved remote dependencies
          // in the ONE_WAY call graph.
          if ((*link)->dependency_type () == RtecScheduler::ONE_WAY_CALL
              && (*link)->caller ().has_unresolved_remote_dependencies ()
              && ! this->has_unresolved_remote_dependencies ())
            {
              // Propagate the unresolved remote dependency flag, and
              // issue a debug scheduler warning.
              this->has_unresolved_remote_dependencies (1);
              ACE_DEBUG ((LM_DEBUG,
                          "Warning: an operation identified by "
                          "\"%s\" has unresolved remote dependencies.\n",
                          (const char*) this->rt_info ()->entry_point));

              // Record entry point in list of unresolved remote
              // dependencies
              ACE_OS::sprintf (string_buffer,
                               "// %s\n",
                               (const char*) this->rt_info ()->entry_point);
              unresolved_remotes +=
                ACE_CString (string_buffer);

            }

          // Check for and warn about unresolved local dependencies in
          // the ONE_WAY call graph.
          if ((*link)->dependency_type () == RtecScheduler::ONE_WAY_CALL
              && (*link)->caller ().has_unresolved_local_dependencies ()
              && ! this->has_unresolved_local_dependencies ())
            {
              // Propagate the unresolved local dependency flag, and
              // issue a debug scheduler warning.
              this->has_unresolved_local_dependencies (1);
              ACE_DEBUG ((LM_DEBUG,
                          "Warning: an operation identified by "
                          "\"%s\" has unresolved local dependencies.\n",
                          (const char*) this->rt_info ()->entry_point));

              // Record entry point in list of unresolved local
              // dependencies
              ACE_OS::sprintf (string_buffer,
                               "// %s\n",
                               (const char*) this->rt_info ()->entry_point);
              unresolved_locals +=
                ACE_CString (string_buffer);
            }

          // Merge the caller's dispatches into the current set.
          if (merge_frames (dispatch_entries,
                            *this,
                            dispatches_,
                            (*link)->caller ().dispatches_, effective_period_,
                            (*link)->caller ().effective_period_,
                            (*link)->number_of_calls ()) < 0)
            return -1;
        }
    }

  return 0;
}

// Perform conjunctive merge of arrival times of calls: all arrival
// times of all dependencies are duplicated by the multiplier and
// repetition over the new frame size and then iteratively merged by
// choosing the maximal arrival time at the current position in each
// queue (iteration is in lockstep over all queues, and ends when any
// queue ends).

int
Task_Entry::conjunctive_merge (Dependency_Type dt,
                               ACE_Unbounded_Set <Dispatch_Entry *> &dispatch_entries,
                               ACE_CString &unresolved_locals,
                               ACE_CString &unresolved_remotes)
{
  int result = 0;
  char string_buffer [BUFSIZ];

  // Iterate over the dependencies, and determine the total frame
  // size.

  u_long frame_size = 1;

  ACE_Unbounded_Set_Iterator <Task_Entry_Link *> dep_iter (callers_);

  for (dep_iter.first ();
       dep_iter.done () == 0;
       dep_iter.advance ())
    {
      Task_Entry_Link **link;

      if (dep_iter.next (link) == 0
          || link == 0
          || *link == 0)
        return -1;

      // The link matches the dependency type given.
      if ((*link)->dependency_type () == dt)
        {
          // Check for and warn about unresolved remote dependencies
          // in the ONE_WAY call graph.
          if ((*link)->dependency_type () == RtecScheduler::ONE_WAY_CALL
              && (*link)->caller ().has_unresolved_remote_dependencies ()
              && ! this->has_unresolved_remote_dependencies ())
            {
              // Propagate the unresolved remote dependency flag, and
              // issue a debug scheduler warning.
              this->has_unresolved_remote_dependencies (1);
              ACE_DEBUG ((LM_DEBUG,
                          "Warning: an operation identified by "
                          "\"%s\" has unresolved remote dependencies.\n",
                          (const char*) this->rt_info ()->entry_point));

              // Record entry point in list of unresolved remote
              // dependencies
              ACE_OS::sprintf (string_buffer,
                               "// %s\n",
                               (const char*) this->rt_info ()->entry_point);
              unresolved_remotes +=
                ACE_CString (string_buffer);
            }

          // Check for and warn about unresolved local dependencies in
          // the ONE_WAY call graph.
          if ((*link)->dependency_type () == RtecScheduler::ONE_WAY_CALL
              && (*link)->caller ().has_unresolved_local_dependencies ()
              && ! this->has_unresolved_local_dependencies ())
            {
              // Propagate the unresolved local dependency flag, and
              // issue a debug scheduler warning.
              this->has_unresolved_local_dependencies (1);
              ACE_DEBUG ((LM_DEBUG,
                          "Warning: an operation identified by "
                          "\"%s\" has unresolved local dependencies.\n",
                          (const char*) this->rt_info ()->entry_point));

              // Record entry point in list of unresolved local dependencies
              ACE_OS::sprintf (string_buffer,
                               "// %s\n",
                               (const char*) this->rt_info ()->entry_point);
              unresolved_locals +=
                ACE_CString (string_buffer);
            }

          frame_size = ACE::minimum_frame_size (frame_size,
                                                (*link)->caller ().effective_period_);
        }
    }

  // Reframe dispatches in the set to the new frame size (expands the
  // set's effective period to be the new enclosing frame).
  if (reframe (dispatch_entries,
               *this, dispatches_,
               effective_period_,
               frame_size) < 0)
    return -1;

  // A container and iterator for virtual dispatch sets over which the
  // conjunction will operate
  ACE_Ordered_MultiSet <Dispatch_Proxy_Iterator *> conj_set;
  ACE_Ordered_MultiSet_Iterator <Dispatch_Proxy_Iterator *> conj_set_iter (conj_set);

  // Iterate over the dependencies, and for each of the given call
  // type, create a Dispatch_Proxy_Iterator for the caller's dispatch
  // set, using the caller's period, the total frame size, and the
  // number of calls: if any of the sets is empty, just return 0;
  for (dep_iter.first ();
       dep_iter.done () == 0;
       dep_iter.advance ())
    {
      Task_Entry_Link **link;
      if (dep_iter.next (link) == 0
          || link == 0
          || *link == 0)
        return -1;

      // The link matches the dependency type given.
      if ((*link)->dependency_type () == dt)
        {
          Dispatch_Proxy_Iterator *proxy_ptr;
          ACE_NEW_RETURN (proxy_ptr,
                          Dispatch_Proxy_Iterator ((*link)->caller ().dispatches_,
                                                   (*link)->caller ().effective_period_,
                                                   frame_size,
                                                   (*link)->number_of_calls ()),
                          -1);

          // If there are no entries in the virtual set, we're done.
          if (proxy_ptr->done ())
            return 0;
          else if (conj_set.insert (proxy_ptr, conj_set_iter) < 0)
            return -1;
        }
    }

  // loop, adding conjunctive dispatches, until one of the conjunctive
  // dispatch sources runs out of entries over the total frame
  conj_set_iter.first ();
  int more_dispatches = (conj_set_iter.done ()) ? 0 : 1;
  while (more_dispatches)
    {
      Time arrival = 0;
      Time deadline = 0;
      Preemption_Priority priority = 0;
      OS_Priority OS_priority = 0;

      for (conj_set_iter.first ();
           conj_set_iter.done () == 0;
           conj_set_iter.advance ())
        {
          // initialize to earliest arrival and deadline, and highest priority
          arrival = 0;
          deadline = 0;
          priority = 0;
          OS_priority = 0;

          // Policy: conjunctively dispatched operations get the
          // latest deadline of any of the dispatches in the
          // conjunction at the time they were dispatched - when and
          // if it is useful to change any of the merge policies, this
          // should be one of the decisions factored out into the
          // conjunctive merge strategy class.

          // Policy: conjunctively dispatched operations get the
          // lowest priority of any of the dispatches in the
          // conjunction at the time they were dispatched - when and
          // if it is useful to change any of the merge policies, this
          // should be one of the decisions factored out into the
          // conjunctive merge strategy class.

          // Obtain a pointer to the current dispatch proxy iterator.
          Dispatch_Proxy_Iterator **proxy_iter;
          if (conj_set_iter.next (proxy_iter) == 0
              || proxy_iter == 0
              || *proxy_iter == 0)
            return -1;

          // Use latest arrival, latest deadline, lowest priority (0 is highest).
          if (arrival <= (*proxy_iter)->arrival ())
            arrival = (*proxy_iter)->arrival ();
          if (deadline <= (*proxy_iter)->deadline ())
            deadline = (*proxy_iter)->deadline ();
          if (priority <= (*proxy_iter)->priority ())
            {
              priority = (*proxy_iter)->priority ();
              OS_priority = (*proxy_iter)->OS_priority ();
            }

          (*proxy_iter)->advance ();

          if ((*proxy_iter)->done ())
            more_dispatches = 0;
        }

      Dispatch_Entry *entry_ptr;
      ACE_NEW_RETURN (entry_ptr,
                      Dispatch_Entry (arrival,
                                      deadline,
                                      priority,
                                      OS_priority,
                                      *this),
                      -1);

      // If even one new dispatch was inserted, result is "something
      // happened".
      result = 1;

      // Add the new dispatch entry to the set of all dispatches, and
      // a link to it to the dispatch links for this task entry.
      if (dispatch_entries.insert (entry_ptr) < 0)
        return -1;

      // Use iterator for efficient insertion into the dispatch set.
      ACE_Ordered_MultiSet_Iterator <Dispatch_Entry_Link> insert_iter (dispatches_);
      if (dispatches_.insert (Dispatch_Entry_Link (*entry_ptr),
                              insert_iter) < 0)
        return -1;

      // TBD - Clients are not assigned priority, but rather obtain it
      // from their call dependencies.  We could complain here if
      // there is a priority specified that doesn't match (or is lower
      // QoS?)
    }

  return result;
}

// This static method is used to reframe an existing dispatch set to
// the given new period multiplier, creating new instances of each
// existing dispatch (with adjusted arrival and deadline) in each
// successive sub-frame.  Returns 1 if the set was reframed to a new
// period, 0 if the set was not changed (the new period was not a
// multiple of the old one), or -1 if an error occurred.

int
Task_Entry::reframe (ACE_Unbounded_Set <Dispatch_Entry *> &dispatch_entries,
                     Task_Entry &owner,
                     ACE_Ordered_MultiSet <Dispatch_Entry_Link> &set,
                     u_long &set_period, u_long new_period)
{
  int result = 0;

  // if the set period is zero, treat it as uninitialized,
  // and simply value the set period with the new period
  if (set_period)
    {
      // make sure the new period is greater than the current
      // set period, and that they are harmonically related
      if (new_period <= set_period)
        // return an error if they're not harmonically related,
        // do nothing if set's frame is a multiple of the new frame
        return (set_period % new_period) ? -1 : 0;
      else if (new_period % set_period)
        return -1;

      // make a shallow copy of the set in a new ordered multiset
      // using the Dispatch_Entry_Link smart pointers
      ACE_Ordered_MultiSet <Dispatch_Entry_Link> new_set;
      ACE_Ordered_MultiSet_Iterator <Dispatch_Entry_Link> new_iter (new_set);
      ACE_Ordered_MultiSet_Iterator <Dispatch_Entry_Link> set_iter (set);

      for (set_iter.first (); set_iter.done () == 0; set_iter.advance ())
        {
          Dispatch_Entry_Link *link;

          if (set_iter.next (link) == 0)
            return -1;
          else if (new_set.insert (*link, new_iter) < 0)
            return -1;
        }

      // Do a deep copy merge back into the set using the new period
      // and starting after the 0th sub-frame: this puts all
      // dispatches after the 0th sub-frame of the new period into the
      // set, and leaves existing dispatches in the 0th sub-frame of
      // the new period in the set as well.
      result = merge_frames (dispatch_entries,
                             owner,
                             set,
                             new_set,
                             new_period,
                             set_period,
                             1,
                             1);
    }

  // update the set's period to be the new frame
  set_period = new_period;

  return result;
}

// This static method is used to merge an existing dispatch set,
// multiplied by the given multipliers for the period and number of
// instances in each period of each existing dispatch, into the given
// "into" set, without affecting the "from set".

int
Task_Entry::merge_frames (ACE_Unbounded_Set <Dispatch_Entry *> &dispatch_entries,
                          Task_Entry &owner,
                          ACE_Ordered_MultiSet <Dispatch_Entry_Link> &dest,
                          ACE_Ordered_MultiSet <Dispatch_Entry_Link> &src,
                          u_long &dest_period,
                          u_long src_period,
                          u_long number_of_calls,
                          u_long starting_dest_sub_frame)
{
  int status = 0;

  // reframe dispatches in the destination set to the new frame size
  // (expands the destination set's period to be the new enclosing frame)
  if (reframe (dispatch_entries,
               owner,
               dest,
               dest_period,
               ACE::minimum_frame_size (dest_period,
                                        src_period)) < 0)
      return -1;

  // use iterator for efficient insertion into the destination set
  ACE_Ordered_MultiSet_Iterator <Dispatch_Entry_Link> dest_iter (dest);

  // do virtual iteration over the source set in the new frame, adding
  // adjusted dispatch entries to the destination
  Dispatch_Proxy_Iterator src_iter (src,
                                    src_period,
                                    dest_period,
                                    number_of_calls,
                                    starting_dest_sub_frame);

  for (src_iter.first (starting_dest_sub_frame);
       src_iter.done () == 0;
       src_iter.advance ())
    {
      // Policy: disjunctively dispatched operations get their
      // deadline and priority from the original dispatch - when and
      // if it is useful to change any of the merge policies, this
      // should be one of the decisions factored out into the
      // disjunctive merge strategy class.

      Dispatch_Entry *entry_ptr;
      ACE_NEW_RETURN (entry_ptr,
                      Dispatch_Entry (src_iter.arrival (),
                                      src_iter.deadline (),
                                      src_iter.priority (),
                                      src_iter.OS_priority (),
                                      owner),
                      -1);

      // if even one new dispatch was inserted, status is "something happened".
      status = 1;

      // add the new dispatch entry to the set of all dispatches, and
      // a link to it to the dispatch links for this task entry
      if (dispatch_entries.insert (entry_ptr) < 0)
        return -1;

      else if (dest.insert (Dispatch_Entry_Link (*entry_ptr), dest_iter) < 0)
        return -1;

      // TBD - Clients are not assigned priority, but rather obtain it
      // from their call dependencies.  We could complain here if
      // there is a priority specified that doesn't match (or is lower
      // QoS?)
    }

  return status;
}

Task_Entry_Link::Task_Entry_Link (Task_Entry &caller,
                                  Task_Entry &called,
                                  CORBA::Long number_of_calls,
                                  RtecScheduler::Dependency_Type_t dependency_type)
  : number_of_calls_ (number_of_calls),
    caller_ (caller),
    called_ (called),
    dependency_type_ (dependency_type)
{
}

Dispatch_Entry::Dispatch_Id Dispatch_Entry::next_id_ = 0;

Dispatch_Entry::Dispatch_Entry (Time arrival,
                                Time deadline,
                                Preemption_Priority priority,
                                OS_Priority os_priority,
                                Task_Entry &task_entry,
                                Dispatch_Entry *original_dispatch)
  : priority_ (priority),
    OS_priority_ (os_priority),
    dynamic_subpriority_ (0),
    static_subpriority_ (0),
    arrival_ (arrival),
    deadline_ (deadline),
    task_entry_ (task_entry),
    original_dispatch_ (original_dispatch)
{
  // obtain, increment the next id
  dispatch_id_ = next_id_++;
}

Dispatch_Entry::Dispatch_Entry (const Dispatch_Entry &d)
  : priority_ (d.priority_),
    OS_priority_ (d.OS_priority_),
    dynamic_subpriority_ (d.dynamic_subpriority_),
    static_subpriority_ (d.static_subpriority_),
    arrival_ (d.arrival_),
    deadline_ (d.deadline_),
    task_entry_ (d.task_entry_),
    original_dispatch_ (d.original_dispatch_)
{
  // obtain, increment the next id
  dispatch_id_ = next_id_++;
}

int
Dispatch_Entry::operator < (const Dispatch_Entry &d) const
{
  // for positioning in the ordered dispatch multiset

  // lowest arrival time first
  if (this->arrival_ != d.arrival_)
    return this->arrival_ < d.arrival_ ? 1 : 0;

  // highest priority second
  if (this->priority_ != d.priority_)
    return this->priority_ > d.priority_ ? 1 : 0;

  // lowest laxity (highest dynamic sub-priority) third Just use low
  // 32 bits of worst_case_execution_time.  This will have to change
  // when TimeBase.idl is finalized.
  //
  // NOTE: Leave the -= code intact as it's a workaround of a BCB4
  // internal compiler error.
  Time this_laxity = deadline_;
  this_laxity -= task_entry ().rt_info ()->worst_case_execution_time;

  Time that_laxity = d.deadline_;
  that_laxity -= d.task_entry ().rt_info ()->worst_case_execution_time;

  if (this_laxity != that_laxity)
    return (this_laxity < that_laxity) ? 1 : 0;

  // finally, by higher importance
  return (task_entry ().rt_info ()->importance >
          d.task_entry ().rt_info ()->importance) ? 1 : 0;
}

// ctor

Dispatch_Entry_Link::Dispatch_Entry_Link (Dispatch_Entry &d)
  : dispatch_entry_ (d)
{
}

// copy ctor

Dispatch_Entry_Link::Dispatch_Entry_Link (const Dispatch_Entry_Link &d)
  : dispatch_entry_ (d.dispatch_entry_)
{
}

// ctor

Dispatch_Proxy_Iterator::Dispatch_Proxy_Iterator
  (ACE_Ordered_MultiSet <Dispatch_Entry_Link> &set,
   u_long actual_frame_size,
   u_long virtual_frame_size,
   u_long number_of_calls,
   u_long starting_sub_frame)
  : number_of_calls_ (number_of_calls),
    current_call_ (0),
    actual_frame_size_ (actual_frame_size),
    virtual_frame_size_ (virtual_frame_size),
    current_frame_offset_ (actual_frame_size * starting_sub_frame),
    iter_ (set)
{
  first (starting_sub_frame);
}

// positions the iterator at the first entry of the passed sub-frame,
// returns 1 if it could position the iterator correctly, 0 if not,
// and -1 if an error occurred.

int
Dispatch_Proxy_Iterator::first (u_int sub_frame)
{
  if (actual_frame_size_ * (sub_frame) >= virtual_frame_size_)
    {
      // can not position the virtual iterator
      // in the given range: do nothing
      return 0;
    }

  // restart the call counter
  current_call_ = 0;

  // use the given sub-frame offset if it's valid
  current_frame_offset_ = actual_frame_size_ * sub_frame;

  // restart the iterator
  return iter_.first ();
}

// positions the iterator at the last entry of the total frame,
// returns 1 if it could position the iterator correctly, 0 if not,
// and -1 if an error occurred.

int
Dispatch_Proxy_Iterator::last (void)
{
  // use the last call
  current_call_ = number_of_calls_ - 1;

  // use the last sub-frame
  current_frame_offset_ = virtual_frame_size_ - actual_frame_size_;

  // position the iterator at the last dispatch
  return iter_.first ();
}

// positions the iterator at the next entry of the total frame,
// returns 1 if it could position the iterator correctly, 0 if not,
// and -1 if an error occurred.

int
Dispatch_Proxy_Iterator::advance (void)
{
  int result = 1;

  if (iter_.done ())
    result = 0; // cannot retreat if we're out of bounds
  else if (current_call_ < number_of_calls_ - 1)
    // if we're still in the same set of calls, increment the call counter
    ++current_call_;
  else
    {
      // roll over the call counter
      current_call_ = 0;

      // advance the iterator in the current sub-frame
      if (! iter_.advance ())
        {
          // if we're not already in the last sub_frame
          if (current_frame_offset_ + actual_frame_size_ < virtual_frame_size_)
            {
              // increment the sub-frame offset
              current_frame_offset_ += actual_frame_size_;

              // restart the iterator at the front of the sub-frame
              result = iter_.first ();
            }
          else
            result = 0; // cannot advance if we're already at the end
        }
    }

  return result;
}

// positions the iterator at the previous entry of the total frame,
// returns 1 if it could position the iterator correctly, 0 if not,
// and -1 if an error occurred.

int
Dispatch_Proxy_Iterator::retreat (void)
{
  int result = 1;

  if (iter_.done ())
    result = 0; // cannot retreat if we're out of bounds
  else if (current_call_ > 0)
    // if we're still in the same set of calls, decrement the call counter
    --current_call_;
  else
    {
      // roll over the call counter
      current_call_ = number_of_calls_ - 1;

      // back up the iterator in the current sub-frame
      if (!iter_.retreat ())
        {
          // if we're not already in the 0th sub_frame
          if (current_frame_offset_ > 0)
            {
              // decrement the sub-frame offset
              current_frame_offset_ -= actual_frame_size_;

              // restart the iterator at the tail of the sub-frame
              result = iter_.last ();
            }
          else
            result = 0; // cannot retreat if we're already at the start
        }
    }

  return result;
}

// returns the adjusted arrival time of the virtual entry

RtecScheduler::Time
Dispatch_Proxy_Iterator::arrival (void) const
{
  Dispatch_Entry_Link *link;
  if (iter_.done ()
      || iter_.next(link) == 0
      || link == 0)
    return 0;

  // Just use low 32 bits of arrival.  This will have to change when
  // TimeBase.idl is finalized.
  return link->dispatch_entry ().arrival () +
         RtecScheduler::Time (current_frame_offset_);
}

// returns the adjusted deadline time of the virtual entry

RtecScheduler::Time
Dispatch_Proxy_Iterator::deadline (void) const
{
  Dispatch_Entry_Link *link;
  if (iter_.done ()
      || iter_.next(link) == 0
      || link == 0)
    return 0;

  // Just use low 32 bits of deadline.  This will have to change when
  // TimeBase.idl is finalized.
  return link->dispatch_entry ().deadline () +
         RtecScheduler::Time (current_frame_offset_);
}

// returns the scheduler priority of the virtual entry

Dispatch_Proxy_Iterator::Preemption_Priority
Dispatch_Proxy_Iterator::priority (void) const
{
  Dispatch_Entry_Link *link;

  if (iter_.done ()
      || iter_.next(link) == 0
      || link == 0)
    return 0;

  return link->dispatch_entry ().priority ();
}

// returns the OS priority of the virtual entry

Dispatch_Proxy_Iterator::OS_Priority
Dispatch_Proxy_Iterator::OS_priority (void) const
{
  Dispatch_Entry_Link *link;
  if (iter_.done ()
      || iter_.next(link) == 0
      || link == 0)
    return 0;

  return link->dispatch_entry ().OS_priority ();
}

// time slice constructor

TimeLine_Entry::TimeLine_Entry (Dispatch_Entry &dispatch_entry,
                                Time start, Time stop,
                                Time arrival, Time deadline,
                                TimeLine_Entry *next,
                                TimeLine_Entry *prev)
  : dispatch_entry_ (dispatch_entry),
    start_ (start),
    stop_ (stop),
    arrival_ (arrival),
    deadline_ (deadline),
    next_ (next),
    prev_ (prev)
{
}