def run(self): while True: time.sleep(.1) self.screen.fill(self.bg) #Entering critical section if: #all OKs have been received if not mutex.ok_list: #the queue is built and not in critical section if mutex.queue and not self.critical: #and you are the first element in the queue if mutex.queue[0][0] == mutex.queue_id and mutex.queue[0][1] == self.seg_id: self.critical = True self.countdown = float(random.randint(3,5)) self.bg = (0, 255, 0) #receive OKs and resend request if timeout else: self.ok_countdown -= .1 if self.ok_countdown <= 0: mutex.send_request([mutex.queue_id], self.seg_id, self.seg_dic, True) self.ok_countdown = self.OK_COUNT_TIMEOUT #Critical section if self.critical: self.countdown -= .1 #when finished send release message and a new request if self.countdown <= 0: self.countdown = 0 self.critical = False mutex.send_release(self.clock, self.seg_id, mutex.queue_id) self.clock[0] += 1 mutex.send_request(self.clock, self.seg_id, self.seg_dic) self.bg = (0,0,0) # Display info self.print_text('ID: ' + str(self.seg_id), 0, 0) self.print_text('NUMSEG: ' + str(len(self.seg_dic.keys())), 0, 25) self.print_text('CLOCK: ' + str(self.clock[0]), 0, 50) self.print_text('QUEUE ID: ' + str(mutex.queue_id), 0, 75) self.print_text('QUEUE: ' + str(mutex.queue), 0, 100) # Display queue position or countdown if it is in critical section big_number = 0 if self.critical: big_number = self.countdown elif (mutex.queue_id, self.seg_id) in mutex.queue: big_number = mutex.queue.index((mutex.queue_id, self.seg_id)) self.print_text(str(big_number), 130, 10, False) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: os._exit(1)