3 /**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * for The ALICE HLT Project. *
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 **************************************************************************/
19 /** @file AliHLTHOMERLibManager.cxx
20 @author Matthias Richter
22 @brief dynamic HLT HOMER reader/writer generation and destruction. */
24 // see header file for class documentation
26 // refer to README to build package
28 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
32 #include "AliHLTHOMERLibManager.h"
33 #include "AliHLTHOMERReader.h"
34 #include "AliHLTHOMERWriter.h"
35 #include "AliHLTLogging.h"
39 /** ROOT macro for the implementation of ROOT specific class methods */
40 ClassImp(AliHLTHOMERLibManager)
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};
48 AliHLTHOMERLibManager::AliHLTHOMERLibManager()
51 fFctCreateReaderFromTCPPort(NULL),
52 fFctCreateReaderFromTCPPorts(NULL),
53 fFctCreateReaderFromBuffer(NULL),
54 fFctDeleteReader(NULL),
55 fFctCreateWriter(NULL),
56 fFctDeleteWriter(NULL),
59 // see header file for class documentation
61 // refer to README to build package
63 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
66 AliHLTHOMERLibManager::~AliHLTHOMERLibManager()
68 // see header file for class documentation
72 AliHLTHOMERReader* AliHLTHOMERLibManager::OpenReader(const char* hostname, unsigned short port )
74 // see header file for class documentation
75 if (fLibraryStatus<0) return NULL;
77 if (fLibraryStatus==0) {
78 fLibraryStatus=LoadHOMERLibrary();
81 AliHLTHOMERReader* pReader=NULL;
82 if (fFctCreateReaderFromTCPPort!=NULL && (pReader=(((AliHLTHOMERReaderCreateFromTCPPort_t)fFctCreateReaderFromTCPPort)(hostname, port)))==NULL) {
83 cout <<"can not create instance of HOMER reader from ports" << endl;
89 AliHLTHOMERReader* AliHLTHOMERLibManager::OpenReader(unsigned int tcpCnt, const char** hostnames, unsigned short* ports)
91 // see header file for class documentation
92 if (fLibraryStatus<0) return NULL;
94 if (fLibraryStatus==0) {
95 fLibraryStatus=LoadHOMERLibrary();
98 AliHLTHOMERReader* pReader=NULL;
99 if (fFctCreateReaderFromTCPPorts!=NULL && (pReader=(((AliHLTHOMERReaderCreateFromTCPPorts_t)fFctCreateReaderFromTCPPorts)(tcpCnt, hostnames, ports)))==NULL) {
100 //HLTError("can not create instance of HOMER reader (function %p)", fFctCreateReaderFromTCPPorts);
101 cout << "can not create instance of HOMER reader from port"<<endl;
107 AliHLTHOMERReader* AliHLTHOMERLibManager::OpenReaderBuffer(const AliHLTUInt8_t* pBuffer, int size)
109 // see header file for class documentation
110 if (fLibraryStatus<0) return NULL;
112 if (fLibraryStatus==0) {
113 fLibraryStatus=LoadHOMERLibrary();
116 AliHLTHOMERReader* pReader=NULL;
117 if (fFctCreateReaderFromBuffer!=NULL && (pReader=(((AliHLTHOMERReaderCreateFromBuffer_t)fFctCreateReaderFromBuffer)(pBuffer, size)))==NULL) {
118 //HLTError("can not create instance of HOMER reader (function %p)", fFctCreateReaderFromBuffer);
124 int AliHLTHOMERLibManager::DeleteReader(AliHLTHOMERReader* pReader)
126 // see header file for class documentation
127 if (fLibraryStatus<0) return fLibraryStatus;
129 if (fLibraryStatus==0) {
130 fLibraryStatus=LoadHOMERLibrary();
133 if (fFctDeleteReader!=NULL) {
134 ((AliHLTHOMERReaderDelete_t)fFctDeleteReader)(pReader);
140 AliHLTHOMERWriter* AliHLTHOMERLibManager::OpenWriter()
142 // see header file for class documentation
143 if (fLibraryStatus<0) return NULL;
145 if (fLibraryStatus==0) {
146 fLibraryStatus=LoadHOMERLibrary();
149 AliHLTHOMERWriter* pWriter=NULL;
150 if (fFctCreateWriter!=NULL && (pWriter=(((AliHLTHOMERWriterCreate_t)fFctCreateWriter)()))==NULL) {
151 // HLTError("can not create instance of HOMER writer (function %p)", fFctCreateWriter);
157 int AliHLTHOMERLibManager::DeleteWriter(AliHLTHOMERWriter* pWriter)
159 // see header file for class documentation
160 if (fLibraryStatus<0) return fLibraryStatus;
162 if (fLibraryStatus==0) {
163 fLibraryStatus=LoadHOMERLibrary();
166 if (fFctDeleteWriter!=NULL) {
167 ((AliHLTHOMERWriterDelete_t)fFctDeleteWriter)(pWriter);
173 int AliHLTHOMERLibManager::LoadHOMERLibrary()
175 // see header file for class documentation
177 const char** library=&fgkLibraries[0];
178 int* refcount = &fgkLibRefCount[0];
180 TString libs = gSystem->GetLibraries();
181 if (libs.Contains(*library)) {
185 if ((gSystem->Load(*library)) >= 0) {
187 fLoadedLib = *library;
193 } while ((*library)!=NULL);
195 if (iResult>0 && *library!=NULL) {
196 // print compile info
197 typedef void (*CompileInfo)( char*& date, char*& time);
199 fFctCreateReaderFromTCPPort=(void (*)())gSystem->DynFindSymbol(*library, ALIHLTHOMERREADER_CREATE_FROM_TCPPORT);
200 fFctCreateReaderFromTCPPorts=(void (*)())gSystem->DynFindSymbol(*library, ALIHLTHOMERREADER_CREATE_FROM_TCPPORTS);
201 fFctCreateReaderFromBuffer=(void (*)())gSystem->DynFindSymbol(*library, ALIHLTHOMERREADER_CREATE_FROM_BUFFER);
202 fFctDeleteReader=(void (*)())gSystem->DynFindSymbol(*library, ALIHLTHOMERREADER_DELETE);
203 fFctCreateWriter=(void (*)())gSystem->DynFindSymbol(*library, ALIHLTHOMERWRITER_CREATE);
204 fFctDeleteWriter=(void (*)())gSystem->DynFindSymbol(*library, ALIHLTHOMERWRITER_DELETE);
205 if (fFctCreateReaderFromTCPPort==NULL ||
206 fFctCreateReaderFromTCPPorts==NULL ||
207 fFctCreateReaderFromBuffer==NULL ||
208 fFctDeleteReader==NULL ||
209 fFctCreateWriter==NULL ||
210 fFctDeleteWriter==NULL) {
215 if (iResult<0 || *library==NULL) {
216 fFctCreateReaderFromTCPPort=NULL;
217 fFctCreateReaderFromTCPPorts=NULL;
218 fFctCreateReaderFromBuffer=NULL;
219 fFctDeleteReader=NULL;
220 fFctCreateWriter=NULL;
221 fFctDeleteWriter=NULL;
227 int AliHLTHOMERLibManager::UnloadHOMERLibrary()
229 // see header file for class documentation
232 if (fLoadedLib != NULL)
234 // Find the corresponding reference count.
235 const char** library=&fgkLibraries[0];
236 int* refcount = &fgkLibRefCount[0];
237 while (*library != NULL)
239 if (strcmp(*library, fLoadedLib) == 0) break;
244 // Decrease the reference count and remove the library if it is zero.
245 if (*refcount >= 0) --(*refcount);
248 // Check that the library we are trying to unload is actually the last library
249 // in the gSystem->GetLibraries() list. If not then we must abort the removal.
250 // This is because of a ROOT bug/feature/limitation. If we try unload the library
251 // then ROOT will also wipe all libraries in the gSystem->GetLibraries() list
252 // following the library we want to unload.
253 TString libstring = gSystem->GetLibraries();
254 TString token, lastlib;
256 Int_t numOfLibs = 0, posOfLib = -1;
257 while (libstring.Tokenize(token, from, " "))
261 if (token.Contains(fLoadedLib)) posOfLib = numOfLibs;
263 if (numOfLibs == posOfLib)
265 gSystem->Unload(fLoadedLib);
267 // Check that the library is gone, since Unload() does not return a status code.
268 libstring = gSystem->GetLibraries();
269 if (libstring.Contains(fLoadedLib)) iResult = -EBADF;
274 log.LoggingVarargs(kHLTLogWarning, Class_Name(), FUNCTIONNAME(), __FILE__, __LINE__,
275 Form("ROOT limitation! Cannot properly cleanup and unload the shared"
276 " library '%s' since another library '%s' was loaded afterwards. Trying to"
277 " unload this library will remove the others and lead to serious memory faults.",
278 fLoadedLib, lastlib.Data()
284 // Clear the function pointers.
285 fFctCreateReaderFromTCPPort = NULL;
286 fFctCreateReaderFromTCPPorts = NULL;
287 fFctCreateReaderFromBuffer = NULL;
288 fFctDeleteReader = NULL;
289 fFctCreateWriter = NULL;
290 fFctDeleteWriter = NULL;