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"
11 #if (defined(__x86_64__) || defined(__i386__)) && defined(__GNUC__)
18 static void PthreadCall(const char* label
, int result
) {
20 fprintf(stderr
, "pthread %s: %s\n", label
, strerror(result
));
25 Mutex::Mutex() { PthreadCall("init mutex", pthread_mutex_init(&mu_
, NULL
)); }
27 Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_
)); }
29 void Mutex::Lock() { PthreadCall("lock", pthread_mutex_lock(&mu_
)); }
31 void Mutex::Unlock() { PthreadCall("unlock", pthread_mutex_unlock(&mu_
)); }
33 CondVar::CondVar(Mutex
* mu
)
35 PthreadCall("init cv", pthread_cond_init(&cv_
, NULL
));
38 CondVar::~CondVar() { PthreadCall("destroy cv", pthread_cond_destroy(&cv_
)); }
40 void CondVar::Wait() {
41 PthreadCall("wait", pthread_cond_wait(&cv_
, &mu_
->mu_
));
44 void CondVar::Signal() {
45 PthreadCall("signal", pthread_cond_signal(&cv_
));
48 void CondVar::SignalAll() {
49 PthreadCall("broadcast", pthread_cond_broadcast(&cv_
));
52 void InitOnce(OnceType
* once
, void (*initializer
)()) {
53 PthreadCall("once", pthread_once(once
, initializer
));
56 bool HasAcceleratedCRC32C() {
57 #if (defined(__x86_64__) || defined(__i386__)) && defined(__GNUC__)
58 unsigned int eax
, ebx
, ecx
, edx
;
59 __get_cpuid(1, &eax
, &ebx
, &ecx
, &edx
);
60 return (ecx
& (1 << 20)) != 0;
67 } // namespace leveldb