Bug 1046109: Add |SocketIOTask|, r=kyle

|SocketIOTask| is a task-class template that holds a reference to
a Socket I/O object. It replaces |UnixSocketImplTask|, which only
supports objects of type |UnixSocketImpl|.
This commit is contained in:
Thomas Zimmermann 2014-07-31 09:29:20 +02:00
parent e3f2e64199
commit 5061486cef
2 changed files with 55 additions and 40 deletions

View File

@ -417,6 +417,46 @@ private:
nsTArray<UnixSocketRawData*> mOutgoingQ;
};
//
// Socket I/O tasks
//
/* |SocketIOTask| holds a reference to a Socket I/O object. It's
* supposed to run on the I/O thread.
*/
template <typename T>
class SocketIOTask : public CancelableTask
{
public:
virtual ~SocketIOTask()
{ }
T* GetIO() const
{
return mIO;
}
void Cancel() MOZ_OVERRIDE
{
mIO = nullptr;
}
bool IsCanceled() const
{
return !mIO;
}
protected:
SocketIOTask(T* aIO)
: mIO(aIO)
{
MOZ_ASSERT(mIO);
}
private:
T* mIO;
};
}
}

View File

@ -173,38 +173,13 @@ private:
CancelableTask* mDelayedConnectTask;
};
class UnixSocketImplTask : public CancelableTask
{
public:
UnixSocketImpl* GetImpl() const
{
return mImpl;
}
void Cancel() MOZ_OVERRIDE
{
mImpl = nullptr;
}
bool IsCanceled() const
{
return !mImpl;
}
protected:
UnixSocketImplTask(UnixSocketImpl* aImpl)
: mImpl(aImpl)
{
MOZ_ASSERT(mImpl);
}
private:
UnixSocketImpl* mImpl;
};
class SocketSendTask : public UnixSocketImplTask
class SocketSendTask : public SocketIOTask<UnixSocketImpl>
{
public:
SocketSendTask(UnixSocketImpl* aImpl,
UnixSocketConsumer* aConsumer,
UnixSocketRawData* aData)
: UnixSocketImplTask(aImpl)
: SocketIOTask<UnixSocketImpl>(aImpl)
, mConsumer(aConsumer)
, mData(aData)
{
@ -216,7 +191,7 @@ public:
MOZ_ASSERT(!NS_IsMainThread());
MOZ_ASSERT(!IsCanceled());
UnixSocketImpl* impl = GetImpl();
UnixSocketImpl* impl = GetIO();
MOZ_ASSERT(!impl->IsShutdownOnIOThread());
impl->Send(mData);
@ -226,42 +201,42 @@ private:
UnixSocketRawData* mData;
};
class SocketListenTask : public UnixSocketImplTask
class SocketListenTask : public SocketIOTask<UnixSocketImpl>
{
public:
SocketListenTask(UnixSocketImpl* aImpl)
: UnixSocketImplTask(aImpl)
: SocketIOTask<UnixSocketImpl>(aImpl)
{ }
void Run() MOZ_OVERRIDE
{
MOZ_ASSERT(!NS_IsMainThread());
if (!IsCanceled()) {
GetImpl()->Listen();
GetIO()->Listen();
}
}
};
class SocketConnectTask : public UnixSocketImplTask
class SocketConnectTask : public SocketIOTask<UnixSocketImpl>
{
public:
SocketConnectTask(UnixSocketImpl* aImpl)
: UnixSocketImplTask(aImpl)
: SocketIOTask<UnixSocketImpl>(aImpl)
{ }
void Run() MOZ_OVERRIDE
{
MOZ_ASSERT(!NS_IsMainThread());
MOZ_ASSERT(!IsCanceled());
GetImpl()->Connect();
GetIO()->Connect();
}
};
class SocketDelayedConnectTask : public UnixSocketImplTask
class SocketDelayedConnectTask : public SocketIOTask<UnixSocketImpl>
{
public:
SocketDelayedConnectTask(UnixSocketImpl* aImpl)
: UnixSocketImplTask(aImpl)
: SocketIOTask<UnixSocketImpl>(aImpl)
{ }
void Run() MOZ_OVERRIDE
@ -270,7 +245,7 @@ public:
if (IsCanceled()) {
return;
}
UnixSocketImpl* impl = GetImpl();
UnixSocketImpl* impl = GetIO();
if (impl->IsShutdownOnMainThread()) {
return;
}
@ -279,11 +254,11 @@ public:
}
};
class ShutdownSocketTask : public UnixSocketImplTask
class ShutdownSocketTask : public SocketIOTask<UnixSocketImpl>
{
public:
ShutdownSocketTask(UnixSocketImpl* aImpl)
: UnixSocketImplTask(aImpl)
: SocketIOTask<UnixSocketImpl>(aImpl)
{ }
void Run() MOZ_OVERRIDE
@ -291,7 +266,7 @@ public:
MOZ_ASSERT(!NS_IsMainThread());
MOZ_ASSERT(!IsCanceled());
UnixSocketImpl* impl = GetImpl();
UnixSocketImpl* impl = GetIO();
// At this point, there should be no new events on the IO thread after this
// one with the possible exception of a SocketListenTask that