The concept of reference counting is well established in computer applications. Many managed languages like Java or any of Microsoft's .NET languages actually manage reference counts within the language itself and use a garbage collector to destroy objects when the reference count reaches zero. In languages like C++, there are helpers like boost::shared_ptr that wrap pointers in an object to provide similar reference counting functionality. These solutions only address reference counting within a single process.
It is often necessary for a process to create an object that will be shared by multiple functional units in multiple processes. In such a scenario, it may be difficult to know when an object can be safely deleted. However, reference counting across multiple processes is complicated for many reasons.
For example, in a single process implementation, a simple global reference counter may be incremented and decremented directly to maintain a reference count. In a multi-process implementation, there is no direct access to a common reference counter. Instead, Inter-Process Communication (IPC) transactions must be used. Shared memory might also be a possibility, but this is not an option if the processes are located in different execution environments. IPC transactions require significantly more overhead than simply updating a global counter, so any efficient implementation must take care to minimize the number of required IPC transactions.
In a multi-process architecture, it is possible for a single process to be abruptly terminated. In order for other processes to continue functioning properly without leaking resources, it is necessary to provide a mechanism for tracking the references owned by each process so that if a single process is abruptly terminated the reference counts can be updated accordingly.
Moreover, in a multi-process architecture, it is possible for a reference to exist in transit between processes. For example, process A owns the only reference to an object and decides to send a copy of the reference to process B. If process A deletes its reference immediately after sending the copy to process B, no process will own the reference for a short period of time, but a reference will exist in transmit between process A and process B. As a result, it is necessary to account for references in transit or provide a mechanism that will ensure process A will not release its reference until process B has received it.
A method and apparatus for reference counting across multiple processes using a global counter. The method includes receiving a request to increment a count and add a reference to a reference list, incrementing the count and updating the reference list with the additional reference, receiving a request to decrement the count and remove a different reference from the reference list, and decrementing the count and updating the reference list by removing the difference reference.
A more detailed understanding may be had from the following description, given by way of example in conjunction with the accompanying drawings wherein:
It is to be understood that the figures and descriptions of embodiments have been simplified to illustrate elements that are relevant for a clear understanding of the invention, while eliminating other elements and steps that are well known in the art and do not facilitate a better understanding of the present invention.
The local counters will increment from 0 to 1, when a message is transmitted to add a process to the global list and increment the global counter. When one of the local counters decrement from 1 to 0, a message is transmitted to remove the process from the global list and decrement the global counter. If a process terminates abruptly and the process is in the global process list, the process is removed from the global process list and the global count is decremented.
When the global count reaches 0, there are no processes that own any references to any object present in the process that owns the global counter. The object can be deleted as long as there is no reference in transit between processes. To ensure there is never a reference in transit between processes when no other process owns a copy of the reference, an additional restriction is added. The additional restriction passes shared references between processes with a synchronous transaction. All messages transmitted to update the global reference counter must be synchronous transactions. Synchronous transactions ensure the process sending the reference will not decrement the reference count before the process receiving the reference has incremented the reference count.
While embodiments of the invention have been described, it will be appreciated that modifications of these embodiments are within the true spirit and scope of the invention. The invention is not limited to any particular element(s) that perform(s) any particular function(s) and some may not necessarily occur in the order shown. For example, in some cases two or more method steps may occur in a different order or simultaneously. Although illustrated in the context of separate functional elements, these functional elements may be embodied in one, or more, integrated circuits (ICs). Similarly, although shown as separate elements, any or all of the elements may be implemented in a stored-program-controlled processor, e.g., a digital signal processor, which executes associated software, e.g., corresponding to one, or more, of the functions. These and other variations of the methods disclosed herein will be readily apparent, especially in view of the description of the method described herein, and are considered to be within the full scope of the invention.
This application claims the benefit of U.S. provisional application No. 61/424,919 filed Dec. 20, 2010, the contents of which is hereby incorporated by reference herein.
Number | Date | Country | |
---|---|---|---|
61424919 | Dec 2010 | US |