1 thilo.boehm 1.1 //%LICENSE////////////////////////////////////////////////////////////////
2 //
3 // Licensed to The Open Group (TOG) under one or more contributor license
4 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
5 // this work for additional information regarding copyright ownership.
6 // Each contributor licenses this file to you under the OpenPegasus Open
7 // Source License; you may not use this file except in compliance with the
8 // License.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 thilo.boehm 1.1 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 //////////////////////////////////////////////////////////////////////////
29 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #include <Pegasus/Common/PegasusAssert.h>
33 #include "DynamicLibrary.h"
34
35 #if defined(PEGASUS_OS_TYPE_WINDOWS)
36 # include "DynamicLibraryWindows.cpp"
37 #elif defined(PEGASUS_OS_TYPE_UNIX) || defined(PEGASUS_OS_VMS)
38 # include "DynamicLibraryPOSIX.cpp"
39 #else
40 # error "Unsupported platform"
41 #endif
42
43 thilo.boehm 1.1 PEGASUS_NAMESPACE_BEGIN
44
45 DynamicLibrary::DynamicLibrary()
46 : _handle(0),
47 _referenceCount(0)
48 {
49 }
50
51 DynamicLibrary::DynamicLibrary(const DynamicLibrary& library)
52 : _fileName(library._fileName),
53 _handle(0),
54 _referenceCount(0)
55 {
56 // load the module again, if necessary. this effectively increments the
57 // operating system's reference count for the module.
58 if (library.isLoaded())
59 {
60 if (load())
61 {
62 _referenceCount = library._referenceCount;
63 }
64 thilo.boehm 1.1 }
65 }
66
67 DynamicLibrary::DynamicLibrary(const String& fileName)
68 : _fileName(fileName),
69 _handle(0),
70 _referenceCount(0)
71 {
72 }
73
74 DynamicLibrary::~DynamicLibrary()
75 {
76 // Unload the module, if necessary, to keep the operating system's
77 // reference count accurate. One call to _unload() takes care of it
78 // no matter how high _referenceCount is.
79
80 if (_referenceCount > 0)
81 {
82 PEGASUS_ASSERT(_handle != 0);
83 _unload();
84 }
85 thilo.boehm 1.1 }
86
87 DynamicLibrary& DynamicLibrary::operator=(const DynamicLibrary& library)
88 {
89 if (this == &library)
90 {
91 return *this;
92 }
93
94 while (isLoaded())
95 {
96 unload();
97 }
98
99 _fileName = library._fileName;
100
101 // load the module again, if necessary. this effectively increments the
102 // operating system's reference count for the module.
103 if (library.isLoaded())
104 {
105 if (load())
106 thilo.boehm 1.1 {
107 _referenceCount = library._referenceCount;
108 }
109 }
110
111 return *this;
112 }
113
114 Boolean DynamicLibrary::isLoaded() const
115 {
116 return _handle != 0;
117 }
118
119 Boolean DynamicLibrary::load()
120 {
121 AutoMutex lock(_loadMutex);
122
123 Boolean loaded = true;
124
125 if (_referenceCount == 0)
126 {
127 thilo.boehm 1.1 PEGASUS_ASSERT(_handle == 0);
128 loaded = _load();
129 }
130
131 if (loaded)
132 {
133 PEGASUS_ASSERT(_handle != 0);
134 _referenceCount++;
135 }
136
137 return loaded;
138 }
139
140 const String& DynamicLibrary::getLoadErrorMessage() const
141 {
142 return _loadErrorMessage;
143 }
144
145 void DynamicLibrary::unload()
146 {
147 AutoMutex lock(_loadMutex);
148 thilo.boehm 1.1
149 PEGASUS_ASSERT(_referenceCount > 0);
150 PEGASUS_ASSERT(_handle != 0);
151
152 _referenceCount--;
153
154 if (_referenceCount == 0)
155 {
156 _unload();
157 _handle = 0;
158 _loadErrorMessage.clear();
159 }
160 }
161
162 const String& DynamicLibrary::getFileName() const
163 {
164 return _fileName;
165 }
166
167 PEGASUS_NAMESPACE_END
|