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_posix.h" 6 7#include <cstdlib> 8#include <stdio.h> 9#include <string.h> 10#include "util/logging.h" 11 12namespace leveldb { 13namespace port { 14 15static void PthreadCall(const char* label, int result) { 16 if (result != 0) { 17 fprintf(stderr, "pthread %s: %s\n", label, strerror(result)); 18 abort(); 19 } 20} 21 22Mutex::Mutex() { PthreadCall("init mutex", pthread_mutex_init(&mu_, NULL)); } 23 24Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_)); } 25 26void Mutex::Lock() { PthreadCall("lock", pthread_mutex_lock(&mu_)); } 27 28void Mutex::Unlock() { PthreadCall("unlock", pthread_mutex_unlock(&mu_)); } 29 30CondVar::CondVar(Mutex* mu) 31 : mu_(mu) { 32 PthreadCall("init cv", pthread_cond_init(&cv_, NULL)); 33} 34 35CondVar::~CondVar() { PthreadCall("destroy cv", pthread_cond_destroy(&cv_)); } 36 37void CondVar::Wait() { 38 PthreadCall("wait", pthread_cond_wait(&cv_, &mu_->mu_)); 39} 40 41void CondVar::Signal() { 42 PthreadCall("signal", pthread_cond_signal(&cv_)); 43} 44 45void CondVar::SignalAll() { 46 PthreadCall("broadcast", pthread_cond_broadcast(&cv_)); 47} 48 49void InitOnce(OnceType* once, void (*initializer)()) { 50 PthreadCall("once", pthread_once(once, initializer)); 51} 52 53} // namespace port 54} // namespace leveldb 55