小组成员:谢瑞阳、徐翔宇
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.

125 lines
2.6 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. //
  5. // See port_example.h for documentation for the following types/functions.
  6. #ifndef STORAGE_LEVELDB_PORT_PORT_OSX_H_
  7. #define STORAGE_LEVELDB_PORT_PORT_OSX_H_
  8. #include <libkern/OSAtomic.h>
  9. #include <machine/endian.h>
  10. #include <pthread.h>
  11. #include <stdint.h>
  12. #include <string>
  13. namespace leveldb {
  14. // The following 4 methods implemented here for the benefit of env_posix.cc.
  15. inline size_t fread_unlocked(void *a, size_t b, size_t c, FILE *d) {
  16. return fread(a, b, c, d);
  17. }
  18. inline size_t fwrite_unlocked(const void *a, size_t b, size_t c, FILE *d) {
  19. return fwrite(a, b, c, d);
  20. }
  21. inline int fflush_unlocked(FILE *f) {
  22. return fflush(f);
  23. }
  24. inline int fdatasync(int fd) {
  25. return fsync(fd);
  26. }
  27. namespace port {
  28. static const bool kLittleEndian = (__DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN);
  29. // ------------------ Threading -------------------
  30. // A Mutex represents an exclusive lock.
  31. class Mutex {
  32. public:
  33. Mutex();
  34. ~Mutex();
  35. void Lock();
  36. void Unlock();
  37. void AssertHeld() { }
  38. private:
  39. friend class CondVar;
  40. pthread_mutex_t mu_;
  41. // No copying
  42. Mutex(const Mutex&);
  43. void operator=(const Mutex&);
  44. };
  45. class CondVar {
  46. public:
  47. explicit CondVar(Mutex* mu);
  48. ~CondVar();
  49. void Wait();
  50. void Signal();
  51. void SignalAll();
  52. private:
  53. pthread_cond_t cv_;
  54. Mutex* mu_;
  55. };
  56. inline void MemoryBarrier() {
  57. #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  58. // See http://gcc.gnu.org/ml/gcc/2003-04/msg01180.html for a discussion on
  59. // this idiom. Also see http://en.wikipedia.org/wiki/Memory_ordering.
  60. __asm__ __volatile__("" : : : "memory");
  61. #else
  62. OSMemoryBarrier();
  63. #endif
  64. }
  65. class AtomicPointer {
  66. private:
  67. void* ptr_;
  68. public:
  69. AtomicPointer() { }
  70. explicit AtomicPointer(void* p) : ptr_(p) {}
  71. inline void* Acquire_Load() const {
  72. void* ptr = ptr_;
  73. MemoryBarrier();
  74. return ptr;
  75. }
  76. inline void Release_Store(void* v) {
  77. MemoryBarrier();
  78. ptr_ = v;
  79. }
  80. inline void* NoBarrier_Load() const {
  81. return ptr_;
  82. }
  83. inline void NoBarrier_Store(void* v) {
  84. ptr_ = v;
  85. }
  86. };
  87. inline bool Snappy_Compress(const char* input, size_t input_length,
  88. std::string* output) {
  89. return false;
  90. }
  91. inline bool Snappy_Uncompress(const char* input_data, size_t input_length,
  92. std::string* output) {
  93. return false;
  94. }
  95. inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
  96. return false;
  97. }
  98. }
  99. }
  100. #endif // STORAGE_LEVELDB_PORT_PORT_OSX_H_