[threads] Update mutex/cond functions to use new CHECK_ERR macros

Added various macros to check return values and log any errors and abort
if the call fails.
Updated logging to handle early errors before logging initialized.
This commit is contained in:
Scott Shambarger
2017-01-21 07:11:20 -08:00
parent b54d94fda6
commit 8e3797ec43
8 changed files with 207 additions and 185 deletions

View File

@@ -97,27 +97,49 @@ timespec_add(struct timespec time1, struct timespec time2);
int
timespec_cmp(struct timespec time1, struct timespec time2);
/* mutex wrappers with checks */
void
fork_mutex_init(pthread_mutex_t *mutex);
void
fork_mutex_lock(pthread_mutex_t *mutex);
void
fork_mutex_unlock(pthread_mutex_t *mutex);
void
fork_mutex_destroy(pthread_mutex_t *mutex);
/* condition wrappers with checks */
void
fork_cond_init(pthread_cond_t *cond);
void
fork_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
/* initialize mutex with error checking (not default on all platforms) */
int
fork_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
const struct timespec *ts);
void
fork_cond_signal(pthread_cond_t *cond);
void
fork_cond_destroy(pthread_cond_t *cond);
mutex_init(pthread_mutex_t *mutex);
/* Check that the function returns 0, logging a fatal error referencing
returned error (type errno) if it fails, and aborts the process.
Example: CHECK_ERR(L_MAIN, my_function()); */
#define CHECK_ERR(d, f) \
do { int chk_err; \
if ( (chk_err = (f)) != 0) \
log_fatal_err(d, #f, __LINE__, chk_err); \
} while(0)
/* Check that the function returns 0 or okval, logging a fatal
error referencing returned erro (type errno) if not, and aborts the process.
Example: int err; CHECK_ERR_EXCEPT(L_MAIN, my_wait(), err, ETIMEDOUT); */
#define CHECK_ERR_EXCEPT(d, f, var, okval) \
do { (var) = (f); \
if (! (((var) == (okval)) || ((var) == 0))) \
log_fatal_err(d, #f, __LINE__, (var)); \
} while(0)
/* Check that the function returns value >= 0, logging a fatal error
referencing errno if it not, and aborts the process.
Example: int ret; CHECK_ERRNO(L_MAIN, ret = my_function()); */
#define CHECK_ERRNO(d, f) \
do { \
if ( (f) < 0 ) \
log_fatal_errno(d, #f, __LINE__); \
} while(0)
/* Check that the function returns non-NULL, logging a fatal error if not,
and aborts the process.
Example: void *ptr; CHECK_NULL(L_MAIN, ptr = my_create()); */
#define CHECK_NULL(d, f) \
do { \
if ( (f) == NULL ) \
log_fatal_null(d, #f, __LINE__); \
} while(0)
/* Used by CHECK_*() macros */
void log_fatal_err(int domain, const char *func, int line, int err);
void log_fatal_errno(int domain, const char *func, int line);
void log_fatal_null(int domain, const char *func, int line);
#endif /* !__MISC_H__ */