-/**
- * @brief Returns a CycleNode corresponding to a promise
- *
- * Gets (or creates, if none exist) a CycleNode corresponding to a promised
- * value.
- *
- * @param promise The Promise generated by a reader
- * @return The CycleNode corresponding to the Promise
- */
-CycleNode * CycleGraph::getNode(const Promise *promise)
-{
- CycleNode *node = getNode_noCreate(promise);
- if (node == NULL) {
- node = new CycleNode(promise);
- putNode(promise, node);
- }
- return node;
-}
-
-/**
- * @return false if the resolution results in a cycle; true otherwise
- */
-bool CycleGraph::resolvePromise(const Promise *promise, ModelAction *writer,
- promise_list_t *mustResolve)
-{
- CycleNode *promise_node = promiseToNode.get(promise);
- CycleNode *w_node = actionToNode.get(writer);
- ASSERT(promise_node);
-
- if (w_node)
- return mergeNodes(w_node, promise_node, mustResolve);
- /* No existing write-node; just convert the promise-node */
- promise_node->resolvePromise(writer);
- erasePromiseNode(promise_node->getPromise());
- putNode(writer, promise_node);
- return true;
-}
-
-/**
- * @brief Merge two CycleNodes that represent the same write
- *
- * Note that this operation cannot be rolled back.
- *
- * @param w_node The write ModelAction node with which to merge
- * @param p_node The Promise node to merge. Will be destroyed after this
- * function.
- * @param mustMerge Return (pass-by-reference) any additional Promises that
- * must also be merged with w_node
- *
- * @return false if the merge results in a cycle; true otherwise
- */
-bool CycleGraph::mergeNodes(CycleNode *w_node, CycleNode *p_node,
- promise_list_t *mustMerge)
-{
- ASSERT(!w_node->is_promise());
- ASSERT(p_node->is_promise());
-
- const Promise *promise = p_node->getPromise();
- if (!promise->is_compatible(w_node->getAction()) ||
- !promise->same_value(w_node->getAction())) {
- hasCycles = true;
- return false;
- }
-
- /* Transfer the RMW */
- CycleNode *promise_rmw = p_node->getRMW();
- if (promise_rmw && promise_rmw != w_node->getRMW() && w_node->setRMW(promise_rmw)) {
- hasCycles = true;
- return false;
- }
-
- /* Transfer back edges to w_node */
- while (p_node->getNumBackEdges() > 0) {
- CycleNode *back = p_node->removeBackEdge();
- if (back == w_node)
- continue;
- if (back->is_promise()) {
- if (checkReachable(w_node, back)) {
- /* Edge would create cycle; merge instead */
- mustMerge->push_back(back->getPromise());
- if (!mergeNodes(w_node, back, mustMerge))
- return false;
- } else
- back->addEdge(w_node);
- } else
- addNodeEdge(back, w_node);
- }
-
- /* Transfer forward edges to w_node */
- while (p_node->getNumEdges() > 0) {
- CycleNode *forward = p_node->removeEdge();
- if (forward == w_node)
- continue;
- if (forward->is_promise()) {
- if (checkReachable(forward, w_node)) {
- mustMerge->push_back(forward->getPromise());
- if (!mergeNodes(w_node, forward, mustMerge))
- return false;
- } else
- w_node->addEdge(forward);
- } else
- addNodeEdge(w_node, forward);
- }
-
- erasePromiseNode(promise);
- /* Not deleting p_node, to maintain consistency if mergeNodes() fails */
-
- return !hasCycles;
-}
-