Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OV-27. use semaphore to protect the read consistency of currentTime #22

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions bin/openVisualizerApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import os
import logging
import json
import threading

from openvisualizer.OVtracer import OVtracer

Expand Down Expand Up @@ -65,7 +66,10 @@ def __init__(self,confdir,datadir,logdir,simulatorMode,numMotes,trace,debug,useP
if self.simulatorMode:
from openvisualizer.SimEngine import SimEngine, MoteHandler

self.simengine = SimEngine.SimEngine(simTopology)
# create semaphore for reading the currentTime
self.currentTime_semaphore = threading.Semaphore()

self.simengine = SimEngine.SimEngine(simTopology=simTopology, currentTime_semaphore=self.currentTime_semaphore)
self.simengine.start()

# import the number of motes from json file given by user (if the pathTopo option is enabled)
Expand All @@ -89,7 +93,7 @@ def __init__(self,confdir,datadir,logdir,simulatorMode,numMotes,trace,debug,useP
MoteHandler.readNotifIds(os.path.join(self.datadir, 'sim_files', 'openwsnmodule_obj.h'))
self.moteProbes = []
for _ in range(self.numMotes):
moteHandler = MoteHandler.MoteHandler(oos_openwsn.OpenMote())
moteHandler = MoteHandler.MoteHandler(oos_openwsn.OpenMote(), self.currentTime_semaphore)
self.simengine.indicateNewMote(moteHandler)
self.moteProbes += [moteProbe.moteProbe(mqtt_broker_address, emulatedMote=moteHandler)]
elif self.iotlabmotes:
Expand Down
7 changes: 6 additions & 1 deletion openvisualizer/BspEmulator/BspUart.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ class BspUart(BspModule.BspModule):
XONXOFF_ESCAPE = 0x12
XONXOFF_MASK = 0x10

def __init__(self,motehandler):
def __init__(self,motehandler, currentTime_semaphore):

# store params
self.engine = SimEngine.SimEngine()
self.motehandler = motehandler
self.ct_semaphore = currentTime_semaphore

# local variables
self.timeline = self.engine.timeline
Expand Down Expand Up @@ -357,9 +358,13 @@ def intr_rx(self):
#======================== private =========================================

def _scheduleNextTx(self):

self.ct_semaphore.acquire()

# calculate time at which byte will get out
timeNextTx = self.timeline.getCurrentTime()+float(1.0/float(self.BAUDRATE))

self.ct_semaphore.release()

# schedule that event
self.timeline.scheduleEvent(
Expand Down
7 changes: 4 additions & 3 deletions openvisualizer/SimEngine/MoteHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ def notifId(s):

class MoteHandler(threading.Thread):

def __init__(self,mote):
def __init__(self,mote, currentTime_semaphore):

# store params
self.engine = SimEngine.SimEngine()
self.engine = SimEngine.SimEngine(currentTime_semaphore)
self.mote = mote
self.ct_semaphore = currentTime_semaphore

#=== local variables
self.loghandler = self.engine.loghandler
Expand All @@ -81,7 +82,7 @@ def __init__(self,mote):
self.bspLeds = BspLeds.BspLeds(self)
self.bspSctimer = BspSctimer.BspSctimer(self)
self.bspRadio = BspRadio.BspRadio(self)
self.bspUart = BspUart.BspUart(self)
self.bspUart = BspUart.BspUart(self, self.ct_semaphore)
# status
self.booted = False
self.cpuRunning = threading.Lock()
Expand Down
5 changes: 3 additions & 2 deletions openvisualizer/SimEngine/SimEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __new__(cls, *args, **kwargs):

#======================== main ============================================

def __init__(self,simTopology='',loghandler=logging.NullHandler()):
def __init__(self,simTopology='',loghandler=logging.NullHandler(), currentTime_semaphore=None):

# don't re-initialize an instance (singleton pattern)
if self._init:
Expand All @@ -61,10 +61,11 @@ def __init__(self,simTopology='',loghandler=logging.NullHandler()):

# store params
self.loghandler = loghandler
self.ct_semaphore = currentTime_semaphore

# local variables
self.moteHandlers = []
self.timeline = TimeLine.TimeLine()
self.timeline = TimeLine.TimeLine(self.ct_semaphore)
self.propagation = Propagation.Propagation(simTopology)
self.idmanager = IdManager.IdManager()
self.locationmanager = LocationManager.LocationManager()
Expand Down
16 changes: 14 additions & 2 deletions openvisualizer/SimEngine/TimeLine.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ class TimeLine(threading.Thread):
The timeline of the engine.
'''

def __init__(self):
def __init__(self,ct_semaphore):

# store params
self.engine = SimEngine.SimEngine()
self.engine = SimEngine.SimEngine(ct_semaphore)
self.ct_semaphore = ct_semaphore

# local variables
self.currentTime = 0 # current time
self.currentTimeLock = threading.Lock()
self.timeline = [] # list of upcoming events
self.firstEventPassed = False
self.firstEvent = threading.Lock()
Expand Down Expand Up @@ -94,7 +96,15 @@ def run(self):
output += ' - currentTime='+str(self.getCurrentTime())+'\n'
self.log.warning(output)
raise StopIteration(output)

'''
use semaphore to resolve the inconsistant reading of currentTime
- acquire right before a pop the next event
- release right after the currentTime is assigned.
'''

self.ct_semaphore.acquire()

# pop the event at the head of the timeline
event = self.timeline.pop(0)

Expand All @@ -103,6 +113,8 @@ def run(self):

# record the current time
self.currentTime = event.atTime

self.ct_semaphore.release()

# log
if self.log.isEnabledFor(logging.DEBUG):
Expand Down