Skip to content

fixed some safety and potential perf issues #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
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
96 changes: 96 additions & 0 deletions aws-cpp-sdk-core/include/aws/core/utils/memory/ObjectPool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#pragma once

#include <atomic>
#include <inttypes.h>


namespace Aws
{
namespace Utils
{
namespace Memory
{


#define ALIGNMENT 8

#ifdef WIN32
#define EXPLICIT_MEMORY_ALIGNMENT __declspec(align(ALIGNMENT))
#define MEM_ALLOC(x) _aligned_malloc(x, ALIGNMENT)
#define MEM_FREE(x) _aligned_free(x)
#elif defined __APPLE__
#include <stdlib.h>
#define EXPLICIT_MEMORY_ALIGNMENT __attribute__((aligned(ALIGNMENT)))
#define MEM_ALLOC(x) malloc(x)
#define MEM_FREE(x) free(x)
#else
#include <malloc.h>
#define EXPLICIT_MEMORY_ALIGNMENT __attribute__((aligned(ALIGNMENT)))
#define MEM_ALLOC(x) memalign(ALIGNMENT, x)
#define MEM_FREE(x) free(x)
#endif


template <class T>
class ObjectPool
{
public:

enum
{
POOL_MAX_SIZE = 4096, ///< must be power of 2
POOL_SIZE_MASK = POOL_MAX_SIZE - 1
};

/// memory pre- allocation is just optional :)
static void PrepareAllocation()
{
for (int i = 0; i < POOL_MAX_SIZE; ++i)
mPool[i] = MEM_ALLOC(sizeof(T));

mTailPos.fetch_add(POOL_MAX_SIZE);
}

static void* operator new(size_t objSize)
{
uint64_t popPos = mHeadPos.fetch_add(1);

void* popVal = std::atomic_exchange(&mPool[popPos & POOL_SIZE_MASK], (void*)nullptr);
if (popVal != nullptr)
return popVal;

return MEM_ALLOC(objSize);
}

static void operator delete(void* obj)
{
uint64_t insPos = mTailPos.fetch_add(1);

void* prevVal = std::atomic_exchange(&mPool[insPos & POOL_SIZE_MASK], obj);

if (prevVal != nullptr)
MEM_FREE(prevVal);
}


private:

static std::atomic<void*> EXPLICIT_MEMORY_ALIGNMENT mPool[POOL_MAX_SIZE];
static std::atomic<uint64_t> EXPLICIT_MEMORY_ALIGNMENT mHeadPos;
static std::atomic<uint64_t> EXPLICIT_MEMORY_ALIGNMENT mTailPos;

static_assert((POOL_MAX_SIZE & POOL_SIZE_MASK) == 0x0, "pool's size must be power of 2");
};

template <class T>
std::atomic<void*> ObjectPool<T>::mPool[POOL_MAX_SIZE] = {};

template <class T>
std::atomic<uint64_t> ObjectPool<T>::mHeadPos(0);

template <class T>
std::atomic<uint64_t> ObjectPool<T>::mTailPos(0);

} // namespace Memory
} // namespace Utils
} // namespace Aws
57 changes: 32 additions & 25 deletions aws-cpp-sdk-core/include/aws/core/utils/threading/Executor.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
/*
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

/*
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#pragma once



#include <aws/core/Core_EXPORTS.h>



#include <functional>
#include <aws/core/utils/memory/stl/AWSFunction.h>
#include <aws/core/utils/memory/stl/AWSFunction.h>

namespace Aws
{
namespace Utils
Expand All @@ -34,20 +37,23 @@ namespace Threading
public:
Executor() {}
virtual ~Executor(){}



template<class Fn, class ... Args>
bool Submit(Fn&& fn, Args&& ... args)
{
return SubmitToThread(AWS_BUILD_TYPED_FUNCTION(std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...), void()));
}



protected:
/**
* To implement your own executor implementation, then simply subclass Executor and implement this method.
*/
virtual bool SubmitToThread(std::function<void()>&&) = 0;
};



/**
* Default Executor implementation. Simply spawns a thread and detaches it.
*/
Expand All @@ -58,7 +64,8 @@ namespace Threading
protected:
bool SubmitToThread(std::function<void()>&&);
};

} // namespace Threading
} // namespace Utils
} // namespace Aws


} // namespace Threading
} // namespace Utils
} // namespace Aws
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#pragma once

#include <functional>
#include <atomic>
#include <memory>
#include <thread>
#include <chrono>
#include <assert.h>

#include <aws/core/utils/threading/Executor.h>
#include <aws/core/utils/memory/ObjectPool.h>

namespace Aws
{
namespace Utils
{
namespace Threading
{

struct NodeEntry
{
NodeEntry() : mNext(nullptr) {}
NodeEntry* volatile mNext;
};

struct JobEntry : public Aws::Utils::Memory::ObjectPool<JobEntry>
{
JobEntry(std::function<void()>&& job) : mTask(std::move(job)) {}
virtual ~JobEntry() {}

NodeEntry mNodeEntry;

std::function<void()> mTask;
};

class JobQueue
{
public:
JobQueue() : mHead(&mStub), mTail(&mStub)
{
mOffset = offsetof(struct JobEntry, mNodeEntry);
assert(mHead.is_lock_free());
}
~JobQueue() {}

/// mutiple produce
void Push(JobEntry* newData)
{
NodeEntry* prevNode = (NodeEntry*)std::atomic_exchange_explicit(&mHead,
&newData->mNodeEntry, std::memory_order_acq_rel);

prevNode->mNext = &(newData->mNodeEntry);
}

/// single consume
JobEntry* Pop()
{
NodeEntry* tail = mTail;
NodeEntry* next = tail->mNext;

if (tail == &mStub)
{
/// in case of empty
if (nullptr == next)
return nullptr;

/// first pop
mTail = next;
tail = next;
next = next->mNext;
}

/// in most cases...
if (next)
{
mTail = next;

return reinterpret_cast<JobEntry*>(reinterpret_cast<int64_t>(tail)-mOffset);
}

NodeEntry* head = mHead;
if (tail != head)
return nullptr;

/// last pop
mStub.mNext = nullptr;

NodeEntry* prevNode = (NodeEntry*)std::atomic_exchange_explicit(&mHead,
&mStub, std::memory_order_acq_rel);

prevNode->mNext = &mStub;

next = tail->mNext;
if (next)
{
mTail = next;

return reinterpret_cast<JobEntry*>(reinterpret_cast<int64_t>(tail)-mOffset);
}

return nullptr;
}

private:

std::atomic<NodeEntry*> mHead;

NodeEntry* mTail;
NodeEntry mStub;

int64_t mOffset;
};


class AWS_CORE_API YetAnotherDefaultExecutor : public Executor
{
public:
YetAnotherDefaultExecutor();
~YetAnotherDefaultExecutor();

protected:
bool SubmitToThread(std::function<void()>&&); ///< Push a task (multi-produce)

private:
void RunExecutor(); ///< Run (single-consume)

private:
bool mStopRequested;

JobQueue mJobQueue;
std::thread mWorkerThread;
};


} // namespace Threading
} // namespace Utils
} // namespace Aws
6 changes: 3 additions & 3 deletions aws-cpp-sdk-core/source/client/ClientConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <aws/core/client/DefaultRetryStrategy.h>
#include <aws/core/utils/memory/AWSMemory.h>
#include <aws/core/utils/threading/Executor.h>
#include <aws/core/utils/threading/YetAnotherDefaultExecutor.h>
#include <aws/core/utils/memory/stl/AWSStringStream.h>
#include <aws/core/utils/OSVersionInfo.h>
#include <aws/core/Version.h>
Expand All @@ -40,13 +40,13 @@ static Aws::String ComputeUserAgentString()
ClientConfiguration::ClientConfiguration() :
userAgent(ComputeUserAgentString()),
scheme(Aws::Http::Scheme::HTTPS),
region(Region::US_EAST_1),
region(Region::AP_NORTHEAST_1),
maxConnections(25),
requestTimeoutMs(3000),
connectTimeoutMs(1000),
retryStrategy(Aws::MakeShared<DefaultRetryStrategy>(allocationTag)),
proxyPort(0),
executor(Aws::MakeShared<Aws::Utils::Threading::DefaultExecutor>(allocationTag)),
executor(Aws::MakeShared<Aws::Utils::Threading::YetAnotherDefaultExecutor>(allocationTag)),
verifySSL(true),
writeRateLimiter(nullptr),
readRateLimiter(nullptr),
Expand Down
Loading