2022-03-09 19:24:49 +01:00
|
|
|
#ifndef _UTIL_H
|
|
|
|
|
#define _UTIL_H
|
|
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
#if !defined(BUFSIZE)
|
|
|
|
|
#define BUFSIZE 256
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if !defined(EXTRA_LEN_PRINT_ERROR)
|
|
|
|
|
#define EXTRA_LEN_PRINT_ERROR 512
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define SYSCALL_EXIT(name, r, sc, str, ...) \
|
|
|
|
|
if ((r=sc) == -1) { \
|
|
|
|
|
perror(#name); \
|
|
|
|
|
int errno_copy = errno; \
|
|
|
|
|
print_error(str, __VA_ARGS__); \
|
|
|
|
|
exit(errno_copy); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define SYSCALL_PRINT(name, r, sc, str, ...) \
|
|
|
|
|
if ((r=sc) == -1) { \
|
|
|
|
|
perror(#name); \
|
|
|
|
|
int errno_copy = errno; \
|
|
|
|
|
print_error(str, __VA_ARGS__); \
|
|
|
|
|
errno = errno_copy; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define SYSCALL_RETURN(name, r, sc, str, ...) \
|
|
|
|
|
if ((r=sc) == -1) { \
|
|
|
|
|
perror(#name); \
|
|
|
|
|
int errno_copy = errno; \
|
|
|
|
|
print_error(str, __VA_ARGS__); \
|
|
|
|
|
errno = errno_copy; \
|
|
|
|
|
return r; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define CHECK_EQ_EXIT(name, X, val, str, ...) \
|
|
|
|
|
if ((X)==val) { \
|
2022-03-12 10:05:58 +01:00
|
|
|
perror(#name); \
|
2022-03-09 19:24:49 +01:00
|
|
|
int errno_copy = errno; \
|
|
|
|
|
print_error(str, __VA_ARGS__); \
|
|
|
|
|
exit(errno_copy); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define CHECK_NEQ_EXIT(name, X, val, str, ...) \
|
|
|
|
|
if ((X)!=val) { \
|
2022-03-12 10:05:58 +01:00
|
|
|
perror(#name); \
|
2022-03-09 19:24:49 +01:00
|
|
|
int errno_copy = errno; \
|
|
|
|
|
print_error(str, __VA_ARGS__); \
|
|
|
|
|
exit(errno_copy); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \brief Procedura di utilita' per la stampa degli errori
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
static inline void print_error(const char * str, ...) {
|
|
|
|
|
const char err[]="ERROR: ";
|
|
|
|
|
va_list argp;
|
|
|
|
|
char * p=(char *)malloc(strlen(str)+strlen(err)+EXTRA_LEN_PRINT_ERROR);
|
|
|
|
|
if (!p) {
|
|
|
|
|
perror("malloc");
|
2022-03-12 10:05:58 +01:00
|
|
|
fprintf(stderr,"FATAL ERROR nella funzione 'print_error'\n");
|
|
|
|
|
return;
|
2022-03-09 19:24:49 +01:00
|
|
|
}
|
|
|
|
|
strcpy(p,err);
|
|
|
|
|
strcpy(p+strlen(err), str);
|
|
|
|
|
va_start(argp, str);
|
|
|
|
|
vfprintf(stderr, p, argp);
|
|
|
|
|
va_end(argp);
|
|
|
|
|
free(p);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 10:05:58 +01:00
|
|
|
#define LOCK(l) if (pthread_mutex_lock(l)!=0) { \
|
|
|
|
|
fprintf(stderr, "ERRORE FATALE lock\n"); \
|
|
|
|
|
pthread_exit((void*)EXIT_FAILURE); \
|
|
|
|
|
}
|
|
|
|
|
#define LOCK_RETURN(l, r) if (pthread_mutex_lock(l)!=0) { \
|
|
|
|
|
fprintf(stderr, "ERRORE FATALE lock\n"); \
|
|
|
|
|
return r; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define UNLOCK(l) if (pthread_mutex_unlock(l)!=0) { \
|
|
|
|
|
fprintf(stderr, "ERRORE FATALE unlock\n"); \
|
|
|
|
|
pthread_exit((void*)EXIT_FAILURE); \
|
|
|
|
|
}
|
|
|
|
|
#define UNLOCK_RETURN(l,r) if (pthread_mutex_unlock(l)!=0) { \
|
|
|
|
|
fprintf(stderr, "ERRORE FATALE unlock\n"); \
|
|
|
|
|
return r; \
|
|
|
|
|
}
|
|
|
|
|
#define WAIT(c,l) if (pthread_cond_wait(c,l)!=0) { \
|
|
|
|
|
fprintf(stderr, "ERRORE FATALE wait\n"); \
|
|
|
|
|
pthread_exit((void*)EXIT_FAILURE); \
|
|
|
|
|
}
|
2022-03-09 19:24:49 +01:00
|
|
|
/* ATTENZIONE: t e' un tempo assoluto! */
|
2022-03-12 10:05:58 +01:00
|
|
|
#define TWAIT(c,l,t) { \
|
|
|
|
|
int r=0; \
|
|
|
|
|
if ((r=pthread_cond_timedwait(c,l,t))!=0 && r!=ETIMEDOUT) { \
|
|
|
|
|
fprintf(stderr, "ERRORE FATALE timed wait\n"); \
|
|
|
|
|
pthread_exit((void*)EXIT_FAILURE); \
|
|
|
|
|
} \
|
|
|
|
|
}
|
|
|
|
|
#define SIGNAL(c) if (pthread_cond_signal(c)!=0) { \
|
|
|
|
|
fprintf(stderr, "ERRORE FATALE signal\n"); \
|
|
|
|
|
pthread_exit((void*)EXIT_FAILURE); \
|
|
|
|
|
}
|
|
|
|
|
#define BCAST(c) if (pthread_cond_broadcast(c)!=0) { \
|
|
|
|
|
fprintf(stderr, "ERRORE FATALE broadcast\n"); \
|
|
|
|
|
pthread_exit((void*)EXIT_FAILURE); \
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 19:24:49 +01:00
|
|
|
static inline int TRYLOCK(pthread_mutex_t* l) {
|
2022-03-12 10:05:58 +01:00
|
|
|
int r=0;
|
|
|
|
|
if ((r=pthread_mutex_trylock(l))!=0 && r!=EBUSY) {
|
|
|
|
|
fprintf(stderr, "ERRORE FATALE unlock\n");
|
|
|
|
|
pthread_exit((void*)EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
return r;
|
2022-03-09 19:24:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* _UTIL_H */
|