diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2012-02-23 10:25:39 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2012-02-23 10:47:35 +0100 |
commit | a342b3e3b0c4c2baa40442ab4580f5091c6231d1 (patch) | |
tree | d2498fe7a445ddabd70ee5a8bf778e719c839116 /salhelper | |
parent | 5b98bb47139588d2b8ca31060f9d48377a4fbfdc (diff) |
Moved dp_gui::Thread to salhelper::Thread, so that all code can use it
...also improved the code somewhat.
Diffstat (limited to 'salhelper')
-rw-r--r-- | salhelper/Library_salhelper.mk | 1 | ||||
-rw-r--r-- | salhelper/Package_inc.mk | 1 | ||||
-rw-r--r-- | salhelper/inc/salhelper/thread.hxx | 104 | ||||
-rw-r--r-- | salhelper/source/gcc3.map | 18 | ||||
-rw-r--r-- | salhelper/source/thread.cxx | 66 |
5 files changed, 190 insertions, 0 deletions
diff --git a/salhelper/Library_salhelper.mk b/salhelper/Library_salhelper.mk index d98cd220f037..e899685185af 100644 --- a/salhelper/Library_salhelper.mk +++ b/salhelper/Library_salhelper.mk @@ -42,6 +42,7 @@ $(eval $(call gb_Library_add_exception_objects,salhelper,\ salhelper/source/condition \ salhelper/source/dynload \ salhelper/source/simplereferenceobject \ + salhelper/source/thread \ salhelper/source/timer \ )) diff --git a/salhelper/Package_inc.mk b/salhelper/Package_inc.mk index 744489ce25ad..37c3da5fc3a4 100644 --- a/salhelper/Package_inc.mk +++ b/salhelper/Package_inc.mk @@ -38,6 +38,7 @@ $(eval $(call gb_Package_add_file,salhelper_inc,inc/salhelper/queue.hxx,queue.hx $(eval $(call gb_Package_add_file,salhelper_inc,inc/salhelper/refobj.hxx,refobj.hxx)) $(eval $(call gb_Package_add_file,salhelper_inc,inc/salhelper/simplereferenceobject.hxx,simplereferenceobject.hxx)) $(eval $(call gb_Package_add_file,salhelper_inc,inc/salhelper/singletonref.hxx,singletonref.hxx)) +$(eval $(call gb_Package_add_file,salhelper_inc,inc/salhelper/thread.hxx,thread.hxx)) $(eval $(call gb_Package_add_file,salhelper_inc,inc/salhelper/timer.hxx,timer.hxx)) # vim: set noet sw=4 ts=4: diff --git a/salhelper/inc/salhelper/thread.hxx b/salhelper/inc/salhelper/thread.hxx new file mode 100644 index 000000000000..619ac449ef17 --- /dev/null +++ b/salhelper/inc/salhelper/thread.hxx @@ -0,0 +1,104 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Version: MPL 1.1 / GPLv3+ / LGPLv3+ + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License or as specified alternatively below. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * Major Contributor(s): + * [ Copyright (C) 2012 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com> + * (initial developer) ] + * + * All Rights Reserved. + * + * For minor contributions see the git repository. + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 3 or later (the "GPLv3+"), or + * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"), + * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable + * instead of those above. + */ + +#ifndef INCLUDED_SALHELPER_THREAD_HXX +#define INCLUDED_SALHELPER_THREAD_HXX + +#include "sal/config.h" + +#include <cstddef> + +#include "osl/thread.hxx" +#include "sal/types.h" +#include "salhelper/salhelperdllapi.h" +#include "salhelper/simplereferenceobject.hxx" + +namespace salhelper { + +/** + A safe encapsulation of ::osl::Thread. + + @since LibreOffice 3.6 +*/ +class SALHELPER_DLLPUBLIC Thread: + public salhelper::SimpleReferenceObject, private osl::Thread +{ +public: + /** + @param name the thread name, see ::osl_setThreadName; must be a non-null + null terminated string + */ + Thread(char const * name); + + /** + Launch the thread. + + This function must be called at most once. + + Each call of this function should eventually be followed by a call to + ::osl::Thread::join before exit(3), to ensure the thread is no longer + relying on any infrastructure while that infrastructure is being shut + down in atexit handlers. + */ + void launch(); + + using osl::Thread::getIdentifier; + using osl::Thread::join; + using osl::Thread::schedule; + using osl::Thread::terminate; + + static inline void * operator new(std::size_t size) + { return SimpleReferenceObject::operator new(size); } + + static inline void operator delete(void * pointer) + { SimpleReferenceObject::operator delete(pointer); } + +protected: + virtual ~Thread(); + + /** + The main function executed by the thread. + + Any uncaught exceptions lead to std::terminate. + */ + virtual void execute() = 0; + +private: + virtual void SAL_CALL run(); + + virtual void SAL_CALL onTerminated(); + + char const * name_; +}; + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/salhelper/source/gcc3.map b/salhelper/source/gcc3.map index a4617835d488..c356dc651f59 100644 --- a/salhelper/source/gcc3.map +++ b/salhelper/source/gcc3.map @@ -96,6 +96,24 @@ UDK_3.1 { } UDK_3_0_0; +LIBO_UDK_3.6 { # symbols available in >= LibO 3.6 + global: + _ZN9salhelper6Thread12onTerminatedEv; + # salhelper::Thread::onTerminated() + _ZN9salhelper6Thread3runEv; # salhelper::Thread::run() + _ZN9salhelper6Thread6launchEv; # salhelper::Thread::launch() + _ZN9salhelper6ThreadC1EPKc; # salhelper::Thread::Thread(char const*) + _ZN9salhelper6ThreadC2EPKc; # salhelper::Thread::Thread(char const*) + _ZN9salhelper6ThreadD0Ev; # salhelper::Thread::~Thread() + _ZN9salhelper6ThreadD1Ev; # salhelper::Thread::~Thread() + _ZN9salhelper6ThreadD2Ev; # salhelper::Thread::~Thread() + _ZTVN9salhelper6ThreadE; # vtable for salhelper::Thread + _ZThn16_N9salhelper6Thread12onTerminatedEv; + # non-virtual thunk to salhelper::Thread::onTerminated() + _ZThn16_N9salhelper6Thread3runEv; + # non-virtual thunk to salhelper::Thread::run() +} UDK_3.1; + # Unique libstdc++ symbols: GLIBCXX_3.4 { global: diff --git a/salhelper/source/thread.cxx b/salhelper/source/thread.cxx new file mode 100644 index 000000000000..bf7c1f196aa8 --- /dev/null +++ b/salhelper/source/thread.cxx @@ -0,0 +1,66 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Version: MPL 1.1 / GPLv3+ / LGPLv3+ + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License or as specified alternatively below. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * Major Contributor(s): + * [ Copyright (C) 2012 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com> + * (initial developer) ] + * + * All Rights Reserved. + * + * For minor contributions see the git repository. + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 3 or later (the "GPLv3+"), or + * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"), + * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable + * instead of those above. + */ + +#include "sal/config.h" + +#include "sal/log.hxx" +#include "salhelper/thread.hxx" + +salhelper::Thread::Thread(char const * name): name_(name) {} + +void salhelper::Thread::launch() { + SAL_INFO("salhelper.thread", "launch " << name_); + // Assumption is that osl::Thread::create returns normally iff it causes + // osl::Thread::run to start executing: + acquire(); + try { + create(); + } catch (...) { + release(); + throw; + } +} + +salhelper::Thread::~Thread() {} + +void salhelper::Thread::run() { + try { + setName(name_); + execute(); + } catch (...) { + // Work around the problem that onTerminated is not called if run throws + // an exception: + onTerminated(); + throw; + } +} + +void salhelper::Thread::onTerminated() { release(); } + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |