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