libps5000 multithreading ...

Post your Linux discussions here
Post Reply
zenon2503
Newbie
Posts: 0
Joined: Fri Dec 17, 2010 10:54 am

libps5000 multithreading ...

Post by zenon2503 »

Hi Guys,

we are working on an integration of the ps5000 library (libps5000_R1_3_6_6_3_x86_64) into MATLAB on a 64 Bit Ubuntu Linux platform. For this task we are using the mex interface of MATLAB. Controlling the scope and transferring data form the scope works quite well.

But there is a severe problem unloading the mex file (i.e. calling "clear mex" in MATLAB). When unloading the mex file Matlab crashes with a segmentation fault. Maybe we have an idea why the crash occurs:

This simple C-Program will illustrate the idea:

-------------------------------------------------------------------------
#include
#include
#include
#include
#include


/* callback function */
void PREF4 CallbackBlockReady(short handle, PICO_STATUS status, void * pParameter) {
printf("CallbackBlockReady called\n");
}

/******************************************************************************/
/* MAIN */
/******************************************************************************/
int main(int argc, char *argv[]) {
short unithandle; /* handle to control the picoscope */
long a_bufferlen = 3000; /* length of buffer of CHANNEL A == trace length */
long timeIndisposedMs;
PICO_STATUS status = PICO_OK; /* return status of picoscope api calls */

char ps_str1[] = "ps -Lm -C ";
char *ps_str2;

ps_str2 = malloc(strlen(argv[0]) - 2 + sizeof(ps_str1));
strcpy(ps_str2, ps_str1); /* copy "ps -Lm -C " */
strcpy(ps_str2 + sizeof(ps_str1) - 1, argv[0] + 2); /* copy command string while removing the leading "./" */

/* printf("%ld: %s\n",strlen(argv[0]) - 2 + sizeof(ps_str1), ps_str2); */


/******************************************************************************/
fflush(stdout);
sleep(1);
system(ps_str2);
sleep(1);

printf("\nOpening Scope ... ");
status = ps5000OpenUnit(&unithandle);
if(status == PICO_OK) {
printf("OK\n");
}
else {
printf("FAIL\n");
return(1);
}


/******************************************************************************/
fflush(stdout);
sleep(1);
system(ps_str2);
sleep(1);

printf("\nStart Block mode ... ");
status = ps5000RunBlock(
unithandle, /* short: scope handle */
0, /* long: pre trigger samples */
a_bufferlen, /* long: post trigger samples */
127, /* unsigned long: timebase */
1, /* short: oversampling, possible when timebase is smaller then max frequency, compute mean of neighbor samples */
&timeIndisposedMs, /* long: time that the scope will spent for collecting data */
0, /* unsigned short: zero based segment number */
CallbackBlockReady , /* pointer to callback function */
NULL /* optional parameter passed to callback function */
);

if(status == PICO_OK) {
printf("OK\n");
}
else {
printf("FAIL\n");
return(1);
}


/******************************************************************************/
fflush(stdout);
sleep(1);
system(ps_str2);
sleep(1);

printf("\nClosing scope ... ");
status = ps5000CloseUnit(unithandle);
if(status == PICO_OK) {
printf("OK\n");
}
else {
printf("FAIL\n");
return(1);
}

fflush(stdout);
sleep(1);
system(ps_str2);
sleep(1);

return(0);
}

--------------------------------------------------------------------------------------

This program opens the scope, starts the block mode, and closes the
scope. Between this tasks the unix command line tool ps is called to print the
running threads of this application on the console.

Calling this program like this

$ ./picoscope

produces the following output:

--------------------------------------------------------------------------------------

PID LWP TTY TIME CMD
25824 - pts/4 00:00:00 picoscope
- 25824 - 00:00:00 -

Opening Scope ... OK
PID LWP TTY TIME CMD
25824 - pts/4 00:00:00 picoscope
- 25824 - 00:00:00 -
- 25827 - 00:00:00 -
- 25829 - 00:00:00 -
- 25830 - 00:00:00 -

Start Block mode ... OK
PID LWP TTY TIME CMD
25824 - pts/4 00:00:00 picoscope
- 25824 - 00:00:00 -
- 25827 - 00:00:00 -
- 25829 - 00:00:00 -
- 25830 - 00:00:00 -
- 25833 - 00:00:00 -
CallbackBlockReady called

Closing scope ... OK
PID LWP TTY TIME CMD
25824 - pts/4 00:00:00 picoscope
- 25824 - 00:00:00 -
- 25827 - 00:00:00 -

-------------------------------------------------------------------------------------

Before opening the scope, only one thread, the main thread LWP=25824 is running. When the connection to the scope is open, three more threads are running (LWP=25827,25829,25830). In block mode one additional thread is running (LWP=25833). But AFTER CLOSING THE CONNECTION TO THE SCOPE, there is still an additional thread running (LWP=25827). I assume that this thread is not joined properly by the library and that this thread causes the MATLAB
crashes we observed when we clear the mex context. The crash may be caused by accesses to addresses freed by the MATLAB environment.

Is there any possibility to close this thread by a library call, or is there a bug in the libraray and we need an update for it?

Best regards

Hermann Seuschek

Post Reply