作者: 韩晨旭 10225101440 李畅 10225102463
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.

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