PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/src/EXTMonitor.cpp

http://github.com/digego/extempore
C++ | 114 lines | 59 code | 21 blank | 34 comment | 2 complexity | 6c26457c8a33b9c5f0ff95751dd8539f MD5 | raw file
  1/*
  2 * Copyright (c) 2011, Andrew Sorensen
  3 *
  4 * All rights reserved.
  5 *
  6 *
  7 * Redistribution and use in source and binary forms, with or without 
  8 * modification, are permitted provided that the following conditions are met:
  9 *
 10 * 1. Redistributions of source code must retain the above copyright notice, 
 11 *    this list of conditions and the following disclaimer.
 12 *
 13 * 2. Redistributions in binary form must reproduce the above copyright notice,
 14 *    this list of conditions and the following disclaimer in the documentation 
 15 *    and/or other materials provided with the distribution.
 16 *
 17 * Neither the name of the authors nor other contributors may be used to endorse
 18 * or promote products derived from this software without specific prior written 
 19 * permission.
 20 *
 21 *
 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 25 * ARE DISCLEXTD. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 32 * POSSIBILITY OF SUCH DAMAGE.
 33 *
 34 */
 35
 36#include <iostream>
 37
 38#include "EXTMonitor.h"
 39
 40
 41#define _EXTMONITOR_DEBUG_
 42
 43
 44namespace extemp
 45{
 46    EXTMonitor::EXTMonitor(std::string _name) :
 47	mutex(_name),
 48	name(_name)
 49    {
 50        initialised = false;
 51        init();
 52    }
 53    
 54    
 55    EXTMonitor::~EXTMonitor()
 56    {
 57        destroy();
 58    }
 59    
 60
 61    void EXTMonitor::init()
 62    {
 63        if (! initialised)
 64        {
 65            mutex.init();
 66            condition.init();
 67            initialised = true;
 68        }
 69    }
 70
 71
 72    void EXTMonitor::destroy()
 73    {
 74        if (initialised)
 75        {
 76            initialised = false;
 77            mutex.destroy();
 78            condition.destroy();
 79        }
 80    }
 81
 82    
 83    int EXTMonitor::lock()
 84    {
 85        int result = mutex.lock();        
 86        return result;
 87    }
 88
 89
 90    int EXTMonitor::unlock()
 91    {
 92        int result = mutex.unlock();
 93        return result;
 94    }
 95
 96
 97    int EXTMonitor::wait()
 98    {
 99        int result = condition.wait(&mutex);
100        return result;
101    }
102
103
104    int EXTMonitor::signal()
105    {
106        int result = condition.signal(&mutex);
107        return result;
108    }
109
110    bool EXTMonitor::isOwnedByCurrentThread()
111    {
112      return mutex.isOwnedByCurrentThread();	
113    }
114}