Synchronization Internals in .NET framework

Paint QR Code in .NET framework Synchronization Internals
Synchronization Internals
Make QR-Code In VS .NET
Using Barcode printer for .NET framework Control to generate, create Denso QR Bar Code image in .NET framework applications.
As we alluded to earlier, thread synchronization primitives such as a Monitor needs to carry with it a certain amount of bookkeeping information to know if the monitor is locked, the owning thread, waiting threads, and so on In this part of the chapter, we take a look at how the CLR manages lock information
Denso QR Bar Code Reader In Visual Studio .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
Synchronization Internals
Printing Bar Code In VS .NET
Using Barcode printer for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.
Object Header
Bar Code Recognizer In VS .NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
As briefly mentioned in 2, CLR Fundamentals, each object on the managed heap has an associated object header that contains a plethora of information regarding the state of the object Examples of information stored in the object header are hash codes, lock information, sync block index, and so on Figure 6-3 shows an example of a managed heap illustrating how each object has an associated object header The sheer amount of potential information that can be stored in the object is greater than the size of object header itself (machine-word-size) The idea behind this concept is that any given object may (or may not) require all the information depending on the execution flow As long as the execution flow (for example, just getting the hash code of the object) stays within the limits of the size of the object header, it will be stored in the object header What happens if additional information needs to be stored when the header is full In these cases, the CLR employs what is known as header inflation by creating an instance of a separate data structure called a sync block and copies all information currently stored in the object header into the sync block The sync block is stored in non GC memory and accessed through an index into a sync block table For the object to know how to locate the sync block associated with the particular object, the object header is now changed to contain the sync table index where the sync block is located Figure 6-4 illustrates the concept of an object header and sync block Step 1 in Figure 6-4 shows an object that has an object header with the value 0x0f78734a In step 2, another piece of information needs to be stored in the object header but unfortunately doesn t fit As a result, the CLR inflates the header by creating a sync block that is stored at index 1 in the sync block table The object header is then set to the value of 0x08000001 to indicate the header now contains a sync block index of 1 How does the CLR know whether the information in the object header refers to a sync block index or simply other data stored in the header
QR Code Drawer In C#
Using Barcode drawer for Visual Studio .NET Control to generate, create QR Code JIS X 0510 image in .NET applications.
6 SYNCHRONIZATION
Denso QR Bar Code Encoder In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create Denso QR Bar Code image in ASP.NET applications.
Current allocation pointer 4 4 Size 4 4 Size 4 4 Size
Creating QR Code JIS X 0510 In VB.NET
Using Barcode creator for .NET Control to generate, create QR Code JIS X 0510 image in .NET applications.
Object Header
Creating European Article Number 13 In .NET
Using Barcode creation for .NET Control to generate, create EAN 13 image in Visual Studio .NET applications.
Object Header
ECC200 Generator In .NET Framework
Using Barcode generation for .NET framework Control to generate, create DataMatrix image in VS .NET applications.
Object Header Type Handle
UCC - 12 Drawer In .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create UPCA image in .NET framework applications.
Type Handle
Barcode Creation In Visual Studio .NET
Using Barcode generator for .NET framework Control to generate, create barcode image in VS .NET applications.
Type Handle
ISBN - 10 Generation In Visual Studio .NET
Using Barcode generation for .NET Control to generate, create ISBN image in Visual Studio .NET applications.
Object Instance
Recognizing UPCA In .NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in VS .NET applications.
Object Instance
Barcode Scanner In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
Object Instance
Drawing Code 128C In Java
Using Barcode printer for Java Control to generate, create Code 128A image in Java applications.
Committed/Reserved Space
European Article Number 13 Generation In .NET
Using Barcode drawer for ASP.NET Control to generate, create GS1 - 13 image in ASP.NET applications.
Figure 6-3 Example of managed heap and object layouts
Print UPCA In Java
Using Barcode printer for Java Control to generate, create UPC Symbol image in Java applications.
6
UPC-A Supplement 2 Generation In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create UPC-A Supplement 2 image in ASP.NET applications.
Synchronization
DataMatrix Printer In Java
Using Barcode encoder for Java Control to generate, create ECC200 image in Java applications.
Step 1: No sync block allocated Object pointer 4 Object Header 0x0f78734a 4 Type Handle
Generate Code 3 Of 9 In Java
Using Barcode generation for Java Control to generate, create USS Code 39 image in Java applications.
Object Instance
Step 2: Sync block allocated Object pointer 4 Object Header 0x08000001 4 Type Handle
Object Instance
Sync Block Table 0 1 2 X Sync Block
Figure 6-4 Object header and sync blocks The answer lays in the organization of the object header bits If the 0x08000000 mask is set in the object header, the rest of the object header contains either a hash code or a sync block index We can further narrow it down by determining if the 0x04000000 mask is also set, in which case the header contains a hash code If that mask is not set, the rest of the header is a sync block index As mentioned before, the object header can store quite a lot of different information To get an in-depth view of the different types of information, please see the Rotor source code at the following location:
sscli20\clr\src\vm\syncblkh
Synchronization Internals
Sync Blocks
To take a pragmatic look at sync blocks, Listing 6-3 is used to help clarify the importance of the sync block and how it relates to locks Listing 6-3 Simple example of an acquired lock
using System; using SystemText; using SystemThreading; namespace AdvancedNETDebugging6 { class Simple { static void Main(string[] args) { Simple s = new Simple(); sRun(); } public void Run() { thisGetHashCode(); ConsoleWriteLine("Press any key to acquire lock"); ConsoleReadLine(); MonitorEnter(this); ConsoleWriteLine("Press any key to release lock"); ConsoleReadLine(); MonitorExit(this); ConsoleWriteLine("Press any key to exit"); ConsoleReadLine(); }