Commit 7e55394d authored by Kirill Smelkov's avatar Kirill Smelkov

golang.pyx: pychan: self -> ch

Use `ch` instead of `self` for pychan methods. This aligns with how
(py)chan objects are currently denoted in (py)select and will reduce the
difference when chan code is moved into libgolang. We are sticking to Go
convention here.

After the code with channels implementation is moved into libgolang, the
rest bits in golang.pyx will be changed to refer to pychan objects as
pych for clarity.
parent 311df9f1
...@@ -261,32 +261,32 @@ class pychan(object): ...@@ -261,32 +261,32 @@ class pychan(object):
# ._sendq deque _SendWaiting: blocked senders # ._sendq deque _SendWaiting: blocked senders
# ._closed bool # ._closed bool
def __init__(self, size=0): def __init__(ch, size=0):
self._cap = size ch._cap = size
self._mu = threading.Lock() ch._mu = threading.Lock()
self._dataq = collections.deque() ch._dataq = collections.deque()
self._recvq = collections.deque() ch._recvq = collections.deque()
self._sendq = collections.deque() ch._sendq = collections.deque()
self._closed = False ch._closed = False
# send sends object to a receiver. # send sends object to a receiver.
# #
# .send(obj) # .send(obj)
def send(self, obj): def send(ch, obj):
if self is pynilchan: if ch is pynilchan:
_blockforever() _blockforever()
self._mu.acquire() ch._mu.acquire()
if 1: if 1:
ok = self._trysend(obj) ok = ch._trysend(obj)
if ok: if ok:
return return
g = _WaitGroup() g = _WaitGroup()
me = _SendWaiting(g, self, obj) me = _SendWaiting(g, ch, obj)
self._sendq.append(me) ch._sendq.append(me)
self._mu.release() ch._mu.release()
g.wait() g.wait()
assert g.which is me assert g.which is me
...@@ -299,21 +299,21 @@ class pychan(object): ...@@ -299,21 +299,21 @@ class pychan(object):
# ok is false - if receive is due to channel being closed and empty. # ok is false - if receive is due to channel being closed and empty.
# #
# .recv_() -> (rx, ok) # .recv_() -> (rx, ok)
def recv_(self): def recv_(ch):
if self is pynilchan: if ch is pynilchan:
_blockforever() _blockforever()
self._mu.acquire() ch._mu.acquire()
if 1: if 1:
rx_, ok = self._tryrecv() rx_, ok = ch._tryrecv()
if ok: if ok:
return rx_ return rx_
g = _WaitGroup() g = _WaitGroup()
me = _RecvWaiting(g, self) me = _RecvWaiting(g, ch)
self._recvq.append(me) ch._recvq.append(me)
self._mu.release() ch._mu.release()
g.wait() g.wait()
assert g.which is me assert g.which is me
...@@ -322,8 +322,8 @@ class pychan(object): ...@@ -322,8 +322,8 @@ class pychan(object):
# recv receives from the channel. # recv receives from the channel.
# #
# .recv() -> rx # .recv() -> rx
def recv(self): def recv(ch):
rx, _ = self.recv_() rx, _ = ch.recv_()
return rx return rx
# _trysend(obj) -> ok # _trysend(obj) -> ok
...@@ -331,34 +331,34 @@ class pychan(object): ...@@ -331,34 +331,34 @@ class pychan(object):
# must be called with ._mu held. # must be called with ._mu held.
# if ok or panic - returns with ._mu released. # if ok or panic - returns with ._mu released.
# if !ok - returns with ._mu still being held. # if !ok - returns with ._mu still being held.
def _trysend(self, obj): def _trysend(ch, obj):
if self._closed: if ch._closed:
self._mu.release() ch._mu.release()
pypanic("send on closed channel") pypanic("send on closed channel")
# synchronous channel # synchronous channel
if self._cap == 0: if ch._cap == 0:
recv = _dequeWaiter(self._recvq) recv = _dequeWaiter(ch._recvq)
if recv is None: if recv is None:
return False return False
self._mu.release() ch._mu.release()
recv.wakeup(obj, True) recv.wakeup(obj, True)
return True return True
# buffered channel # buffered channel
else: else:
if len(self._dataq) >= self._cap: if len(ch._dataq) >= ch._cap:
return False return False
self._dataq.append(obj) ch._dataq.append(obj)
recv = _dequeWaiter(self._recvq) recv = _dequeWaiter(ch._recvq)
if recv is not None: if recv is not None:
rx = self._dataq.popleft() rx = ch._dataq.popleft()
self._mu.release() ch._mu.release()
recv.wakeup(rx, True) recv.wakeup(rx, True)
else: else:
self._mu.release() ch._mu.release()
return True return True
...@@ -367,61 +367,61 @@ class pychan(object): ...@@ -367,61 +367,61 @@ class pychan(object):
# must be called with ._mu held. # must be called with ._mu held.
# if ok or panic - returns with ._mu released. # if ok or panic - returns with ._mu released.
# if !ok - returns with ._mu still being held. # if !ok - returns with ._mu still being held.
def _tryrecv(self): def _tryrecv(ch):
# buffered # buffered
if len(self._dataq) > 0: if len(ch._dataq) > 0:
rx = self._dataq.popleft() rx = ch._dataq.popleft()
# wakeup a blocked writer, if there is any # wakeup a blocked writer, if there is any
send = _dequeWaiter(self._sendq) send = _dequeWaiter(ch._sendq)
if send is not None: if send is not None:
self._dataq.append(send.obj) ch._dataq.append(send.obj)
self._mu.release() ch._mu.release()
send.wakeup(True) send.wakeup(True)
else: else:
self._mu.release() ch._mu.release()
return (rx, True), True return (rx, True), True
# closed # closed
if self._closed: if ch._closed:
self._mu.release() ch._mu.release()
return (None, False), True return (None, False), True
# sync | empty: there is waiting writer # sync | empty: there is waiting writer
send = _dequeWaiter(self._sendq) send = _dequeWaiter(ch._sendq)
if send is None: if send is None:
return (None, False), False return (None, False), False
self._mu.release() ch._mu.release()
rx = send.obj rx = send.obj
send.wakeup(True) send.wakeup(True)
return (rx, True), True return (rx, True), True
# close closes sending side of the channel. # close closes sending side of the channel.
def close(self): def close(ch):
if self is pynilchan: if ch is pynilchan:
pypanic("close of nil channel") pypanic("close of nil channel")
recvv = [] recvv = []
sendv = [] sendv = []
with self._mu: with ch._mu:
if self._closed: if ch._closed:
pypanic("close of closed channel") pypanic("close of closed channel")
self._closed = True ch._closed = True
# schedule: wake-up all readers # schedule: wake-up all readers
while 1: while 1:
recv = _dequeWaiter(self._recvq) recv = _dequeWaiter(ch._recvq)
if recv is None: if recv is None:
break break
recvv.append(recv) recvv.append(recv)
# schedule: wake-up all writers (they will panic) # schedule: wake-up all writers (they will panic)
while 1: while 1:
send = _dequeWaiter(self._sendq) send = _dequeWaiter(ch._sendq)
if send is None: if send is None:
break break
sendv.append(send) sendv.append(send)
...@@ -433,14 +433,14 @@ class pychan(object): ...@@ -433,14 +433,14 @@ class pychan(object):
send.wakeup(False) send.wakeup(False)
def __len__(self): def __len__(ch):
return len(self._dataq) return len(ch._dataq)
def __repr__(self): def __repr__(ch):
if self is pynilchan: if ch is pynilchan:
return "nilchan" return "nilchan"
else: else:
return super(pychan, self).__repr__() return super(pychan, ch).__repr__()
# pynilchan is the nil py channel. # pynilchan is the nil py channel.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment