Code Search for Developers
 
 
  

sema.cpp from Equalizer - Multipipe Rendering at Krugle


Show sema.cpp syntax highlighted


/* Copyright (c) 2006, Stefan Eilemann <eile@equalizergraphics.com> 
   All rights reserved. */

#include "sema.h"

#include <eq/base/log.h>

#include <errno.h>

using namespace eqBase;
using namespace std;

Sema::Sema()
        : _value( 0 )
{
    int error = pthread_cond_init( &_cond, NULL );
    if( error )
    {
        EQERROR << "Error creating pthread condition: " << strerror( error )
                << endl;
        return;
    } 
            
    error = pthread_mutex_init( &_mutex, NULL );
    if( error )
    {
        EQERROR << "Error creating pthread mutex: " << strerror(error) << endl;
        return;
    } 
}

Sema::~Sema()
{
    pthread_cond_destroy( &_cond );
    pthread_mutex_destroy( &_mutex );
}

void Sema::post()
{
    pthread_mutex_lock( &_mutex );
    ++_value;
    pthread_cond_signal( &_cond );
    pthread_mutex_unlock( &_mutex );
}

void Sema::wait()
{
    pthread_mutex_lock( &_mutex );
    while( _value == 0 )
        pthread_cond_wait( &_cond, &_mutex );

    --_value;
    pthread_mutex_unlock( &_mutex );
}

void Sema::adjust( const int delta )
{
    if( delta == 0 )
        return;

    pthread_mutex_lock( &_mutex );
    
    if( delta > 0 )
    {
        _value += delta;
        pthread_cond_broadcast( &_cond );
        pthread_mutex_unlock( &_mutex );
        return;
    };

    uint32_t amount = (uint32_t)-delta;
    while( amount )
    {
        while( _value == 0 )
            pthread_cond_wait( &_cond, &_mutex );
                
        if( _value < amount )
        {
            amount -= _value;
            _value = 0;
        }
        else
        {
            _value -= amount;
            amount = 0;
        }
    }
    pthread_mutex_unlock( &_mutex );
}





See more files for this project here

Equalizer - Multipipe Rendering

Equalizer is a programming interface and resource management system for scalable graphics applications for clusters and shared memory systems. It is build upon a scalable programming interface solving the problems common to any multipipe application.

Project homepage: http://sourceforge.net/projects/equalizer
Programming language(s): C,C++
License: lgpl21

  barrier.cpp
  barrier.h
  base.h
  clock.h
  debug.cpp
  debug.h
  definesWin32.h
  executionListener.h
  hash.h
  idPool.cpp
  idPool.h
  launcher.cpp
  launcher.h
  lock.cpp
  lock.h
  log.cpp
  log.h
  monitor.h
  mtQueue.h
  nonCopyable.h
  perThread.h
  process.cpp
  process.h
  refPtr.h
  referenced.h
  requestHandler.cpp
  requestHandler.h
  rng.h
  scopedMutex.h
  sema.cpp
  sema.h
  spinLock.cpp
  spinLock.h
  stdExt.h
  thread.cpp
  thread.h
  timedLock.cpp
  timedLock.h
  types.h
  userdata.h