Method and apparatus for automatically managing concurrent access to a shared resource in a multi-threaded programming environment
First Claim
1. A method for sharing a resource between a plurality of threads of a multi-threaded program, the method comprising the steps of:
- (A) causing a first thread of said plurality of threads to perform the steps of (A1) acquiring a resource lock associated with said resource;
(A2) holding said resource lock until detecting that another thread of said plurality of threads requires use of said resource;
(A3) in response to detecting that another thread of said plurality of threads requires use of said resource, performing the steps of (a) finishing any use of said resource;
(b) releasing said resource lock; and
(c) requesting said resource lock.
2 Assignments
0 Petitions
Accused Products
Abstract
A method and apparatus for managing how threads of a multi-threaded computer program share a resource is provided One thread of the program is given priority over other threads of the program by granting to the thread possession of the lock associated with the resource regardless of whether the thread currently requires use of the resource. The other threads are designed to indicate to the priority thread when they require use of the resource. If the priority thread is done using the resource and detects that another thread is waiting to use the resource, the priority thread releases the resource lock for the resource. After releasing the lock for the resource, the priority thread automatically requests the resource lock. After using the resource, any non-priority thread releases the resource lock to the priority thread if the priority thread has requested the resource, without regard to whether any other threads may be waiting for the resource. According to one embodiment, a timer mechanism is used to cause the priority thread to periodically check whether any threads are waiting to use the resource.
93 Citations
23 Claims
-
1. A method for sharing a resource between a plurality of threads of a multi-threaded program, the method comprising the steps of:
-
(A) causing a first thread of said plurality of threads to perform the steps of (A1) acquiring a resource lock associated with said resource;
(A2) holding said resource lock until detecting that another thread of said plurality of threads requires use of said resource;
(A3) in response to detecting that another thread of said plurality of threads requires use of said resource, performing the steps of (a) finishing any use of said resource;
(b) releasing said resource lock; and
(c) requesting said resource lock. - View Dependent Claims (2, 3, 4, 5, 6, 7, 8, 9, 10)
causing a second thread of said plurality of threads to indicate that said second thread desires to use said resource by incrementing a counter; and
causing said first thread to detect that another thread of said plurality of threads requires use of said resource by inspecting said counter.
-
-
3. The method of claim 2 further comprising the steps of:
-
causing said second thread to acquire a counter lock associated with said counter prior to incrementing said counter; and
causing said second thread to release the counter lock associated with said counter after incrementing said counter.
-
-
4. The method of claim 2 further comprising the steps of:
-
acquiring a counter lock associated with said counter;
decrementing said counter in response to said first thread releasing said resource lock; and
releasing said counter lock associated with said counter.
-
-
5. The method of claim 4 wherein said steps of acquiring said counter lock, decrementing said counter, and releasing said counter lock are performed by said first thread.
-
6. The method of claim 4 herein said steps of acquiring said counter lock, decrementing said counter, and releasing said counter lock are performed by said second thread.
-
7. The method of claim 1 further comprising the steps of:
-
initiating a timer;
causing said first thread to determine whether another thread of said plurality of threads requires use of said resource when a predetermined time interval elapses on said timer.
-
-
8. The method of claim 1 wherein:
-
said resource is a connection to a window system; and
the step of finishing any use of said resource includes flushing said connection to said window system.
-
-
9. The method of claim 1 further comprising the step of causing each thread of said plurality of threads other than said first thread to release said resource lock to said first thread after finishing any use of said resource.
-
10. The method of claim 9 further comprising the steps of:
-
causing said first thread to request the resource lock by transmitting a lock request that contains a first key; and
causing each thread of said plurality of threads other than said first thread to release said resource lock in a state that corresponds to said first key.
-
-
11. A method for indicating to a first thread that holds a resource lock associated with a resource that one or more other threads require use of said resource, the method comprising the steps of:
-
initiating a timer;
periodically activating said first thread based on said timer;
when said first thread is activated based on said timer, causing said first thread to determine whether any other thread requires use of said resource by inspecting a value at a memory location; and
causing any thread other than said first thread to update the value at the memory location when said thread requires use of the resource. - View Dependent Claims (12, 13, 14)
causing said first thread to suspend after inspecting said value at said memory location if said value indicates that no other threads require use of said resource;
causing said first thread to release said resource lock after inspecting said value at said memory location if said value indicates that another thread requires use of said resource.
-
-
13. The method of claim 12 further comprising the step of causing said first thread to request said resource lock in response to releasing said resource lock.
-
14. The method of claim 12 wherein:
-
the value at the memory location indicates how many threads other than said first thread are waiting to use said resource; and
the method further comprises the step of updating the value at the memory location in response to said first thread releasing said resource lock to indicate that one less thread is waiting to use said resource.
-
-
15. A computer-readable medium having stored thereon sequences of instructions which, when executed by a processor, cause the processor to manage how a resource is shared between a plurality of threads of a multi-threaded program, the sequences of instructions including instructions for:
-
(A) causing a first thread of said plurality of threads to perform the steps of (A1) acquiring a resource lock associated with said resource;
(A2) holding said resource lock until detecting that another thread of said plurality of threads requires use of said resource;
(A3) in response to detecting that another thread of said plurality of threads requires use of said resource, performing the steps of (a) finishing any use of said resource;
(b) releasing said resource lock; and
(c) requesting said resource lock. - View Dependent Claims (16, 17, 18, 19, 20)
causing said first thread to request the resource lock by transmitting a lock request that contains a first key; and
causing each thread of said plurality of threads other than said first thread to release said resource lock in a state that corresponds to said first key.
-
-
18. The computer-readable medium of claim 15 further comprising instructions for:
-
causing a second thread of said plurality of threads to indicate that said second thread desires to use said resource by incrementing a counter; and
causing said first thread to detect that another thread of said plurality of threads requires use of said resource by inspecting said counter.
-
-
19. The computer-readable medium of claim 18 further comprising instructions for:
-
causing said second thread to acquire a counter lock associated with said counter prior to incrementing said counter; and
causing said second thread to release the counter lock associated with said counter after incrementing said counter.
-
-
20. The computer-readable medium of claim 15 further comprising instructions for:
-
initiating a timer;
causing said first thread to determine whether another thread of said plurality of threads requires use of said resource when a predetermined time interval elapses on said timer.
-
-
21. A computer system comprising:
-
memory storing a resource lock associated with a resource, and a value;
at least one processor executing a multi-threaded program;
said multi-threaded program including a first thread that holds the resource lock associated with the resource, and one or more other threads;
a timer;
said at least one processor being configured to indicate to the first thread that one or more other threads require use of said resource by;
periodically activating said first thread based on said timer;
when said first thread is activated based on said timer, causing said first thread to determine whether any other thread requires use of said resource by inspecting the value;
causing any thread other than said first thread to update the value when said at least one processor is further configured to;
cause said first thread to suspend after inspecting said value at said memory location if said value indicates that no other threads require use of said resource;
cause said first thread to release said resource lock after inspecting said value at said memory location if said value indicates that another thread requires use of said resource.- View Dependent Claims (22, 23)
the value indicates how many threads other than said first thread are waiting to use said resource; and
the at least one processor is further configured to update the value in response to said first thread releasing said resource lock to indicate that one less thread is waiting to use said resource.
-
Specification