source: trunk/Cosy/candrv/sdolist.cc@ 16765

Last change on this file since 16765 was 14586, checked in by tbretz, 12 years ago
Moved cout to gLog and added a debug file which can be switched on on purpose
File size: 3.9 KB
Line 
1#include "sdolist.h"
2
3#include "MLog.h"
4#include "MLogManip.h"
5
6#include "MTime.h"
7
8
9ClassImp(PendingSDO);
10ClassImp(PendingSDOList);
11
12using namespace std;
13
14#undef DEBUG
15
16#ifdef DEBUG
17#include <fstream>
18static ofstream fout;
19#endif
20
21PendingSDOList::PendingSDOList()
22{
23#ifdef DEBUG
24 fout.open("sdolist.txt", ios::app);
25 fout.rdbuf()->pubsetbuf(0,0);
26 fout << hex;
27#endif
28
29 fFirst = new PendingSDO;
30 fLast = fFirst;
31}
32
33PendingSDOList::~PendingSDOList()
34{
35 DelAll();
36 delete fFirst;
37
38#ifdef DEBUG
39 fout.close();
40#endif
41}
42
43void PendingSDOList::DelAll()
44{
45 if (fMux.Lock()==13)
46 gLog << err << MTime(-1) << " - PendingSDOList::DelAll - mutex is already locked by this thread" << endl;
47
48#ifdef DEBUG
49 fout << MTime(-1) << " - DelAll" << endl;
50#endif
51
52 PendingSDO *prev = fFirst;
53 PendingSDO *sdo;
54
55 do
56 {
57 sdo = prev->Next;
58 delete prev;
59
60 } while((prev=sdo));
61
62 fFirst = new PendingSDO;
63 fLast = fFirst;
64
65 if (fMux.UnLock()==13)
66 gLog << err << MTime(-1) << " - PendingSDOList::DelAll - tried to unlock mutex locked by other thread." << endl;
67}
68
69void PendingSDOList::Add(BYTE_t node, WORD_t idx, BYTE_t subidx)
70{
71 PendingSDO *sdo = fFirst;
72
73 if (fMux.Lock()==13)
74 gLog << err << MTime(-1) << " - PendingSDOList::Add - mutex is already locked by this thread" << endl;
75
76#ifdef DEBUG
77 fout << MTime(-1) << " - Add " << idx << "/" << subidx << " @ " << node << endl;
78#endif
79
80 while ((sdo=sdo->Next))
81 if (sdo->Node==node && sdo->Idx==idx && sdo->Subidx==subidx)
82 {
83 gLog << warn << MTime(-1) << " - SDO Node #" << dec << (int)node << ", 0x";
84 gLog << hex << idx << "/" << (int)subidx;
85 gLog << " still pending." << endl;
86 break;
87 }
88
89 fLast->Next = new PendingSDO(node, idx, subidx);
90 fLast = fLast->Next;
91 if (fMux.UnLock()==13)
92 gLog << err << MTime(-1) << " - PendingSDOList::Add - tried to unlock mutex locked by other thread." << endl;
93}
94
95void PendingSDOList::Del(BYTE_t node, WORD_t idx, BYTE_t subidx)
96{
97 PendingSDO *prev = fFirst;
98 PendingSDO *sdo;
99
100 if (fMux.Lock()==13)
101 gLog << err << MTime(-1) << " - PendingSDOList::Del - mutex is already locked by this thread" << endl;
102
103#ifdef DEBUG
104 fout << MTime(-1) << " - Del " << idx << "/" << subidx << " @ " << node << endl;
105#endif
106
107 while ((sdo=prev->Next))
108 {
109 if (sdo->Node!=node || sdo->Idx!=idx || sdo->Subidx!=subidx)
110 {
111 prev = sdo;
112 continue;
113 }
114
115 prev->Next = sdo->Next;
116
117 delete sdo;
118
119 if (!prev->Next)
120 fLast = prev;
121
122 break;
123 }
124 if (fMux.UnLock()==13)
125 gLog << err << MTime(-1) << " - PendingSDOList::Del - tried to unlock mutex locked by other thread." << endl;
126}
127
128int PendingSDOList::IsPending() const
129{
130 return fFirst->Next ? 1 : 0;
131}
132
133int PendingSDOList::IsPending(BYTE_t node)
134{
135 int rc = FALSE;
136 PendingSDO *sdo = fFirst;
137
138 if (fMux.Lock()==13)
139 gLog << err << MTime(-1) << " - PendingSDOList::IsPending(byte) - mutex is already locked by this thread" << endl;
140 while ((sdo=sdo->Next))
141 if (sdo->Node==node)
142 {
143 rc = TRUE;
144 break;
145 }
146 if (fMux.UnLock()==13)
147 gLog << err << MTime(-1) << " - PendingSDOList::IsPending(byte) - tried to unlock mutex locked by other thread." << endl;
148
149 return rc;
150}
151
152int PendingSDOList::IsPending(BYTE_t node, WORD_t idx, BYTE_t subidx)
153{
154 int rc = FALSE;
155 PendingSDO *sdo = fFirst;
156
157 if (fMux.Lock()==13)
158 gLog << err << MTime(-1) << " - PendingSDOList::IsPending - mutex is already locked by this thread" << endl;
159 while ((sdo=sdo->Next))
160 if (sdo->Node==node && sdo->Idx==idx && sdo->Subidx==subidx)
161 {
162 rc = TRUE;
163 break;
164 }
165 if (fMux.UnLock()==13)
166 gLog << err << MTime(-1) << " - PendingSDOList::IsPending - tried to unlock mutex locked by other thread." << endl;
167
168 return rc;
169}
Note: See TracBrowser for help on using the repository browser.