-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoboGrams.cpp
175 lines (152 loc) · 3.89 KB
/
RoboGrams.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// RoboGrams.cpp : Defines the entry point for the console application.
//
#include "RoboGrams.h"
#include <stdarg.h>
#include <cstdio>
#include <iostream>
// **********************
// * *
// * Printf To String *
// * *
// **********************
namespace robograms {
std::string strprintf (const char* format, ...)
{
char buf[1024];
va_list ap;
va_start (ap, format);
vsnprintf (buf, sizeof(buf), format, ap);
va_end (ap);
return std::string(buf);
}
// ****************************
// * *
// * Thread-Safe References *
// * *
// ****************************
MutexSemaphore Reference::mu_;
// A Mutex is necessary because reading the pointer and incrementing the counter
// must be atomic.
Reference::Reference(const Reference& r)
{
Mutex mu(mu_);
rc_ = r.rc_;
if (rc_) rc_->grab();
}
// The argument is a Reference, not a const Reference&, so a new reference is
// safely constructed using the above copy constructor. Once this is done
// the atomicExchange keeps this thread-safe. There is never a point at which
// references and their counters are inconsistent.
Reference& Reference::operator=(Reference r)
{
if (r.rc_) r.rc_->grab();
RefCounter* temp = (RefCounter*)atomicExchange(r.rc_, &rc_);
if (temp) temp->drop();
return *this;
}
// ******************
// * *
// * Message Pool *
// * *
// ******************
//
// The special pool message that is returned for over-allocations is index 0. This
// is accomplished by making the free list circular at the end: index 0 points to
// itself.
MessagePoolBase::MessagePoolBase(int maxSize)
: maxSize_(maxSize), highWaterMark_(0), inUse_(0), initialize_(true), finalize_(false)
{
assert(maxSize > 0);
list_ = new int[maxSize];
list_[0] = 0;
for (int i = 1; i < maxSize; ++i)
list_[i] = i - 1;
freeList_ = maxSize - 1;
}
MessagePoolBase::~MessagePoolBase()
{
delete [] list_;
}
int MessagePoolBase::alloc()
{
assert(inUse_ < maxSize_);
if (inUse_ >= maxSize_)
{
std::cout << "WARNING: A message pool has exceeded its max size."
<< std::endl;
}
int i;
{
Mutex mu(mu_);
i = freeList_;
freeList_ = list_[i];
if (i)
list_[i] = -1;
++inUse_;
if (inUse_ > highWaterMark_)
highWaterMark_ = inUse_;
}
return i;
}
void MessagePoolBase::free(int i)
{
{
Mutex mu(mu_);
if (i)
{
list_[i] = freeList_;
freeList_ = i;
}
--inUse_;
}
assert(inUse_ >= 0);
}
std::string MessagePoolBase::describe(const char* poolName) const
{
std::string s = strprintf("\nPool %s: %d of %d buffers in use, max %d used\n",
poolName, inUse_, maxSize_, highWaterMark_);
for (int i = 0; i < maxSize_; ++i)
{
s += strprintf("%2d: ", i);
if (list_[i] >= 0 && !(i == 0 && inUse_ >= maxSize_))
s += "free\n";
else
{
std::string t = getDescription(i);
if (t.at(t.length()-1) == '\n')
t.erase(t.end()-1);
int p = 0;
while (true)
{
if (p != 0)
s += " ";
unsigned int q = t.find('\n', p);
if (q == std::string::npos)
{
s += t.substr(p);
break;
}
else
{
s += t.substr(p, q - p) + '\n';
p = q + 1;
}
}
s += '\n';
}
}
return s;
}
// ****************************
// * *
// * RoboCup Wiring Diagram *
// * *
// ****************************
void RoboGram::run()
{
for (unsigned int i = 0; i < modules_.size(); ++i)
modules_[i]->reset();
for (unsigned int i = 0; i < modules_.size(); ++i)
modules_[i]->run();
}
}