You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 line
1.3 KiB

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