Click here to Skip to main content
15,878,959 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 141.1K   3.4K   132  
DynObj is an open source library that provides a C++ application with run-time class loading facilities
#include "SpinLock.h"

// Can adjust for upwards/downwards stack
#define SL_ON_SAME_STACK(stack,ostack) (stack<ostack && stack>ostack-SL_STACK_LOCALITY)

int SpinLock::Enter( ){_CFE_; 
    int stack = reinterpret_cast<int>(&stack);
    int ostack = AtomicExchange( &m_stack, stack );
    if( !ostack ||  // First entry
        SL_ON_SAME_STACK(stack,ostack) ) // Repeated entry, same thread
        return ostack;      
    
    int cnt = SL_MAX_WAIT;
    while( cnt-->0 ){
        ostack = AtomicExchange( &m_stack, ostack );
        if( !ostack ) return 0;     // First entry
        if( SL_ON_SAME_STACK(stack,ostack) )
            return ostack;      // Repeated entry, same thread
    }
    
    // Failed
    return -1;
}

int SpinLock::Leave( int ostack ){_CFE_; 
    AtomicExchange( (int*)&m_stack, ostack );
    return ostack;
}


#define SL_IS_LOCAL(s,v) ((s>v-SL_STACK_LOCALITY) && (s<v+SL_STACK_LOCALITY))

/*
int SpinLock::Enter( ){_CFE_; 
    SIUINT stack = (int)&stack;
    // Are we the owner ?
    if( SL_IS_LOCAL(m_stack,stack) ){
        SIUINT old_stack = m_stack;
        m_stack = stack;
        //m_cnt++; // Increase thread count
        return (int)old_stack;
    }
    
    // Wait until we read back 0, then we can take ownership
    int cnt = SL_MAX_WAIT;
    SIUINT value;
    while( cnt-->0 ) { 
        value = AtomicExchange( &m_lock, (int)1 );
        if( !value ){
            // Obtained it
            m_stack = stack;
            // assert( ++m_cnt==1 );
            return 0;
        }
    }
    
    // Failed
    return -1;
}

int SpinLock::Leave( int old_stack ){_CFE_; 
    m_stack = old_stack;
    if( !old_stack )
    //if( !--m_cnt )
        // Release any waiting thread
        AtomicExchange( (int*)&m_lock, 0 );
    return old_stack;
}
*/

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