Skip to content

Add ThreadSafeArrayReturn cpp class attribute #216

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: release/1.8.0-alpha
Choose a base branch
from

Conversation

Kimeek42
Copy link

@Kimeek42 Kimeek42 commented Apr 10, 2025

Problem - The way ACT handles array returning is not thread safe in C++.

auto object = library()->getObject();
	for (int i = 0; i < 3; ++i)
	{
		std::thread([object ]() {
			for (int j = 0; j < 100; ++j)
			{
				std::string array = object->getArray();
			}
		}).detach();
	}

Even though getArray is a function that only returns simple string, it may crash because under the hood there is reset call on std::unique_ptr and it's not thread safe operation.

Solution - Add new class attribute that will ensure thread safe array return. To enable this feature just add arrayReturnOption="ThreadSafe" in class component definition:

<class name="Object" arrayReturnOption="ThreadSafe">
	<method name="getArray">
		...

It will add mutex to the class declaration and lock it every time array is being returned from the function:

class CObject: public CBase {
private:
    std::mutex ArrayReturnMutex;
public:
    ...

std::string CObject::getArray(const std::string & sType, const std::string & sLocation)
{
    std::lock_guard<std::mutex> lock(ArrayReturnMutex);
    eagleapi_librarymanager_getstorageid(m_pHandle, sType.c_str(), sLocation.c_str(), 0, &bytesNeededStorageId, nullptr));
    std::vector<char> bufferStorageId(bytesNeededStorageId);
    eagleapi_librarymanager_getstorageid(m_pHandle, sType.c_str(), sLocation.c_str(), bytesNeededStorageId, &bytesWrittenStorageId, &bufferStorageId[0]));
    
    return std::string(&bufferStorageId[0]);
}

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