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

Fixed freezing issue in XAsyncSockets lib and updated thread locking #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
84 changes: 39 additions & 45 deletions MicroWebSrv2/libs/XAsyncSockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,24 @@ def __init__(self) :
# ------------------------------------------------------------------------

def _incThreadsCount(self) :
self._opLock.acquire()
self._threadsCount += 1
self._opLock.release()
with self._opLock :
self._threadsCount += 1

# ------------------------------------------------------------------------

def _decThreadsCount(self) :
self._opLock.acquire()
self._threadsCount -= 1
self._opLock.release()
with self._opLock :
self._threadsCount -= 1

# ------------------------------------------------------------------------

def _addSocket(self, socket, asyncSocket) :
if socket :
socketno = socket.fileno()
self._opLock.acquire()
ok = (socketno not in self._asyncSockets)
if ok :
self._asyncSockets[socketno] = asyncSocket
self._opLock.release()
with self._opLock :
ok = (socketno not in self._asyncSockets)
if ok :
self._asyncSockets[socketno] = asyncSocket
return ok
return False

Expand All @@ -67,36 +64,35 @@ def _addSocket(self, socket, asyncSocket) :
def _removeSocket(self, socket) :
if socket :
socketno = socket.fileno()
self._opLock.acquire()
ok = (socketno in self._asyncSockets)
if ok :
del self._asyncSockets[socketno]
if socket in self._readList :
self._readList.remove(socket)
if socket in self._writeList :
self._writeList.remove(socket)
self._opLock.release()
with self._opLock :
ok = (socketno in self._asyncSockets)
if ok :
del self._asyncSockets[socketno]
if socket in self._readList :
self._readList.remove(socket)
if socket in self._writeList :
self._writeList.remove(socket)
return ok
return False

# ------------------------------------------------------------------------

def _socketListAdd(self, socket, socketsList) :
self._opLock.acquire()
ok = (socket.fileno() in self._asyncSockets and socket not in socketsList)
if ok :
socketsList.append(socket)
self._opLock.release()
if self._opLock.locked() :
return False
with self._opLock :
ok = (socket.fileno() in self._asyncSockets and socket not in socketsList)
if ok :
socketsList.append(socket)
return ok

# ------------------------------------------------------------------------

def _socketListRemove(self, socket, socketsList) :
self._opLock.acquire()
ok = (socket.fileno() in self._asyncSockets and socket in socketsList)
if ok :
socketsList.remove(socket)
self._opLock.release()
with self._opLock :
ok = (socket.fileno() in self._asyncSockets and socket in socketsList)
if ok :
socketsList.remove(socket)
return ok

# ------------------------------------------------------------------------
Expand Down Expand Up @@ -1024,13 +1020,12 @@ def __init__(self, slotsCount, slotsSize, keepAlloc=True) :

def GetAvailableSlot(self) :
ret = None
self._lock.acquire()
for slot in self._slots :
if slot.Available :
slot.Available = False
ret = slot
break
self._lock.release()
with self._lock :
for slot in self._slots :
if slot.Available :
slot.Available = False
ret = slot
break
return ret

@property
Expand Down Expand Up @@ -1060,14 +1055,13 @@ def __init__(self) :
self._last = None

def Put(self, obj) :
self._lock.acquire()
if self._first :
self._last[1] = [obj, None]
self._last = self._last[1]
else :
self._last = [obj, None]
self._first = self._last
self._lock.release()
with self._lock :
if self._first :
self._last[1] = [obj, None]
self._last = self._last[1]
else :
self._last = [obj, None]
self._first = self._last

def Get(self) :
self._lock.acquire()
Expand Down