Accesing interprocess mutex in simulink S-function.

I'm trying to communicate with matlab via boost::interprocess, using shared memory in combination with boost::interprocesss::conditions. Much(completly) like the example on the boost website.
A plain c++ .exe file is responable of the creation of the shared memory, all matlab has to is open it and unlock/lock the mutexes and notify.
I can get this to work inside MDL start, this works. But this is obviously not what im looking for, i want to acces the data during simulation:
static void mdlStart(SimStruct *S)
{
using namespace boost::interprocess;
shared_memory_object shm
(open_only //only create
,"MySharedMemory" //name
,read_write //read-write mode
);
try{
//Map the whole shared memory in this process
mapped_region region
(shm //What to map
,read_write //Map it as read-write
);
//Get the address of the mapped region
void * addr = region.get_address();
//Obtain a pointer to the shared structure
trace_queue * data = static_cast<trace_queue*>(addr);
void **PWork = ssGetPWork(S);
PWork[0] = data;
trace_queue * data2;
data2 = (trace_queue *) ssGetPWorkValue(S,0);
}
catch(interprocess_exception &ex){
mexPrintf("Interprocess exeception: %s \n", ex.what());
}
Now i want to acces this data every time step. So i use the PWork vector created above do a simple loop. However the a soon as matlab reaches the first line that acces data (scoped lock) it crashes!
using namespace boost::interprocess;
trace_queue * data2 = (trace_queue *) ssGetPWorkValue(S,0);
bool end_loop = false;
do{
scoped_lock<interprocess_mutex> lock(data2->mutex);
if(!data2->message_in){
data2->cond_empty.wait(lock);
}
if(std::strcmp(data2->items, "last message") == 0){
end_loop = true;
}
else{
//Print the message
mexPrintf("%s \n", data2->items);
//Notify the other process that the buffer is empty
data2->message_in = false;
data2->cond_full.notify_one();
}
}
while(!end_loop);
Nesting this piece code in together with the rest in mdlStart works fine. Same thing goes if i put everything together in mdlInitializeConditions or mdlInitializeSizes. When i do this in mdlOutput or update it crashes.
All cause the same crash, ill post the stacktrace in a reply to keep things a bit more organized.

3 comentarios

Stack Trace (from fault):
[ 0] 0xb75d0411 /usr/local/MATLAB/R2012a/bin/glnx86/libmwfl.so+00300049 _ZN2fl4diag15stacktrace_base7captureERKNS0_14thread_contextEj+000169
[ 1] 0xb75d2b36 /usr/local/MATLAB/R2012a/bin/glnx86/libmwfl.so+00310070
[ 2] 0xb75d2fcf /usr/local/MATLAB/R2012a/bin/glnx86/libmwfl.so+00311247 _ZN2fl4diag13terminate_logEPKcRKNS0_14thread_contextE+000190
[ 3] 0xb7142a71 /usr/local/MATLAB/R2012a/bin/glnx86/libmwmcr.so+01096305 _ZN2fl4diag13terminate_logEPKcPK8ucontext+000076
[ 4] 0xb713f5f6 /usr/local/MATLAB/R2012a/bin/glnx86/libmwmcr.so+01082870
[ 5] 0xb7141226 /usr/local/MATLAB/R2012a/bin/glnx86/libmwmcr.so+01090086
[ 6] 0xb71419da /usr/local/MATLAB/R2012a/bin/glnx86/libmwmcr.so+01092058
[ 7] 0xb774a40c __kernel_rt_sigreturn+000000
[ 8] 0xb67b6cb7 /lib/i386-linux-gnu/libpthread.so.0+00036023 __pthread_mutex_lock+000023
[ 9] 0x857177f2 /home/s041723/Dropbox/Afstuderen/gazebo1.0/matlab/gazebo_cpp.mexglx+00006130
[ 10] 0x95359920 /usr/local/MATLAB/R2012a/bin/glnx86/libmwsimulink.so+22464800
[ 11] 0x9537d54f /usr/local/MATLAB/R2012a/bin/glnx86/libmwsimulink.so+22611279
[ 12] 0x94b7f864 /usr/local/MATLAB/R2012a/bin/glnx86/libmwsimulink.so+14231652
[ 13] 0x949e3adf /usr/local/MATLAB/R2012a/bin/glnx86/libmwsimulink.so+12544735
[ 14] 0x949e3c43 /usr/local/MATLAB/R2012a/bin/glnx86/libmwsimulink.so+12545091
[ 15] 0x9498e376 /usr/local/MATLAB/R2012a/bin/glnx86/libmwsimulink.so+12194678
[ 16] 0x94b2db9e /usr/local/MATLAB/R2012a/bin/glnx86/libmwsimulink.so+13896606
[ 17] 0x953bfa60 /usr/local/MATLAB/R2012a/bin/glnx86/libmwsimulink.so+22882912
[ 18] 0x91aabc79 /usr/local/MATLAB/R2012a/bin/glnx86/libmwsl_solver.so+00412793 _ZN9sl_solver9SimStepFS10outputStepEv+000107
[ 19] 0x91aab862 /usr/local/MATLAB/R2012a/bin/glnx86/libmwsl_solver.so+00411746 _ZN9sl_solver9SimStepFS4stepEv+000018
[ 20] 0x953bd74e /usr/local/MATLAB/R2012a/bin/glnx86/libmwsimulink.so+22873934
[ 21] 0x94b26540 /usr/local/MATLAB/R2012a/bin/glnx86/libmwsimulink.so+13866304
[ 22] 0x94ae712b /usr/local/MATLAB/R2012a/bin/glnx86/libmwsimulink.so+13607211
[ 23] 0x941b7bc3 /usr/local/MATLAB/R2012a/bin/glnx86/libmwsimulink.so+03976131
[ 24] 0xb2cc048a /usr/local/MATLAB/R2012a/bin/glnx86/libmwuix.so+00222346
[ 25] 0xb1fcc211 /usr/lib/i386-linux-gnu/libXt.so.6+00156177
[ 26] 0xb1fcd880 /usr/lib/i386-linux-gnu/libXt.so.6+00161920 XtAppNextEvent+000176
[ 27] 0xb2cf8667 /usr/local/MATLAB/R2012a/bin/glnx86/libmwuix.so+00452199
[ 28] 0xb2cf889b /usr/local/MATLAB/R2012a/bin/glnx86/libmwuix.so+00452763
[ 29] 0xb73199a1 /usr/local/MATLAB/R2012a/bin/glnx86/libmwservices.so+00817569 ioGetChar+000135
[ 30] 0xb719d066 /usr/local/MATLAB/R2012a/bin/glnx86/libmwbridge.so+00102502
[ 31] 0xb719d56d /usr/local/MATLAB/R2012a/bin/glnx86/libmwbridge.so+00103789 _Z10ioReadLinebP8_IO_FILEPcS1_iPbRKN5boost8optionalIKP15inWorkSpace_tagEEb+000887
[ 32] 0xb719d98e /usr/local/MATLAB/R2012a/bin/glnx86/libmwbridge.so+00104846
[ 33] 0xb71a287a /usr/local/MATLAB/R2012a/bin/glnx86/libmwbridge.so+00125050
[ 34] 0xb71a346e /usr/local/MATLAB/R2012a/bin/glnx86/libmwbridge.so+00128110
[ 35] 0xb71a3baa /usr/local/MATLAB/R2012a/bin/glnx86/libmwbridge.so+00129962 mnParser+000581
[ 36] 0xb7126c3c /usr/local/MATLAB/R2012a/bin/glnx86/libmwmcr.so+00982076 _ZN11mcrInstance30mnParser_on_interpreter_threadEv+000050
[ 37] 0xb712ed6c /usr/local/MATLAB/R2012a/bin/glnx86/libmwmcr.so+01015148 _ZN5boost6detail8function26void_function_obj_invoker0INS_3_bi6bind_tIvNS_4_mfi3mf0Iv11mcrInstanceEENS3_5list1INS3_5valueIPS7_EEEEEEvE6invokeERNS1_15function_bufferE+000029
[ 38] 0xb7108d82 /usr/local/MATLAB/R2012a/bin/glnx86/libmwmcr.so+00859522
[ 39] 0xb7108e4a /usr/local/MATLAB/R2012a/bin/glnx86/libmwmcr.so+00859722
[ 40] 0xb2cf0ca5 /usr/local/MATLAB/R2012a/bin/glnx86/libmwuix.so+00421029
[ 41] 0xb2ce88d2 /usr/local/MATLAB/R2012a/bin/glnx86/libmwuix.so+00387282
[ 42] 0xb2cf3616 /usr/local/MATLAB/R2012a/bin/glnx86/libmwuix.so+00431638
[ 43] 0xb7381492 /usr/local/MATLAB/R2012a/bin/glnx86/libmwservices.so+01242258 _ZSt8for_eachIN9__gnu_cxx17__normal_iteratorIPN5boost8weak_ptrIN4sysq10ws_ppeHookEEESt6vectorIS6_SaIS6_EEEENS4_8during_FIS6_NS2_10shared_ptrIS5_EEEEET0_T_SH_SG_+000077
[ 44] 0xb738287f /usr/local/MATLAB/R2012a/bin/glnx86/libmwservices.so+01247359
[ 45] 0xb737fbf1 /usr/local/MATLAB/R2012a/bin/glnx86/libmwservices.so+01235953 _Z25svWS_ProcessPendingEventsiib+000742
[ 46] 0xb7380aa0 /usr/local/MATLAB/R2012a/bin/glnx86/libmwservices.so+01239712 _Z25svWS_ProcessPendingEventsii+000044
[ 47] 0xb710806f /usr/local/MATLAB/R2012a/bin/glnx86/libmwmcr.so+00856175
[ 48] 0xb7108520 /usr/local/MATLAB/R2012a/bin/glnx86/libmwmcr.so+00857376
[ 49] 0xb710867a /usr/local/MATLAB/R2012a/bin/glnx86/libmwmcr.so+00857722
[ 50] 0xb67b4d4c /lib/i386-linux-gnu/libpthread.so.0+00027980
[ 51] 0xb66f3ace /lib/i386-linux-gnu/libc.so.6+00961230 clone+000094
I think this might be a broader MEX/MATLAB issue. I don't know too much about boost::interprocess, but from a web search it seems like others have run into similar issues with interprocess and MEX.
Yeah, i've noticed the same. Line 8 is the actual problem as far as i can see. One of my collegeas had a simular problem when using plain pthread in output/update, Matlab does not seem to like it at all. A run with valgrind againt pointed to the mutex lock which is the first attempt of accesing the shm.
People suggested building the external against matlab libraries, this didnt have any effect. It was not the problem for it works fine elsewhere in the s-function.
This: http://www.mathworks.com/matlabcentral/fileexchange/28572-sharedmatrix is simular, but is just a shared memory matrix, i would like to use mutex for triggering purposes.
I've just noticed this: http://www.mathworks.com/matlabcentral/fileexchange/32489, from the same author. I will try making something simular inside my s-function. Using this code, or recoding for use with conditions.
Still strange that it will run in intialization/start but not at actual runtime.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre MATLAB Mobile en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 11 de Jun. de 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by