[9082e0f1] | 1 | #include <fstream.hfa>
|
---|
| 2 | #include <stdlib.hfa>
|
---|
[edf3ff1] | 3 | #include <bits/stack.hfa>
|
---|
| 4 | #include <bits/queue.hfa>
|
---|
| 5 | #include <bits/sequence.hfa>
|
---|
[9082e0f1] | 6 |
|
---|
| 7 | struct Task; // node type
|
---|
| 8 |
|
---|
| 9 | struct TaskDL {
|
---|
| 10 | inline Seqable;
|
---|
[f7386f7] | 11 | Task & node;
|
---|
[9082e0f1] | 12 | };
|
---|
| 13 | void ?{}( TaskDL & this, Task & task ) with( this ) {
|
---|
| 14 | ((Seqable &)this){};
|
---|
[f7386f7] | 15 | &node = &task; // pointer to containing node
|
---|
[9082e0f1] | 16 | }
|
---|
| 17 | Task & task( TaskDL & this ) with( this ) { // getter routine for containing node
|
---|
[f7386f7] | 18 | return node;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
[c8025a21] | 21 | TaskDL *& Back( TaskDL * n ) {
|
---|
| 22 | return (TaskDL *)Back( (Seqable *)n );
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | TaskDL *& Next( TaskDL * n ) {
|
---|
| 26 | return (TaskDL *)Next( (Colable *)n );
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | bool listed( TaskDL * n ) {
|
---|
| 30 | return Next( (Colable *)n ) != 0p;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
[f7386f7] | 33 | struct TaskSL {
|
---|
| 34 | inline Colable;
|
---|
| 35 | Task & node;
|
---|
| 36 | };
|
---|
| 37 | void ?{}( TaskSL & this, Task & task ) with( this ) {
|
---|
| 38 | ((Colable &)this){};
|
---|
| 39 | &node = &task; // pointer to containing node
|
---|
| 40 | }
|
---|
| 41 | Task & task( TaskSL & this ) with( this ) { // getter routine for containing node
|
---|
| 42 | return node;
|
---|
[9082e0f1] | 43 | }
|
---|
| 44 |
|
---|
[c8025a21] | 45 | TaskSL *& Next( TaskSL * n ) {
|
---|
| 46 | return (TaskSL *)Next( (Colable *)n );
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | bool listed( TaskSL * n ) {
|
---|
| 50 | return Next( (Colable *)n ) != 0p;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[9082e0f1] | 53 | struct Task {
|
---|
| 54 | TaskDL clusterRef; // list of tasks on cluster
|
---|
| 55 | TaskDL readyRef; // list of tasks on ready queue
|
---|
[f7386f7] | 56 | TaskSL mutexRef; // list of tasks on mutex queue
|
---|
| 57 | int id;
|
---|
[9082e0f1] | 58 | };
|
---|
[f7386f7] | 59 | void ?{}( Task & this, int id ) with( this ) {
|
---|
[9082e0f1] | 60 | ((TaskDL &)clusterRef){ this };
|
---|
| 61 | ((TaskDL &)readyRef){ this };
|
---|
[f7386f7] | 62 | ((TaskSL &)mutexRef){ this };
|
---|
| 63 | this.id = id;
|
---|
[9082e0f1] | 64 | }
|
---|
| 65 |
|
---|
| 66 | int main() {
|
---|
| 67 | Sequence(TaskDL) clustList, readyList; // task lists
|
---|
[f7386f7] | 68 | Queue(TaskSL) mutexList;
|
---|
[9082e0f1] | 69 | enum { Lnth = 10 };
|
---|
| 70 |
|
---|
[f7386f7] | 71 | for ( id; Lnth ) {
|
---|
| 72 | Task & task = *new( id ); // create task node
|
---|
[9082e0f1] | 73 | addHead( clustList, task.clusterRef ); // insert on lists in opposite directions
|
---|
| 74 | addTail( readyList, task.readyRef );
|
---|
[f7386f7] | 75 | addHead( mutexList, task.mutexRef );
|
---|
[9082e0f1] | 76 | }
|
---|
| 77 |
|
---|
[1ac1f0b] | 78 | SeqIter(TaskDL) sqiter;
|
---|
[7c1144b] | 79 | TaskDL & dl; // iterator index
|
---|
[f7386f7] | 80 | TaskSL & sl;
|
---|
[9082e0f1] | 81 |
|
---|
| 82 | sout | nlOff;
|
---|
[f7386f7] | 83 | for ( over( sqiter, clustList ); sqiter >> dl; ) { // print lists
|
---|
[4f649cb] | 84 | Task & tmp = task( dl ); sout | tmp.id;
|
---|
| 85 | // sout | task( dl ).id;
|
---|
[9082e0f1] | 86 | }
|
---|
| 87 | sout | nl;
|
---|
[f7386f7] | 88 | for ( over( sqiter, readyList ); sqiter >> dl; ) {
|
---|
[4f649cb] | 89 | Task & tmp = task( dl ); sout | tmp.id;
|
---|
| 90 | // sout | task( dl ).id;
|
---|
[9082e0f1] | 91 | }
|
---|
| 92 | sout | nl;
|
---|
[f7386f7] | 93 | for ( QueueIter(TaskSL) qiter = { mutexList }; qiter >> sl; ) { // print lists
|
---|
[4f649cb] | 94 | Task & tmp = task( sl ); sout | tmp.id;
|
---|
| 95 | // sout | task( sl ).id;
|
---|
[f7386f7] | 96 | }
|
---|
| 97 | sout | nl;
|
---|
[4f649cb] | 98 | for ( Lnth ) { // remove nodes from clustList. mutexList
|
---|
[9082e0f1] | 99 | dropHead( clustList );
|
---|
[f7386f7] | 100 | drop( mutexList );
|
---|
[9082e0f1] | 101 | }
|
---|
[4f649cb] | 102 | // Simultaneous deletion only safe if all nodes are traversed in same direction.
|
---|
[9082e0f1] | 103 | for ( Lnth ) { // remove nodes from readyList and safe to delete nodes
|
---|
| 104 | delete( &task( dropHead( readyList ) ) );
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[34dcc78c] | 107 | // Re-purpose Seqable as Colable
|
---|
| 108 | Stack(TaskDL) mutexStack;
|
---|
[f7386f7] | 109 | for ( id; Lnth ) {
|
---|
| 110 | Task & task = *new( id ); // create task node
|
---|
[34dcc78c] | 111 | push( mutexStack, task.clusterRef ); // insert on lists in opposite directions
|
---|
| 112 | }
|
---|
[1ac1f0b] | 113 | for ( StackIter(TaskDL) stiter = { mutexStack }; stiter >> dl; ) {
|
---|
[4f649cb] | 114 | Task & tmp = task( dl ); sout | tmp.id;
|
---|
| 115 | // sout | task( dl ).id;
|
---|
[34dcc78c] | 116 | }
|
---|
| 117 | sout | nl;
|
---|
| 118 | for ( Lnth ) { // remove nodes from readyList and safe to delete nodes
|
---|
| 119 | delete( &task( pop( mutexStack ) ) );
|
---|
| 120 | }
|
---|
[9082e0f1] | 121 | }
|
---|