CPN
Computational Process Networks
D4RNode.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
23 #ifndef D4R_NODE_H
24 #define D4R_NODE_H
25 #pragma once
26 #include <cpn/common.h>
27 #include <cpn/d4r/D4RTag.h>
29 #include <list>
30 #include <tr1/memory>
31 namespace D4R {
32 
33 /*
34  * Implementation notes
35  *
36  * In the D4R algorithm there are four state transitions.
37  *
38  * BLOCK:
39  * This state transition happens when a node blocks on a queue. There
40  * are two nodes in the block, the one who is blocked and the one who is being
41  * blocked on. I will call these the blockee and the blocker respectively in
42  * this discussion.
43  *
44  * The following will happen in this transition:
45  * 1. the queue will be marked as blocked
46  * 2. blockee will update the queue size in its private tag.
47  * 3. all fields in the blockee's private tag are copied to the blockees public tag.
48  * 4. a copy of the blockers public tag is obtained and a change notification is registered.
49  * 5. the blockee's public and private count will be set to 1 plus the maximum of the current blockee's
50  * count and the blocker's count.
51  *
52  * TRANSMIT:
53  * This state transition happens whenever a change notification happens and the
54  * queue is marked blocked and the tags are NOT equal.
55  *
56  * The following happens in this transition:
57  * 1. The blockee and blocker tags are compared. If the blockee tag is less than
58  * the blocker tag then proceed otherwise ignore the notification.
59  * 2. Copy the count and nodekey of the public tag of the blocker to the public tag of the blockee
60  * and set the qsize of the blockee to the minimum of the qsize of the blocker and blockee.
61  *
62  * DETECT:
63  * If the tags are equal and the blockee's private qsize and public qsize are equal then
64  * we perform the DETECT step. This is where we resolve the deadlock.
65  *
66  * ACTIVATE:
67  * Go back to normal activity.
68  *
69  */
70 
71  using std::tr1::weak_ptr;
72  using std::tr1::shared_ptr;
73  class QueueBase;
74 
84  class Node {
85  public:
86 
87  Node(uint64_t key);
88 
89  ~Node();
90 
91  Tag GetPublicTag() const;
92  void SetPublicTag(const Tag &t);
93  Tag GetPrivateTag() const;
94  void SetPrivateTag(const Tag &t);
95 
99  void AddReader(weak_ptr<QueueBase> q);
103  void AddWriter(weak_ptr<QueueBase> q);
104 
112  void Block(const Tag &t, unsigned qsize);
121  bool Transmit(const Tag &t);
122 
123  protected:
124  void SignalTagChanged();
125 
129  std::list<weak_ptr<QueueBase> > readerlist;
130  std::list<weak_ptr<QueueBase> > writerlist;
131  private:
132  Node(const Node&);
133  Node &operator=(const Node&);
134  };
135 
136 }
137 #endif
void AddReader(weak_ptr< QueueBase > q)
Definition: D4RNode.cc:107
void SetPrivateTag(const Tag &t)
Definition: D4RNode.cc:61
Node(uint64_t key)
Definition: D4RNode.cc:38
Tag GetPrivateTag() const
Definition: D4RNode.cc:56
Tag publicTag
Definition: D4RNode.h:126
std::list< weak_ptr< QueueBase > > writerlist
Definition: D4RNode.h:130
void SetPublicTag(const Tag &t)
Definition: D4RNode.cc:51
PthreadMutex taglock
Definition: D4RNode.h:128
Tag GetPublicTag() const
Definition: D4RNode.cc:46
void SignalTagChanged()
Definition: D4RNode.cc:140
void AddWriter(weak_ptr< QueueBase > q)
Definition: D4RNode.cc:111
std::list< weak_ptr< QueueBase > > readerlist
Definition: D4RNode.h:129
Tag privateTag
Definition: D4RNode.h:127
void Block(const Tag &t, unsigned qsize)
Definition: D4RNode.cc:66
bool Transmit(const Tag &t)
Definition: D4RNode.cc:76
Node & operator=(const Node &)