]>
Commit | Line | Data |
---|---|---|
6580df1c | 1 | // $Id$ |
2 | ||
3 | /************************************************************************** | |
4 | * This file is property of and copyright by the ALICE HLT Project * | |
5 | * ALICE Experiment at CERN, All rights reserved. * | |
6 | * * | |
7 | * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> * | |
8 | * for The ALICE HLT Project. * | |
9 | * * | |
10 | * Permission to use, copy, modify and distribute this software and its * | |
11 | * documentation strictly for non-commercial purposes is hereby granted * | |
12 | * without fee, provided that the above copyright notice appears in all * | |
13 | * copies and that both the copyright notice and this permission notice * | |
14 | * appear in the supporting documentation. The authors make no claims * | |
15 | * about the suitability of this software for any purpose. It is * | |
16 | * provided "as is" without express or implied warranty. * | |
17 | **************************************************************************/ | |
18 | ||
19 | /** @file AliHLTHOMERLibManager.cxx | |
20 | @author Matthias Richter | |
21 | @date | |
22 | @brief dynamic HLT HOMER reader/writer generation and destruction. */ | |
23 | ||
24 | // see header file for class documentation | |
25 | // or | |
26 | // refer to README to build package | |
27 | // or | |
28 | // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt | |
29 | ||
30 | #include <cerrno> | |
31 | #include <cassert> | |
32 | #include "AliHLTHOMERLibManager.h" | |
33 | #include "AliHLTHOMERReader.h" | |
34 | #include "AliHLTHOMERWriter.h" | |
1b820a65 | 35 | #include "AliHLTLogging.h" |
6580df1c | 36 | #include "TString.h" |
37 | #include "TSystem.h" | |
38 | ||
39 | /** ROOT macro for the implementation of ROOT specific class methods */ | |
40 | ClassImp(AliHLTHOMERLibManager) | |
41 | ||
1b820a65 | 42 | // This list must be NULL terminated, since we use it as a marker to identify |
43 | // the end of the list. | |
44 | const char* AliHLTHOMERLibManager::fgkLibraries[] = {"libAliHLTHOMER.so", "libHOMER.so", NULL}; | |
45 | // The size of the list of reference counts must be one less than fgkLibraries. | |
46 | int AliHLTHOMERLibManager::fgkLibRefCount[] = {0, 0}; | |
47 | ||
6580df1c | 48 | AliHLTHOMERLibManager::AliHLTHOMERLibManager() |
49 | : | |
50 | fLibraryStatus(0), | |
a183f221 | 51 | fFctCreateReaderFromTCPPort(NULL), |
52 | fFctCreateReaderFromTCPPorts(NULL), | |
6580df1c | 53 | fFctCreateReaderFromBuffer(NULL), |
54 | fFctDeleteReader(NULL), | |
55 | fFctCreateWriter(NULL), | |
1b820a65 | 56 | fFctDeleteWriter(NULL), |
57 | fLoadedLib(NULL) | |
6580df1c | 58 | { |
59 | // see header file for class documentation | |
60 | // or | |
61 | // refer to README to build package | |
62 | // or | |
63 | // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt | |
64 | } | |
65 | ||
66 | AliHLTHOMERLibManager::~AliHLTHOMERLibManager() | |
67 | { | |
68 | // see header file for class documentation | |
1b820a65 | 69 | UnloadHOMERLibrary(); |
6580df1c | 70 | } |
71 | ||
a183f221 | 72 | AliHLTHOMERReader* AliHLTHOMERLibManager::OpenReader(const char* hostname, unsigned short port ) |
73 | { | |
74 | // see header file for class documentation | |
75 | if (fLibraryStatus<0) return NULL; | |
76 | ||
77 | if (fLibraryStatus==0) { | |
78 | fLibraryStatus=LoadHOMERLibrary(); | |
79 | } | |
80 | ||
81 | AliHLTHOMERReader* pReader=NULL; | |
82 | if (fFctCreateReaderFromTCPPort!=NULL && (pReader=(((AliHLTHOMERReaderCreateFromTCPPort_t)fFctCreateReaderFromTCPPort)(hostname, port)))==NULL) { | |
cfa641b1 | 83 | cout <<"can not create instance of HOMER reader from ports" << endl; |
a183f221 | 84 | } |
85 | ||
86 | return pReader; | |
87 | } | |
88 | ||
89 | AliHLTHOMERReader* AliHLTHOMERLibManager::OpenReader(unsigned int tcpCnt, const char** hostnames, unsigned short* ports) | |
90 | { | |
91 | // see header file for class documentation | |
92 | if (fLibraryStatus<0) return NULL; | |
93 | ||
94 | if (fLibraryStatus==0) { | |
95 | fLibraryStatus=LoadHOMERLibrary(); | |
96 | } | |
97 | ||
98 | AliHLTHOMERReader* pReader=NULL; | |
99 | if (fFctCreateReaderFromTCPPorts!=NULL && (pReader=(((AliHLTHOMERReaderCreateFromTCPPorts_t)fFctCreateReaderFromTCPPorts)(tcpCnt, hostnames, ports)))==NULL) { | |
cef34fbe | 100 | //HLTError("can not create instance of HOMER reader (function %p)", fFctCreateReaderFromTCPPorts); |
cfa641b1 | 101 | cout << "can not create instance of HOMER reader from port"<<endl; |
a183f221 | 102 | } |
103 | ||
104 | return pReader; | |
105 | } | |
106 | ||
18b56222 | 107 | AliHLTHOMERReader* AliHLTHOMERLibManager::OpenReaderBuffer(const AliHLTUInt8_t* pBuffer, int size) |
6580df1c | 108 | { |
109 | // see header file for class documentation | |
110 | if (fLibraryStatus<0) return NULL; | |
111 | ||
112 | if (fLibraryStatus==0) { | |
113 | fLibraryStatus=LoadHOMERLibrary(); | |
114 | } | |
115 | ||
116 | AliHLTHOMERReader* pReader=NULL; | |
117 | if (fFctCreateReaderFromBuffer!=NULL && (pReader=(((AliHLTHOMERReaderCreateFromBuffer_t)fFctCreateReaderFromBuffer)(pBuffer, size)))==NULL) { | |
cef34fbe | 118 | //HLTError("can not create instance of HOMER reader (function %p)", fFctCreateReaderFromBuffer); |
6580df1c | 119 | } |
120 | ||
121 | return pReader; | |
122 | } | |
123 | ||
124 | int AliHLTHOMERLibManager::DeleteReader(AliHLTHOMERReader* pReader) | |
125 | { | |
126 | // see header file for class documentation | |
127 | if (fLibraryStatus<0) return fLibraryStatus; | |
128 | ||
129 | if (fLibraryStatus==0) { | |
130 | fLibraryStatus=LoadHOMERLibrary(); | |
131 | } | |
132 | ||
133 | if (fFctDeleteReader!=NULL) { | |
134 | ((AliHLTHOMERReaderDelete_t)fFctDeleteReader)(pReader); | |
135 | } | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
140 | AliHLTHOMERWriter* AliHLTHOMERLibManager::OpenWriter() | |
141 | { | |
142 | // see header file for class documentation | |
143 | if (fLibraryStatus<0) return NULL; | |
144 | ||
145 | if (fLibraryStatus==0) { | |
146 | fLibraryStatus=LoadHOMERLibrary(); | |
147 | } | |
148 | ||
149 | AliHLTHOMERWriter* pWriter=NULL; | |
77fe3666 | 150 | if (fFctCreateWriter!=NULL && (pWriter=(((AliHLTHOMERWriterCreate_t)fFctCreateWriter)()))==NULL) { |
6580df1c | 151 | // HLTError("can not create instance of HOMER writer (function %p)", fFctCreateWriter); |
77fe3666 | 152 | } |
6580df1c | 153 | |
154 | return pWriter; | |
155 | } | |
156 | ||
157 | int AliHLTHOMERLibManager::DeleteWriter(AliHLTHOMERWriter* pWriter) | |
158 | { | |
159 | // see header file for class documentation | |
160 | if (fLibraryStatus<0) return fLibraryStatus; | |
161 | ||
162 | if (fLibraryStatus==0) { | |
163 | fLibraryStatus=LoadHOMERLibrary(); | |
164 | } | |
165 | ||
166 | if (fFctDeleteWriter!=NULL) { | |
167 | ((AliHLTHOMERWriterDelete_t)fFctDeleteWriter)(pWriter); | |
168 | } | |
169 | ||
170 | return 0; | |
171 | } | |
172 | ||
173 | int AliHLTHOMERLibManager::LoadHOMERLibrary() | |
174 | { | |
175 | // see header file for class documentation | |
176 | int iResult=-EBADF; | |
1b820a65 | 177 | const char** library=&fgkLibraries[0]; |
178 | int* refcount = &fgkLibRefCount[0]; | |
6580df1c | 179 | do { |
180 | TString libs = gSystem->GetLibraries(); | |
ac3389e3 | 181 | if (libs.Contains(*library) || |
182 | (gSystem->Load(*library)) >= 0) { | |
1b820a65 | 183 | ++(*refcount); |
184 | fLoadedLib = *library; | |
6580df1c | 185 | iResult=1; |
186 | break; | |
187 | } | |
1b820a65 | 188 | ++library; |
189 | ++refcount; | |
190 | } while ((*library)!=NULL); | |
6580df1c | 191 | |
192 | if (iResult>0 && *library!=NULL) { | |
193 | // print compile info | |
194 | typedef void (*CompileInfo)( char*& date, char*& time); | |
6580df1c | 195 | |
d93ec7ca | 196 | fFctCreateReaderFromTCPPort=(void (*)())gSystem->DynFindSymbol(*library, ALIHLTHOMERREADER_CREATE_FROM_TCPPORT); |
197 | fFctCreateReaderFromTCPPorts=(void (*)())gSystem->DynFindSymbol(*library, ALIHLTHOMERREADER_CREATE_FROM_TCPPORTS); | |
198 | fFctCreateReaderFromBuffer=(void (*)())gSystem->DynFindSymbol(*library, ALIHLTHOMERREADER_CREATE_FROM_BUFFER); | |
199 | fFctDeleteReader=(void (*)())gSystem->DynFindSymbol(*library, ALIHLTHOMERREADER_DELETE); | |
200 | fFctCreateWriter=(void (*)())gSystem->DynFindSymbol(*library, ALIHLTHOMERWRITER_CREATE); | |
201 | fFctDeleteWriter=(void (*)())gSystem->DynFindSymbol(*library, ALIHLTHOMERWRITER_DELETE); | |
a183f221 | 202 | if (fFctCreateReaderFromTCPPort==NULL || |
203 | fFctCreateReaderFromTCPPorts==NULL || | |
204 | fFctCreateReaderFromBuffer==NULL || | |
205 | fFctDeleteReader==NULL || | |
206 | fFctCreateWriter==NULL || | |
207 | fFctDeleteWriter==NULL) { | |
6580df1c | 208 | iResult=-ENOSYS; |
209 | } else { | |
6580df1c | 210 | } |
211 | } | |
212 | if (iResult<0 || *library==NULL) { | |
a183f221 | 213 | fFctCreateReaderFromTCPPort=NULL; |
214 | fFctCreateReaderFromTCPPorts=NULL; | |
6580df1c | 215 | fFctCreateReaderFromBuffer=NULL; |
216 | fFctDeleteReader=NULL; | |
217 | fFctCreateWriter=NULL; | |
218 | fFctDeleteWriter=NULL; | |
219 | } | |
220 | ||
221 | return iResult; | |
222 | } | |
1b820a65 | 223 | |
224 | int AliHLTHOMERLibManager::UnloadHOMERLibrary() | |
225 | { | |
226 | // see header file for class documentation | |
227 | int iResult=0; | |
228 | ||
229 | if (fLoadedLib != NULL) | |
230 | { | |
231 | // Find the corresponding reference count. | |
232 | const char** library=&fgkLibraries[0]; | |
233 | int* refcount = &fgkLibRefCount[0]; | |
234 | while (*library != NULL) | |
235 | { | |
236 | if (strcmp(*library, fLoadedLib) == 0) break; | |
237 | ++library; | |
238 | ++refcount; | |
239 | } | |
240 | ||
241 | // Decrease the reference count and remove the library if it is zero. | |
242 | if (*refcount >= 0) --(*refcount); | |
243 | if (*refcount == 0) | |
244 | { | |
245 | // Check that the library we are trying to unload is actually the last library | |
246 | // in the gSystem->GetLibraries() list. If not then we must abort the removal. | |
247 | // This is because of a ROOT bug/feature/limitation. If we try unload the library | |
248 | // then ROOT will also wipe all libraries in the gSystem->GetLibraries() list | |
249 | // following the library we want to unload. | |
250 | TString libstring = gSystem->GetLibraries(); | |
251 | TString token, lastlib; | |
252 | Ssiz_t from = 0; | |
253 | Int_t numOfLibs = 0, posOfLib = -1; | |
254 | while (libstring.Tokenize(token, from, " ")) | |
255 | { | |
256 | ++numOfLibs; | |
257 | lastlib = token; | |
258 | if (token.Contains(fLoadedLib)) posOfLib = numOfLibs; | |
259 | } | |
260 | if (numOfLibs == posOfLib) | |
261 | { | |
262 | gSystem->Unload(fLoadedLib); | |
263 | ||
264 | // Check that the library is gone, since Unload() does not return a status code. | |
265 | libstring = gSystem->GetLibraries(); | |
266 | if (libstring.Contains(fLoadedLib)) iResult = -EBADF; | |
267 | } | |
268 | else | |
269 | { | |
270 | AliHLTLogging log; | |
271 | log.LoggingVarargs(kHLTLogWarning, Class_Name(), FUNCTIONNAME(), __FILE__, __LINE__, | |
272 | Form("ROOT limitation! Cannot properly cleanup and unload the shared" | |
273 | " library '%s' since another library '%s' was loaded afterwards. Trying to" | |
274 | " unload this library will remove the others and lead to serious memory faults.", | |
275 | fLoadedLib, lastlib.Data() | |
276 | )); | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
281 | // Clear the function pointers. | |
282 | fFctCreateReaderFromTCPPort = NULL; | |
283 | fFctCreateReaderFromTCPPorts = NULL; | |
284 | fFctCreateReaderFromBuffer = NULL; | |
285 | fFctDeleteReader = NULL; | |
286 | fFctCreateWriter = NULL; | |
287 | fFctDeleteWriter = NULL; | |
288 | ||
289 | return iResult; | |
290 | } |