Skip to content

Commit

Permalink
basic shared_lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Aug 16, 2022
1 parent bcaa5ec commit 6e422b0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 14 deletions.
3 changes: 3 additions & 0 deletions include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef TORRENT_GLOBALS_H
#define TORRENT_GLOBALS_H

#include <unordered_set>
#include <torrent/utils/priority_queue_default.h>
#include <torrent/utils/timer.h>

Expand All @@ -24,4 +25,6 @@ extern torrent::utils::timer cachedTime;
extern Control* control;
extern RpcThreadManager* worker_thread;

extern std::unordered_set<std::string> readonly_command;

#endif
7 changes: 6 additions & 1 deletion src/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
torrent::utils::priority_queue_default taskScheduler;
torrent::utils::timer cachedTime;

Control* control = nullptr;
Control* control = nullptr;
RpcThreadManager* worker_thread = nullptr;

std::unordered_set<std::string> readonly_command = {
"throttle.global_up.rate", "throttle.global_down.rate",
"download_list", "d.name"
};
16 changes: 11 additions & 5 deletions src/rpc/rpc_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "rpc/parse_commands.h"
#include "thread_base.h"
#include "utils/jsonrpc/common.h"
#include "globals.h"

using jsonrpccxx::JsonRpcException;
using nlohmann::json;
Expand Down Expand Up @@ -243,8 +244,16 @@ jsonrpc_call_command(const std::string& method, const json& params) {
torrent::Object object;
rpc::target_type target = rpc::make_target();

torrent::thread_base::acquire_global_lock();
torrent::main_thread()->interrupt();
std::shared_lock<std::shared_mutex> read_lock;
std::unique_lock<std::shared_mutex> write_lock;

if (readonly_command.count(method)) {
read_lock = std::shared_lock(torrent::thread_base::m_global.lock);
}
else {
write_lock = std::unique_lock(torrent::thread_base::m_global.lock);
torrent::main_thread()->interrupt();
}

if (itr->second.m_flags & CommandMap::flag_no_target) {
json_to_object(params, command_base::target_generic, &target)
Expand All @@ -260,13 +269,10 @@ jsonrpc_call_command(const std::string& method, const json& params) {

const auto& result = rpc::commands.call_command(itr, object, target);

torrent::thread_base::release_global_lock();
return object_to_json(result);
} catch (torrent::input_error& e) {
torrent::thread_base::release_global_lock();
throw JsonRpcException(-32602, e.what());
} catch (torrent::local_error& e) {
torrent::thread_base::release_global_lock();
throw JsonRpcException(-32000, e.what());
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/rpc/rpc_xml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Copyright (C) 2005-2011, Jari Sundell <[email protected]>

#include "buildinfo.h"
#include "globals.h"
#include <torrent/torrent.h>


#ifdef HAVE_XMLRPC_C

Expand Down Expand Up @@ -575,6 +578,9 @@ RpcXml::cleanup() {

bool
RpcXml::process(const char* inBuffer, uint32_t length, res_callback callback) {
std::unique_lock lock(torrent::thread_base::m_global.lock);
torrent::main_thread()->interrupt();

xmlrpc_env localEnv;
xmlrpc_env_init(&localEnv);

Expand Down Expand Up @@ -607,7 +613,7 @@ RpcXml::insert_command(const char* name, const char* parm, const char* doc) {
doc);

if (localEnv.fault_occurred)
throw torrent::internal_error("Fault occured while inserting xmlrpc call.");
throw ::torrent::internal_error("Fault occured while inserting xmlrpc call.");

xmlrpc_env_clean(&localEnv);
}
Expand Down
6 changes: 3 additions & 3 deletions src/rpc/scgi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ SCgi::receive_call(SCgiTask* task, const char* buffer, uint32_t length) {
break;
case SCgiTask::ContentType::XML:
default:
torrent::thread_base::acquire_global_lock();
torrent::main_thread()->interrupt();
// torrent::thread_base::acquire_global_lock();
// torrent::main_thread()->interrupt();
result = rpc.dispatch(RpcManager::RPCType::XML, buffer, length, callback);
torrent::thread_base::release_global_lock();
// torrent::thread_base::release_global_lock();
}

return result;
Expand Down
3 changes: 1 addition & 2 deletions src/thread_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ void
ThreadWorker::msg_change_rpc_log(ThreadBase* baseThread) {
ThreadWorker* thread = (ThreadWorker*)baseThread;

acquire_global_lock();
std::unique_lock lock(m_global.lock);
thread->change_rpc_log();
release_global_lock();
}

void
Expand Down
3 changes: 1 addition & 2 deletions src/websockets_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void
WebsocketsThread::set_rpc_log(const std::string &filename) {
m_rpcLog = filename;

torrent::thread_base::acquire_global_lock();
std::unique_lock lock(torrent::thread_base::m_global.lock);

if (m_log_fd != -1) {
::close(m_log_fd);
Expand All @@ -53,7 +53,6 @@ WebsocketsThread::set_rpc_log(const std::string &filename) {
}
control->core()->push_log_std("Logging RPC events to '" + m_rpcLog + "'.");

torrent::thread_base::release_global_lock();
}

void
Expand Down

0 comments on commit 6e422b0

Please sign in to comment.