ACE_DLL_Manager Class Reference

This class is a singleton and serves as a factory and repository for instances of ACE_DLL_Handle. More...

#include <DLL_Manager.h>

Collaboration diagram for ACE_DLL_Manager:

Collaboration graph
[legend]
List of all members.

Public Types

enum  { DEFAULT_SIZE = ACE_DEFAULT_DLL_MANAGER_SIZE }

Public Member Functions

ACE_DLL_Handleopen_dll (const ACE_TCHAR *dll_name, int openmode, ACE_SHLIB_HANDLE handle)
int close_dll (const ACE_TCHAR *dll_name)
 Close the underlying dll. Decrements the refcount.

u_long unload_policy (void) const
 Returns the current per-process UNLOAD_POLICY.

void unload_policy (u_long unload_policy)

Static Public Member Functions

ACE_DLL_Managerinstance (int size=ACE_DLL_Manager::DEFAULT_SIZE)
 Return a unique instance.


Protected Member Functions

 ACE_DLL_Manager (int size=ACE_DLL_Manager::DEFAULT_SIZE)
 Default constructor.

 ~ACE_DLL_Manager (void)
 Destructor.

int open (int size)
int close (void)
ACE_DLL_Handlefind_dll (const ACE_TCHAR *dll_name) const
int unload_dll (ACE_DLL_Handle *dll_handle, int force_unload=0)

Private Member Functions

 ACE_DLL_Manager (const ACE_DLL_Manager &)
void operator= (const ACE_DLL_Manager &)

Static Private Member Functions

void close_singleton (void)
 Close the singleton instance.


Private Attributes

ACE_DLL_Handle ** handle_vector_
 Vector containing all loaded handle objects.

int current_size_
 Current number of handles.

int total_size_
 Maximum number of handles.

u_long unload_policy_
 Unload strategy.


Static Private Attributes

ACE_DLL_Managerinstance_ = 0
 Pointer to a process-wide .


Friends

class ACE_Framework_Repository
class ACE_Object_Manager

Detailed Description

This class is a singleton and serves as a factory and repository for instances of ACE_DLL_Handle.

This class is a singleton whose lifetime is managed by the ACE_Framework_Repository. Although it is normally meant to be used directly only by ACE_DLL, applications can call the unload_policy() methods in order get/set the the dll unload policy. Unload policies include per_process/per-dll and eager/lazy. Dlls can export set their own policy by using the ACE_DLL_UNLOAD_POLICY macro found in config-all.h. If a dll choses to set an unload policy, it will be used when the per-dll policy (the default) is in effect. If the per-dll policy is in effect and a dll has not chosen to set a policy, the current per-process policy will be used.

The following policy macros are provided in config-all.h:

ACE_DLL_UNLOAD_POLICY_PER_PROCESS - Per-process policy that unloads dlls eagerly.

ACE_DLL_UNLOAD_POLICY_PER_DLL - Apply policy on a per-dll basis. If the dll doesn't use one of the macros below, the current per-process policy will be used.

ACE_DLL_UNLOAD_POLICY_LAZY - Don't unload dll when refcount reaches zero, i.e., wait for either an explicit unload request or program exit.

ACE_DLL_UNLOAD_POLICY_DEFAULT - Default policy allows dlls to control their own destinies, but will unload those that don't make a choice eagerly.

Definition at line 184 of file DLL_Manager.h.


Member Enumeration Documentation

anonymous enum
 

Enumeration values:
DEFAULT_SIZE 

Definition at line 190 of file DLL_Manager.h.

00191   {
00192     DEFAULT_SIZE = ACE_DEFAULT_DLL_MANAGER_SIZE
00193   };


Constructor & Destructor Documentation

ACE_DLL_Manager::ACE_DLL_Manager int  size = ACE_DLL_Manager::DEFAULT_SIZE  )  [protected]
 

Default constructor.

Definition at line 500 of file DLL_Manager.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TRACE, ACE::debug(), LM_ERROR, and open().

00501   : handle_vector_ (0),
00502     current_size_ (0),
00503     total_size_ (0),
00504     unload_policy_ (ACE_DLL_UNLOAD_POLICY_PER_DLL)
00505 {
00506   ACE_TRACE ("ACE_DLL_Manager::ACE_DLL_Manager");
00507 
00508   if (this->open (size) != 0 && ACE::debug ())
00509     ACE_ERROR ((LM_ERROR,
00510                 ACE_LIB_TEXT ("ACE_DLL_Manager ctor failed to allocate ")
00511                 ACE_LIB_TEXT ("handle_vector_.\n")));
00512 }

ACE_DLL_Manager::~ACE_DLL_Manager void   )  [protected]
 

Destructor.

Definition at line 514 of file DLL_Manager.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TRACE, close(), ACE::debug(), and LM_ERROR.

00515 {
00516   ACE_TRACE ("ACE_DLL_Manager::~ACE_DLL_Manager");
00517 
00518   if (this->close () != 0 && ACE::debug ())
00519     ACE_ERROR ((LM_ERROR,
00520                 ACE_LIB_TEXT ("ACE_DLL_Manager dtor failed to close ")
00521                 ACE_LIB_TEXT ("properly.\n")));
00522 }

ACE_DLL_Manager::ACE_DLL_Manager const ACE_DLL_Manager  )  [private]
 


Member Function Documentation

int ACE_DLL_Manager::close void   )  [protected]
 

Definition at line 640 of file DLL_Manager.cpp.

References ACE_TRACE, current_size_, handle_vector_, and unload_dll().

Referenced by ~ACE_DLL_Manager().

00641 {
00642   ACE_TRACE ("ACE_DLL_Manager::close");
00643 
00644   int force_close = 1;
00645 
00646   if (this->handle_vector_ != 0)
00647     {
00648       // Delete components in reverse order.
00649       for (int i = this->current_size_ - 1; i >= 0; i--)
00650         {
00651           if (this->handle_vector_[i])
00652             {
00653               ACE_DLL_Handle *s =
00654                 const_cast<ACE_DLL_Handle *> (this->handle_vector_[i]);
00655               this->handle_vector_[i] = 0;
00656               this->unload_dll (s, force_close);
00657               delete s;
00658             }
00659         }
00660 
00661       delete [] this->handle_vector_;
00662       this->handle_vector_ = 0;
00663       this->current_size_ = 0;
00664     }
00665   return 0;
00666 }

int ACE_DLL_Manager::close_dll const ACE_TCHAR dll_name  ) 
 

Close the underlying dll. Decrements the refcount.

Definition at line 574 of file DLL_Manager.cpp.

References ACE_GUARD_RETURN, ACE_TCHAR, ACE_TRACE, find_dll(), and unload_dll().

Referenced by ACE_DLL::close().

00575 {
00576   ACE_TRACE ("ACE_DLL_Manager::close_dll");
00577   ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, 0));
00578 
00579   ACE_DLL_Handle *handle = this->find_dll (dll_name);
00580   if (handle)
00581     {
00582       return this->unload_dll (handle, 0);
00583     }
00584 
00585   return -1;
00586 }

void ACE_DLL_Manager::close_singleton void   )  [static, private]
 

Close the singleton instance.

Definition at line 489 of file DLL_Manager.cpp.

References ACE_GUARD, ACE_TRACE, and instance_.

Referenced by ACE_Framework_Repository::close(), and ACE_Object_Manager::fini().

00490 {
00491   ACE_TRACE ("ACE_DLL_Manager::close_singleton");
00492 
00493   ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
00494                      *ACE_Static_Object_Lock::instance ()));
00495 
00496   delete ACE_DLL_Manager::instance_;
00497   ACE_DLL_Manager::instance_ = 0;
00498 }

ACE_DLL_Handle * ACE_DLL_Manager::find_dll const ACE_TCHAR dll_name  )  const [protected]
 

Definition at line 669 of file DLL_Manager.cpp.

References ACE_TCHAR, ACE_TRACE, current_size_, handle_vector_, and ACE_OS::strcmp().

Referenced by close_dll(), and open_dll().

00670 {
00671   ACE_TRACE ("ACE_DLL_Manager::find_dll");
00672 
00673   int i;
00674   for (i = 0; i < this->current_size_; i++)
00675     if (this->handle_vector_[i] &&
00676         ACE_OS::strcmp (this->handle_vector_[i]->dll_name (), dll_name) == 0)
00677       {
00678         return this->handle_vector_[i];
00679       }
00680 
00681   return 0;
00682 }

ACE_DLL_Manager * ACE_DLL_Manager::instance int  size = ACE_DLL_Manager::DEFAULT_SIZE  )  [static]
 

Return a unique instance.

Definition at line 468 of file DLL_Manager.cpp.

References ACE_GUARD_RETURN, ACE_NEW_RETURN, ACE_TRACE, and instance_.

Referenced by ACE_DLL::close(), and ACE_DLL::open_i().

00469 {
00470   ACE_TRACE ("ACE_DLL_Manager::instance");
00471 
00472   if (ACE_DLL_Manager::instance_ == 0)
00473     {
00474       // Perform Double-Checked Locking Optimization.
00475       ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00476                                 *ACE_Static_Object_Lock::instance (), 0));
00477       if (ACE_DLL_Manager::instance_ == 0)
00478         {
00479           ACE_NEW_RETURN (ACE_DLL_Manager::instance_,
00480                           ACE_DLL_Manager (size),
00481                           0);
00482         }
00483     }
00484 
00485   return ACE_DLL_Manager::instance_;
00486 }

int ACE_DLL_Manager::open int  size  )  [protected]
 

Definition at line 624 of file DLL_Manager.cpp.

References ACE_NEW_RETURN, ACE_TRACE, and handle_vector_.

Referenced by ACE_DLL_Manager().

00625 {
00626   ACE_TRACE ("ACE_DLL_Manager::open");
00627 
00628   ACE_DLL_Handle **temp = 0;
00629 
00630   ACE_NEW_RETURN (temp,
00631                   ACE_DLL_Handle *[size],
00632                   -1);
00633 
00634   this->handle_vector_ = temp;
00635   this->total_size_ = size;
00636   return 0;
00637 }

ACE_DLL_Handle * ACE_DLL_Manager::open_dll const ACE_TCHAR dll_name,
int  openmode,
ACE_SHLIB_HANDLE  handle
 

Factory for ACE_DLL_Handle objects. If one already exits, its refcount is incremented.

Definition at line 525 of file DLL_Manager.cpp.

References ACE_ERROR, ACE_GUARD_RETURN, ACE_LIB_TEXT, ACE_NEW_RETURN, ACE_SHLIB_HANDLE, ACE_TCHAR, ACE_TRACE, current_size_, ACE::debug(), find_dll(), handle_vector_, LM_ERROR, and ACE_DLL_Handle::open().

Referenced by ACE_DLL::open_i().

00528 {
00529   ACE_TRACE ("ACE_DLL_Manager::open_dll");
00530   ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, 0));
00531 
00532   ACE_DLL_Handle *temp_handle = 0;
00533   ACE_DLL_Handle *dll_handle = this->find_dll (dll_name);
00534   if (!dll_handle)
00535     {
00536       if (this->current_size_ < this->total_size_)
00537         {
00538           ACE_NEW_RETURN (temp_handle,
00539                           ACE_DLL_Handle,
00540                           0);
00541 
00542           dll_handle = temp_handle;
00543         }
00544     }
00545 
00546   if (dll_handle)
00547     {
00548       if (dll_handle->open (dll_name, open_mode, handle) != 0)
00549         {
00550           // Error while openind dll. Free temp handle
00551           if (ACE::debug ())
00552             ACE_ERROR ((LM_ERROR,
00553                         ACE_LIB_TEXT ("ACE_DLL_Manager::open_dll: Could not ")
00554                         ACE_LIB_TEXT ("open dll %s.\n"),
00555                         dll_name));
00556 
00557           delete temp_handle;
00558           return 0;
00559         }
00560       
00561       // Add the handle to the vector only if the dll is successfully
00562       // opened.
00563       if (temp_handle != NULL)
00564         {
00565           this->handle_vector_[this->current_size_] = dll_handle;
00566           this->current_size_++;
00567         }
00568     }
00569 
00570   return dll_handle;
00571 }

void ACE_DLL_Manager::operator= const ACE_DLL_Manager  )  [private]
 

int ACE_DLL_Manager::unload_dll ACE_DLL_Handle dll_handle,
int  force_unload = 0
[protected]
 

Definition at line 685 of file DLL_Manager.cpp.

References ACE_BIT_DISABLED, ACE_ERROR, ACE_LIB_TEXT, ACE_TRACE, ACE_DLL_Handle::close(), ACE::debug(), LM_ERROR, and ACE_DLL_Handle::symbol().

Referenced by close(), and close_dll().

00686 {
00687   ACE_TRACE ("ACE_DLL_Manager::unload_dll");
00688 
00689   if (dll_handle)
00690     {
00691       int unload = force_unload;
00692       if (unload == 0)
00693         {
00694           // apply strategy
00695           if (ACE_BIT_DISABLED (this->unload_policy_,
00696                                 ACE_DLL_UNLOAD_POLICY_PER_DLL))
00697             {
00698               unload = ACE_BIT_DISABLED (this->unload_policy_,
00699                                          ACE_DLL_UNLOAD_POLICY_LAZY);
00700             }
00701           else
00702             {
00703               // Declare the type of the symbol:
00704               typedef int (*dll_unload_policy)(void);
00705               dll_unload_policy the_policy = 0;
00706               void *unload_policy_ptr =
00707                 dll_handle->symbol (ACE_LIB_TEXT ("_get_dll_unload_policy"), 1);
00708               ptrdiff_t temp_p =
00709                 reinterpret_cast<ptrdiff_t> (unload_policy_ptr);
00710               the_policy =
00711                 reinterpret_cast<dll_unload_policy> (temp_p);
00712               if (the_policy != 0)
00713                 unload = ACE_BIT_DISABLED (the_policy (),
00714                                            ACE_DLL_UNLOAD_POLICY_LAZY);
00715               else
00716                 unload = ACE_BIT_DISABLED (this->unload_policy_,
00717                                            ACE_DLL_UNLOAD_POLICY_LAZY);
00718             }
00719         }
00720 
00721       if (dll_handle->close (unload) != 0)
00722         {
00723           if (ACE::debug ())
00724             ACE_ERROR ((LM_ERROR,
00725                         ACE_LIB_TEXT ("ACE_DLL_Manager::unload error.\n")));
00726 
00727           return -1;
00728         }
00729     }
00730   else
00731     {
00732       if (ACE::debug ())
00733         ACE_ERROR ((LM_ERROR, 
00734                     ACE_LIB_TEXT ("ACE_DLL_Manager::unload_dll called with ")
00735                     ACE_LIB_TEXT ("null pointer.\n")));
00736 
00737       return -1;
00738     }
00739 
00740   return 0;
00741 }

void ACE_DLL_Manager::unload_policy u_long  unload_policy  ) 
 

Set the per-process UNLOAD_POLICY. If the policy is changed from LAZY to EAGER, then it will also unload any dlls with zero refcounts.

Definition at line 596 of file DLL_Manager.cpp.

References ACE_BIT_DISABLED, ACE_BIT_ENABLED, ACE_GUARD, ACE_TRACE, current_size_, handle_vector_, ACE_DLL_Handle::refcount(), and unload_policy_.

00597 {
00598   ACE_TRACE ("ACE_DLL_Manager::unload_policy");
00599   ACE_MT (ACE_GUARD (ACE_Thread_Mutex, ace_mon, this->lock_));
00600 
00601   u_long old_policy = this->unload_policy_;
00602   this->unload_policy_ = unload_policy;
00603 
00604   // If going from LAZY to EAGER or from PER_DLL to PER_PROCESS|EAGER,
00605   // call close(1) on all the ACE_DLL_Handle objects with refcount == 0
00606   // which will force those that are still loaded to be unloaded.
00607   if (this->handle_vector_)
00608     if (( ACE_BIT_ENABLED (old_policy, ACE_DLL_UNLOAD_POLICY_LAZY) &&
00609           ACE_BIT_DISABLED (this->unload_policy_, ACE_DLL_UNLOAD_POLICY_LAZY) ) ||
00610         ( ACE_BIT_DISABLED (this->unload_policy_, ACE_DLL_UNLOAD_POLICY_LAZY) &&
00611           ACE_BIT_ENABLED (old_policy, ACE_DLL_UNLOAD_POLICY_PER_DLL) &&
00612           ACE_BIT_DISABLED (this->unload_policy_, ACE_DLL_UNLOAD_POLICY_PER_DLL) ))
00613       {
00614         for (int i = this->current_size_ - 1; i >= 0; i--)
00615           {
00616             if (this->handle_vector_[i] &&
00617                 this->handle_vector_[i]->refcount () == 0)
00618               this->handle_vector_[i]->close (1);
00619           }
00620       }
00621 }

u_long ACE_DLL_Manager::unload_policy void   )  const
 

Returns the current per-process UNLOAD_POLICY.

Definition at line 589 of file DLL_Manager.cpp.

References ACE_TRACE, and unload_policy_.

00590 {
00591   ACE_TRACE ("ACE_DLL_Manager::unload_policy");
00592   return this->unload_policy_;
00593 }


Friends And Related Function Documentation

friend class ACE_Framework_Repository [friend]
 

Definition at line 187 of file DLL_Manager.h.

friend class ACE_Object_Manager [friend]
 

Definition at line 188 of file DLL_Manager.h.


Member Data Documentation

int ACE_DLL_Manager::current_size_ [private]
 

Current number of handles.

Definition at line 250 of file DLL_Manager.h.

Referenced by close(), find_dll(), open_dll(), and unload_policy().

ACE_DLL_Handle** ACE_DLL_Manager::handle_vector_ [private]
 

Vector containing all loaded handle objects.

Definition at line 247 of file DLL_Manager.h.

Referenced by close(), find_dll(), open(), open_dll(), and unload_policy().

ACE_DLL_Manager * ACE_DLL_Manager::instance_ = 0 [static, private]
 

Pointer to a process-wide .

Definition at line 464 of file DLL_Manager.cpp.

Referenced by close_singleton(), and instance().

int ACE_DLL_Manager::total_size_ [private]
 

Maximum number of handles.

Definition at line 253 of file DLL_Manager.h.

u_long ACE_DLL_Manager::unload_policy_ [private]
 

Unload strategy.

Definition at line 256 of file DLL_Manager.h.

Referenced by unload_policy().


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:21:39 2006 for ACE by doxygen 1.3.6