The present invention provides a method and apparatus in a data processing system for managing access to objects (called an element) in a collection of objects (called a container) in a high-concurrency environment. A request is received from a requester for an element's mutex given an identifier for the element. This mutex can be used as a mutex in a synchronized block to lock the object during that synchronized block. To compute the mutex for an element, the first instance seen by the container of the element's identifier is used. To do this, a determination is made as to whether there is already an instance of the identifier with the same value in a value set. If so, that value is returned to the requester for use as a mutex. If not, the current identifier instance is saved in the value set and returned. This allows the object to be locked prior to the existence of the object.
|
1. A method for multithreaded execution in a data processing system for managing access to objects in a container by way of synchronized blocks, the method comprising:
using an identifier object to retrieve a mutex object from a value set, wherein the identifier object has a value that distinguishes the mutex object from other objects in the value set and that also distinguishes a desired object from other objects in the container; in response to retrieving the mutex object from the value set, entering a synchronized block that locks the mutex object; while in the synchronized block that locks the mutex object, accessing the container to determine whether the desired object is present within the container; while in the synchronized block that locks the mutex object, in response to a determination that the desired object is not within the container, producing the desired object from an alternative source for insertion into the container; while in the synchronized block that locks the mutex object, accessing the desired object from the container; and exiting the synchronized block that locks the mutex object, whereby mutual exclusion with respect to accessing the desired object is maintained regardless or whether the desired object is present with the container.
13. A data processing system for managing access to objects in a container by way of synchronized blocks comprising:
means for using an identifier object to retrieve a mutex object from a value set, wherein the identifier object has a value that distinguishes the mutex object from other objects in the value set and that also distinguishes a desired object from other objects in the container; means, responsive to retrieving the mutex object from the value set, for entering a synchronized block that locks the mutex object; means, operative while in the synchronized block that locks the mutex object, for accessing the containew to determine whether the desired object is present within the container; means, operative while in the synchronized block that locks the mutex object and responsive to a determination that the desired object is not within the container, for producing the desired object from an alternative source for insertion into the container; means, operative while in the synchronized block that locks the mutex object, for accessing the desired object form the container; and means for exiting the synchronized block that locks the mutex object, whereby mutual exclusion with respect to accessing the desired object is maintained regardless of whether the desired object is present with the container.
7. A multithreaded computer program product for managing access to objects in a container by way of synchronized blocks, the computer program product being in a computer-readable medium and comprising instructions that, when executed by a computer, cause the computer to perform actions that include:
using an identifier object to retrieve a mutex object from a value set, wherein the identifier object has a value that distinguishes the mutex object from other objects in the value set and that also distinguishes a desired object from other objects in the container; in response to retrieving the mutex object from the value set, entering a synchronized block that locks the mutex object; while in the synchronized block that locks the mutex object, accessing the container to determine whether the desired object is present within the container; while in the synchronized block that locks the mutex object, in response to a determination that the desired object is not within the container, producing the desired object from an alternative source for insertion into the container; while in the synchronized block that locks the mutex object, accessing the desired object from the container; and exiting the synchronized block that locks the mutex object, whereby mutual exclusion with respect to accessing the desired object is maintained regardless of whether the desired object is present within the container.
2. The method of
determining whether any mutex object that is associated with the value of the identifier object exists within the value set; and in response to a determination that no mutex object that is associated with the value of the identifier object exists within the value set, creating the mutex object for insertion into the value set.
3. The method of
while in the first synchronized block, entering a second synchronized block that locks the container; while in the second synchronized block, retrieving the desired object from the container; and exiting the second synchronized block.
6. The method of
8. The computer program product of
determining whether any mutex object that is associated with the value of the identifier object exists within the value set; and in response to a determination that no mutex object that is associated with the value of the identifier object exists within the value set, creating the mutex object for insertion into the container.
9. The computer program product of
while in the first synchronized block, entering a second synchronized block that locks the container; while in the second synchronized block, retrieving the desired object from the container; and exiting the second synchronized block.
12. The computer program product of
14. The data processing system of
means for determining whether any mutex object that is associated with the value of the identifier object exists within the value set; and means, responsive to a determination that no mutex object that is associated with the value of the identifier object exists within the value set, for creating the mutex object for insertion into the value set.
15. The data processing system of
means, operative while in the first synchronized block, for entering a second synchronized block that locks the container; means, operative while in the second synchronized block, for retrieving the desired object from the container; and means for exiting the second synchronized block.
18. The data processing system of
|
The present invention is related to applications entitled METHOD AND APPARATUS FOR MANAGING INTERNAL CACHES AND EXTERNAL CACHES IN A DATA PROCESSING SYSTEM, Ser. No. 09/359,275 now U.S. Pat. No. 6,507,891; METHOD AND APPARATUS FOR CACHE COORDINATION FOR MULTIPLE ADDRESS SPACES, Ser. No. 09/359,270 now U.S. Pat. No. 6,615,235; METHOD AND APPARATUS FOR INVALIDATING DATA IN A CACHE, Ser. No. 09/359,277 now U.S. Pat. No. 6,584,598; METHOD AND APPARATUS FOR AGGRESSIVELY RENDERING DATA IN A DATA PROCESSING SYSTEM, Ser. No. 09/359,279 now U.S. Pat. No. 6,557,076; and A METHOD AND APPARATUS FOR CACHING CONTENT IN A DATA PROCESSING SYSTEM WITH FRAGMENT GRANULARITY, Ser. No. 09/359,279 now U.S. Pat. No. 6,457,103; all of which are filed even date hereof, assigned to the same assignee, and incorporated herein by reference.
1. Technical Field
The present invention relates generally to an improved data processing system and in particular to a method and apparatus for locking objects with high concurrency. Still more particularly, the present invention provides a method and apparatus for locking objects in a Java virtual machine with high concurrency.
2. Description of Related Art
Java™ is an object oriented programming language and environment focusing on defining data as objects and the methods that may be applied to those objects. Java supports only a single inheritance, meaning that each class can inherit from only one other class at any given time. Java also allows for the creation of totally abstract classes known as interfaces, which allow the defining of methods that may be shared with several classes without regard for how other classes are handling the methods. Java provides a mechanism to distribute software and extends the capabilities of a Web browser because programmers can write an applet once and the applet can be run on any Java enabled machine on the Web.
The Java virtual machine (JVM) is a virtual computer component that resides only in memory. The JVM allows Java programs to be executed on different platforms as opposed to only the one platform for which the code was compiled. Java programs are compiled for the JVM. In this manner Java is able to support applications for many types of data processing systems, which may contain a variety of central processing units and operating systems architectures. To enable a Java application to execute on different types of data processing systems, a compiler typically generates an architecture-neutral file format--the compiled code is executable on many processors, given the presence of the Java run time system. The Java compiler generates bytecode instructions that are non-specific to particular computer architectures. A bytecode is a machine independent code generated by the Java compiler and executed by a Java interpreter. A Java interpreter is a module in the JVM that alternatively decodes and executes a bytecode or bytecodes. These bytecode instructions are designed to be easy to interpret on any machine and easily translated on the fly into native machine code.
A development environment, such as the Java Development Kit (JDK) available from Sun Microsystems, Inc., may be used to build Java bytecode from Java language source code and libraries. This Java bytecode may be stored as a Java application or applet on a Web Server, where it can be downloaded over a network to a user's machine and executed on a local JVM.
A mutex is the thing that is locked. A lock is the state of holding the mutex. Locking is the act of getting a lock on the mutex.
Each object in a JVM is associated with a "mutex" for locking. This locking is used to coordinate multi-threaded access to an object. With respect to accessing objects in a JVM, Java has limitations on how objects are locked. In particular, synchronization blocks are employed to lock objects instead of explicit locking and unlocking of objects. This implies that a lock must be obtained and released in a contiguous block of code. Also, within Java a mechanism is absent to see if an object is locked. This implies that the only way to see if an object is locked it to lock it. Existing high concurrency mechanisms (e.g. cache managers) are implemented without these restrictions. Therefore, it would be advantageous to have a method and apparatus for providing high-concurrency locking mechanism for objects in a Java environment.
The present invention provides a method and apparatus in a data processing system for managing access to objects (called an element) in a collection of objects (called a container) in a high-concurrency environment. A request is received from a requester for an element's mutex given an identifier for the element. This mutex can be used as a mutex in a synchronized block to lock the object during that synchronized block.
To compute the mutex for an element, the first instance seen by the container of the element's identifier is used. To do this, a determination is made as to whether there is already an instance of the identifier with the same value in a value set. If so, that value is returned to the requester for use as a mutex. If not, the current identifier instance is saved in the value set and returned. This allows the object to be locked prior to the existence of the object.
The novel features characteristic of the invention are set forth in the appended claims. The invention itself, however, as well as a preferred mode of use, further objectives and advantages thereof, will best be understood by reference to the following detailed description of an illustrative embodiment when read in conjunction with the accompanying drawings, wherein:
With reference now to the figures and in particular with reference to
An operating system runs on processor 202 and is used to coordinate and provide control of various components within data processing system 200 in FIG. 2. The operating system may be a commercially available operating system such as OS/2, which is available from International Business Machines Corporation. "OS/2" is a trademark of International Business Machines Corporation. An object oriented programming system such as Java may run in conjunction with the operating system and provides calls to the operating system from Java programs or applications executing on data processing system 200. "Java" is a trademark of Sun Microsystems, Inc. Instructions for the operating system, the object-oriented operating system, and applications or programs are located on storage devices, such as hard disk drive 226, and may be loaded into main memory 204 for execution by processor 202.
Those of ordinary skill in the art will appreciate that the hardware in
For example, data processing system 200, if optionally configured as a network computer, may not include SCSI host bus adapter 212, hard disk drive 226, tape drive 228, and CD-ROM 230, as noted by dotted line 232 in
The depicted example in FIG. 2 and above-described examples are not meant to imply architectural limitations. For example, data processing system 200 also may be a notebook computer or hand held computer in addition to taking the form of a PDA. Data processing system 200 also may be a kiosk or a Web appliance.
With reference now to
Runtime data areas 304 contain native method stacks 314, Java stacks 316, PC registers 318, method area 320, and heap 322. These different data areas represent the organization of memory needed by JVM 300 to execute a program.
Java stacks 316 are used to store the state of Java method invocations. When a new thread is launched, the JVM creates a new Java stack for the thread. The JVM performs only two operations directly on Java stacks: it pushes and pops frames. A thread's Java stack stores the state of Java method invocations for the thread. The state of a Java method invocation includes its local variables, the parameters with which it was invoked, its return value, if any, and intermediate calculations. Java stacks are composed of stack frames. A stack frame contains the state of a single Java method invocation. When a thread invokes a method, the JVM pushes a new frame onto the Java stack of the thread. When the method completes, the JVM pops the frame for that method and discards it. A JVM does not have any registers for holding intermediate values; any Java instruction that requires or produces an intermediate value uses the stack for holding the intermediate values. In this manner, the Java instruction set is well-defined for a variety of platform architectures.
PC registers 318 are used to indicate the next instruction to be executed. Each instantiated thread gets its own pc register (program counter) and Java stack. If the thread is executing a JVM method, the value of the pc register indicates the next instruction to execute. If the thread is executing a native method, then the contents of the pc register are undefined.
Native method stacks 314 store the state of invocations of native methods. The state of native method invocations is stored in an implementation-dependent way in native method stacks, registers, or other implementation-dependent memory areas. In some JVM implementations, native method stacks 314 and Java stacks 316 are combined.
Method area 320 contains class data while heap 322 contains all instantiated objects. The JVM specification strictly defines data types and operations. Most JVM implementations choose to have one method area and one heap, each of which are shared by all threads running inside the JVM. When the JVM loads a class file, it parses information about a type from the binary data contained in the class file. It places this type information into the method area. Each time a class instance or array is created, the memory for the new object is allocated from heap 322. JVM 300 includes an instruction that allocates memory space within the memory for heap 322 but includes no instruction for freeing that space within the memory. Memory management 324 in the depicted example manages memory space within the memory allocated to heap 322. Memory management 324 may include a garbage collector which automatically reclaims memory used by objects that are no longer referenced by an application. Additionally, a garbage collector also may move objects to reduce heap fragmentation.
The present invention provides a method, apparatus, and instructions for providing high concurrency client-locking in Java. The mechanism of the present invention supports a collection of objects, called elements, in a container object in which a client or requester may use a Java synchronization block to lock a specific element. This synchronization block can spans multiple methods on the container.
The client asks the container for a mutex using the getMutex method to be used in this synchronization block. The mechanism of the present invention for computing a mutex exploits the fact that the String.hashCode and String.equals methods in Java are based on a string's value instead of its identity. This fact allows a value set to be used where membership is based on value instead of identity by calling the instance's hashcode and equals methods. The value set contains a plurality of mutexes for elements. An element's mutex is the first instance of the element's identifier seen by the container. During a getMutex method, if an instance of an element's identifier is not found in the value set, the instance of the element's identifier provided as a getMutex method parameter becomes the mutex for the element and is put in the value set. This mutex may be used to lock elements and allows an element to be locked prior to the existence of the element in the collection.
The locking mechanism for the present invention may be applied to various containers in a JVM. The multithread concurrency design principles for a container are:
(1) It is OK for a thread holding an element lock to wait on the container lock.
(2) It is not OK for a thread holding the container lock to wait on any lock other than the container lock.
(3) In some cases, the client needs to hold an element lock across container method calls (e.g. while producing an element). This should block other clients wanting to use the element, but should not block any other clients. A client cannot hold a lock on the entire container.
(4) In other cases, the client only needs to hold a lock during the execution of an individual method on the container.
The following multithread concurrency design supports the above principles efficiently:
(1) The getMutex method is used to get a mutex for locking elements. This method allows an element to be locked prior to the existence of an element.
(2) When the client wants to hold a lock on an element across multiple methods, it gets a lock on the element's ID mutex which is obtained via the getMutex method, which is described in more detail below.
(3) When the client wants to hold a lock only during a single method, the method is simply called.
(4) For implementation of methods that operate on the container's global state, synchronizion uses the container as a mutex because these take very short time intervals.
With reference now to
In
In the depicted example, client A thread 418, client B thread 420, and client C thread 422 concurrently request access to elements within container 400. These client threads send a request in the form of a string in which the string contains the identifier for the element. Each thread using its own element ID as a mutex is not possible because the ID is a string and each client may create a new instance each time. For correct synchronization, the same instance must be used for a mutex by all clients, so the first ID seen by the container for an element is saved in the value set and returned to all client threads as a mutex for the element.
Client A thread 418 request element 2. The element may or may not be located in the container. An element is in the container only if a client has previously added it to the container. The method described here allows the element to be locked even if it does not yet exist--as long as an identifier value has been determined for it. For example, an element may have an ID value in value set 402 but is absent from hashtable 404 because the element has been locked by a thread but not yet added by that thread.
In the example of
Concurrently with the other two threads, client C thread 422 in this example sends a request including an element identifier for element 3. Client C thread is not queued waiting on either client A or B, because a different mutex is used. Instead, client C thread can access element 3 concurrently with clients A and B access element 2.
With reference now to
Using the element itself is not possible, because the get method would have to be done prior to the synchronize block, and the element could be removed from the container during the window between the get method and the synchronize block. A client using its own ID is not possible, because the ID is a string and a new instance of it may be created every time.
With reference now to
Line 702 in code 700 will lock the entire value set while the mutex is being identified or retrieved. This does not limit concurrency because it happens is a very short period of time. Line 704 sets the mutex equal to the identifier that was in the ValueSet having an equal value to the identifier that was supplied by the requesting client as an input parameter to the getMutex method. In line 706, a determination is made as to whether there was an entry present for the value in the value set. If an entry was not present, then in line 708 an entry is added to the value set with the element identifier, which is then returned in line 710 to be used as the mutex for the element. If an entry was present, then in line 712 the mutex found in the value set is returned to be used as the mutex for the element.
With reference now to
Turning next to
It is important to note that while the present invention has been described in the context of a fully functioning data processing system, those of ordinary skill in the art will appreciate that the processes of the present invention are capable of being distributed in the form of a computer readable medium of instructions and a variety of forms and that the present invention applies equally regardless of the particular type of signal bearing media actually used to carry out the a distribution. Examples of computer readable media include recordable-type media such a floppy disc, a hard disk drive, a RAM, and CD-ROMs and transmission-type media such as digital and analog communications links.
The description of the present invention has been presented for purposes of illustration and description, but is not intended to be exhaustive or limited to the invention in the form disclosed. Many modifications and variations will be apparent to those of ordinary skill in the art. For example, although the depicted examples are implemented in Java, the processes of the present invention may be applied to other types of interpretive languages and even to non-interpretive languages using object oriented paradigms. The embodiment was chosen and described in order to best explain the principles of the invention, the practical application, and to enable others of ordinary skill in the art to understand the invention for various embodiments with various modifications as are suited to the particular use contemplated.
Copeland, George Prentice, McClain, Matthew D.
Patent | Priority | Assignee | Title |
7340478, | Jan 07 2005 | International Business Machines Corporation | Method and apparatus for implementing container managed uses, ownerships, and references in an enterprise JavaBean environment |
7383368, | Sep 25 2003 | Dell Products L.P. | Method and system for autonomically adaptive mutexes by considering acquisition cost value |
7958172, | Jan 07 2005 | International Business Machines Corporation | Apparatus for implementing container managed uses, ownerships, and references in an enterprise javabean environment |
8037479, | Jan 07 2005 | Meta Platforms, Inc | Implementing container managed batch jobs in an enterprise java bean environment |
8281060, | Sep 27 2006 | Intel Corporation | Virtual heterogeneous channel for message passing |
Patent | Priority | Assignee | Title |
5057996, | Jun 29 1989 | HEWLETT-PACKARD DEVELOPMENT COMPANY, L P | Waitable object creation system and method in an object based computer operating system |
5875461, | Apr 03 1997 | Oracle America, Inc | Method of synchronizing one of the objects with one of the threads at a time |
6029190, | Sep 24 1997 | Sony Corporation; Sony Pictures Entertainment, Inc | Read lock and write lock management system based upon mutex and semaphore availability |
6141794, | Oct 16 1998 | Oracle America, Inc | System and method for synchronizing access to shared variables in a virtual machine in a digital computer system |
6173442, | Feb 05 1999 | Sun Microsystems, Inc | Busy-wait-free synchronization |
6510437, | Nov 04 1996 | Sun Microsystems, Inc. | Method and apparatus for concurrent thread synchronization |
Executed on | Assignor | Assignee | Conveyance | Frame | Reel | Doc |
Jul 02 1999 | COPELAND, GEORGE PRENTICE | International Business Machines Corporation | ASSIGNMENT OF ASSIGNORS INTEREST SEE DOCUMENT FOR DETAILS | 010123 | /0618 | |
Jul 02 1999 | MCCLAIN, MATTHEW D | International Business Machines Corporation | ASSIGNMENT OF ASSIGNORS INTEREST SEE DOCUMENT FOR DETAILS | 010123 | /0618 | |
Jul 22 1999 | International Business Machines Corporation | (assignment on the face of the patent) | / | |||
Aug 17 2011 | International Business Machines Corporation | Google Inc | ASSIGNMENT OF ASSIGNORS INTEREST SEE DOCUMENT FOR DETAILS | 026894 | /0001 | |
Sep 29 2017 | Google Inc | GOOGLE LLC | CHANGE OF NAME SEE DOCUMENT FOR DETAILS | 044127 | /0735 |
Date | Maintenance Fee Events |
Jul 13 2004 | ASPN: Payor Number Assigned. |
Sep 19 2007 | M1551: Payment of Maintenance Fee, 4th Year, Large Entity. |
Sep 23 2011 | M1552: Payment of Maintenance Fee, 8th Year, Large Entity. |
Dec 22 2015 | M1553: Payment of Maintenance Fee, 12th Year, Large Entity. |
Date | Maintenance Schedule |
Jun 22 2007 | 4 years fee payment window open |
Dec 22 2007 | 6 months grace period start (w surcharge) |
Jun 22 2008 | patent expiry (for year 4) |
Jun 22 2010 | 2 years to revive unintentionally abandoned end. (for year 4) |
Jun 22 2011 | 8 years fee payment window open |
Dec 22 2011 | 6 months grace period start (w surcharge) |
Jun 22 2012 | patent expiry (for year 8) |
Jun 22 2014 | 2 years to revive unintentionally abandoned end. (for year 8) |
Jun 22 2015 | 12 years fee payment window open |
Dec 22 2015 | 6 months grace period start (w surcharge) |
Jun 22 2016 | patent expiry (for year 12) |
Jun 22 2018 | 2 years to revive unintentionally abandoned end. (for year 12) |