作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

106 lignes
2.5 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "leveldb/env.h"
  5. #include "port/port.h"
  6. #include "util/testharness.h"
  7. namespace leveldb {
  8. static const int kDelayMicros = 100000;
  9. static const int kReadOnlyFileLimit = 4;
  10. static const int kMMapLimit = 4;
  11. class EnvTest {
  12. private:
  13. port::Mutex mu_;
  14. std::string events_;
  15. public:
  16. Env* env_;
  17. EnvTest() : env_(Env::Default()) { }
  18. };
  19. static void SetBool(void* ptr) {
  20. reinterpret_cast<port::AtomicPointer*>(ptr)->NoBarrier_Store(ptr);
  21. }
  22. TEST(EnvTest, RunImmediately) {
  23. port::AtomicPointer called (NULL);
  24. env_->Schedule(&SetBool, &called);
  25. env_->SleepForMicroseconds(kDelayMicros);
  26. ASSERT_TRUE(called.NoBarrier_Load() != NULL);
  27. }
  28. TEST(EnvTest, RunMany) {
  29. port::AtomicPointer last_id (NULL);
  30. struct CB {
  31. port::AtomicPointer* last_id_ptr; // Pointer to shared slot
  32. uintptr_t id; // Order# for the execution of this callback
  33. CB(port::AtomicPointer* p, int i) : last_id_ptr(p), id(i) { }
  34. static void Run(void* v) {
  35. CB* cb = reinterpret_cast<CB*>(v);
  36. void* cur = cb->last_id_ptr->NoBarrier_Load();
  37. ASSERT_EQ(cb->id-1, reinterpret_cast<uintptr_t>(cur));
  38. cb->last_id_ptr->Release_Store(reinterpret_cast<void*>(cb->id));
  39. }
  40. };
  41. // Schedule in different order than start time
  42. CB cb1(&last_id, 1);
  43. CB cb2(&last_id, 2);
  44. CB cb3(&last_id, 3);
  45. CB cb4(&last_id, 4);
  46. env_->Schedule(&CB::Run, &cb1);
  47. env_->Schedule(&CB::Run, &cb2);
  48. env_->Schedule(&CB::Run, &cb3);
  49. env_->Schedule(&CB::Run, &cb4);
  50. env_->SleepForMicroseconds(kDelayMicros);
  51. void* cur = last_id.Acquire_Load();
  52. ASSERT_EQ(4, reinterpret_cast<uintptr_t>(cur));
  53. }
  54. struct State {
  55. port::Mutex mu;
  56. int val;
  57. int num_running;
  58. };
  59. static void ThreadBody(void* arg) {
  60. State* s = reinterpret_cast<State*>(arg);
  61. s->mu.Lock();
  62. s->val += 1;
  63. s->num_running -= 1;
  64. s->mu.Unlock();
  65. }
  66. TEST(EnvTest, StartThread) {
  67. State state;
  68. state.val = 0;
  69. state.num_running = 3;
  70. for (int i = 0; i < 3; i++) {
  71. env_->StartThread(&ThreadBody, &state);
  72. }
  73. while (true) {
  74. state.mu.Lock();
  75. int num = state.num_running;
  76. state.mu.Unlock();
  77. if (num == 0) {
  78. break;
  79. }
  80. env_->SleepForMicroseconds(kDelayMicros);
  81. }
  82. ASSERT_EQ(state.val, 3);
  83. }
  84. } // namespace leveldb
  85. int main(int argc, char** argv) {
  86. return leveldb::test::RunAllTests();
  87. }