Permalink
Browse files

Vararg min() & max(), const String& Thread::name()

- ozCore
  * variable argument min() & max()
  * String: public STATIC_SIZE <- BUFFER_SIZE
  * Thread::name(): const char* -> const String&
  • Loading branch information...
1 parent 1f0d366 commit 06ea98bfcd0c22fa47a4dab8cde9175d0aef4a8e @ducakar committed Sep 14, 2017
Showing with 59 additions and 44 deletions.
  1. +2 −2 src/ozCore/Barrier.hh
  2. +12 −12 src/ozCore/String.cc
  3. +9 −11 src/ozCore/String.hh
  4. +13 −15 src/ozCore/Thread.cc
  5. +1 −1 src/ozCore/Thread.hh
  6. +19 −0 src/ozCore/common.hh
  7. +3 −3 src/ozEngine/Lua.hh
View
@@ -45,12 +45,12 @@ private:
struct Descriptor;
- Descriptor* descriptor_ = nullptr; ///< Internal semaphore descriptor.
+ Descriptor* descriptor_ = nullptr; ///< Internal barrier descriptor.
public:
/**
- * Create and initialise barrier.
+ * Create a barrier for synchonising a given number of threads.
*/
Barrier(int count);
View
@@ -38,13 +38,13 @@ char* String::resize(int newSize, bool keepContents)
OZ_ASSERT(size_ >= 0 && newSize >= 0);
OZ_ASSERT(!keepContents || newSize > size_);
- if (newSize < BUFFER_SIZE) {
- if (size_ >= BUFFER_SIZE) {
+ if (newSize < STATIC_SIZE) {
+ if (size_ >= STATIC_SIZE) {
delete[] data_ ;
}
size_ = newSize;
- return baseData_;
+ return staticData_;
}
if (newSize != size_) {
@@ -54,7 +54,7 @@ char* String::resize(int newSize, bool keepContents)
if (keepContents) {
memcpy(newBuffer, oldBuffer, min<int>(size_, newSize) + 1);
}
- if (size_ >= BUFFER_SIZE) {
+ if (size_ >= STATIC_SIZE) {
delete[] data_;
}
@@ -278,19 +278,19 @@ String::String(bool b)
String::String(int i, const char* format)
{
- size_ = snprintf(baseData_, BUFFER_SIZE, format, i);
+ size_ = snprintf(staticData_, STATIC_SIZE, format, i);
}
String::String(double d, const char* format)
{
- size_ = snprintf(baseData_, BUFFER_SIZE, format, d);
+ size_ = snprintf(staticData_, STATIC_SIZE, format, d);
}
#pragma GCC diagnostic pop
String::~String()
{
- if (size_ >= BUFFER_SIZE) {
+ if (size_ >= STATIC_SIZE) {
delete[] data_;
}
}
@@ -303,8 +303,8 @@ String::String(String&& other) noexcept
{
memcpy(this, &other, sizeof(String));
- other.size_ = 0;
- other.baseData_[0] = '\0';
+ other.size_ = 0;
+ other.staticData_[0] = '\0';
}
String& String::operator=(const String& other)
@@ -318,14 +318,14 @@ String& String::operator=(const String& other)
String& String::operator=(String&& other) noexcept
{
if (&other != this) {
- if (size_ >= BUFFER_SIZE) {
+ if (size_ >= STATIC_SIZE) {
delete[] data_;
}
memcpy(this, &other, sizeof(String));
- other.size_ = 0;
- other.baseData_[0] = '\0';
+ other.size_ = 0;
+ other.staticData_[0] = '\0';
}
return *this;
}
View
@@ -36,18 +36,16 @@ namespace oz
/**
* String.
*
- * Class has static storage of `BUFFER_SIZE` bytes, if the string is larger it is stored in a
+ * Class has static storage of `STATIC_SIZE` bytes, if the string is larger it is stored in a
* dynamically allocated storage. To deallocate storage just assign an empty string.
*/
class String
{
-private:
-
- /// Size of static buffer for short strin optimisation.
- static const int BUFFER_SIZE = 32 - sizeof(int);
-
public:
+ /// Size of static buffer for short string optimisation.
+ static const int STATIC_SIZE = 32 - sizeof(int);
+
/// Empty string. Useful when a function needs to return a reference to an empty string.
static const String EMPTY;
@@ -65,11 +63,11 @@ public:
private:
- int size_ = 0; ///< Length in bytes (without the final null char).
+ int size_ = 0; ///< Length in bytes (without the final null char).
union
{
- char* data_ = nullptr; ///< Pointer to the current buffer (iff not static).
- char baseData_[BUFFER_SIZE]; ///< Static buffer.
+ char* data_ = nullptr; ///< Pointer to the current buffer (iff not static).
+ char staticData_[STATIC_SIZE]; ///< Static buffer.
};
/**
@@ -438,7 +436,7 @@ public:
OZ_ALWAYS_INLINE
const char* begin() const
{
- return size_ < BUFFER_SIZE ? baseData_ : data_;
+ return size_ < STATIC_SIZE ? staticData_ : data_;
}
/**
@@ -447,7 +445,7 @@ public:
OZ_ALWAYS_INLINE
char* begin()
{
- return size_ < BUFFER_SIZE ? baseData_ : data_;
+ return size_ < STATIC_SIZE ? staticData_ : data_;
}
/**
View
@@ -41,8 +41,10 @@
namespace oz
{
-static const pthread_t MAIN_THREAD = pthread_self();
-static thread_local const char* threadName = "";
+static const String UNKNOWN_NAME = "UNKNOWN";
+static const String MAIN_NAME = "MAIN";
+static const pthread_t MAIN_THREAD = pthread_self();
+static thread_local const String* threadName = &UNKNOWN_NAME;
struct Thread::Descriptor
{
@@ -61,17 +63,12 @@ void* Thread::Descriptor::mainWrapper(void* handle)
Descriptor* descriptor = static_cast<Descriptor*>(handle);
Main* main = descriptor->main;
void* data = descriptor->data;
- char name[NAME_LENGTH + 1];
-
- if (descriptor->name != nullptr) {
- strncpy(name, descriptor->name, NAME_LENGTH);
- name[NAME_LENGTH] = '\0';
-
- threadName = name;
- }
+ String name = descriptor->name;
descriptor->lock.unlock();
+ threadName = &name;
+
#if defined(__ANDROID__)
JavaVM* javaVM = Java::vm();
@@ -119,9 +116,9 @@ void Thread::sleepUntil(Instant instant)
# endif
}
-const char* Thread::name()
+const String& Thread::name()
{
- return isMain() ? "MAIN" : threadName;
+ return isMain() ? MAIN_NAME : *threadName;
}
bool Thread::isMain()
@@ -144,9 +141,6 @@ Thread::Thread(const char* name, Main* main, void* data)
if (pthread_create(&descriptor_->thread, nullptr, Descriptor::mainWrapper, descriptor_) != 0) {
OZ_ERROR("oz::Thread: Thread creation failed");
}
-
- // Wait while the thread accesses name pointer and descriptor during its initialisation.
- descriptor_->lock.lock();
}
Thread::~Thread()
@@ -184,6 +178,10 @@ void Thread::detach()
pthread_detach(descriptor_->thread);
+ // Wait while the thread accesses name pointer and descriptor during its initialisation. We need this to assure the
+ // descriptor is not freed while the new thread still accesses it.
+ descriptor_->lock.lock();
+
free(descriptor_);
descriptor_ = nullptr;
}
View
@@ -72,7 +72,7 @@ public:
* This method always returns "main" for the main thread and "" for other threads that were not
* started through this class.
*/
- static const char* name();
+ static const String& name();
/**
* True iff called from the main thread.
View
@@ -340,6 +340,16 @@ inline constexpr const Value& min(const Value& a, const Value& b)
}
/**
+ * Minimum of more than two arguments.
+ */
+template <typename Value, typename ...Args>
+OZ_ALWAYS_INLINE
+inline constexpr const Value& min(const Value& a, const Value& b, const Args&... args)
+{
+ return min<Args...>(min<Value>(a, b), args...);
+}
+
+/**
* `a` if `a >= b`, `b` otherwise.
*/
template <typename Value>
@@ -348,6 +358,15 @@ inline constexpr const Value& max(const Value& a, const Value& b)
{
return a < b ? b : a;
}
+/**
+ *Maxiimum of more than two arguments.
+ */
+template <typename Value, typename ...Args>
+OZ_ALWAYS_INLINE
+inline constexpr const Value& max(const Value& a, const Value& b, const Args&... args)
+{
+ return max<Args...>(max<Value>(a, b), args...);
+}
/**
* `c` if `a <= c <= b`, respective boundary otherwise.
View
@@ -244,7 +244,7 @@ public:
/**
* Read stack values starting with (1-based) index N into given variables.
*/
- template <int N = 1, typename Head, typename... Tail>
+ template <int N = 1, typename Head, typename ...Tail>
void to(Head& value, Tail&... tail) const
{
read(N, value);
@@ -329,7 +329,7 @@ public:
/**
* Push values onto the stack.
*/
- template <typename Value, typename... Tail>
+ template <typename Value, typename ...Tail>
void pushValue(const Value& value, const Tail&... tail) const
{
pushValue(value);
@@ -433,7 +433,7 @@ public:
/**
* Call the current field (must be a function).
*/
- template <typename... Params>
+ template <typename ...Params>
Result operator()(const Params&... params) const
{
push();

0 comments on commit 06ea98b

Please sign in to comment.