You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
3.1 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: threads.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2010 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_IMPL_THREADS_IMPL_HEADER_
  23. #define LOG4CPLUS_IMPL_THREADS_IMPL_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #if defined (_WIN32)
  29. #include <log4cplus/config/windowsh-inc.h>
  30. #endif
  31. #include <log4cplus/tstring.h>
  32. #include <log4cplus/helpers/pointer.h>
  33. #include <log4cplus/thread/threads.h>
  34. #if ! defined (INSIDE_LOG4CPLUS)
  35. # error "This header must not be be used outside log4cplus' implementation files."
  36. #endif
  37. namespace log4cplus { namespace thread { namespace impl {
  38. #if defined (LOG4CPLUS_USE_PTHREADS)
  39. typedef pthread_t os_handle_type;
  40. typedef pthread_t os_id_type;
  41. inline
  42. pthread_t
  43. getCurrentThreadId ()
  44. {
  45. return pthread_self ();
  46. }
  47. #elif defined (LOG4CPLUS_USE_WIN32_THREADS)
  48. typedef HANDLE os_handle_type;
  49. typedef DWORD os_id_type;
  50. inline
  51. DWORD
  52. getCurrentThreadId ()
  53. {
  54. return GetCurrentThreadId ();
  55. }
  56. #elif defined (LOG4CPLUS_SINGLE_THREADED)
  57. typedef void * os_handle_type;
  58. typedef int os_id_type;
  59. inline
  60. int
  61. getCurrentThreadId ()
  62. {
  63. return 1;
  64. }
  65. #endif
  66. #ifndef LOG4CPLUS_SINGLE_THREADED
  67. struct ThreadStart
  68. {
  69. # ifdef LOG4CPLUS_USE_PTHREADS
  70. static void* threadStartFuncWorker(void *);
  71. # elif defined(LOG4CPLUS_USE_WIN32_THREADS)
  72. static unsigned threadStartFuncWorker(void *);
  73. # endif
  74. };
  75. /**
  76. * There are many cross-platform C++ Threading libraries. The goal of
  77. * this class is not to replace (or match in functionality) those
  78. * libraries. The goal of this class is to provide a simple Threading
  79. * class with basic functionality.
  80. */
  81. class Thread
  82. : public ThreadImplBase
  83. {
  84. public:
  85. Thread();
  86. bool isRunning() const;
  87. os_id_type getThreadId() const;
  88. os_handle_type getThreadHandle () const;
  89. void start();
  90. void join ();
  91. protected:
  92. // Force objects to be constructed on the heap
  93. virtual ~Thread();
  94. virtual void run() = 0;
  95. private:
  96. // Friends.
  97. friend struct ThreadStart;
  98. enum Flags
  99. {
  100. fRUNNING = 0x01,
  101. fJOINED = 0x02
  102. };
  103. unsigned flags;
  104. os_handle_type handle;
  105. # if defined(LOG4CPLUS_USE_WIN32_THREADS)
  106. unsigned thread_id;
  107. # endif
  108. // Disallow copying of instances of this class.
  109. Thread(const Thread&);
  110. Thread& operator=(const Thread&);
  111. };
  112. typedef helpers::SharedObjectPtr<Thread> ThreadPtr;
  113. #endif // LOG4CPLUS_SINGLE_THREADED
  114. } } } // namespace log4cplus { namespace thread { namespace impl {
  115. #endif // LOG4CPLUS_IMPL_THREADS_IMPL_HEADER_