source: libcfa/src/bits/multi_list.cfa@ f7386f7

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since f7386f7 was f7386f7, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add queue to multi-list container test

  • Property mode set to 100644
File size: 2.5 KB
Line 
1#include <fstream.hfa>
2#include <stdlib.hfa>
3#include "sequence.hfa"
4#include "queue.hfa"
5#include "stack.hfa"
6
7struct Task; // node type
8
9struct TaskDL {
10 inline Seqable;
11 Task & node;
12};
13void ?{}( TaskDL & this, Task & task ) with( this ) {
14 ((Seqable &)this){};
15 &node = &task; // pointer to containing node
16}
17Task & task( TaskDL & this ) with( this ) { // getter routine for containing node
18 return node;
19}
20
21struct TaskSL {
22 inline Colable;
23 Task & node;
24};
25void ?{}( TaskSL & this, Task & task ) with( this ) {
26 ((Colable &)this){};
27 &node = &task; // pointer to containing node
28}
29Task & task( TaskSL & this ) with( this ) { // getter routine for containing node
30 return node;
31}
32
33struct Task {
34 TaskDL clusterRef; // list of tasks on cluster
35 TaskDL readyRef; // list of tasks on ready queue
36 TaskSL mutexRef; // list of tasks on mutex queue
37 int id;
38};
39void ?{}( Task & this, int id ) with( this ) {
40 ((TaskDL &)clusterRef){ this };
41 ((TaskDL &)readyRef){ this };
42 ((TaskSL &)mutexRef){ this };
43 this.id = id;
44}
45
46int main() {
47 Sequence(TaskDL) clustList, readyList; // task lists
48 Queue(TaskSL) mutexList;
49 enum { Lnth = 10 };
50
51 for ( id; Lnth ) {
52 Task & task = *new( id ); // create task node
53 addHead( clustList, task.clusterRef ); // insert on lists in opposite directions
54 addTail( readyList, task.readyRef );
55 addHead( mutexList, task.mutexRef );
56 }
57
58 SeqIter(TaskDL) sqiter;
59 TaskDL & dl;
60 TaskSL & sl;
61
62 sout | nlOff;
63 for ( over( sqiter, clustList ); sqiter >> dl; ) { // print lists
64 Task & fred = task( dl );
65 sout | fred.id; // works
66 sout | task( dl ).id; // does not work
67 }
68 sout | nl;
69 for ( over( sqiter, readyList ); sqiter >> dl; ) {
70 sout | &task( dl );
71 }
72 sout | nl;
73 for ( QueueIter(TaskSL) qiter = { mutexList }; qiter >> sl; ) { // print lists
74 sout | &task( sl );
75 }
76 sout | nl;
77 for ( Lnth ) { // remove nodes from clustList
78 dropHead( clustList );
79 drop( mutexList );
80 }
81 for ( Lnth ) { // remove nodes from readyList and safe to delete nodes
82 delete( &task( dropHead( readyList ) ) );
83 }
84
85 // Re-purpose Seqable as Colable
86 Stack(TaskDL) mutexStack;
87 for ( id; Lnth ) {
88 Task & task = *new( id ); // create task node
89 push( mutexStack, task.clusterRef ); // insert on lists in opposite directions
90 }
91 for ( StackIter(TaskDL) stiter = { mutexStack }; stiter >> dl; ) {
92 sout | &task( dl );
93 }
94 sout | nl;
95 for ( Lnth ) { // remove nodes from readyList and safe to delete nodes
96 delete( &task( pop( mutexStack ) ) );
97 }
98}
Note: See TracBrowser for help on using the repository browser.