Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / leveldatabase / port / port_chromium.cc
blob2bf8c309081e265dbbaed06252c61c2f2c95aa04
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.
5 #include "port/port_chromium.h"
7 #include "base/threading/platform_thread.h"
8 #include "third_party/snappy/src/snappy.h"
9 #include "util/logging.h"
11 namespace leveldb {
12 namespace port {
14 Mutex::Mutex() {
17 Mutex::~Mutex() {
20 void Mutex::Lock() {
21 mu_.Acquire();
24 void Mutex::Unlock() {
25 mu_.Release();
28 void Mutex::AssertHeld() {
29 mu_.AssertAcquired();
32 CondVar::CondVar(Mutex* mu)
33 : cv_(&mu->mu_) {
36 CondVar::~CondVar() { }
38 void CondVar::Wait() {
39 cv_.Wait();
42 void CondVar::Signal(){
43 cv_.Signal();
46 void CondVar::SignalAll() {
47 cv_.Broadcast();
50 void InitOnceImpl(OnceType* once, void (*initializer)()) {
51 OnceType state = base::subtle::Acquire_Load(once);
52 if (state == ONCE_STATE_DONE)
53 return;
55 state = base::subtle::NoBarrier_CompareAndSwap(once, ONCE_STATE_UNINITIALIZED,
56 ONCE_STATE_EXECUTING_CLOSURE);
58 if (state == ONCE_STATE_UNINITIALIZED) {
59 // We are the first thread, we have to call the closure.
60 (*initializer)();
61 base::subtle::Release_Store(once, ONCE_STATE_DONE);
62 } else {
63 // Another thread is running the closure, wait until completion.
64 while (state == ONCE_STATE_EXECUTING_CLOSURE) {
65 base::PlatformThread::YieldCurrentThread();
66 state = base::subtle::Acquire_Load(once);
71 bool Snappy_Compress(const char* input,
72 size_t input_length,
73 std::string* output) {
74 output->resize(snappy::MaxCompressedLength(input_length));
75 size_t outlen;
76 snappy::RawCompress(input, input_length, &(*output)[0], &outlen);
77 output->resize(outlen);
78 return true;
81 bool Snappy_GetUncompressedLength(const char* input_data,
82 size_t input_length,
83 size_t* result) {
84 return snappy::GetUncompressedLength(input_data, input_length, result);
87 bool Snappy_Uncompress(const char* input_data,
88 size_t input_length,
89 char* output) {
90 return snappy::RawUncompress(input_data, input_length, output);
93 } // namespace port
94 } // namespace leveldb