static int cgxe_mex_unlock_call(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { while (mexIsLocked()) { mexUnlock(); } return 1; }
unsigned int sf_mex_unlock_call( int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[] ) { char commandName[20]; if (nrhs<1 || !mxIsChar(prhs[0]) ) return 0; /* Possible call to get the checksum */ mxGetString(prhs[0], commandName,sizeof(commandName)/sizeof(char)); commandName[(sizeof(commandName)/sizeof(char)-1)] = '\0'; if(strcmp(commandName,"sf_mex_unlock")) return 0; while(mexIsLocked()) { mexUnlock(); } return(1); }
//called at exit from mex (mexAtExit) //runs when clear mex is called void closeLeap() { //if locked, remove all locks while(mexIsLocked()) mexUnlock(); mexPrintf("exiting mex...\n"); if(controller){ mexPrintf("deleting controller...\n"); delete controller; controller = 0; } mexPrintf("Done\n"); return; }
static void unlock_persistent() { if ( mexIsLocked() ) { mexUnlock(); if ( atExitUnsubscribe(&persistentAtExit) < 0 ) { mexErrMsgTxt("unlock_persistent(): atExitUnsubscribe() failed.\n" "This is a bug probably. Please report it.\n"); } } else { mexErrMsgTxt("unlock_persistent(): persistent is already unlocked.\n" "This is a bug probably. Please report it.\n"); } return; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { bool amLocked = mexIsLocked(); /* Our check for if this has been called already */ if(nlhs == 1) { mwSize dims[2]; dims[0] = 1; dims[1] = 1; plhs[0] = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL); } int x, y; x = MPI_Initialized(&y); if(amLocked || y) { if(nlhs == 1) *(mxGetPr(plhs[0])) = 1.0; return; } mexLock(); MPI_Init(NULL, NULL); if(nlhs == 1) *(mxGetPr(plhs[0])) = 0.0; }
void mexFunction (int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { int rc, enabled; UINT32_T masterid = 0; time_t timallow = 0; UINT64_T memallow = 0; UINT64_T rss, vs; /* this function will be called upon unloading of the mex file */ mexAtExit(exitFun); if (nrhs<3) mexErrMsgTxt ("invalid number of input arguments"); if (mxIsScalar(prhs[0])) masterid = mxGetScalar(prhs[0]); else if (mxIsEmpty(prhs[0])) masterid = 0; else mexErrMsgTxt ("invalid input argument #1"); if (mxIsScalar(prhs[1])) timallow = mxGetScalar(prhs[1]); else if (mxIsEmpty(prhs[1])) timallow = 0; else mexErrMsgTxt ("invalid input argument #2"); if (mxIsScalar(prhs[2])) memallow = mxGetScalar(prhs[2]); else if (mxIsEmpty(prhs[2])) memallow = 0; else mexErrMsgTxt ("invalid input argument #3"); if (masterid!=0 || timallow!=0 || memallow!=0) { enabled = 1; /* in this case the mex file is not allowed to be cleared from memory */ if (!mexIsLocked()) mexLock(); } if (masterid==0 && timallow==0 && memallow==0) { enabled = 0; /* in this case the mex file is allowed to be cleared from memory */ #ifdef SOLUTION_FOR_UNEXPLAINED_CRASH if (mexIsLocked()) mexUnlock(); #endif } if (!peerInitialized) { mexPrintf("watchdog: init\n"); peerinit(NULL); peerInitialized = 1; } /* start the discover thread */ pthread_mutex_lock(&mutexstatus); if (!discoverStatus) { pthread_mutex_unlock(&mutexstatus); mexPrintf("watchdog: spawning discover thread\n"); rc = pthread_create(&discoverThread, NULL, discover, (void *)NULL); if (rc) mexErrMsgTxt("problem with return code from pthread_create()"); else { /* wait until the thread has properly started */ pthread_mutex_lock(&mutexstatus); if (!discoverStatus) pthread_cond_wait(&condstatus, &mutexstatus); pthread_mutex_unlock(&mutexstatus); } } else { pthread_mutex_unlock(&mutexstatus); } /* start the expire thread */ pthread_mutex_lock(&mutexstatus); if (!expireStatus) { pthread_mutex_unlock(&mutexstatus); mexPrintf("watchdog: spawning expire thread\n"); rc = pthread_create(&expireThread, NULL, expire, (void *)NULL); if (rc) mexErrMsgTxt("problem with return code from pthread_create()"); else { /* wait until the thread has properly started */ pthread_mutex_lock(&mutexstatus); if (!expireStatus) pthread_cond_wait(&condstatus, &mutexstatus); pthread_mutex_unlock(&mutexstatus); } } else { pthread_mutex_unlock(&mutexstatus); } if (timallow>0) { /* timallow should be relative to now */ timallow += time(NULL); } if (memallow>0) { /* memallow should be in absolute numbers, add the current memory footprint */ getmem(&rss, &vs); memallow += rss; } /* enable the watchdog: the expire thread will exit if the master is not seen any more */ pthread_mutex_lock(&mutexwatchdog); watchdog.enabled = enabled; watchdog.evidence = 0; watchdog.masterid = masterid; watchdog.memory = memallow; watchdog.time = timallow; pthread_mutex_unlock(&mutexwatchdog); if (enabled) mexPrintf("watchdog: enabled for masterid = %lu, time = %d, memory = %lu\n", masterid, timallow, memallow); else mexPrintf("watchdog: disabled for masterid = %lu, time = %d, memory = %lu\n", masterid, timallow, memallow); return; } /* main */