threads.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: threads.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2017 Tad E. Smith
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. /** @file */
  22. #ifndef LOG4CPLUS_THREADS_HEADER_
  23. #define LOG4CPLUS_THREADS_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <memory>
  29. #include <thread>
  30. #include <log4cplus/tstring.h>
  31. #include <log4cplus/helpers/pointer.h>
  32. namespace log4cplus { namespace thread {
  33. LOG4CPLUS_EXPORT log4cplus::tstring const & getCurrentThreadName();
  34. LOG4CPLUS_EXPORT log4cplus::tstring const & getCurrentThreadName2();
  35. LOG4CPLUS_EXPORT void setCurrentThreadName(const log4cplus::tstring & name);
  36. LOG4CPLUS_EXPORT void setCurrentThreadName2(const log4cplus::tstring & name);
  37. LOG4CPLUS_EXPORT void yield();
  38. LOG4CPLUS_EXPORT void blockAllSignals();
  39. /**
  40. * This class blocks all POSIX signals when created and unblocks them when
  41. * destroyed.
  42. */
  43. class LOG4CPLUS_EXPORT SignalsBlocker
  44. {
  45. public:
  46. SignalsBlocker();
  47. ~SignalsBlocker();
  48. private:
  49. struct SignalsBlockerImpl;
  50. std::unique_ptr<SignalsBlockerImpl> impl;
  51. };
  52. #ifndef LOG4CPLUS_SINGLE_THREADED
  53. /**
  54. * There are many cross-platform C++ Threading libraries. The goal of
  55. * this class is not to replace (or match in functionality) those
  56. * libraries. The goal of this class is to provide a simple Threading
  57. * class with basic functionality.
  58. */
  59. class LOG4CPLUS_EXPORT AbstractThread
  60. : public virtual log4cplus::helpers::SharedObject
  61. {
  62. public:
  63. AbstractThread();
  64. // Disallow copying of instances of this class.
  65. AbstractThread(const AbstractThread&) = delete;
  66. AbstractThread& operator=(const AbstractThread&) = delete;
  67. bool isRunning() const;
  68. virtual void start();
  69. void join () const;
  70. virtual void run() = 0;
  71. protected:
  72. // Force objects to be constructed on the heap
  73. virtual ~AbstractThread();
  74. private:
  75. enum Flags
  76. {
  77. fRUNNING = 1,
  78. fJOINED = 2
  79. };
  80. std::unique_ptr<std::thread> thread;
  81. mutable std::atomic<int> flags;
  82. };
  83. typedef helpers::SharedObjectPtr<AbstractThread> AbstractThreadPtr;
  84. #endif // LOG4CPLUS_SINGLE_THREADED
  85. } } // namespace log4cplus { namespace thread {
  86. #endif // LOG4CPLUS_THREADS_HEADER_