Iso init stops segfaulting by allocating a giant stack

This commit is contained in:
Chris Esposito 2018-01-12 14:54:23 +11:00
parent 9b7ce8c90c
commit 5a66ee170d
9 changed files with 40 additions and 10 deletions

View File

@ -16,6 +16,7 @@
#include <math.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
//Defines
#define ISO_PACKET_SIZE (750)
@ -34,7 +35,6 @@ typedef struct thread_data {
//Second thread to check for libusb complete
void *thread_run(thread_data *thread_init)
{
//Copied inputs
libusb_context *ctx;
timeval tv;
@ -51,10 +51,10 @@ void *thread_run(thread_data *thread_init)
while(1){
if(libusb_event_handling_ok(ctx)){
libusb_handle_events_timeout(ctx, &tv);
mexPrintf("libusb_handle_events_timeout completed!");
mexPrintf("libusb_handle_events_timeout completed!\n");
} else {
mexPrintf("Cannot handle libusb events. Backing off...");
usleep(20000);
usleep(100000);
}
}
}
@ -75,6 +75,28 @@ static void LIBUSB_CALL isoCallback(struct libusb_transfer * transfer){
//Main mex function
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
//Code to set stack size after compilation
//https://stackoverflow.com/questions/2275550/change-stack-size-for-a-c-application-in-linux-during-compilation-with-gnu-com
const rlim_t kStackSize = 128 * 1024 * 1024; // min stack size = 128 MB
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0)
{
if (rl.rlim_cur < kStackSize)
{
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0)
{
mexPrintf("setrlimit returned result = %d\n", result);
}
}
} else mexPrintf("Result was not zero!\n");
/*
char test1_string[9] = "12345678";
char test2_string[9] = "abcdabcd";
@ -171,9 +193,12 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
//Create the libusb thread;
pthread_t thread;
error = pthread_create(&thread, NULL, thread_run, &ctx);
error = pthread_create(&thread, NULL, thread_run, &thread_init);
if(error) mexPrintf("Could not create Libusb thread!");
//Short sleep to ensure the other thread has enough time to copy everything from memory. Just in case...
usleep(100000);
mexPrintf("Iso Stack initialised!\n");
return;
}

View File

@ -2,7 +2,7 @@ clc
fprintf("\n\n\nCompiling Thread_Example...\n\n\n");
fflush(stdout);
mex C/Thread_Example.c -IC/build_linux/libusb -lusb-1.0 -Lbin\lib\x64 -O0
mex C/Thread_Example.c -IC/build_linux/libusb -lusb-1.0 -Lbin\lib\x64
copyfile Thread_Example.mex C/mex_outputs
copyfile Thread_Example.o C/mex_outputs
delete Thread_Example.mex
@ -10,7 +10,7 @@ delete Thread_Example.o
fprintf("\n\n\nCompiling USB_INIT_LIBUSB...\n\n\n");
fflush(stdout);
mex C/USB_INIT_LIBUSB.c -IC/build_linux/libusb -lusb-1.0 -Lbin\lib\x64 -O0
mex C/USB_INIT_LIBUSB.c -IC/build_linux/libusb -lusb-1.0 -Lbin\lib\x64
copyfile USB_INIT_LIBUSB.mex C/mex_outputs
copyfile USB_INIT_LIBUSB.o C/mex_outputs
delete USB_INIT_LIBUSB.mex
@ -19,7 +19,7 @@ delete USB_INIT_LIBUSB.o
fprintf("\n\n\nCompiling USB_EXIT_LIBUSB...\n\n\n");
fflush(stdout);
mex C/USB_EXIT_LIBUSB.c -IC/build_linux/libusb -lusb-1.0 -Lbin\lib\x64 -O0
mex C/USB_EXIT_LIBUSB.c -IC/build_linux/libusb -lusb-1.0 -Lbin\lib\x64
copyfile USB_EXIT_LIBUSB.mex C/mex_outputs
copyfile USB_EXIT_LIBUSB.o C/mex_outputs
delete USB_EXIT_LIBUSB.mex
@ -27,7 +27,7 @@ delete USB_EXIT_LIBUSB.o
fprintf("\n\n\nCompiling USB_CONTROL_SEND_LIBUSB...\n\n\n");
fflush(stdout);
mex C/USB_CONTROL_SEND_LIBUSB.c -IC/build_linux/libusb -lusb-1.0 -Lbin\lib\x64 -O0
mex C/USB_CONTROL_SEND_LIBUSB.c -IC/build_linux/libusb -lusb-1.0 -Lbin\lib\x64
copyfile USB_CONTROL_SEND_LIBUSB.mex C/mex_outputs
copyfile USB_CONTROL_SEND_LIBUSB.o C/mex_outputs
delete USB_CONTROL_SEND_LIBUSB.mex
@ -35,7 +35,7 @@ delete USB_CONTROL_SEND_LIBUSB.o
fprintf("\n\n\nCompiling USB_ISO_INIT_LIBUSB...\n\n\n");
fflush(stdout);
mex C/USB_ISO_INIT_LIBUSB.c -IC/build_linux/libusb -lusb-1.0 -Lbin\lib\x64 -O0
mex C/USB_ISO_INIT_LIBUSB.c -IC/build_linux/libusb -lusb-1.0 -Lbin\lib\x64
copyfile USB_ISO_INIT_LIBUSB.mex C/mex_outputs
copyfile USB_ISO_INIT_LIBUSB.o C/mex_outputs
delete USB_ISO_INIT_LIBUSB.mex

View File

@ -1,4 +1,5 @@
%clear all
fflush(stdout);
clc
#https://docs.google.com/document/d/1ZDO1RTarQTNB6Pdfi_T4YukL51oYan_kk_teb1cu6-o/edit?usp=sharing
@ -15,8 +16,12 @@ if(isequal(usb_handle, "0000000000000000"))
return;
end
return;
fprintf("\nInitialising USB ISO...\n");
fflush(stdout)
mex_usb_iso_init(usb_handle, usb_context, '81');
fflush(stdout)
fprintf("\nSending AVR Debug Command...\n");
fflush(stdout);
@ -44,4 +49,4 @@ labrador_set_psu_voltage(usb_handle, usb_context, 6.6);
fprintf("\nExiting Libusb...\n");
fflush(stdout);
mex_usb_exit(usb_handle, usb_context);
mex_usb_exit(usb_handle, usb_context);

Binary file not shown.