1 | // ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
|
---|
2 | // Eric Aubourg
|
---|
3 | // Christophe Magneville
|
---|
4 | // Reza Ansari
|
---|
5 | // $Id: ringprocessor.cc,v 1.1 2003-05-19 23:31:29 aubourg Exp $
|
---|
6 |
|
---|
7 | #include "ringprocessor.h"
|
---|
8 | #include "ringpipe.h"
|
---|
9 |
|
---|
10 | #include <iostream>
|
---|
11 | #include <typeinfo> // ajout pour linux
|
---|
12 | #ifdef HAVE_VALUES_H
|
---|
13 | #include <values.h>
|
---|
14 | #endif
|
---|
15 | #ifndef MAXINT
|
---|
16 | #define MAXINT 2147483647
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | #ifdef HAVE_STDINT_H
|
---|
20 | #include <stdint.h>
|
---|
21 | #endif
|
---|
22 |
|
---|
23 |
|
---|
24 | #ifdef WITH_SOPHYA
|
---|
25 | #include "pexceptions.h"
|
---|
26 | #else
|
---|
27 | #include "apexceptions.h"
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | RingProcessor::RingProcessor() {
|
---|
31 | outRings = NULL;
|
---|
32 | inRings = NULL;
|
---|
33 | inited = false;
|
---|
34 |
|
---|
35 | ringBegin = 0;
|
---|
36 | ringEnd = MAXINT;
|
---|
37 |
|
---|
38 | wontNeedRing = -1;
|
---|
39 | }
|
---|
40 |
|
---|
41 | RingProcessor::~RingProcessor() {
|
---|
42 | delete[] inRings;
|
---|
43 | delete[] outRings;
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | void RingProcessor::init()
|
---|
48 | {}
|
---|
49 |
|
---|
50 | void RingProcessor::afterinit() {
|
---|
51 | inRings = new (RingPipe*[inRingIx.size()]);
|
---|
52 | for (int i=0; i<inRingIx.size(); i++)
|
---|
53 | inRings[i] = NULL;
|
---|
54 | outRings = new (RingPipe*[outRingIx.size()]);
|
---|
55 | for (int i=0; i<outRingIx.size(); i++)
|
---|
56 | outRings[i] = NULL;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 | int RingProcessor::declareRingInput(string ring) {
|
---|
62 | if (inRingIx.find(ring) != inRingIx.end())
|
---|
63 | throw DuplicateIdExc("RingProcessor::declareRingInput : "+ring+
|
---|
64 | " already declared");
|
---|
65 | int i = inRingIx.size();
|
---|
66 | inRingIx[ring] = i;
|
---|
67 | return i;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | int RingProcessor::declareRingOutput(string ring) {
|
---|
72 | if (outRingIx.find(ring) != outRingIx.end())
|
---|
73 | throw DuplicateIdExc("RingProcessor::declareRingOutput : "+ring+
|
---|
74 | " already declared");
|
---|
75 | int i = outRingIx.size();
|
---|
76 | outRingIx[ring] = i;
|
---|
77 | return i;
|
---|
78 | }
|
---|
79 |
|
---|
80 | void RingProcessor::addRingInput(string name, RingPipe* ring) {
|
---|
81 | chkinit();
|
---|
82 | map<string, int>::iterator i = inRingIx.find(name);
|
---|
83 | if (i == inRingIx.end()) throw NotFoundExc("RingProcessor::addInput "+
|
---|
84 | name+" not declared");
|
---|
85 | inRings[(*i).second] = ring;
|
---|
86 | ring->addConsumer(this);
|
---|
87 | }
|
---|
88 |
|
---|
89 | void RingProcessor::addRingOutput(string name, RingPipe* ring) {
|
---|
90 | chkinit();
|
---|
91 | map<string, int>::iterator i = outRingIx.find(name);
|
---|
92 | if (i == outRingIx.end()) throw NotFoundExc("RingProcessor::addOutput "+
|
---|
93 | name+" not declared");
|
---|
94 | ring->setProducer(this);
|
---|
95 | outRings[(*i).second] = ring;
|
---|
96 | }
|
---|
97 |
|
---|
98 | void RingProcessor::run()
|
---|
99 | {}
|
---|
100 |
|
---|
101 | void* RingProcessor::ThreadStart(void* arg) {
|
---|
102 | RingProcessor* r = (RingProcessor*) arg;
|
---|
103 | try {
|
---|
104 | r->run();
|
---|
105 | }
|
---|
106 | catch (PThrowable & exc) {
|
---|
107 | cerr << "\n RingProcessor::ThreadStart() Catched Exception RingProcessor@"
|
---|
108 | << hex << r << dec << "\n"
|
---|
109 | << (string)typeid(exc).name()
|
---|
110 | << " - Msg= " << exc.Msg() << endl;
|
---|
111 | }
|
---|
112 | catch (const std::exception & sex) {
|
---|
113 | cerr << "\n RingProcessor::ThreadStart() Catched std::exception RingProcessor@"
|
---|
114 | << hex << r << dec << "\n"
|
---|
115 | << (string)typeid(sex).name() << endl;
|
---|
116 | }
|
---|
117 | catch (...) {
|
---|
118 | cerr << "\n RingProcessor::ThreadStart() Catched ... exception RingProcessor@"
|
---|
119 | << hex << r << dec << endl;
|
---|
120 | }
|
---|
121 | // r->warnPutDone();
|
---|
122 | pthread_exit(NULL);
|
---|
123 | return NULL;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | void RingProcessor::start() {
|
---|
128 | pthread_create(&thread, NULL, ThreadStart, this);
|
---|
129 | }
|
---|
130 |
|
---|
131 | RingPipe* RingProcessor::getInputRing(int index) {
|
---|
132 | if (index >= inRingIx.size())
|
---|
133 | throw RangeCheckError("RingProcessor::getInputRing() out of bound");
|
---|
134 | RingPipe* ring = inRings[index];
|
---|
135 | if (ring == NULL)
|
---|
136 | throw NullPtrError("RingProcessor::getInputRing() - Not assigned Ring !");
|
---|
137 | return(ring);
|
---|
138 | }
|
---|
139 |
|
---|
140 | RingPipe* RingProcessor::getOutputRing(int index) {
|
---|
141 | if (index >= outRingIx.size())
|
---|
142 | throw RangeCheckError("RingProcessor::getOutputRing() out of bound");
|
---|
143 | RingPipe* ring = outRings[index];
|
---|
144 | if (ring == NULL)
|
---|
145 | throw NullPtrError("RingProcessor::getOutputRing() - Not assigned Ring !");
|
---|
146 | return(ring);
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | Ring const* RingProcessor::getRing(int index, int i) {
|
---|
151 | RingPipe* pipe = getInputRing(index);
|
---|
152 | pipe->waitForRing(i);
|
---|
153 | return pipe->getRing(i);
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | void RingProcessor::putRing(int index, int i, Ring const* ring) {
|
---|
158 | RingPipe* pipe = getOutputRing(index);
|
---|
159 | if (pipe == NULL) return;
|
---|
160 | pipe->putRing(i, ring);
|
---|
161 | }
|
---|
162 |
|
---|
163 | void RingProcessor::wontNeedRingBefore(int i) {
|
---|
164 | if (i<wontNeedRing) return;
|
---|
165 | wontNeedRing = i;
|
---|
166 | for (int j=0; j< (int) inRingIx.size(); j++) {
|
---|
167 | if (inRings[j]) inRings[j]->wontNeedRingBefore(i);
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | void RingProcessor::getRingRange(int& min, int&max) {
|
---|
173 | if (min < ringBegin) min = ringBegin;
|
---|
174 | if (max > ringEnd) max = ringEnd;
|
---|
175 |
|
---|
176 | for (int i=0; i<inRingIx.size(); i++)
|
---|
177 | inRings[i]->getRingRange(min, max);
|
---|
178 |
|
---|
179 | ringBegin = min;
|
---|
180 | ringEnd = max;
|
---|
181 | }
|
---|
182 |
|
---|