Fix semaphores and mutexes

This commit is contained in:
momo5502
2025-01-24 13:00:06 +01:00
parent 5160bacd1e
commit 32d91bd139
5 changed files with 87 additions and 9 deletions

View File

@@ -108,17 +108,17 @@ struct mutant : ref_counted_object
return true;
}
uint32_t release()
std::pair<uint32_t, bool> release(const uint32_t thread_id)
{
const auto old_count = this->locked_count;
if (this->locked_count <= 0)
if (this->locked_count <= 0 || this->owning_thread_id != thread_id)
{
return old_count;
return {old_count, false};
}
--this->locked_count;
return old_count;
return {old_count, true};
}
void serialize(utils::buffer_serializer& buffer) const
@@ -239,9 +239,34 @@ struct section
struct semaphore : ref_counted_object
{
std::u16string name{};
volatile uint32_t current_count{};
uint32_t current_count{};
uint32_t max_count{};
bool try_lock()
{
if (this->current_count > 0)
{
--this->current_count;
return true;
}
return false;
}
std::pair<uint32_t, bool> release(const uint32_t release_count)
{
const auto old_count = this->current_count;
if (this->current_count + release_count > this->max_count)
{
return {old_count, false};
}
this->current_count += release_count;
return {old_count, true};
}
void serialize(utils::buffer_serializer& buffer) const
{
buffer.write(this->name);