I created the following macros to lock the mutex and return (from the function in which this macro is called) in the event of a failed lock attempt. Currently, I have narrowed it down to two macros - one for returning from functions that return a value, regardless of type, and another for returning from functions that don't return anything (i.e. Void).
The code outside the macros (see below) is for illustration only and has very little to do with the actual production code in which the macros will be used.
#define MUTEX_LOCK()\ {\ if (pthread_mutex_lock(&mutex) != 0)\ {\ printf("Failed to lock mutex.\n");\ return;\ }\ } #define MUTEX_LOCK_RVAL(err_val)\ {\ if (pthread_mutex_lock(&mutex) != 0)\ {\ printf("Failed to lock mutex.\n");\ return err_val;\ }\ } void vfunc() { printf("\nIn vfunc()\n"); MUTEX_LOCK(); printf("\nOut of vfunc()\n"); } UINT16 uint16func() { printf("\nIn uint16func()\n"); MUTEX_LOCK_RVAL(0); printf("\nOut of uint16func()\n"); return 9; } CHAR* errstr = "Hoo boy!"; CHAR* strfunc() { printf("\nIn strfunc()\n"); MUTEX_LOCK_RVAL(errstr); printf("\nOut of strfunc()\n"); return NULL; }
Is there a way to reduce them to a single macro that can be used in functions that return a value, as well as void.
source share