LCOV - code coverage report
Current view: top level - lib/Support - Mutex.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 28 29 96.6 %
Date: 2017-09-14 15:23:50 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements the llvm::sys::Mutex class.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/Support/Mutex.h"
      15             : #include "llvm/Support/ErrorHandling.h"
      16             : #include "llvm/Config/config.h"
      17             : 
      18             : //===----------------------------------------------------------------------===//
      19             : //=== WARNING: Implementation here must contain only TRULY operating system
      20             : //===          independent code.
      21             : //===----------------------------------------------------------------------===//
      22             : 
      23             : #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
      24             : // Define all methods as no-ops if threading is explicitly disabled
      25             : namespace llvm {
      26             : using namespace sys;
      27             : MutexImpl::MutexImpl( bool recursive) { }
      28             : MutexImpl::~MutexImpl() { }
      29             : bool MutexImpl::acquire() { return true; }
      30             : bool MutexImpl::release() { return true; }
      31             : bool MutexImpl::tryacquire() { return true; }
      32             : }
      33             : #else
      34             : 
      35             : #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
      36             : 
      37             : #include <cassert>
      38             : #include <pthread.h>
      39             : #include <stdlib.h>
      40             : 
      41             : namespace llvm {
      42             : using namespace sys;
      43             : 
      44             : // Construct a Mutex using pthread calls
      45      334932 : MutexImpl::MutexImpl( bool recursive)
      46      334932 :   : data_(nullptr)
      47             : {
      48             :   // Declare the pthread_mutex data structures
      49             :   pthread_mutex_t* mutex =
      50      334932 :     static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
      51             : 
      52      334932 :   if (mutex == nullptr)
      53           0 :     report_bad_alloc_error("Mutex allocation failed");
      54             : 
      55             :   pthread_mutexattr_t attr;
      56             : 
      57             :   // Initialize the mutex attributes
      58      334932 :   int errorcode = pthread_mutexattr_init(&attr);
      59             :   assert(errorcode == 0); (void)errorcode;
      60             : 
      61             :   // Initialize the mutex as a recursive mutex, if requested, or normal
      62             :   // otherwise.
      63      334932 :   int kind = ( recursive  ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
      64      334932 :   errorcode = pthread_mutexattr_settype(&attr, kind);
      65             :   assert(errorcode == 0);
      66             : 
      67             :   // Initialize the mutex
      68      334932 :   errorcode = pthread_mutex_init(mutex, &attr);
      69             :   assert(errorcode == 0);
      70             : 
      71             :   // Destroy the attributes
      72      334932 :   errorcode = pthread_mutexattr_destroy(&attr);
      73             :   assert(errorcode == 0);
      74             : 
      75             :   // Assign the data member
      76      334932 :   data_ = mutex;
      77      334932 : }
      78             : 
      79             : // Destruct a Mutex
      80      316432 : MutexImpl::~MutexImpl()
      81             : {
      82      158216 :   pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
      83             :   assert(mutex != nullptr);
      84      158216 :   pthread_mutex_destroy(mutex);
      85      158216 :   free(mutex);
      86      158216 : }
      87             : 
      88             : bool
      89     1473084 : MutexImpl::acquire()
      90             : {
      91     1473084 :   pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
      92             :   assert(mutex != nullptr);
      93             : 
      94     1473084 :   int errorcode = pthread_mutex_lock(mutex);
      95     1473084 :   return errorcode == 0;
      96             : }
      97             : 
      98             : bool
      99     1473084 : MutexImpl::release()
     100             : {
     101     1473084 :   pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
     102             :   assert(mutex != nullptr);
     103             : 
     104     1473084 :   int errorcode = pthread_mutex_unlock(mutex);
     105     1473084 :   return errorcode == 0;
     106             : }
     107             : 
     108             : bool
     109           6 : MutexImpl::tryacquire()
     110             : {
     111           6 :   pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
     112             :   assert(mutex != nullptr);
     113             : 
     114           6 :   int errorcode = pthread_mutex_trylock(mutex);
     115           6 :   return errorcode == 0;
     116             : }
     117             : 
     118             : }
     119             : 
     120             : #elif defined(LLVM_ON_UNIX)
     121             : #include "Unix/Mutex.inc"
     122             : #elif defined( LLVM_ON_WIN32)
     123             : #include "Windows/Mutex.inc"
     124             : #else
     125             : #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
     126             : #endif
     127             : #endif

Generated by: LCOV version 1.13