Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions include/ts/ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,26 @@ TSReturnCode TSMutexLockTry(TSMutex mutexp);

void TSMutexUnlock(TSMutex mutexp);

/** Scoped lock guard for a @c TSMutex.

Locks @a mutexp on construction and unlocks it when the guard leaves scope.
*/
class TSMutexLockGuard
{
public:
explicit TSMutexLockGuard(TSMutex mutexp) : m_mutex(mutexp) { TSMutexLock(m_mutex); }
Copy link
Copy Markdown
Contributor

@zwoop zwoop May 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[[nodiscard("Did you forget to name your lock guard variable?")]]
explicit TSMutexLockGuard(TSMutex mutexp) : m_mutex(mutexp) { TSMutexLock(m_mutex); }

or some such ? This prevents a user from accidentally making a no-op lock guard (which almost certainly is not what they want). Albeit nitpicky, it feels useful in a public (plugin) API.


TSMutexLockGuard(const TSMutexLockGuard &) = delete;
TSMutexLockGuard &operator=(const TSMutexLockGuard &) = delete;
TSMutexLockGuard(TSMutexLockGuard &&) = delete;
TSMutexLockGuard &operator=(TSMutexLockGuard &&) = delete;

~TSMutexLockGuard() { TSMutexUnlock(m_mutex); }

private:
TSMutex m_mutex = nullptr;
};

/* --------------------------------------------------------------------------
cachekey */
/**
Expand Down
3 changes: 1 addition & 2 deletions plugins/background_fetch/background_fetch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,13 @@ class BgFetchState
{
bool ret;

TSMutexLock(_lock);
TSMutexLockGuard lock(_lock);
if (_urls.end() == _urls.find(url)) {
ret = false;
} else {
_urls.erase(url);
ret = true;
}
TSMutexUnlock(_lock);

return ret;
}
Expand Down
35 changes: 15 additions & 20 deletions plugins/certifier/certifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ class SslLRUList
SSL_CTX *
lookup_and_create(const char *servername, void *edata, bool &wontdo)
{
SslData *ssl_data = nullptr;
scoped_SslData scoped_ssl_data = nullptr;
SSL_CTX *ref_ctx = nullptr;
std::string commonName(servername);
TSMutexLock(list_mutex);
auto dataItr = cnDataMap.find(commonName);
SslData *ssl_data = nullptr;
scoped_SslData scoped_ssl_data = nullptr;
SSL_CTX *ref_ctx = nullptr;
std::string commonName(servername);
TSMutexLockGuard lock(list_mutex);
auto dataItr = cnDataMap.find(commonName);
/// If such a context exists in dict
if (dataItr != cnDataMap.end()) {
/// Reuse context if already built, self queued if not
Expand Down Expand Up @@ -165,7 +165,6 @@ class SslLRUList
ssl_data->scheduled = true;
}
}
TSMutexUnlock(list_mutex);
return ref_ctx;
}

Expand Down Expand Up @@ -265,42 +264,38 @@ class SslLRUList
SslData *
get_newest()
{
TSMutexLock(list_mutex);
SslData *ret = head;
TSMutexUnlock(list_mutex);
TSMutexLockGuard lock(list_mutex);
SslData *ret = head;
return ret;
}

SslData *
get_oldest()
{
TSMutexLock(list_mutex);
SslData *ret = tail;
TSMutexUnlock(list_mutex);
TSMutexLockGuard lock(list_mutex);
SslData *ret = tail;
return ret;
}

int
get_size()
{
TSMutexLock(list_mutex);
int ret = size;
TSMutexUnlock(list_mutex);
TSMutexLockGuard lock(list_mutex);
int ret = size;
return ret;
}

// Set scheduled flag
int
set_schedule(const std::string &commonName, bool flag)
{
int ret = -1;
TSMutexLock(list_mutex);
auto iter = cnDataMap.find(commonName);
int ret = -1;
TSMutexLockGuard lock(list_mutex);
auto iter = cnDataMap.find(commonName);
if (iter != cnDataMap.end()) {
iter->second->scheduled = flag;
ret = 0;
}
TSMutexUnlock(list_mutex);
return ret;
}
};
Expand Down
3 changes: 1 addition & 2 deletions plugins/experimental/cache_fill/background_fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,13 @@ class BgFetchState
{
bool ret;

TSMutexLock(_lock);
TSMutexLockGuard lock(_lock);
if (_urls.end() == _urls.find(url)) {
ret = false;
} else {
_urls.erase(url);
ret = true;
}
TSMutexUnlock(_lock);

return ret;
}
Expand Down
16 changes: 4 additions & 12 deletions plugins/experimental/rate_limit/ip_reputation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ SieveLru::parseYaml(const YAML::Node &node)
std::tuple<uint32_t, uint32_t>
SieveLru::increment(KeyClass key)
{
TSMutexLock(_lock);
TSMutexLockGuard lock(_lock);
TSAssert(_initialized);

auto map_it = _map.find(key);
Expand All @@ -166,7 +166,6 @@ SieveLru::increment(KeyClass key)
lru->push_front({key, 1, entryBucket(), SystemClock::now()});
}
_map[key] = lru->begin();
TSMutexUnlock(_lock);

return {entryBucket(), 1};
} else {
Expand Down Expand Up @@ -213,7 +212,6 @@ SieveLru::increment(KeyClass key)
lru->moveTop(lru.get(), map_item);
}
}
TSMutexUnlock(_lock);

return {bucket, count};
}
Expand All @@ -223,21 +221,17 @@ SieveLru::increment(KeyClass key)
std::tuple<uint32_t, uint32_t>
SieveLru::lookup(KeyClass key) const
{
TSMutexLock(_lock);
TSMutexLockGuard lock(_lock);
TSAssert(_initialized);

auto map_it = _map.find(key);

if (_map.end() == map_it) {
TSMutexUnlock(_lock);

return {0, entryBucket()}; // Nothing found, return 0 hits and the entry bucket #
} else {
auto &[map_key, map_item] = *map_it;
auto &[list_key, count, bucket, added] = *map_item;

TSMutexUnlock(_lock);

return {bucket, count};
}
}
Expand All @@ -247,7 +241,7 @@ SieveLru::lookup(KeyClass key) const
int32_t
SieveLru::move_bucket(KeyClass key, uint32_t to_bucket)
{
TSMutexLock(_lock);
TSMutexLockGuard lock(_lock);
TSAssert(_initialized);

auto map_it = _map.find(key);
Expand Down Expand Up @@ -289,7 +283,6 @@ SieveLru::move_bucket(KeyClass key, uint32_t to_bucket)
added = SystemClock::now();
}
}
TSMutexUnlock(_lock);

return to_bucket; // Just as a convenience, return the destination bucket for this entry
}
Expand Down Expand Up @@ -336,7 +329,7 @@ SieveBucket::memorySize() const
size_t
SieveLru::memoryUsed() const
{
TSMutexLock(_lock);
TSMutexLockGuard lock(_lock);
TSAssert(_initialized);

size_t total = sizeof(SieveLru);
Expand All @@ -347,7 +340,6 @@ SieveLru::memoryUsed() const

total += _map.size() * (sizeof(void *) + sizeof(SieveBucket::iterator));
total += _map.bucket_count() * (sizeof(size_t) + sizeof(void *));
TSMutexUnlock(_lock);

return total;
}
Expand Down
7 changes: 4 additions & 3 deletions plugins/experimental/stale_response/stale_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,12 @@ free_state_info(StateInfo *state)
int64_t
aync_memory_total_add(ConfigInfo *plugin_config, int64_t change)
{
int64_t total;
TSMutexLock(plugin_config->body_data_mutex);
int64_t total;
TSMutexLockGuard lock(plugin_config->body_data_mutex);

plugin_config->body_data_memory_usage += change;
total = plugin_config->body_data_memory_usage;
TSMutexUnlock(plugin_config->body_data_mutex);

return total;
}
/*-----------------------------------------------------------------------------------------------*/
Expand Down
4 changes: 1 addition & 3 deletions plugins/experimental/system_stats/system_stats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ statAdd(const char *name, TSRecordDataType record_type, TSMutex create_mutex)
{
int stat_id = -1;

TSMutexLock(create_mutex);
TSMutexLockGuard lock(create_mutex);

if (TS_ERROR == TSStatFindName(name, &stat_id)) {
stat_id = TSStatCreate(name, record_type, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_SUM);
Expand All @@ -114,8 +114,6 @@ statAdd(const char *name, TSRecordDataType record_type, TSMutex create_mutex)
}
}

TSMutexUnlock(create_mutex);

return stat_id;
}

Expand Down
7 changes: 3 additions & 4 deletions plugins/experimental/wasm/ats_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,9 @@ Context::getConfiguration()
WasmResult
Context::setTimerPeriod(std::chrono::milliseconds period, uint32_t *timer_token_ptr)
{
Wasm *wasm = this->wasm();
Context *root_context = this->root_context();
TSMutexLock(wasm->mutex());
Wasm *wasm = this->wasm();
Context *root_context = this->root_context();
TSMutexLockGuard lock(wasm->mutex());
if (!wasm->existsTimerPeriod(root_context->id())) {
Dbg(dbg_ctl, "[%s] no previous timer period set", __FUNCTION__);
TSCont contp = root_context->scheduler_cont();
Expand All @@ -511,7 +511,6 @@ Context::setTimerPeriod(std::chrono::milliseconds period, uint32_t *timer_token_

wasm->setTimerPeriod(root_context->id(), period);
*timer_token_ptr = 0;
TSMutexUnlock(wasm->mutex());
return WasmResult::Ok;
}

Expand Down
7 changes: 2 additions & 5 deletions plugins/experimental/wasm/wasm_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,13 @@ schedule_handler(TSCont contp, TSEvent /*event*/, void * /*data*/)

auto *c = static_cast<ats_wasm::Context *>(TSContDataGet(contp));

auto *old_wasm = static_cast<ats_wasm::Wasm *>(c->wasm());
TSMutexLock(old_wasm->mutex());
auto *old_wasm = static_cast<ats_wasm::Wasm *>(c->wasm());
TSMutexLockGuard lock(old_wasm->mutex());

c->onTick(0); // use 0 as token

if (wasm_config->configs.empty()) {
TSError("[wasm][%s] Configuration objects are empty", __FUNCTION__);
TSMutexUnlock(old_wasm->mutex());
return 0;
}

Expand Down Expand Up @@ -365,8 +364,6 @@ schedule_handler(TSCont contp, TSEvent /*event*/, void * /*data*/)
Dbg(ats_wasm::dbg_ctl, "[%s] config wasm has changed. thus not scheduling", __FUNCTION__);
}

TSMutexUnlock(old_wasm->mutex());

return 0;
}

Expand Down
5 changes: 1 addition & 4 deletions plugins/lua/ts_lua.cc
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ ts_lua_remap_plugin_init(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
pthread_setspecific(lua_state_key, main_ctx);
}

TSMutexLock(main_ctx->mutexp);
TSMutexLockGuard lock(main_ctx->mutexp);

http_ctx = ts_lua_create_http_ctx(main_ctx, instance_conf);

Expand All @@ -551,7 +551,6 @@ ts_lua_remap_plugin_init(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
if (lua_type(L, -1) != LUA_TFUNCTION) {
lua_pop(L, 1);
ts_lua_destroy_http_ctx(http_ctx);
TSMutexUnlock(main_ctx->mutexp);
return TSREMAP_NO_REMAP;
}

Expand All @@ -574,8 +573,6 @@ ts_lua_remap_plugin_init(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
ts_lua_destroy_http_ctx(http_ctx);
}

TSMutexUnlock(main_ctx->mutexp);

return TSRemapStatus(ret);
}

Expand Down
3 changes: 1 addition & 2 deletions plugins/lua/ts_lua_fetch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ ts_lua_fetch_multi_handler(TSCont contp, TSEvent event ATS_UNUSED, void *edata)
}

// all finish
TSMutexLock(lmutex);
TSMutexLockGuard lock(lmutex);

if (fmi->total == 1 && !fmi->multi) {
ts_lua_fill_one_result(L, fi);
Expand All @@ -559,7 +559,6 @@ ts_lua_fetch_multi_handler(TSCont contp, TSEvent event ATS_UNUSED, void *edata)
TSContCall(ci->contp, TSEvent(TS_LUA_EVENT_COROUTINE_CONT), reinterpret_cast<void *>(1));
}

TSMutexUnlock(lmutex);
return 0;
}

Expand Down
Loading