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 #include "port/port_chromium.h" 6 7 #include "base/threading/platform_thread.h" 8 #include "util/logging.h" 9 10 #if defined(USE_SNAPPY) 11 # include "third_party/snappy/src/snappy.h" 12 #endif 13 14 namespace leveldb { 15 namespace port { 16 17 Mutex::Mutex() { 18 } 19 20 Mutex::~Mutex() { 21 } 22 23 void Mutex::Lock() { 24 mu_.Acquire(); 25 } 26 27 void Mutex::Unlock() { 28 mu_.Release(); 29 } 30 31 void Mutex::AssertHeld() { 32 mu_.AssertAcquired(); 33 } 34 35 CondVar::CondVar(Mutex* mu) 36 : cv_(&mu->mu_) { 37 } 38 39 CondVar::~CondVar() { } 40 41 void CondVar::Wait() { 42 cv_.Wait(); 43 } 44 45 void CondVar::Signal(){ 46 cv_.Signal(); 47 } 48 49 void CondVar::SignalAll() { 50 cv_.Broadcast(); 51 } 52 53 void InitOnceImpl(OnceType* once, void (*initializer)()) { 54 OnceType state = ::base::subtle::Acquire_Load(once); 55 if (state == ONCE_STATE_DONE) 56 return; 57 58 state = ::base::subtle::NoBarrier_CompareAndSwap( 59 once, 60 ONCE_STATE_UNINITIALIZED, 61 ONCE_STATE_EXECUTING_CLOSURE); 62 63 if (state == ONCE_STATE_UNINITIALIZED) { 64 // We are the first thread, we have to call the closure. 65 (*initializer)(); 66 ::base::subtle::Release_Store(once, ONCE_STATE_DONE); 67 } else { 68 // Another thread is running the closure, wait until completion. 69 while (state == ONCE_STATE_EXECUTING_CLOSURE) { 70 ::base::PlatformThread::YieldCurrentThread(); 71 state = ::base::subtle::Acquire_Load(once); 72 } 73 } 74 } 75 76 bool Snappy_Compress(const char* input, size_t input_length, 77 std::string* output) { 78 #if defined(USE_SNAPPY) 79 output->resize(snappy::MaxCompressedLength(input_length)); 80 size_t outlen; 81 snappy::RawCompress(input, input_length, &(*output)[0], &outlen); 82 output->resize(outlen); 83 return true; 84 #else 85 return false; 86 #endif 87 } 88 89 bool Snappy_GetUncompressedLength(const char* input_data, 90 size_t input_length, 91 size_t* result) { 92 #if defined(USE_SNAPPY) 93 return snappy::GetUncompressedLength(input_data, input_length, result); 94 #else 95 return false; 96 #endif 97 } 98 99 bool Snappy_Uncompress(const char* input_data, size_t input_length, 100 char* output) { 101 #if defined(USE_SNAPPY) 102 return snappy::RawUncompress(input_data, input_length, output); 103 #else 104 return false; 105 #endif 106 } 107 108 } 109 } 110