thread.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2015 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef CRASHPAD_UTIL_THREAD_THREAD_H_
  15. #define CRASHPAD_UTIL_THREAD_THREAD_H_
  16. #include "base/macros.h"
  17. #include "build/build_config.h"
  18. #if defined(OS_POSIX)
  19. #include <pthread.h>
  20. #elif defined(OS_WIN)
  21. #include <windows.h>
  22. #endif // OS_POSIX
  23. namespace crashpad {
  24. //! \brief Basic thread abstraction. Users should derive from this
  25. //! class and implement ThreadMain().
  26. class Thread {
  27. public:
  28. Thread();
  29. virtual ~Thread();
  30. //! \brief Create a platform thread, and run ThreadMain() on that thread. Must
  31. //! be paired with a call to Join().
  32. void Start();
  33. //! \brief Block until ThreadMain() exits. This may be called from any thread.
  34. //! Must paired with a call to Start().
  35. void Join();
  36. private:
  37. //! \brief The thread entry point to be implemented by the subclass.
  38. virtual void ThreadMain() = 0;
  39. static
  40. #if defined(OS_POSIX)
  41. void*
  42. #elif defined(OS_WIN)
  43. DWORD WINAPI
  44. #endif // OS_POSIX
  45. ThreadEntryThunk(void* argument);
  46. #if defined(OS_POSIX)
  47. pthread_t platform_thread_;
  48. #elif defined(OS_WIN)
  49. HANDLE platform_thread_;
  50. #endif
  51. DISALLOW_COPY_AND_ASSIGN(Thread);
  52. };
  53. } // namespace crashpad
  54. #endif // CRASHPAD_UTIL_THREAD_THREAD_H_