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