core/matlab/examples/mex/mexlock.c

00001 /*=================================================================
00002  * mexlock.c
00003  *
00004  * This example demonstrates how to use mexLock, mexUnlock, and 
00005  * mexIsLocked.
00006  *
00007  * You must call mexlock with one argument.  If you pass in a 1, it
00008  * will lock the MEX-file. If you pass in a -1, it will unlock the
00009  * MEX-file. If you pass in 0, it will report lock status.   It uses
00010  * mexIsLocked to check the status of the MEX-file. If the file is
00011  * already in the state you requested, the MEX-file errors out.
00012  *
00013  * This is a MEX-file for MATLAB.  
00014  * Copyright 1984-2006 The MathWorks, Inc.
00015  * All rights reserved.
00016  *=================================================================*/
00017 /* $Revision: 1.1 $ */
00018 #include "mex.h"
00019 
00020 void
00021 mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
00022 {
00023     double lock;
00024     
00025     (void) plhs;     /* unused parameter */
00026 
00027     /* Check for proper number of input and output arguments */   
00028     if (nrhs != 1 || !mxIsDouble(prhs[0]) ||  
00029     mxGetN(prhs[0])*mxGetM(prhs[0]) != 1  || mxIsComplex(prhs[0])) {
00030     mexErrMsgTxt("Input argument must be a real scalar double");
00031     }
00032     if(nlhs > 0){
00033     mexErrMsgTxt("No output arguments expected.");
00034     }
00035     lock = mxGetScalar(prhs[0]);
00036     if((lock != 0.0) && lock != 1.0 && lock != -1.0) {
00037     mexErrMsgTxt("Input argument must be either 1 to lock or -1 to\
00038  unlock or 0 for lock status.\n");
00039     }
00040     if(mexIsLocked()) {
00041     if(lock > 0.0) {
00042         mexErrMsgTxt("MEX-file is already locked\n");
00043     }
00044     else if(lock < 0.0) {
00045         mexUnlock();
00046         mexPrintf("MEX-file is unlocked\n");
00047     }
00048     else {
00049         mexPrintf("MEX-file is locked\n");
00050     }    
00051     } else {
00052     if(lock < 0.0) {
00053         mexErrMsgTxt("MEX-file is already unlocked\n");
00054     }
00055     else if(lock > 0.0) {
00056         mexLock();
00057         mexPrintf("MEX-file is locked\n");
00058     }
00059     else {
00060         mexPrintf("MEX-file is unlocked\n");
00061     }    
00062     }
00063 }

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy