Removed libev dependency in core_c

This commit is contained in:
casey langen 2020-01-28 21:09:38 -08:00
parent 66e849a0b5
commit aff52e88e8
2 changed files with 21 additions and 84 deletions

View File

@ -15,9 +15,4 @@ add_library(musikcore_c SHARED ${CORE_C_SOURCES})
set_target_properties(musikcore_c PROPERTIES set_target_properties(musikcore_c PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${musikcube_SOURCE_DIR}/bin) LIBRARY_OUTPUT_DIRECTORY ${musikcube_SOURCE_DIR}/bin)
if (${LINK_STATICALLY} MATCHES "true") target_link_libraries(musikcore_c ${musikcube_LINK_LIBS} musikcore)
find_library(EVLIB NAMES libev.a ev)
target_link_libraries(musikcore_c ${musikcube_LINK_LIBS} ${EVLIB} musikcore)
else()
target_link_libraries(musikcore_c ${musikcube_LINK_LIBS} ev musikcore)
endif()

View File

@ -34,7 +34,6 @@
#include "musikcore_c.h" #include "musikcore_c.h"
#include <ev++.h>
#include <thread> #include <thread>
#include <core/debug.h> #include <core/debug.h>
@ -59,95 +58,38 @@ using namespace musik::core::runtime;
/* /*
* *
* ev_message_queue * internal_message_queue
* *
*/ */
static const short EVENT_DISPATCH = 1; class internal_message_queue: public MessageQueue {
static const short EVENT_QUIT = 2;
class ev_message_queue: public MessageQueue {
public: public:
ev_message_queue(): MessageQueue() { internal_message_queue(): MessageQueue() {
this->quit = false; this->quit = false;
if (pipe(pipeFd) != 0) {
std::cerr << "\n ERROR! couldn't create pipe\n\n";
exit(EXIT_FAILURE);
}
} }
virtual ~ev_message_queue() { void Quit() {
if (pipeFd[0]) { {
close(pipeFd[0]); LockT lock(this->mutex);
}
if (pipeFd[1]) {
close(pipeFd[1]);
}
}
void Post(IMessagePtr message, int64_t delayMs) {
std::unique_lock<std::mutex> lock(this->mutex);
if (this->quit) {
return;
}
MessageQueue::Post(message, delayMs);
if (delayMs <= 0) {
write(pipeFd[1], &EVENT_DISPATCH, sizeof(EVENT_DISPATCH));
}
else {
double delayTs = (double) delayMs / 1000.0;
loop.once<
ev_message_queue,
&ev_message_queue::DelayedDispatch
>(-1, ev::TIMER, (ev::tstamp) delayTs, this);
}
}
void DelayedDispatch(int revents) {
std::unique_lock<std::mutex> lock(this->mutex);
if (!this->quit) {
this->Dispatch();
}
}
void SignalQuit() {
std::unique_lock<std::mutex> lock(this->mutex);
this->quit = true; this->quit = true;
write(pipeFd[1], &EVENT_QUIT, sizeof(EVENT_QUIT));
}
void ReadCallback(ev::io& watcher, int revents) {
short type;
if (read(pipeFd[0], &type, sizeof(type)) == 0) {
std::cerr << "read() failed.\n";
exit(EXIT_FAILURE);
}
switch (type) {
case EVENT_DISPATCH: this->Dispatch(); break;
case EVENT_QUIT: loop.break_loop(ev::ALL); break;
} }
this->Post(Message::Create(0, 0, 0, 0));
} }
void Run() { void Run() {
io.set(loop); while (true) {
io.set(pipeFd[0], ev::READ); this->WaitAndDispatch();
io.set<ev_message_queue, &ev_message_queue::ReadCallback>(this); {
io.start(); LockT lock(this->mutex);
if (this->quit) {
sio.set(loop); return;
}
write(pipeFd[1], &EVENT_DISPATCH, sizeof(EVENT_DISPATCH)); }
}
loop.run(0);
} }
private: private:
int pipeFd[2]; using LockT = std::unique_lock<std::mutex>;
ev::dynamic_loop loop;
ev::io io;
ev::sig sio;
bool quit; bool quit;
std::mutex mutex; std::mutex mutex;
}; };
@ -159,7 +101,7 @@ class ev_message_queue: public MessageQueue {
*/ */
struct mcsdk_context_internal { struct mcsdk_context_internal {
ev_message_queue message_queue; internal_message_queue message_queue;
std::thread thread; std::thread thread;
ILibraryPtr library; ILibraryPtr library;
LocalMetadataProxy* metadata; LocalMetadataProxy* metadata;
@ -213,7 +155,7 @@ mcsdk_export void mcsdk_context_release(mcsdk_context** context) {
internal->library.reset(); internal->library.reset();
internal->preferences.reset(); internal->preferences.reset();
delete internal->metadata; delete internal->metadata;
internal->message_queue.SignalQuit(); internal->message_queue.Quit();
internal->thread.join(); internal->thread.join();
delete internal; delete internal;
delete c; delete c;