@@ -102,8 +102,13 @@ def __init__(self, prefix, output_path, persistent=False, gui=False):
102102 self ._coverage_files = set ()
103103 assert not (persistent and gui )
104104 self ._create_modelsim_ini ()
105+ # Contains design already optimized, i.e. the optimized design can be reused
105106 self ._optimized_designs = {}
107+ # Contains locks for each library. If locked, a design belonging to the library
108+ # is being optimized and no other design in that library can be optimized at the
109+ # same time (from another thread)
106110 self ._library_locks = {}
111+ # Lock to access the two shared variables above
107112 self ._shared_state_lock = Lock ()
108113
109114 def _create_modelsim_ini (self ):
@@ -216,7 +221,7 @@ def create_library(self, library_name, path, mapped_libraries=None):
216221 os .makedirs (apath )
217222
218223 if not file_exists (path ):
219- proc = Process ([str (Path (self ._prefix ) / "vlib" ), "-unix" , "-type" , "directory" , path ], env = self .get_env ())
224+ proc = Process ([str (Path (self ._prefix ) / "vlib" ), "-unix" , path ], env = self .get_env ())
220225 proc .consume_output (callback = None )
221226
222227 if library_name in mapped_libraries and mapped_libraries [library_name ] == path :
@@ -238,7 +243,7 @@ def _get_mapped_libraries(self):
238243
239244 def _optimize_design (self , config ):
240245 """
241- Return if design shall be optimized.
246+ Return True if design shall be optimized.
242247 """
243248
244249 return config .sim_options .get ("modelsim.three_step_flow" , False )
@@ -309,7 +314,7 @@ def _create_optimize_function(self, config):
309314 return true
310315 }}
311316
312- return False
317+ return false
313318}}
314319""" .format (
315320 vopt_flags = " " .join (vopt_flags )
@@ -364,7 +369,7 @@ def _wait_for_file_lock(library):
364369 log_waiting = True
365370 while (Path (library .directory ) / "_lock" ).exists ():
366371 if log_waiting :
367- LOGGER .debug ("Waiting for %s to be removed" , Path (library .directory ) / "_lock" )
372+ LOGGER .debug ("Waiting for %s to be removed. " , Path (library .directory ) / "_lock" )
368373 log_waiting = False
369374 sleep (0.05 )
370375
@@ -376,13 +381,13 @@ def _acquire_library_lock(self, library, config, design_to_optimize):
376381 library_lock = self ._library_locks [config .library_name ]
377382
378383 if library_lock .locked ():
379- LOGGER .debug ("Waiting for library lock for %s to optimize %s" , config .library_name , design_to_optimize )
384+ LOGGER .debug ("Waiting for library lock for %s to optimize %s. " , config .library_name , design_to_optimize )
380385 # Do not completely block to allow for Ctrl+C
381386 while not library_lock .acquire (timeout = 0.05 ):
382387 pass
383388
384389 self ._wait_for_file_lock (library )
385- LOGGER .debug ("Acquired library lock for %s to optimize %s" , config .library_name , design_to_optimize )
390+ LOGGER .debug ("Acquired library lock for %s to optimize %s. " , config .library_name , design_to_optimize )
386391
387392 def _release_library_lock (self , library , config ):
388393 """
@@ -393,6 +398,9 @@ def _release_library_lock(self, library, config):
393398 self ._library_locks [config .library_name ].release ()
394399
395400 def _optimize (self , config , script_path ):
401+ """
402+ Optimize design and return simulation target or False if optimization failed.
403+ """
396404 design_to_optimize = self ._design_to_optimize (config )
397405
398406 libraries = {lib .name : lib for lib in self ._libraries }
@@ -401,7 +409,7 @@ def _optimize(self, config, script_path):
401409 optimize = False
402410 with self ._shared_state_lock :
403411 if design_to_optimize not in self ._optimized_designs :
404- LOGGER .debug ("%s scheduled for optimization" , design_to_optimize )
412+ LOGGER .debug ("%s scheduled for optimization. " , design_to_optimize )
405413 self ._optimized_designs [design_to_optimize ] = {
406414 "optimized_design" : None ,
407415 "optimization_completed" : Event (),
@@ -416,7 +424,7 @@ def _optimize(self, config, script_path):
416424 if optimize :
417425 self ._acquire_library_lock (library , config , design_to_optimize )
418426
419- LOGGER .debug ("Optimizing %s" , design_to_optimize )
427+ LOGGER .debug ("Optimizing %s. " , design_to_optimize )
420428
421429 optimized_design = self ._to_optimized_design (design_to_optimize )
422430
@@ -428,11 +436,11 @@ def _optimize(self, config, script_path):
428436
429437 else :
430438 tcl = f"""\
431- onerror {{quit -code 1}}
432- source "{ fix_path (str (optimize_file_name ))!s} "
433- set failed [vunit_optimize]
434- if {{$failed}} {{quit -code 1}}
435- quit -code 0
439+ onerror {{quit -code 1}}
440+ source "{ fix_path (str (optimize_file_name ))!s} "
441+ set failed [vunit_optimize]
442+ if {{$failed}} {{quit -code 1}}
443+ quit -code 0
436444 """
437445 batch_file_name = script_path / "batch_optimize.do"
438446 write_file (str (batch_file_name ), tcl )
@@ -449,7 +457,7 @@ def _optimize(self, config, script_path):
449457
450458 with self ._shared_state_lock :
451459 self ._optimized_designs [design_to_optimize ]["optimized_design" ] = optimized_design
452- self ._optimized_designs [design_to_optimize ]["optimization_completed" ].set ()
460+ self ._optimized_designs [design_to_optimize ]["optimization_completed" ].set ()
453461
454462 elif not optimized_design :
455463 LOGGER .debug ("Waiting for %s to be optimized." , design_to_optimize )
0 commit comments