summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/lib/bb/utils.py')
-rw-r--r--bitbake/lib/bb/utils.py29
1 files changed, 25 insertions, 4 deletions
diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py
index 0db7e56651..d380a3708c 100644
--- a/bitbake/lib/bb/utils.py
+++ b/bitbake/lib/bb/utils.py
@@ -31,6 +31,7 @@ import subprocess
31import glob 31import glob
32import traceback 32import traceback
33import errno 33import errno
34import signal
34from commands import getstatusoutput 35from commands import getstatusoutput
35from contextlib import contextmanager 36from contextlib import contextmanager
36 37
@@ -412,10 +413,30 @@ def fileslocked(files):
412 for lock in locks: 413 for lock in locks:
413 bb.utils.unlockfile(lock) 414 bb.utils.unlockfile(lock)
414 415
415def lockfile(name, shared=False, retry=True): 416@contextmanager
417def timeout(seconds):
418 def timeout_handler(signum, frame):
419 pass
420
421 original_handler = signal.signal(signal.SIGALRM, timeout_handler)
422
423 try:
424 signal.alarm(seconds)
425 yield
426 finally:
427 signal.alarm(0)
428 signal.signal(signal.SIGALRM, original_handler)
429
430def lockfile(name, shared=False, retry=True, block=False):
416 """ 431 """
417 Use the file fn as a lock file, return when the lock has been acquired. 432 Use the specified file as a lock file, return when the lock has
418 Returns a variable to pass to unlockfile(). 433 been acquired. Returns a variable to pass to unlockfile().
434 Parameters:
435 retry: True to re-try locking if it fails, False otherwise
436 block: True to block until the lock succeeds, False otherwise
437 The retry and block parameters are kind of equivalent unless you
438 consider the possibility of sending a signal to the process to break
439 out - at which point you want block=True rather than retry=True.
419 """ 440 """
420 dirname = os.path.dirname(name) 441 dirname = os.path.dirname(name)
421 mkdirhier(dirname) 442 mkdirhier(dirname)
@@ -428,7 +449,7 @@ def lockfile(name, shared=False, retry=True):
428 op = fcntl.LOCK_EX 449 op = fcntl.LOCK_EX
429 if shared: 450 if shared:
430 op = fcntl.LOCK_SH 451 op = fcntl.LOCK_SH
431 if not retry: 452 if not retry and not block:
432 op = op | fcntl.LOCK_NB 453 op = op | fcntl.LOCK_NB
433 454
434 while True: 455 while True: