Permalink
Browse files

~sync with scx

  • Loading branch information...
1 parent 473205c commit 947c7631544c285160d919595a62c6372a7d1afc @bsdelf committed Apr 9, 2012
Showing with 175 additions and 62 deletions.
  1. +14 −14 sdk/scx/CondVar.hpp
  2. +65 −0 sdk/scx/FakeSemVar.hpp
  3. +2 −1 sdk/scx/Function.hpp
  4. +34 −6 sdk/scx/Mutex.hpp
  5. +26 −24 sdk/scx/PVBuffer.hpp
  6. +15 −1 sdk/scx/RunTimer.hpp
  7. +12 −9 sdk/scx/SemVar.hpp
  8. +7 −7 sdk/scx/Singleton.hpp
View
@@ -1,5 +1,5 @@
-#ifndef SCX_COND_VAR_HPP
-#define SCX_COND_VAR_HPP
+#ifndef SCX_CONDVAR_HPP
+#define SCX_CONDVAR_HPP
#include <pthread.h>
@@ -10,40 +10,40 @@ class Mutex;
class CondVar
{
public:
- explicit CondVar(Mutex& refMutex):
- m_refMutex(refMutex)
+ CondVar()
{
- pthread_cond_init(&m_Cond, NULL);
+ pthread_cond_init(&cond, NULL);
}
~CondVar()
{
- pthread_cond_destroy(&m_Cond);
+ pthread_cond_destroy(&cond);
}
- int Wait()
+ int Wait(Mutex* mutex)
{
- return pthread_cond_wait(&m_Cond, &(m_refMutex.m_Mutex));
+ return pthread_cond_wait(&cond, &(mutex->mutex));
}
int Signal()
{
- return pthread_cond_signal(&m_Cond);
+ return pthread_cond_signal(&cond);
}
- int SignalAll()
+ int Broadcast()
{
- return pthread_cond_broadcast(&m_Cond);
+ return pthread_cond_broadcast(&cond);
}
+ /*
void TimeWait()
{
- //pthread_cond_timewait(&m_Cond, &(m_refMutex.m_Mutex));
+ //pthread_cond_timewait(&cond, &(m_Mutex.m_Mutex));
}
+ */
private:
- pthread_cond_t m_Cond;
- Mutex& m_refMutex;
+ pthread_cond_t cond;
};
}
View
@@ -0,0 +1,65 @@
+#ifndef SCX_FAKESEMVAR_HPP
+#define SCX_FAKESEMVAR_HPP
+
+#include "Mutex.hpp"
+#include "CondVar.hpp"
+
+namespace scx {
+
+class FakeSemVar
+{
+public:
+ explicit FakeSemVar(int value = 0):
+ m_Value(value)
+ {
+ }
+
+ ~FakeSemVar()
+ {
+ }
+
+ void Post(int n = 1)
+ {
+ MutexLocker mlocker(&m_Mutex);
+ m_Value += n;
+ m_CondVar.Broadcast();
+ }
+
+ bool TryWait(int n = 1)
+ {
+ MutexLocker mlocker(&m_Mutex);
+ if (n > m_Value)
+ return false;
+ m_Value -= n;
+ return true;
+ }
+
+ void Wait(int n = 1)
+ {
+ MutexLocker mlocker(&m_Mutex);
+ while (n > m_Value)
+ m_CondVar.Wait(&m_Mutex);
+ m_Value -= n;
+ }
+
+ int GetValue() const
+ {
+ MutexLocker mlocker(const_cast<Mutex*>(&m_Mutex));
+ return m_Value;
+ }
+
+ void Clear()
+ {
+ MutexLocker mlocker(&m_Mutex);
+ m_Value = 0;
+ }
+
+private:
+ int m_Value;
+ Mutex m_Mutex;
+ CondVar m_CondVar;
+};
+
+}
+
+#endif
View
@@ -269,7 +269,8 @@ class FunctionPtr<ret_t (arg1_t, arg2_t)>:
SCX_FUNCTION_COPY_FUNCTION_PTR_COMMON(arg1_t, arg2_t);
public:
- ret_t operator()(arg1_t arg1, arg2_t arg2) const {
+ ret_t operator()(arg1_t arg1, arg2_t arg2) const
+ {
return m_Function(arg1, arg2);
}
};
View
@@ -14,31 +14,59 @@ friend class CondVar;
public:
Mutex()
{
- pthread_mutex_init(&m_Mutex, NULL);
+ pthread_mutex_init(&mutex, NULL);
}
~Mutex()
{
- pthread_mutex_destroy(&m_Mutex);
+ pthread_mutex_destroy(&mutex);
}
int Lock()
{
- return pthread_mutex_lock(&m_Mutex);
+ return pthread_mutex_lock(&mutex);
}
int Unlock()
{
- return pthread_mutex_unlock(&m_Mutex);
+ return pthread_mutex_unlock(&mutex);
}
int TryLock()
{
- return pthread_mutex_trylock(&m_Mutex);
+ return pthread_mutex_trylock(&mutex);
}
private:
- pthread_mutex_t m_Mutex;
+ pthread_mutex_t mutex;
+};
+
+class MutexLocker
+{
+public:
+ explicit MutexLocker(Mutex* mutex):
+ m_Mutex(mutex)
+ {
+ m_Mutex->Lock();
+ }
+
+ ~MutexLocker()
+ {
+ Unlock();
+ }
+
+ void Unlock()
+ {
+ m_Mutex->Unlock();
+ }
+
+ void Relock()
+ {
+ m_Mutex->Lock();
+ }
+
+private:
+ Mutex* m_Mutex;
};
}
View
@@ -3,7 +3,14 @@
#include <deque>
#include "Mutex.hpp"
+#ifndef __MACH__
#include "SemVar.hpp"
+#else
+#include "FakeSemVar.hpp"
+namespace scx {
+typedef FakeSemVar SemVar;
+}
+#endif
namespace scx {
@@ -12,18 +19,13 @@ class PVBuffer
{
public:
PVBuffer():
- m_pFreeListSemVar(new SemVar(0, 0)),
- m_pDataListSemVar(new SemVar(0, 0))
+ m_FreeListSemVar(0),
+ m_DataListSemVar(0)
{
}
~PVBuffer()
{
- if (m_pFreeListSemVar != NULL)
- delete m_pFreeListSemVar;
-
- if (m_pDataListSemVar != NULL)
- delete m_pDataListSemVar;
}
void AllocBuffer(size_t bufCount)
@@ -34,10 +36,10 @@ class PVBuffer
for (size_t i = 0; i < bufCount; ++i) {
m_BufferQueue[i] = new item_t;
m_FreeQueue[i] = m_BufferQueue[i];
- m_pFreeListSemVar->Post();
+ m_FreeListSemVar.Post();
}
- m_pDataListSemVar->Clear();
+ m_DataListSemVar.Clear();
}
void ClearBuffer()
@@ -50,8 +52,8 @@ class PVBuffer
m_FreeQueue.clear();
m_DataQueue.clear();
- m_pFreeListSemVar->Clear();
- m_pDataListSemVar->Clear();
+ m_FreeListSemVar.Clear();
+ m_DataListSemVar.Clear();
}
/**
@@ -67,17 +69,17 @@ class PVBuffer
// No mutex here,
// because we assume both thread has been suspended.
m_DataQueue.clear();
- m_pDataListSemVar->Clear();
+ m_DataListSemVar.Clear();
// Lock the FreeQueue first,
// because after SemVar::Post() the producer will begin to work,
// and it will take the first item in FreeQueue.
//m_FreeListMutex.Lock();
m_FreeQueue.resize(bufCount);
- m_pFreeListSemVar->Clear();
+ m_FreeListSemVar.Clear();
for (size_t i = 0; i < bufCount; ++i) {
m_FreeQueue[i] = m_BufferQueue[i];
- m_pFreeListSemVar->Post();
+ m_FreeListSemVar.Post();
}
//m_FreeListMutex.Unlock();
}
@@ -89,12 +91,12 @@ class PVBuffer
size_t GetFreeCount() const
{
- return m_pFreeListSemVar->GetValue();
+ return m_FreeListSemVar.GetValue();
}
size_t GetDataCount() const
{
- return m_pDataListSemVar->GetValue();
+ return m_DataListSemVar.GetValue();
}
/**
@@ -107,7 +109,7 @@ class PVBuffer
item_t* TakeFree()
{
- m_pFreeListSemVar->Wait();
+ m_FreeListSemVar.Wait();
m_FreeListMutex.Lock();
item_t* pItem = m_FreeQueue.front();
m_FreeQueue.pop_front();
@@ -120,12 +122,12 @@ class PVBuffer
m_DataListMutex.Lock();
m_DataQueue.push_back(pItem);
m_DataListMutex.Unlock();
- m_pDataListSemVar->Post();
+ m_DataListSemVar.Post();
}
item_t* TakeData()
{
- m_pDataListSemVar->Wait();
+ m_DataListSemVar.Wait();
m_DataListMutex.Lock();
item_t* pItem = m_DataQueue.front();
m_DataQueue.pop_front();
@@ -138,7 +140,7 @@ class PVBuffer
m_FreeListMutex.Lock();
m_FreeQueue.push_back(pItem);
m_FreeListMutex.Unlock();
- m_pFreeListSemVar->Post();
+ m_FreeListSemVar.Post();
}
/**
@@ -153,7 +155,7 @@ class PVBuffer
*/
void ClearFree()
{
- m_pFreeListSemVar->Clear();
+ m_FreeListSemVar.Clear();
m_FreeListMutex.Lock();
m_FreeQueue.clear();
m_FreeListMutex.Unlock();
@@ -164,7 +166,7 @@ class PVBuffer
*/
void ClearData()
{
- m_pDataListSemVar->Clear();
+ m_DataListSemVar.Clear();
m_DataListMutex.Lock();
m_DataQueue.clear();
m_DataListMutex.Unlock();
@@ -175,11 +177,11 @@ class PVBuffer
std::deque<item_t*> m_FreeQueue;
Mutex m_FreeListMutex;
- SemVar* m_pFreeListSemVar;
+ SemVar m_FreeListSemVar;
std::deque<item_t*> m_DataQueue;
Mutex m_DataListMutex;
- SemVar* m_pDataListSemVar;
+ SemVar m_DataListSemVar;
};
}
Oops, something went wrong.

0 comments on commit 947c763

Please sign in to comment.