diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2015-01-07 12:32:45 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2015-01-07 12:32:45 +0100 |
commit | 763ef75d12f0ad6e4b79a7df304c7b5f1b5a11f2 (patch) | |
tree | ef3cc767ac84d87d33d587ddce4170f74ba17d49 /git/util.py | |
parent | c86bea60dde4016dd850916aa2e0db5260e1ff61 (diff) | |
download | gitpython-763ef75d12f0ad6e4b79a7df304c7b5f1b5a11f2.tar.gz |
Using a wait-group seems to properly sync the threads for buffer depletion
Diffstat (limited to 'git/util.py')
-rw-r--r-- | git/util.py | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/git/util.py b/git/util.py index 34b09d32..e211ca41 100644 --- a/git/util.py +++ b/git/util.py @@ -12,6 +12,7 @@ import stat import shutil import platform import getpass +import threading # NOTE: Some of the unused imports might be used/imported by others. # Handle once test-cases are back up and running. @@ -32,7 +33,7 @@ from gitdb.util import ( # NOQA __all__ = ("stream_copy", "join_path", "to_native_path_windows", "to_native_path_linux", "join_path_native", "Stats", "IndexFileSHA1Writer", "Iterable", "IterableList", "BlockingLockFile", "LockFile", 'Actor', 'get_user_id', 'assure_directory_exists', - 'RemoteProgress', 'rmtree') + 'RemoteProgress', 'rmtree', 'WaitGroup') #{ Utility Methods @@ -699,3 +700,32 @@ class Iterable(object): raise NotImplementedError("To be implemented by Subclass") #} END classes + + +class WaitGroup(object): + """WaitGroup is like Go sync.WaitGroup. + + Without all the useful corner cases. + By Peter Teichman, taken from https://gist.github.com/pteichman/84b92ae7cef0ab98f5a8 + """ + def __init__(self): + self.count = 0 + self.cv = threading.Condition() + + def add(self, n): + self.cv.acquire() + self.count += n + self.cv.release() + + def done(self): + self.cv.acquire() + self.count -= 1 + if self.count == 0: + self.cv.notify_all() + self.cv.release() + + def wait(self): + self.cv.acquire() + while self.count > 0: + self.cv.wait() + self.cv.release() |