作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

50 linhas
1.2 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_osx.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. }