Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
gleocadie committed Jun 24, 2024
1 parent a991df6 commit 48712f4
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ void LibrariesInfoCache::UpdateCache()
{
NbCallsToDlopenDlclose = nbCallsToDlopenDlclose;
IterationData data = {.Index = 0, .Cache = this};
LibrariesInfo.clear();
dl_iterate_phdr(
[](struct dl_phdr_info* info, std::size_t size, void* data) {
auto* iterationData = static_cast<IterationData*>(data);
Expand All @@ -68,9 +67,7 @@ void LibrariesInfoCache::UpdateCache()
},
&data);

// erase the remaining (closed) libraries if any
Libraries.pop_back();
Libraries.erase(Libraries.begin() + iterationData.Index, Libraries.end());
LibrariesInfo.erase(LibrariesInfo.begin() + data.Index, LibrariesInfo.end());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "IThreadInfo.h"
#include "ScopedHandle.h"
#include "Semaphore2.h"
#include "Semaphore.h"
#include "shared/src/native-src/string.h"

#include <atomic>
Expand Down
126 changes: 126 additions & 0 deletions profiler/src/ProfilerEngine/Datadog.Profiler.Native/Semaphore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc.

#pragma once

#include <condition_variable>
#include <mutex>

class Semaphore
{
public:
inline Semaphore();
explicit inline Semaphore(std::uint32_t initialCount);
inline Semaphore(std::uint32_t initialCount, std::uint32_t maxCount);

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

inline bool TryAcquire();
inline std::uint32_t Acquire();
inline std::uint32_t Release();
inline std::uint32_t GetCurrentCount() const;
inline std::uint32_t GetMaxCount() const;

private:
std::mutex _syncLock;
std::condition_variable _signal;
std::uint32_t _currentCount;
const std::uint32_t _maxCount;
};

class SemaphoreScope
{
public:
explicit inline SemaphoreScope(Semaphore& semaphore);
inline ~SemaphoreScope() noexcept;

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

private:
Semaphore& _semaphore;
};

inline Semaphore::Semaphore() :
Semaphore(1, 1)
{
}

inline Semaphore::Semaphore(std::uint32_t initialCount) :
Semaphore(initialCount, initialCount)
{
}

inline Semaphore::Semaphore(std::uint32_t initialCount, std::uint32_t maxCount) :
_currentCount{initialCount},
_maxCount{maxCount}
{
}

inline bool Semaphore::TryAcquire()
{
if (_currentCount > 0)
{
std::lock_guard<std::mutex> lock(_syncLock);

if (_currentCount > 0)
{
_currentCount--;
return true;
}
}

return false;
}

inline std::uint32_t Semaphore::Acquire()
{
std::unique_lock<std::mutex> lock(_syncLock);

while (_currentCount == 0)
{
_signal.wait(lock);
}

return --_currentCount;
}

inline std::uint32_t Semaphore::Release()
{
std::uint32_t newCount;
{
std::lock_guard<std::mutex> lock(_syncLock);
newCount = ++_currentCount;

if (_currentCount > _maxCount)
{
_currentCount = _maxCount;
}
}

_signal.notify_one();
return newCount;
}

inline std::uint32_t Semaphore::GetCurrentCount() const
{
return _currentCount;
}

inline std::uint32_t Semaphore::GetMaxCount() const
{
return _maxCount;
}

inline SemaphoreScope::SemaphoreScope(Semaphore& semaphore) :
_semaphore(semaphore)
{
_semaphore.Acquire();
}

inline SemaphoreScope::~SemaphoreScope() noexcept
{
_semaphore.Release();
}

0 comments on commit 48712f4

Please sign in to comment.