Thread.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef _MEMORY_REPLAY_THREAD_H
  17. #define _MEMORY_REPLAY_THREAD_H
  18. #include <pthread.h>
  19. #include <stdint.h>
  20. #include <sys/types.h>
  21. class Action;
  22. class Pointers;
  23. constexpr size_t ACTION_MEMORY_SIZE = 128;
  24. class Thread {
  25. public:
  26. Thread();
  27. virtual ~Thread();
  28. void WaitForReady();
  29. void WaitForPending();
  30. void SetPending();
  31. void ClearPending();
  32. Action* CreateAction(uintptr_t key_pointer, const char* type, const char* line);
  33. void AddTimeNsecs(uint64_t nsecs) { total_time_nsecs_ += nsecs; }
  34. void set_pointers(Pointers* pointers) { pointers_ = pointers; }
  35. Pointers* pointers() { return pointers_; }
  36. Action* GetAction() { return reinterpret_cast<Action*>(action_memory_); }
  37. private:
  38. pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER;
  39. pthread_cond_t cond_;
  40. bool pending_ = false;
  41. pthread_t thread_id_;
  42. pid_t tid_ = 0;
  43. uint64_t total_time_nsecs_ = 0;
  44. Pointers* pointers_ = nullptr;
  45. // Per thread memory for an Action. Only one action can be processed.
  46. // at a time.
  47. static constexpr size_t ACTION_SIZE = 128;
  48. uint8_t action_memory_[ACTION_SIZE];
  49. friend class Threads;
  50. };
  51. #endif // _MEMORY_REPLAY_THREAD_H