aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-06-30 00:50:57 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-06-30 00:50:57 +0000
commit4082c5da864b30104810c7dd3104a31b914b7cf7 (patch)
treefe696466d84cafa8d601344e5bf5b2759582d041
parent77706e5457f8cd6a98857301a79477e899d3c09a (diff)
downloadpaludis-4082c5da864b30104810c7dd3104a31b914b7cf7.tar.gz
paludis-4082c5da864b30104810c7dd3104a31b914b7cf7.tar.xz
r3671@snowflake: ciaranm | 2007-06-30 01:50:17 +0100
More thread work
-rw-r--r--paludis/util/exception.cc18
-rw-r--r--paludis/util/log.cc16
-rw-r--r--paludis/util/thread.cc56
-rw-r--r--paludis/util/thread.hh10
-rw-r--r--paludis/util/thread_TEST.cc22
5 files changed, 29 insertions, 93 deletions
diff --git a/paludis/util/exception.cc b/paludis/util/exception.cc
index 48177d7..bbf04c4 100644
--- a/paludis/util/exception.cc
+++ b/paludis/util/exception.cc
@@ -20,6 +20,11 @@
#include <paludis/util/exception.hh>
#include <paludis/util/stringify.hh>
#include <libebt/libebt.hh>
+
+#ifdef PALUDIS_ENABLE_THREADS
+# include <libebt/libebt_pthread_threads.hh>
+#endif
+
#include "config.h"
#ifdef HAVE_CXA_DEMANGLE
@@ -33,6 +38,19 @@ namespace
struct ContextTag;
}
+#ifdef PALUDIS_ENABLE_THREADS
+
+namespace libebt
+{
+ template <>
+ struct BacktraceContextHolder<ContextTag> :
+ PthreadBacktraceContextHolder<ContextTag>
+ {
+ };
+}
+
+#endif
+
struct Context::ContextData
{
libebt::BacktraceContext<ContextTag> context;
diff --git a/paludis/util/log.cc b/paludis/util/log.cc
index 7e3a5a7..352a97f 100644
--- a/paludis/util/log.cc
+++ b/paludis/util/log.cc
@@ -50,6 +50,7 @@ namespace paludis
LogLevel log_level;
std::ostream * stream;
std::string program_name;
+ std::string previous_context;
mutable ActionQueue action_queue;
@@ -60,7 +61,7 @@ namespace paludis
{
}
- void message(const LogLevel l, const LogContext c, const std::string & s)
+ void message(const LogLevel l, const LogContext c, const std::string cs, const std::string s)
{
if (l >= log_level)
{
@@ -94,13 +95,11 @@ namespace paludis
if (lc_context == c)
{
- static std::string previous_context;
- std::string context(Context::backtrace("\n ... "));
- if (previous_context == context)
+ if (previous_context == cs)
*stream << "(same context) " << s << std::endl;
else
- *stream << Context::backtrace("\n ... ") << s << std::endl;
- previous_context = context;
+ *stream << cs << s << std::endl;
+ previous_context = cs;
}
else
*stream << s << std::endl;
@@ -156,7 +155,10 @@ Log::log_level() const
void
Log::_message(const LogLevel l, const LogContext c, const std::string & s)
{
- _imp->action_queue.enqueue(tr1::bind(tr1::mem_fn(&Implementation<Log>::message), _imp.get(), l, c, s));
+ if (lc_context == c)
+ _imp->action_queue.enqueue(tr1::bind(tr1::mem_fn(&Implementation<Log>::message), _imp.get(), l, c, Context::backtrace("\n ... "), s));
+ else
+ _imp->action_queue.enqueue(tr1::bind(tr1::mem_fn(&Implementation<Log>::message), _imp.get(), l, c, "", s));
}
LogMessageHandler::LogMessageHandler(const LogMessageHandler & o) :
diff --git a/paludis/util/thread.cc b/paludis/util/thread.cc
index 08586ad..ecb430e 100644
--- a/paludis/util/thread.cc
+++ b/paludis/util/thread.cc
@@ -28,22 +28,10 @@ using namespace paludis;
Thread::Thread(const tr1::function<void () throw ()> & f) :
_thread(new pthread_t),
- _func(f),
- _detached(false)
+ _func(f)
{
int err;
- if (0 != ((err = pthread_create(_thread, 0, &thread_func, this))))
- throw InternalError(PALUDIS_HERE, "pthread_create failed: " + stringify(strerror(err)));
-}
-Thread::Thread(const tr1::function<void () throw ()> & f,
- const tr1::function<void (Thread * const) throw ()> & pf) :
- _thread(new pthread_t),
- _func(f),
- _post_func(pf),
- _detached(false)
-{
- int err;
if (0 != ((err = pthread_create(_thread, 0, &thread_func, this))))
throw InternalError(PALUDIS_HERE, "pthread_create failed: " + stringify(strerror(err)));
}
@@ -52,29 +40,15 @@ void *
Thread::thread_func(void * r)
{
static_cast<Thread *>(r)->_func();
- if (static_cast<Thread *>(r)->_post_func)
- static_cast<Thread *>(r)->_post_func(static_cast<Thread *>(r));
-
return 0;
}
Thread::~Thread()
{
- if (! _detached)
- pthread_join(*_thread, 0);
+ pthread_join(*_thread, 0);
delete _thread;
}
-void
-Thread::detach()
-{
- if (! _detached)
- {
- pthread_detach(*_thread);
- _detached = true;
- }
-}
-
#else
Thread::Thread(const tr1::function<void () throw ()> & f)
@@ -82,35 +56,9 @@ Thread::Thread(const tr1::function<void () throw ()> & f)
f();
}
-Thread::Thread(const tr1::function<void () throw ()> & f,
- const tr1::function<void (Thread * const) throw ()> & pf)
-{
- f();
- pf();
-}
-
Thread::~Thread()
{
}
-void
-Thread::detach()
-{
-}
-
#endif
-namespace
-{
- void post_delete_func(Thread * const t) throw ()
- {
- delete t;
- }
-}
-
-void
-paludis::run_detached(const tr1::function<void () throw ()> & f)
-{
- (new Thread(f, &post_delete_func))->detach();
-}
-
diff --git a/paludis/util/thread.hh b/paludis/util/thread.hh
index 049a60f..5729d88 100644
--- a/paludis/util/thread.hh
+++ b/paludis/util/thread.hh
@@ -35,24 +35,14 @@ namespace paludis
#ifdef PALUDIS_ENABLE_THREADS
pthread_t * const _thread;
const tr1::function<void () throw ()> _func;
- const tr1::function<void (Thread * const) throw ()> _post_func;
- bool _detached;
static void * thread_func(void *);
#endif
public:
Thread(const tr1::function<void () throw ()> &);
-
- Thread(const tr1::function<void () throw ()> &,
- const tr1::function<void (Thread * const) throw ()> &);
-
~Thread();
-
- void detach();
};
-
- void run_detached(const tr1::function<void () throw ()> &) PALUDIS_VISIBLE;
}
#endif
diff --git a/paludis/util/thread_TEST.cc b/paludis/util/thread_TEST.cc
index 046510b..7dd9811 100644
--- a/paludis/util/thread_TEST.cc
+++ b/paludis/util/thread_TEST.cc
@@ -48,27 +48,5 @@ namespace test_cases
TEST_CHECK(x);
}
} test_thread;
-
- struct RunDetachedTest : TestCase
- {
- RunDetachedTest() : TestCase("run detached") { }
-
- void run()
- {
- bool x(false);
- run_detached(tr1::bind(&make_true, tr1::ref(x)));
- while (true)
- if (x)
- break;
- else
- {
- struct timespec t;
- t.tv_sec = 0;
- t.tv_nsec = 100000;
- ::nanosleep(&t, 0);
- TEST_CHECK(true);
- }
- }
- } test_run_detached;
}