Advertisement
QenBau

utils.h

Aug 2nd, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <errno.h>
  5.  
  6. #define READ_END 0
  7. #define WRITE_END 1
  8.  
  9. #define RETURN_SYSCALL(r,c,e) if((r=c)==-1) { perror(e);exit(errno); }
  10. #define SYSCALL(c,e) if(c==-1) { perror(e);exit(errno);}
  11. #define THREAD_CREATE(a, b, c, d, text) if(pthread_create(a, b, c, d) != 0) { perror(text);exit(EXIT_FAILURE);}
  12. #define THREAD_JOIN(a, b, text) if(pthread_join(a, b) != 0) { perror(text);exit(EXIT_FAILURE);}
  13. //usare con le funzioni che ritornano NULL quando falliscono e di cui si vuole memorizzare il valore di ritorno (es: fopen)
  14. #define RETURN_NULLFUN(retrunVar, fun, text) if((r=fun == NULL) { perror(text);exit(errno); }
  15. //usare per le syscall che quando falliscono ritornano un valore != 0
  16. #define SYSCALL_ZERO(syscall, text) if(syscall != 0) {perror(text);exit(errno);}
  17.  
  18. #define LOCK(l) \
  19. if (pthread_mutex_lock(l) != 0) \
  20. { \
  21. fprintf(stderr, "ERRORE FATALE lock\n"); \
  22. pthread_exit((void*)EXIT_FAILURE); \
  23. }
  24.  
  25. #define UNLOCK(l) \
  26. if (pthread_mutex_unlock(l) != 0) \
  27. { \
  28. fprintf(stderr, "ERRORE FATALE unlock\n"); \
  29. pthread_exit((void*)EXIT_FAILURE); \
  30. }
  31.  
  32. #define SIGNAL(c) \
  33. if (pthread_cond_signal(c) != 0) \
  34. { \
  35. fpr intf(stderr, "ERRORE FATALE signal\n"); \
  36. pthread_exit((void*)EXIT_FAILURE); \
  37. }
  38.  
  39. #define WAIT(c, l) \
  40. if (pthread_cond_wait(c,l) != 0) \
  41. { \
  42. fprintf(stderr, "ERRORE FATALE wait\n"); \
  43. pthread_exit((void*)EXIT_FAILURE); \
  44. }
  45.  
  46. #define SCANF_STRINGA(stringa) \
  47. if(scanf("%s", stringa) == 0) \
  48. { \
  49. perror("Impossibile leggere la stringa"); \
  50. exit(EXIT_FAILURE); \
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement