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 2562db8e47..f217ae366c 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
@@ -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
389def lockfile(name, shared=False, retry=True): 390@contextmanager
391def 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
404def 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: