blob: 5810332bc720b828e54406195ad9f7b62f891354 [file] [log] [blame]
Christoffer Quist Adamsen65ef2982023-08-24 08:45:39 +02001#!/usr/bin/env python3
2# Copyright (c) 2023, the R8 project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5
6import sys
7import threading
8from threading import Thread
9import traceback
10
11# A thread that is given a list of jobs. The thread will repeatedly take a job
12# from the list of jobs (which is shared with other threads) and execute it
13# until there is no more jobs.
14#
15# If stop_on_first_failure is True, then the thread will exit upon the first
16# failing job. The thread will then clear the jobs to ensure that all other
17# workers will also terminate after completing there current job.
18#
19# Each job is a lambda that takes the worker_id as an argument. To guarantee
20# termination each job must itself terminate (i.e., each job is responsible for
21# setting an appropriate timeout).
22class WorkerThread(Thread):
23
24 # The initialization of a WorkerThread is never run concurrently with the
25 # initialization of other WorkerThreads.
26 def __init__(self, jobs, jobs_lock, stop_on_first_failure, worker_id):
27 Thread.__init__(self)
Christoffer Quist Adamsen60d73702023-09-05 12:05:47 +020028 self.completed = False
Christoffer Quist Adamsen65ef2982023-08-24 08:45:39 +020029 self.jobs = jobs
30 self.jobs_lock = jobs_lock
31 self.number_of_jobs = len(jobs)
32 self.stop_on_first_failure = stop_on_first_failure
33 self.success = True
34 self.worker_id = worker_id
35
36 def run(self):
37 print_thread("Starting worker", self.worker_id)
38 while True:
39 (job, job_id) = self.take_job(self.jobs, self.jobs_lock)
40 if job is None:
41 break
42 try:
43 print_thread(
44 "Starting job %s/%s" % (job_id, self.number_of_jobs),
45 self.worker_id)
46 exit_code = job(self.worker_id)
47 print_thread(
48 "Job %s finished with exit code %s"
49 % (job_id, exit_code),
50 self.worker_id)
51 if exit_code:
52 self.success = False
53 if self.stop_on_first_failure:
Christoffer Quist Adamsen60d73702023-09-05 12:05:47 +020054 self.clear_jobs(self.jobs, self.jobs_lock)
Christoffer Quist Adamsen65ef2982023-08-24 08:45:39 +020055 break
56 except:
57 print_thread("Job %s crashed" % job_id, self.worker_id)
58 print_thread(traceback.format_exc(), self.worker_id)
59 self.success = False
60 if self.stop_on_first_failure:
Christoffer Quist Adamsen60d73702023-09-05 12:05:47 +020061 self.clear_jobs(self.jobs, self.jobs_lock)
Christoffer Quist Adamsen65ef2982023-08-24 08:45:39 +020062 break
63 print_thread("Exiting", self.worker_id)
Christoffer Quist Adamsen60d73702023-09-05 12:05:47 +020064 self.completed = True
Christoffer Quist Adamsen65ef2982023-08-24 08:45:39 +020065
66 def take_job(self, jobs, jobs_lock):
67 jobs_lock.acquire()
68 job_id = self.number_of_jobs - len(jobs) + 1
69 job = jobs.pop(0) if jobs else None
70 jobs_lock.release()
71 return (job, job_id)
72
73 def clear_jobs(self, jobs, jobs_lock):
74 jobs_lock.acquire()
75 jobs.clear()
76 jobs_lock.release()
77
78def run_in_parallel(jobs, number_of_workers, stop_on_first_failure):
79 assert number_of_workers > 0
80 if number_of_workers > len(jobs):
81 number_of_workers = len(jobs)
82 if number_of_workers == 1:
83 return run_in_sequence(jobs, stop_on_first_failure)
84 jobs_lock = threading.Lock()
85 threads = []
86 for worker_id in range(1, number_of_workers + 1):
87 threads.append(
88 WorkerThread(jobs, jobs_lock, stop_on_first_failure, worker_id))
89 for thread in threads:
90 thread.start()
91 for thread in threads:
92 thread.join()
93 for thread in threads:
Christoffer Quist Adamsen60d73702023-09-05 12:05:47 +020094 if not thread.completed or not thread.success:
Christoffer Quist Adamsen65ef2982023-08-24 08:45:39 +020095 return 1
96 return 0
97
98def run_in_sequence(jobs, stop_on_first_failure):
99 combined_exit_code = 0
100 worker_id = None
101 for job in jobs:
102 try:
103 exit_code = job(worker_id)
104 if exit_code:
105 combined_exit_code = exit_code
106 if stop_on_first_failure:
107 break
108 except:
109 print(traceback.format_exc())
110 combined_exit_code = 1
111 if stop_on_first_failure:
112 break
113 return combined_exit_code
114
115def print_thread(msg, worker_id):
116 if worker_id is None:
117 print(msg)
Christoffer Quist Adamsencca1aa32023-08-24 09:17:37 +0200118 else:
119 print('WORKER %s: %s' % (worker_id, msg))