OpenASIP  2.0
PSocketResource.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file PSocketResource.cc
26  *
27  * Implementation of prototype of Resource Model:
28  * implementation of the abstract PSocketResource.
29  *
30  * @author Vladimir Guzma 2006 (vladimir.guzma-no.spam-tut.fi)
31  * @note rating: red
32  */
33 #include <iostream>
34 #include "PSocketResource.hh"
35 #include "MapTools.hh"
36 #include "Conversion.hh"
37 #include "MoveNode.hh"
38 #include "MoveGuard.hh"
39 #include "Guard.hh"
40 #include "Move.hh"
41 
42 /**
43  * Constructor defining name of resource
44  * @param name Name of resource
45  */
46 PSocketResource::PSocketResource(const std::string& name, unsigned int initiationInterval) :
47  SchedulingResource(name, initiationInterval) {}
48 
49 /**
50  * Empty constructor
51  */
53 
54 /**
55  * Test if resource PSocketResource is used in given cycle
56  *
57  * The PSocket is inUse if it is read from or written to at
58  * least once.
59  *
60  * @param cycle Cycle which to test
61  * @return True if p-socket is already used in cycle
62  */
63 bool
64 PSocketResource::isInUse(const int cycle) const {
65  ResourceRecordType::const_iterator iter =
66  resourceRecord_.find(instructionIndex(cycle));
67  if (iter != resourceRecord_.end() && iter->second.size() > 0) {
68  return true;
69  }
70  return false;
71 }
72 
73 /**
74  * Test if resource PSocketResource is available
75  *
76  * @param cycle Cycle which to test
77  * @return True if p-socket is available in cycle
78  */
79 bool
80 PSocketResource::isAvailable(const int cycle) const {
81  ResourceRecordType::const_iterator iter =
82  resourceRecord_.find(instructionIndex(cycle));
83  if (iter != resourceRecord_.end() && iter->second.size() > 0) {
84  const std::set<MoveNode*>& movesInCycle = iter->second;
85  for (std::set<MoveNode*>::const_iterator i = movesInCycle.begin();
86  i != movesInCycle.end(); i++) {
87  if ((*i)->move().isUnconditional()) {
88  return false;
89  }
90  }
91  }
92  return true;
93 }
94 
95 /**
96  * Assign resource to given node for given cycle.
97  *
98  * @param cycle Cycle to assign
99  * @param node MoveNode to assign
100  * @throw In case PSocket can not be assigned
101  * @note Exception is internal error, resource should be tested
102  * with canAssign() before assign() is called.
103  */
104 void
105 PSocketResource::assign(const int cycle, MoveNode& mn)
106 {
107  resourceRecord_[instructionIndex(cycle)].insert(&mn);
109  return;
110 }
111 
112 /**
113  * Unassign resource from given node for given cycle.
114  *
115  * @param cycle Cycle to remove assignment from
116  * @param node MoveNode to remove assignment from
117  * @throw In case the PSocket was not assigned before.
118  */
119 void
120 PSocketResource::unassign(const int cycle, MoveNode& mn)
121 {
122  if (isInUse(cycle)) {
123  resourceRecord_[instructionIndex(cycle)].erase(&mn);
125  return;
126  }
127  std::string msg = "PSocket was not assigned so it can not be unassigned!";
128  throw InvalidData(__FILE__, __LINE__, __func__, msg);
129 }
130 
131 /**
132  * Return true if resource can be assigned for given resource in given cycle.
133  *
134  * @param cycle Cycle to test
135  * @param node MoveNode to test
136  * @return true if node can be assigned to cycle
137  */
138 bool
139 PSocketResource::canAssign(const int cycle, const MoveNode& node) const {
140 
141  ResourceRecordType::const_iterator iter = resourceRecord_.find(cycle);
142  if (iter != resourceRecord_.end()) {
143  const std::set<MoveNode*>& movesInCycle = iter->second;
144  for (std::set<MoveNode*>::const_iterator i = movesInCycle.begin();
145  i != movesInCycle.end(); i++) {
146  MoveNode* mn = *i;
147 #ifdef NO_OVERCOMMIT
148  return false;
149 #else
150  if (node.move().isUnconditional() ||
151  mn->move().isUnconditional()) {
152  return false;
153  }
154  if (!node.move().guard().guard().isOpposite(
155  mn->move().guard().guard())) {
156  return false;
157  }
158 #endif
159  }
160  }
161  return true;
162 }
163 
164 /**
165  * Clears bookkeeping of the scheduling resource.
166  *
167  * After this call the state of the resource should be identical to a
168  * newly-created and initialized resource.
169  */
170 void
173  resourceRecord_.clear();
174 }
SchedulingResource::clear
virtual void clear()
Definition: SchedulingResource.cc:397
PSocketResource::assign
virtual void assign(const int cycle, MoveNode &node) override
Definition: PSocketResource.cc:105
TTAProgram::Move::isUnconditional
bool isUnconditional() const
Definition: Move.cc:154
MapTools.hh
MoveNode
Definition: MoveNode.hh:65
PSocketResource::~PSocketResource
virtual ~PSocketResource()
Definition: PSocketResource.cc:52
PSocketResource::unassign
virtual void unassign(const int cycle, MoveNode &node) override
Definition: PSocketResource.cc:120
PSocketResource::canAssign
virtual bool canAssign(const int cycle, const MoveNode &node) const override
Definition: PSocketResource.cc:139
InvalidData
Definition: Exception.hh:149
SchedulingResource
Definition: SchedulingResource.hh:52
Conversion.hh
TTAProgram::Move::guard
MoveGuard & guard() const
Definition: Move.cc:345
__func__
#define __func__
Definition: Application.hh:67
Guard.hh
PSocketResource::PSocketResource
PSocketResource(const std::string &name, unsigned int initiationInterval=0)
Definition: PSocketResource.cc:46
SchedulingResource::instructionIndex
int instructionIndex(int cycle) const
PSocketResource::isInUse
virtual bool isInUse(const int cycle) const override
Definition: PSocketResource.cc:64
PSocketResource::clear
void clear() override
Definition: PSocketResource.cc:171
TTAMachine::Guard::isOpposite
virtual bool isOpposite(const Guard &guard) const =0
MoveNode::move
TTAProgram::Move & move()
PSocketResource::isAvailable
virtual bool isAvailable(const int cycle) const override
Definition: PSocketResource.cc:80
PSocketResource.hh
SchedulingResource::decreaseUseCount
virtual void decreaseUseCount()
Definition: SchedulingResource.cc:364
TTAProgram::MoveGuard::guard
const TTAMachine::Guard & guard() const
Definition: MoveGuard.cc:86
Move.hh
MoveNode.hh
PSocketResource::resourceRecord_
ResourceRecordType resourceRecord_
Definition: PSocketResource.hh:63
SchedulingResource::increaseUseCount
virtual void increaseUseCount()
Definition: SchedulingResource.cc:355
MoveGuard.hh