diff options
Diffstat (limited to 'bitbake/lib/bb/utils.py')
-rw-r--r-- | bitbake/lib/bb/utils.py | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index 2562db8e47..f217ae366c 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py | |||
@@ -31,6 +31,7 @@ import subprocess | |||
31 | import glob | 31 | import glob |
32 | import traceback | 32 | import traceback |
33 | import errno | 33 | import errno |
34 | import signal | ||
34 | from commands import getstatusoutput | 35 | from commands import getstatusoutput |
35 | from contextlib import contextmanager | 36 | from contextlib import contextmanager |
36 | 37 | ||
@@ -386,10 +387,30 @@ def fileslocked(files): | |||
386 | for lock in locks: | 387 | for lock in locks: |
387 | bb.utils.unlockfile(lock) | 388 | bb.utils.unlockfile(lock) |
388 | 389 | ||
389 | def lockfile(name, shared=False, retry=True): | 390 | @contextmanager |
391 | def timeout(seconds): | ||
392 | def timeout_handler(signum, frame): | ||
393 | pass | ||
394 | |||
395 | original_handler = signal.signal(signal.SIGALRM, timeout_handler) | ||
396 | |||
397 | try: | ||
398 | signal.alarm(seconds) | ||
399 | yield | ||
400 | finally: | ||
401 | signal.alarm(0) | ||
402 | signal.signal(signal.SIGALRM, original_handler) | ||
403 | |||
404 | def lockfile(name, shared=False, retry=True, block=False): | ||
390 | """ | 405 | """ |
391 | Use the file fn as a lock file, return when the lock has been acquired. | 406 | Use the specified file as a lock file, return when the lock has |
392 | Returns a variable to pass to unlockfile(). | 407 | been acquired. Returns a variable to pass to unlockfile(). |
408 | Parameters: | ||
409 | retry: True to re-try locking if it fails, False otherwise | ||
410 | block: True to block until the lock succeeds, False otherwise | ||
411 | The retry and block parameters are kind of equivalent unless you | ||
412 | consider the possibility of sending a signal to the process to break | ||
413 | out - at which point you want block=True rather than retry=True. | ||
393 | """ | 414 | """ |
394 | dirname = os.path.dirname(name) | 415 | dirname = os.path.dirname(name) |
395 | mkdirhier(dirname) | 416 | mkdirhier(dirname) |
@@ -402,7 +423,7 @@ def lockfile(name, shared=False, retry=True): | |||
402 | op = fcntl.LOCK_EX | 423 | op = fcntl.LOCK_EX |
403 | if shared: | 424 | if shared: |
404 | op = fcntl.LOCK_SH | 425 | op = fcntl.LOCK_SH |
405 | if not retry: | 426 | if not retry and not block: |
406 | op = op | fcntl.LOCK_NB | 427 | op = op | fcntl.LOCK_NB |
407 | 428 | ||
408 | while True: | 429 | while True: |