2021-11-13 01:50:50 +08:00
|
|
|
#include "intr.h"
|
|
|
|
|
|
|
|
#include "util/log.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
bool
|
|
|
|
sc_intr_init(struct sc_intr *intr) {
|
|
|
|
bool ok = sc_mutex_init(&intr->mutex);
|
|
|
|
if (!ok) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2021-11-13 01:50:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-13 16:58:52 +08:00
|
|
|
intr->socket = SC_SOCKET_NONE;
|
2021-11-13 01:50:50 +08:00
|
|
|
intr->process = SC_PROCESS_NONE;
|
|
|
|
|
|
|
|
atomic_store_explicit(&intr->interrupted, false, memory_order_relaxed);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
sc_intr_set_socket(struct sc_intr *intr, sc_socket socket) {
|
|
|
|
assert(intr->process == SC_PROCESS_NONE);
|
|
|
|
|
|
|
|
sc_mutex_lock(&intr->mutex);
|
|
|
|
bool interrupted =
|
|
|
|
atomic_load_explicit(&intr->interrupted, memory_order_relaxed);
|
|
|
|
if (!interrupted) {
|
|
|
|
intr->socket = socket;
|
|
|
|
}
|
|
|
|
sc_mutex_unlock(&intr->mutex);
|
|
|
|
|
|
|
|
return !interrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
sc_intr_set_process(struct sc_intr *intr, sc_pid pid) {
|
2021-11-13 16:58:52 +08:00
|
|
|
assert(intr->socket == SC_SOCKET_NONE);
|
2021-11-13 01:50:50 +08:00
|
|
|
|
|
|
|
sc_mutex_lock(&intr->mutex);
|
|
|
|
bool interrupted =
|
|
|
|
atomic_load_explicit(&intr->interrupted, memory_order_relaxed);
|
|
|
|
if (!interrupted) {
|
|
|
|
intr->process = pid;
|
|
|
|
}
|
|
|
|
sc_mutex_unlock(&intr->mutex);
|
|
|
|
|
|
|
|
return !interrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_intr_interrupt(struct sc_intr *intr) {
|
|
|
|
sc_mutex_lock(&intr->mutex);
|
|
|
|
|
|
|
|
atomic_store_explicit(&intr->interrupted, true, memory_order_relaxed);
|
|
|
|
|
|
|
|
// No more than one component to interrupt
|
2021-11-13 16:58:52 +08:00
|
|
|
assert(intr->socket == SC_SOCKET_NONE ||
|
2021-11-13 01:50:50 +08:00
|
|
|
intr->process == SC_PROCESS_NONE);
|
|
|
|
|
2021-11-13 16:58:52 +08:00
|
|
|
if (intr->socket != SC_SOCKET_NONE) {
|
2021-11-13 01:50:50 +08:00
|
|
|
LOGD("Interrupting socket");
|
|
|
|
net_interrupt(intr->socket);
|
2021-11-13 16:58:52 +08:00
|
|
|
intr->socket = SC_SOCKET_NONE;
|
2021-11-13 01:50:50 +08:00
|
|
|
}
|
|
|
|
if (intr->process != SC_PROCESS_NONE) {
|
|
|
|
LOGD("Interrupting process");
|
|
|
|
sc_process_terminate(intr->process);
|
|
|
|
intr->process = SC_PROCESS_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc_mutex_unlock(&intr->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_intr_destroy(struct sc_intr *intr) {
|
2021-11-13 16:58:52 +08:00
|
|
|
assert(intr->socket == SC_SOCKET_NONE);
|
2021-11-13 01:50:50 +08:00
|
|
|
assert(intr->process == SC_PROCESS_NONE);
|
|
|
|
|
|
|
|
sc_mutex_destroy(&intr->mutex);
|
|
|
|
}
|