Click here to Skip to main content
15,895,740 members
Articles / Programming Languages / C++

DynObj - C++ Cross Platform Plugin Objects

Rate me:
Please Sign up or sign in to vote.
4.95/5 (34 votes)
27 Sep 200727 min read 146.3K   3.4K   132  
DynObj is an open source library that provides a C++ application with run-time class loading facilities
#include "pi/CritSect.h"
#include "pi/Atomic.h"
#include "pi/Thread.h"

#include <assert.h>

CritSect::CritSect( bool do_construct ){_CFE_;
	if( do_construct ){
		m_cnt = 0;
		m_thid = 0;
	}
}

void CritSect::Enter( ){_CFE_;
    int cnt = AtomicIncrement( &m_cnt );
	if( m_thid==(int)piGetThreadId() ){
        if( cnt>1 )   
            return; // Repeated entry
        assert( !m_thid );
	}
	
    while( cnt>1 ){
        if( !AtomicDecrement(&m_cnt) )
        	// If final release for other thread happened when we had temp increase
        	m_thid = 0;	
        piYield();
        cnt = AtomicIncrement(&m_cnt);
    }
    
    // Have it
    m_thid = piGetThreadId();
}

void CritSect::Leave( ){_CFE_;
    int cnt = AtomicDecrement( &m_cnt );
    if( !cnt )
        // Thread leaving it
        m_thid = 0;
    else
        // Check not released too many times
        assert( cnt>0 );
}

bool CritSect::TryEnter( ){_CFE_;
    int cnt = AtomicIncrement( &m_cnt );
	if( m_thid==(int)piGetThreadId() ){
        if( cnt>1 )   
            return true; // Repeated entry
        assert( !m_thid );
	}
	
    // Have it
    if( cnt==1 ){
        m_thid = piGetThreadId();
        return true;
    }
    else{
        // Failed
        AtomicDecrement( &m_cnt );
        return false;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions