// ************************************************************** // * * // * Spheres -- Minimum.cpp * // * * // * (C) Christian Scheideler, 2003 * // * * // ************************************************************** // Minimum.cpp simply generates a list of 10 nodes that compute the minimum // of the node IDs // Special definitions and operations provided by Spheres.h: // // - Sphere: class that enables non-blocking method invocations (NMI). // To ensure the correct execution of NMIs, every method in a class // derived from Sphere that is to be called by NMI has to have the form // // void MethodName(usertype *plist) // // where plist is a pointer to any kind of usertype. // // - void Send(SpherePtr, SphereObj::Method, ParList): // This requests to call the method SphereObj::Method in the user object // referenced by SpherePtr using the parameter list referenced by ParList. // // - void Simulate(Time): // This runs a simulation for Time many rounds. // The Spheres.h class provides an environment for the simulation of // concurrent data structures, where concurrent executions are done at the // level of method invocations (i.e. each method invocation completes before // another method is invoked). To avoid inconsistencies in the data structure, // some general rules have to be obeyed: // // - A method may not have any side-effects other than modifying the // data inside the object. // // - A method must be total, meaning that it is well-defined for // EVERY legal state of the object. #include #include #include "Spheres.h" // parameter list for Node::Ping method class MinPList { public: int min; MinPList(int m) { min = m; } }; class Node: public Sphere { private: int ID; // own ID int minID; // current minimum ID seen // pointers to left and right neighbor Node *left; Node *right; public: Node(int i) { ID = i; minID = 1000; // set min ID to infinity }; void Connect(Node *l, Node *r) { // later, connections should be established via NMIs, but here // we just do it directly left = l; right = r; } void Min(MinPList *p) { if (p->min < minID) { cout << "Node " << ID << ": minimum ID changes from " << minID << " to " << p->min << "\n"; minID = p->min; // create two parameter lists for Node::Min calls MinPList *np1 = new MinPList(minID); MinPList *np2 = new MinPList(minID); // invoke Node::Min in left and right neighbor if (left!=NULL) Send(left, Node::Min, np1); if (right!=NULL) Send(right, Node::Min, np2); } delete p; } }; void main() { int i; MinPList *p; // generate array of node objects Node *List[10]; for (i=0; i<10; i++) List[i] = new Node(i); // connect node objects to a doubly linked list List[0]->Connect(NULL, List[1]); for (i=1; i<9; i++) List[i]->Connect(List[i-1], List[i+1]); List[9]->Connect(List[8], NULL); // generate requests to call Node::Min in every node of the list for (i=0; i<10; i++) { p = new MinPList(i); Send(List[i], Node::Min, p); } // run the simulation for 50 rounds Simulate(50); }