forked from quantcast/promise
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Corrected minor race condition in Get()
Occasionally, while one process executing on one CPU was calling `Complete()` and another was calling `Get()`, the one calling `Get()` would observe the value of the `state` as `PENDING`. During this time, another process on another CPU would invoke `Complete()` which would change the state and update the waitgroup as `Done()`. After this occurred, the earlier goroutine would continue and invoke `Wait()` on the waitgroup — whose state would thereafter never change. This has been corrected, by a two phase approach. First, all access to the state has been moved to the `atomic` package so it is delegated to the `CMPXCHG` and `CMOVE` instructions — ensuring that the status of the state was stable during the examination. Second, the waitgroup has been replaced with a condition variable which uses the mutex central to the promise. Callers of the `Get()` method now lock the promise before entering the wait state (which unlocks the promise), ensuring the promise MUST be in a stable state in order to wait on it. Resolves: quantcast#3
- Loading branch information
Showing
2 changed files
with
81 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters