Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. """
  2. This module represents a device.
  3.  
  4. Computer Systems Architecture Course
  5. Assignment 1
  6. March 2019
  7. """
  8.  
  9. from threading import *
  10.  
  11. class ReusableBarrier():
  12. """
  13. Reusable Barrier class from lab
  14. """
  15. def __init__(self, num_threads):
  16. self.num_threads = num_threads
  17. self.count_threads1 = [self.num_threads]
  18. self.count_threads2 = [self.num_threads]
  19. self.count_lock = Lock() # protejam accesarea/modificarea contoarelor
  20. self.threads_sem1 = Semaphore(0) # blocam thread-urile in prima etapa
  21. self.threads_sem2 = Semaphore(0) # blocam thread-urile in a doua etapa
  22.  
  23. def wait(self):
  24. self.phase(self.count_threads1, self.threads_sem1)
  25. self.phase(self.count_threads2, self.threads_sem2)
  26.  
  27. def phase(self, count_threads, threads_sem):
  28. with self.count_lock:
  29. count_threads[0] -= 1
  30. if count_threads[0] == 0: # a ajuns la bariera si ultimul thread
  31. for i in range(self.num_threads):
  32. threads_sem.release() # incrementarea semaforului va debloca num_threads thread-uri
  33. count_threads[0] = self.num_threads # reseteaza contorul
  34. threads_sem.acquire() # num_threads-1 threaduri se blocheaza aici
  35. # contorul semaforului se decrementeaza de num_threads ori
  36.  
  37. class MyThread(Thread):
  38. def __init__(self, tid, barrier):
  39. Thread.__init__(self)
  40. self.tid = tid
  41. self.barrier = barrier
  42.  
  43. def run(self):
  44. for i in xrange(10):
  45. self.barrier.wait()
  46. print "I'm Thread " + str(self.tid) + " after barrier, in step " + str(i) + "\n",
  47.  
  48.  
  49. class Device(object):
  50. """
  51. Class that represents a device.
  52. """
  53.  
  54. def __init__(self, device_id, sensor_data, supervisor):
  55. """
  56. Constructor.
  57.  
  58. @type device_id: Integer
  59. @param device_id: the unique id of this node; between 0 and N-1
  60.  
  61. @type sensor_data: List of (Integer, Float)
  62. @param sensor_data: a list containing (location, data) as measured by this device
  63.  
  64. @type supervisor: Supervisor
  65. @param supervisor: the testing infrastructure's control and validation component
  66. """
  67. self.device_id = device_id
  68. self.sensor_data = sensor_data
  69. self.supervisor = supervisor
  70. self.script_received = Event()
  71. self.scripts = []
  72. self.timepoint_done = Event()
  73. self.thread = DeviceThread(self)
  74. self.thread.start()
  75.  
  76. def __str__(self):
  77. """
  78. Pretty prints this device.
  79.  
  80. @rtype: String
  81. @return: a string containing the id of this device
  82. """
  83. return "Device %d" % self.device_id
  84.  
  85. def setup_devices(self, devices):
  86. """
  87. Setup the devices before simulation begins.
  88.  
  89. @type devices: List of Device
  90. @param devices: list containing all devices
  91. """
  92. # we don't need no stinkin' setup
  93. pass
  94.  
  95. def assign_script(self, script, location):
  96. """
  97. Provide a script for the device to execute.
  98.  
  99. @type script: Script
  100. @param script: the script to execute from now on at each timepoint; None if the
  101. current timepoint has ended
  102.  
  103. @type location: Integer
  104. @param location: the location for which the script is interested in
  105. """
  106. if script is not None:
  107. self.scripts.append((script, location))
  108. self.script_received.set()
  109. else:
  110. self.timepoint_done.set()
  111.  
  112. def get_data(self, location):
  113. """
  114. Returns the pollution value this device has for the given location.
  115.  
  116. @type location: Integer
  117. @param location: a location for which obtain the data
  118.  
  119. @rtype: Float
  120. @return: the pollution value
  121. """
  122. return self.sensor_data[location] if location in self.sensor_data else None
  123.  
  124. def set_data(self, location, data):
  125. """
  126. Sets the pollution value stored by this device for the given location.
  127.  
  128. @type location: Integer
  129. @param location: a location for which to set the data
  130.  
  131. @type data: Float
  132. @param data: the pollution value
  133. """
  134. if location in self.sensor_data:
  135. self.sensor_data[location] = data
  136.  
  137. def shutdown(self):
  138. """
  139. Instructs the device to shutdown (terminate all threads). This method
  140. is invoked by the tester. This method must block until all the threads
  141. started by this device terminate.
  142. """
  143. self.thread.join()
  144.  
  145.  
  146. class DeviceThread(Thread):
  147. """
  148. Class that implements the device's worker thread.
  149. """
  150.  
  151. def __init__(self, device):
  152. """
  153. Constructor.
  154.  
  155. @type device: Device
  156. @param device: the device which owns this thread
  157. """
  158. Thread.__init__(self, name="Device Thread %d" % device.device_id)
  159. self.device = device
  160.  
  161. def run(self):
  162. # hope there is only one timepoint, as multiple iterations of the loop are not supported
  163. while True:
  164. # get the current neighbourhood
  165. neighbours = self.device.supervisor.get_neighbours()
  166. if neighbours is None:
  167. break
  168.  
  169. self.device.script_received.wait()
  170.  
  171. # run scripts received until now
  172. for (script, location) in self.device.scripts:
  173. script_data = []
  174. # collect data from current neighbours
  175. for device in neighbours:
  176. data = device.get_data(location)
  177. if data is not None:
  178. script_data.append(data)
  179. # add our data, if any
  180. data = self.device.get_data(location)
  181. if data is not None:
  182. script_data.append(data)
  183.  
  184. if script_data != []:
  185. # run script on data
  186. result = script.run(script_data)
  187.  
  188. # update data of neighbours, hope no one is updating at the same time
  189. for device in neighbours:
  190. device.set_data(location, result)
  191. # update our data, hope no one is updating at the same time
  192. self.device.set_data(location, result)
  193.  
  194. # hope we don't get more than one script
  195. self.device.timepoint_done.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement