PLUGIN share lang: "C++" version: "1.0" date: "2022-09-21" author: "Julien BRUGUIER" maintainer: "Julien BRUGUIER " synopsis: "A simple shared memory handler." description: %{ This plugin allows several process within an application to share copiable values through keys acting as addresses. .P The access to the storage is protected by a lock, but the stored values shall be synchronised by their own means. %} example: "Simple example" %{ .nf #!===SVMBIN=== LOG PLUGIN "svmcom.so" PLUGIN "===PLUGINLIB===" PROCESS "test" CODE "main" INLINE :memory STR/s :share.set "key" "value" :share.get "key" -> &s :com.message @&s END END .fi %} example: "Several processes" %{ .nf #!===SVMBIN=== LOG PLUGIN "svmcom.so" PLUGIN "svmrun.so" PLUGIN "===PLUGINLIB===" PROCESS "test" CODE "main" INLINE :share.set "key" "value" END SCHEDULER run.parallel END PROCESS "test" CODE "main" INLINE :run.sleep HARD 1 :memory STR/s :share.get "key" -> &s :com.message @&s END SCHEDULER run.parallel END .fi %} seealso: %{ .BR svm_plugin_mem (7) for basic memory management. %} includes: %{ #include %} code: %{ static std::map storage; SVM_Lock lock; %} initialisation: %{ lock = ::svm_lock_new(svm); ::svm_variable_scope_set_global(svm,lock); %} finalisation: %{ for(auto& s: storage) { ::svm_variable_scope_set_local(svm,s.second); } ::svm_variable_scope_set_local(svm,lock); storage.clear(); %} DEFINE WAITING INSTRUCTION share.set STR : key VALUE ? : value %{ SVM_LockGuard_Write guard = ::svm_lock_writeguard_new(svm,lock,TRUE); ::svm_variable_scope_set_local(svm,guard); auto raw_key = ARGV_VALUE(0,string); std::string key(raw_key.string,raw_key.size); auto it = storage.find(key); if(argc==1) { if(it!=storage.end()) { storage.erase(it); } return nullptr; } SVM_Value value = ::svm_parameter_value_get(svm,argv[1]); ::svm_variable_scope_set_global(svm,value); if(it==storage.end()) { storage.insert(std::make_pair(key,value)); } else { ::svm_variable_scope_set_local(svm,it->second); it->second = value; } %} help: %{ This instruction associates a value to a key or overrides a value for a key when a key and a value are specified. .P The instruction removes the key and the value when the key only is specified. %} WAITING INSTRUCTION share.get STR : key -> VALUE ? : value %{ SVM_LockGuard_Read guard = ::svm_lock_readguard_new(svm,lock,TRUE); ::svm_variable_scope_set_local(svm,guard); auto raw_key = ARGV_VALUE(0,string); std::string key(raw_key.string,raw_key.size); auto it = storage.find(key); if(it==storage.end()) { return ::svm_value_automatic_new_null(svm); } return it->second; %} help: %{ This instruction returns the value associated to a key. .P When the key is not associated to a value, a null value is returned. %}