Skip to content

Add C++ binding/implementation thread safety options #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

Kimeek42
Copy link

@Kimeek42 Kimeek42 commented May 5, 2025

This solution adds 2 new virtual methods IBase::_lockInstance and IBase::_UnlockInstance on the implementation side with empty implementation. It also adds new class IBaseWithMutex that derives from Ibase. Those methods are being injected to base class methods so they will be exposed through and API (as private). It all happens only if one (or more) class has ThreadSafetyOption attribute set to "strict" or "soft".
Difference between soft and strict:
soft - binding side will call _lockInstance only if string / array is returned from API function
strict - binding side will call _lockInstance everytime, no matter what is returned from function

IBaseWithMutex Definition:

/**
 Definition of a class with mutex for IBase
*/
class IBaseWithMutex : public virtual IBase
{
private:
    std::mutex m_mutex;
public:

    /**
    * IBaseWithMutex::_lockInstance - If thread safety for class in library is enabled it should lock object for calling thread
    */
    virtual void _lockInstance() { m_mutex.lock(); }

    /**
    * IBaseWithMutex::_unlockInstance - If thread safety for class in library is enabled it should unlock object for other threads
    */
    virtual void _unlockInstance() { m_mutex.unlock();}
};

When ThreadSafetyOption attribute in LibraryManager component class is set to "soft" or "strict", ILibraryManager will derive from IBaseWithMutex instead of Ibase

class ILibraryManager : public virtual IBaseWithMutex {
public:
    ...
};

Example binding side functions created when ThreadSafetyOption is set to soft:

std::string CLibraryManager::GetLocalLibrariesPath()
{
    EagleAPI_uint32 bytesNeededPath = 0;
    EagleAPI_uint32 bytesWrittenPath = 0;
    _lockInstance();
    CheckError(m_pWrapper->m_WrapperTable.m_LibraryManager_GetLocalLibrariesPath(m_pHandle, 0, &bytesNeededPath, nullptr), true);
    std::vector<char> bufferPath(bytesNeededPath);
    CheckError(m_pWrapper->m_WrapperTable.m_LibraryManager_GetLocalLibrariesPath(m_pHandle, bytesNeededPath, &bytesWrittenPath, &bufferPath[0]), true);
    _unlockInstance();
    
    return std::string(&bufferPath[0]);
}

void CLibraryManager::RefreshLocalLibraries()
{
    CheckError(m_pWrapper->m_WrapperTable.m_LibraryManager_RefreshLocalLibraries(m_pHandle), false);
}

With this approach we are locking / unlocking mutex on implementation side so we can have X different instances of the Binding-Class pointing to the same Implementations-Class and all of this should work fine. On the binding side _lockInstance and _unlockInstance are private so one should use them by accident.
One more thing, CheckError will unlock hande in case of "exception propagation"

inline void CWrapper::CheckError(CBase * pBaseClass, EagleAPIResult nResult, bool unlockIfError)
    {
        if (nResult != 0) {
            std::string sErrorMessage;
            if (pBaseClass != nullptr) {
                GetLastError(pBaseClass, sErrorMessage);
                if (unlockIfError) {
                    _unlockInstance(pBaseClass);
                }
            }
            throw EEagleAPIException(nResult, sErrorMessage);
        }
    }

Just like with binding side, any changes on binding side will happen only when one (or more) class has ThreadSafetyOption attribute set to "strict" or "soft".

@gangatp gangatp force-pushed the uck/ThreadSafety branch from 8fc6423 to e5be6e6 Compare May 28, 2025 04:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant