Extra AE functionality

This commit is contained in:
John Sully 2019-02-18 18:56:45 -05:00
parent 65e48b868e
commit e9c1d30749
3 changed files with 2809 additions and 5 deletions

View File

@ -30,6 +30,8 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <condition_variable>
#include <atomic>
#include <mutex> #include <mutex>
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
@ -75,18 +77,29 @@ enum class AE_ASYNC_OP
PostFunction, PostFunction,
PostCppFunction, PostCppFunction,
DeleteFileEvent, DeleteFileEvent,
CreateFileEvent,
}; };
typedef struct aeCommand
struct aeCommandControl
{
std::condition_variable cv;
std::atomic<int> rval;
std::mutex mutexcv;
};
struct aeCommand
{ {
AE_ASYNC_OP op; AE_ASYNC_OP op;
int fd; int fd;
int mask; int mask;
union { union {
aePostFunctionProc *proc; aePostFunctionProc *proc;
aeFileProc *fproc;
std::function<void()> *pfn; std::function<void()> *pfn;
}; };
void *clientData; void *clientData;
} aeCommand; aeCommandControl *pctl;
};
void aeProcessCmd(aeEventLoop *eventLoop, int fd, void *, int ) void aeProcessCmd(aeEventLoop *eventLoop, int fd, void *, int )
{ {
@ -106,6 +119,14 @@ void aeProcessCmd(aeEventLoop *eventLoop, int fd, void *, int )
aeDeleteFileEvent(eventLoop, cmd.fd, cmd.mask); aeDeleteFileEvent(eventLoop, cmd.fd, cmd.mask);
break; break;
case AE_ASYNC_OP::CreateFileEvent:
{
std::unique_lock<std::mutex> ulock(cmd.pctl->mutexcv);
std::atomic_store(&cmd.pctl->rval, aeCreateFileEvent(eventLoop, cmd.fd, cmd.mask, cmd.fproc, cmd.clientData));
cmd.pctl->cv.notify_all();
}
break;
case AE_ASYNC_OP::PostFunction: case AE_ASYNC_OP::PostFunction:
{ {
std::unique_lock<decltype(g_lock)> ulock(g_lock); std::unique_lock<decltype(g_lock)> ulock(g_lock);
@ -119,10 +140,34 @@ void aeProcessCmd(aeEventLoop *eventLoop, int fd, void *, int )
(*cmd.pfn)(); (*cmd.pfn)();
delete cmd.pfn; delete cmd.pfn;
} }
break;
} }
} }
} }
int aeCreateRemoteFileEventSync(aeEventLoop *eventLoop, int fd, int mask,
aeFileProc *proc, void *clientData)
{
if (eventLoop == g_eventLoopThisThread)
return aeCreateFileEvent(eventLoop, fd, mask, proc, clientData);
aeCommand cmd;
cmd.op = AE_ASYNC_OP::CreateFileEvent;
cmd.fd = fd;
cmd.mask = mask;
cmd.fproc = proc;
cmd.clientData = clientData;
cmd.pctl = new aeCommandControl();
std::unique_lock<std::mutex> ulock(cmd.pctl->mutexcv);
auto size = write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd));
AE_ASSERT(size == sizeof(cmd));
cmd.pctl->cv.wait(ulock);
int ret = cmd.pctl->rval;
delete cmd.pctl;
return ret;
}
int aePostFunction(aeEventLoop *eventLoop, aePostFunctionProc *proc, void *arg) int aePostFunction(aeEventLoop *eventLoop, aePostFunctionProc *proc, void *arg)
{ {
aeCommand cmd; aeCommand cmd;
@ -295,7 +340,6 @@ extern "C" void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask)
} }
extern "C" int aeGetFileEvents(aeEventLoop *eventLoop, int fd) { extern "C" int aeGetFileEvents(aeEventLoop *eventLoop, int fd) {
AE_ASSERT(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);
if (fd >= eventLoop->setsize) return 0; if (fd >= eventLoop->setsize) return 0;
aeFileEvent *fe = &eventLoop->events[fd]; aeFileEvent *fe = &eventLoop->events[fd];
@ -674,3 +718,8 @@ void aeReleaseLock()
{ {
g_lock.unlock(); g_lock.unlock();
} }
int aeThreadOwnsLock()
{
return g_lock.fOwnLock();
}

View File

@ -138,6 +138,8 @@ void aeDeleteEventLoop(aeEventLoop *eventLoop);
void aeStop(aeEventLoop *eventLoop); void aeStop(aeEventLoop *eventLoop);
int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
aeFileProc *proc, void *clientData); aeFileProc *proc, void *clientData);
int aeCreateRemoteFileEventSync(aeEventLoop *eventLoop, int fd, int mask,
aeFileProc *proc, void *clientData);
void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask); void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);
void aeDeleteFileEventAsync(aeEventLoop *eventLoop, int fd, int mask); void aeDeleteFileEventAsync(aeEventLoop *eventLoop, int fd, int mask);
int aeGetFileEvents(aeEventLoop *eventLoop, int fd); int aeGetFileEvents(aeEventLoop *eventLoop, int fd);
@ -156,6 +158,7 @@ int aeResizeSetSize(aeEventLoop *eventLoop, int setsize);
void aeAcquireLock(); void aeAcquireLock();
void aeReleaseLock(); void aeReleaseLock();
int aeThreadOwnsLock();
#ifdef __cplusplus #ifdef __cplusplus
} }

2752
src/replication.cpp Normal file

File diff suppressed because it is too large Load Diff