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