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

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

re-purpose Seqable as Colable for stack

  • Property mode set to 100644
File size: 1.9 KB
Line 
1#include <fstream.hfa>
2#include <stdlib.hfa>
3#include "sequence.hfa"
4#include "stack.hfa"
5
6struct Task; // node type
7
8struct TaskDL {
9 inline Seqable;
10 Task & task_;
11};
12void ?{}( TaskDL & this, Task & task ) with( this ) {
13 ((Seqable &)this){};
14 &task_ = &task; // pointer to containing node
15}
16Task & task( TaskDL & this ) with( this ) { // getter routine for containing node
17 return task_;
18}
19
20struct Task {
21 TaskDL clusterRef; // list of tasks on cluster
22 TaskDL readyRef; // list of tasks on ready queue
23 // other stuff
24};
25void ?{}( Task & this ) with( this ) {
26 ((TaskDL &)clusterRef){ this };
27 ((TaskDL &)readyRef){ this };
28}
29
30int main() {
31 Sequence(TaskDL) clustList, readyList; // task lists
32 enum { Lnth = 10 };
33
34 for ( Lnth ) {
35 Task & task = *new(); // create task node
36 addHead( clustList, task.clusterRef ); // insert on lists in opposite directions
37 addTail( readyList, task.readyRef );
38 }
39
40 SeqIter(TaskDL) iter;
41 TaskDL & dl;
42
43 sout | nlOff;
44 for ( over( iter, clustList ); iter >> dl; ) { // print lists
45 sout | &task( dl );
46 }
47 sout | nl;
48 for ( SeqIter(TaskDL) iter = { readyList }; iter >> dl; ) {
49 sout | &task( dl );
50 }
51 sout | nl;
52 for ( Lnth ) { // remove nodes from clustList
53 dropHead( clustList );
54 }
55 for ( Lnth ) { // remove nodes from readyList and safe to delete nodes
56 delete( &task( dropHead( readyList ) ) );
57 }
58
59 // Re-purpose Seqable as Colable
60 Stack(TaskDL) mutexStack;
61 for ( Lnth ) {
62 Task & task = *new(); // create task node
63 push( mutexStack, task.clusterRef ); // insert on lists in opposite directions
64 }
65 for ( StackIter(TaskDL) iter = { mutexStack }; (StackIter(TaskDL) &)iter >> dl; ) { // cast required
66 sout | &task( dl );
67 }
68 sout | nl;
69 for ( Lnth ) { // remove nodes from readyList and safe to delete nodes
70 delete( &task( pop( mutexStack ) ) );
71 }
72}
Note: See TracBrowser for help on using the repository browser.