Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






General Best Practices

Introduction

A dynamic-link library (DLL) is shared code and data that an application can load and call at run time. A DLL typically exports a set of routines for applications to use and contains other routines for internal use. This technique enables code reuse by allowing multiple applications to share common functionality in a library and load it on demand. Advantages of using DLLs include reduced code footprint, lower memory utilization due to single-copy-sharing, flexible development and testing, modularity, and functional isolation.

Creating DLLs presents a number of challenges for developers. DLLs do not have system-enforced versioning. When multiple versions of a DLL exist on a system, the ease of being overwritten coupled with the lack of a versioning schema creates dependency and API conflicts. Complexity in the development environment, the loader implementation, and the DLL dependencies has created fragility in load order and application behavior. Lastly, many applications rely on DLLs and have complex sets of dependencies that must be honored for the applications to function properly. This document provides guidelines for DLL developers to help in building more robust, portable, and extensible DLLs.

The three main components of the DLL development model are:

· The library loader. DLLs often have complex interdependencies that implicitly define the order in which they must be loaded. The library loader efficiently analyzes these dependencies, calculates the correct load order, and loads the DLLs in that order.

· The DllMain entry-point function. This function is called by the loader when it loads or unloads a DLL. The loader serializes calls to DllMain so that only a single DllMain function is run at a time. For more information, see http://msdn.microsoft.com/library/en-us/dllproc/base/dllmain.asp.

· The loader lock. This is a process-wide synchronization primitive that the loader uses to ensure serialized loading of DLLs. Any function that must read or modify the per-process library-loader data structures must acquire this lock before performing such an operation. The loader lock is recursive, which means that it can be acquired again by the same thread.

 

Figure 1 illustrates what happens when a library is loaded.

Figure 1. What Happens When a Library Is Loaded

Improper synchronization within DllMain can cause an application to deadlock or access data or code in an uninitialized DLL. Calling certain functions from within DllMain causes such problems.

General Best Practices

DllMain is called while the loader-lock is held. Therefore, significant restrictions are imposed on the functions that can be called within DllMain. As such, DllMain is designed to perform minimal initialization tasks, by using a small subset of the Microsoft® Windows® API. You cannot call any function in DllMain that directly or indirectly tries to acquire the loader lock. Otherwise, you will introduce the possibility that your application deadlocks or crashes. An error in a DllMain implementation can jeopardize the entire process and all of its threads.



The ideal DllMain would be just an empty stub. However, given the complexity of many applications, this is generally too restrictive. A good rule of thumb for DllMain is to postpone as much initialization as possible. Lazy initialization increases robustness of the application because this initialization is not performed while the loader lock is held. Also, lazy initialization enables you to safely use much more of the Windows API.

Some initialization tasks cannot be postponed. For example, a DLL that depends on a configuration file should fail to load if the file is malformed or contains garbage. For this type of initialization, the DLL should attempt the action and fail quickly rather than waste resources by completing other work.

You should never perform the following tasks from within DllMain:

· Call LoadLibrary or LoadLibraryEx (either directly or indirectly). This can cause a deadlock or a crash.

· Synchronize with other threads. This can cause a deadlock.

· Acquire a synchronization object that is owned by code that is waiting to acquire the loader lock. This can cause a deadlock.

· Initialize COM threads by using CoInitializeEx. Under certain conditions, this function can call LoadLibraryEx.

· Call the registry functions. These functions are implemented in Advapi32.dll. If Advapi32.dll is not initialized before your DLL, the DLL can access uninitialized memory and cause the process to crash.

· Call CreateProces. Creating a process can load another DLL.

· Call ExitThread.Exiting a thread during DLL detach can cause the loader lock to be acquired again, causing a deadlock or a crash.

· Call CreateThread. Creating a thread can work if you do not synchronize with other threads, but it is risky.

· Create a named pipe or other named object (Windows 2000 only). In Windows 2000, named objects are provided by the Terminal Services DLL. If this DLL is not initialized, calls to the DLL can cause the process to crash.

· Use the memory management function from the dynamic C Run-Time (CRT). If the CRT DLL is not initialized, calls to these functions can cause the process to crash.

· Call functions in User32.dll or Gdi32.dll. Some functions load another DLL, which may not be initialized.

· Use managed code.

 

The following tasks are safe to perform within DllMain:

· Initialize static data structures and members at compile time.

· Create and initialize synchronization objects.

· Allocate memory and initialize dynamic data structures (avoiding the functions listed above.)

· Set up thread local storage (TLS).

· Open, read from, and write to files.

· Call functions in Kernel32.dll (except the functions that are listed above).

· Set global pointers to NULL, putting off the initialization of dynamic members. In Microsoft Windows Vista™, you can use the one-time initialization functions to ensure that a block of code is executed only once in a multithreaded environment.

 


Date: 2016-03-03; view: 840


<== previous page | next page ==>
Read the text and find in it the words and word combinations from the list, supply them with Russian equivalents. | Deadlocks Caused by Lock Order Inversion
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)