excl.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #! /usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2011-2015 (ita)
  4. """
  5. Prevents link tasks from executing in parallel. This can be used to
  6. improve the linker execution, which may use a lot of memory.
  7. The variable 'MAX' represents the tasks able to run
  8. concurrently (just one by default). The variable 'count'
  9. is the amount of tasks of one kind being executed.
  10. Different constraints can be enforced by changing the scope
  11. of some variables. Remember that:
  12. * the counter could be set on the class level
  13. * the MAX amount of concurrent tasks can be more than 1
  14. """
  15. from waflib.Utils import threading
  16. from waflib import Task
  17. lock = threading.Lock()
  18. count = 0
  19. MAX = 1
  20. def make_exclusive(cls):
  21. old_runnable_status = cls.runnable_status
  22. def runnable_status(self):
  23. global count, lock, MAX
  24. ret = Task.ASK_LATER
  25. if count >= MAX:
  26. return ret
  27. self.m1_excl = getattr(self, 'm1_excl', 0) + 1
  28. ret = old_runnable_status(self)
  29. self.m1_excl -= 1
  30. if ret == Task.RUN_ME and not self.m1_excl:
  31. lock.acquire()
  32. count += 1
  33. lock.release()
  34. return ret
  35. cls.runnable_status = runnable_status
  36. old_run = cls.run
  37. def run(self):
  38. global count, lock
  39. try:
  40. self.m2_excl = getattr(self, 'm2_excl', 0) + 1
  41. ret = old_run(self)
  42. finally:
  43. self.m2_excl -= 1
  44. if not self.m2_excl:
  45. lock.acquire()
  46. count -= 1
  47. lock.release()
  48. return ret
  49. cls.run = run
  50. for x in 'cprogram cxxprogram cshlib cxxshlib cstlib cxxstlib fcprogram fcshlib fcstlib'.split():
  51. if x in Task.classes:
  52. make_exclusive(Task.classes[x])