mcatan 2004/04/02 00:41:21
Modified: include/log4cxx/helpers criticalsection.h
src criticalsection.cpp
Log:
optimization
Revision Changes Path
1.5 +13 -1 logging-log4cxx/include/log4cxx/helpers/criticalsection.h
Index: criticalsection.h
===================================================================
RCS file:
/home/cvs/logging-log4cxx/include/log4cxx/helpers/criticalsection.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- criticalsection.h 20 Jan 2004 22:16:12 -0000 1.4
+++ criticalsection.h 2 Apr 2004 08:41:21 -0000 1.5
@@ -19,6 +19,12 @@
#include <log4cxx/config.h>
+#ifdef HAVE_PTHREAD
+#include <pthread.h>
+#elif defined(HAVE_MS_THREAD)
+#include <windows.h>
+#endif
+
namespace log4cxx
{
namespace helpers
@@ -30,8 +36,14 @@
~CriticalSection();
void lock();
void unlock();
+ unsigned long getOwningThread();
- void * mutex;
+#ifdef HAVE_PTHREAD
+ pthread_mutex_t mutex;
+#elif defined(HAVE_MS_THREAD)
+ CRITICAL_SECTION mutex;
+#endif
+ unsigned long owningThread;
};
/** CriticalSection helper class to be used on call stack
1.5 +19 -20 logging-log4cxx/src/criticalsection.cpp
Index: criticalsection.cpp
===================================================================
RCS file: /home/cvs/logging-log4cxx/src/criticalsection.cpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- criticalsection.cpp 9 Aug 2003 08:11:22 -0000 1.4
+++ criticalsection.cpp 2 Apr 2004 08:41:21 -0000 1.5
@@ -11,59 +11,58 @@
* *
* This software is published under the terms of the Apache Software *
* License version 1.1, a copy of which has been included with this *
- * distribution in the LICENSE.txt file. *
+ * distribution in the license.apl file. *
***************************************************************************/
#include <log4cxx/helpers/criticalsection.h>
-
-#ifdef HAVE_PTHREAD
-#include <pthread.h>
-#elif defined(HAVE_MS_THREAD)
-#include <windows.h>
-#endif
+#include <log4cxx/helpers/thread.h>
using namespace log4cxx::helpers;
-CriticalSection::CriticalSection()
+CriticalSection::CriticalSection() : owningThread(0)
{
#ifdef HAVE_PTHREAD
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
- mutex = new pthread_mutex_t;
- pthread_mutex_init((pthread_mutex_t*)mutex, &attr);
+ pthread_mutex_init(&mutex, &attr);
pthread_mutexattr_destroy(&attr);
#elif defined(HAVE_MS_THREAD)
- mutex = new CRITICAL_SECTION;
- InitializeCriticalSection((CRITICAL_SECTION *)mutex);
+ InitializeCriticalSection(&mutex);
#endif
}
CriticalSection::~CriticalSection()
{
#ifdef HAVE_PTHREAD
- pthread_mutex_destroy((pthread_mutex_t*)mutex);
- delete (pthread_mutex_t*)mutex;
+ pthread_mutex_destroy(&mutex);
#elif defined(HAVE_MS_THREAD)
- DeleteCriticalSection((CRITICAL_SECTION *)mutex);
- delete (CRITICAL_SECTION *)mutex;
+ DeleteCriticalSection(&mutex);
#endif
}
void CriticalSection::lock()
{
#ifdef HAVE_PTHREAD
- pthread_mutex_lock((pthread_mutex_t*)mutex);
+ pthread_mutex_lock(&mutex);
#elif defined(HAVE_MS_THREAD)
- EnterCriticalSection((CRITICAL_SECTION *)mutex);
+ EnterCriticalSection(&mutex);
#endif
+ owningThread = Thread::getCurrentThreadId();
}
void CriticalSection::unlock()
{
+ owningThread = 0;
+
#ifdef HAVE_PTHREAD
- pthread_mutex_unlock((pthread_mutex_t*)mutex);
+ pthread_mutex_unlock(&mutex);
#elif defined(HAVE_MS_THREAD)
- LeaveCriticalSection((CRITICAL_SECTION *)mutex);
+ LeaveCriticalSection(&mutex);
#endif
+}
+
+unsigned long CriticalSection::getOwningThread()
+{
+ return owningThread;
}