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_posix.h"
10 #include "util/logging.h"
15 static void PthreadCall(const char* label, int result) {
17 fprintf(stderr, "pthread %s: %s\n", label, strerror(result));
22 Mutex::Mutex() { PthreadCall("init mutex", pthread_mutex_init(&mu_, NULL)); }
24 Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_)); }
26 void Mutex::Lock() { PthreadCall("lock", pthread_mutex_lock(&mu_)); }
28 void Mutex::Unlock() { PthreadCall("unlock", pthread_mutex_unlock(&mu_)); }
30 CondVar::CondVar(Mutex* mu)
32 PthreadCall("init cv", pthread_cond_init(&cv_, NULL));
35 CondVar::~CondVar() { PthreadCall("destroy cv", pthread_cond_destroy(&cv_)); }
37 void CondVar::Wait() {
38 PthreadCall("wait", pthread_cond_wait(&cv_, &mu_->mu_));
41 void CondVar::Signal() {
42 PthreadCall("signal", pthread_cond_signal(&cv_));
45 void CondVar::SignalAll() {
46 PthreadCall("broadcast", pthread_cond_broadcast(&cv_));
49 void InitOnce(OnceType* once, void (*initializer)()) {
50 PthreadCall("once", pthread_once(once, initializer));
54 } // namespace leveldb