PageRenderTime 25ms CodeModel.GetById 15ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/src/EXTCondition.cpp

http://github.com/digego/extempore
C++ | 150 lines | 87 code | 28 blank | 35 comment | 5 complexity | 2feab6e8e2f5c46318b03c1d410ae8eb 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#ifdef EXT_BOOST
 39#else
 40#include "pthread.h"
 41#endif
 42
 43#include "EXTCondition.h"
 44#include "EXTMutex.h"
 45
 46
 47#define EXTCONDITION_DEBUG
 48
 49
 50namespace extemp
 51{
 52    EXTCondition::EXTCondition()
 53    {
 54        initialised = false;
 55    }
 56    
 57    
 58    EXTCondition::~EXTCondition()
 59    {
 60        destroy();
 61    }
 62    
 63
 64    int EXTCondition::init()
 65    {  
 66#ifdef EXT_BOOST
 67      //boost_cond = boost::condition_variable;
 68	int result = 0;
 69#else
 70        int result = pthread_cond_init(&pthread_cond, NULL);
 71#endif
 72        initialised = ! result;
 73
 74        
 75#ifdef _EXTCONDITION_DEBUG_
 76        if (result)
 77        {
 78            std::cerr << "Error initialising condition: " << result << std::endl;
 79        }
 80#endif
 81
 82        return result;
 83    }
 84
 85    
 86    void EXTCondition::destroy()
 87    {
 88        int result = 0;
 89        
 90        if (initialised)
 91        {
 92            initialised = false;
 93#ifdef EXT_BOOST        
 94            result = 0;
 95#else
 96            result = pthread_cond_destroy(&pthread_cond);
 97#endif
 98        }
 99
100#ifdef _EXTCONDITION_DEBUG_
101        if (result)
102        {
103            std::cerr << "Error destroying condition: " << result << std::endl;
104        }
105#endif
106    }
107
108    
109    int EXTCondition::wait(EXTMutex *aimeMutex)
110    {
111
112#ifdef EXT_BOOST
113        boost::recursive_mutex::scoped_lock lock(aimeMutex->bmutex);
114        boost_cond.wait(lock); //aimeMutex->block); // bmutex.scoped_lock); //lock);
115        int result = 0;
116#else
117        int result = pthread_cond_wait(&pthread_cond, &aimeMutex->pthread_mutex);
118#endif
119
120
121#ifdef _EXTCONDITION_DEBUG_
122        if (result)
123        {
124            std::cerr << "Error waiting on condition: " << result << std::endl;
125        }
126#endif
127
128        return result;
129    }
130
131    
132    int EXTCondition::signal(EXTMutex* aimeMutex)
133    {
134#ifdef EXT_BOOST
135        boost_cond.notify_one();
136        int result = 0;
137#else
138        int result = pthread_cond_signal(&pthread_cond);
139#endif
140
141#ifdef _EXTCONDITION_DEBUG_
142        if (result)
143        {
144            std::cerr << "Error signalling condition: " << result << std::endl;
145        }
146#endif
147
148        return result;
149    }
150}