-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxerrors.c
139 lines (119 loc) · 4.01 KB
/
xerrors.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#define _GNU_SOURCE
#include "xerrors.h"
// xerrors.c
// system calls with output checking
#define Buflen 100
#define Thread_error_wait 5
// write error message associated to code en similarly to perror
void xperror(int en, char *msg) {
char buf[Buflen];
char *errmsg = strerror_r(en, buf, Buflen);
if(msg!=NULL)
fprintf(stderr,"%s: %s\n",msg, errmsg);
else
fprintf(stderr,"%s\n",errmsg);
}
// ----- threads
int xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg, int linea, const char *file) {
int e = pthread_create(thread, attr, start_routine, arg);
if (e!=0) {
xperror(e, "Error in pthread_create");
fprintf(stderr,"== %d == Line: %d, File: %s\n",getpid(),linea,file);
sleep(Thread_error_wait); // do not kill immediately other threads
exit(1);
}
return e;
}
int xpthread_join(pthread_t thread, void **retval, int linea, const char *file) {
int e = pthread_join(thread, retval);
if (e!=0) {
xperror(e, "Error in pthread_join");
fprintf(stderr,"== %d == Line: %d, File: %s\n",getpid(),linea,file);
sleep(Thread_error_wait); // do not kill immediately other threads
exit(1);
}
return e;
}
// mutex
int xpthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr, int linea, const char *file) {
int e = pthread_mutex_init(mutex, attr);
if (e!=0) {
xperror(e, "Error in pthread_mutex_init");
fprintf(stderr,"== %d == Line: %d, File: %s\n",getpid(),linea,file);
sleep(Thread_error_wait); // do not kill immediately other threads
exit(1);
}
return e;
}
int xpthread_mutex_destroy(pthread_mutex_t *mutex, int linea, const char *file) {
int e = pthread_mutex_destroy(mutex);
if (e!=0) {
xperror(e, "Error in pthread_mutex_destroy");
fprintf(stderr,"== %d == Line: %d, File: %s\n",getpid(),linea,file);
sleep(Thread_error_wait); // do not kill immediately other threads
exit(1);
}
return e;
}
int xpthread_mutex_lock(pthread_mutex_t *mutex, int linea, const char *file) {
int e = pthread_mutex_lock(mutex);
if (e!=0) {
xperror(e, "Error in pthread_mutex_lock");
fprintf(stderr,"== %d == Line: %d, File: %s\n",getpid(),linea,file);
sleep(Thread_error_wait); // do not kill immediately other threads
exit(1);
}
return e;
}
int xpthread_mutex_unlock(pthread_mutex_t *mutex, int linea, const char *file) {
int e = pthread_mutex_unlock(mutex);
if (e!=0) {
xperror(e, "Error in pthread_mutex_unlock");
fprintf(stderr,"== %d == Line: %d, File: %s\n",getpid(),linea,file);
sleep(Thread_error_wait); // do not kill immediately other threads
exit(1);
}
return e;
}
// semaphores
int xsem_init(sem_t *sem, int pshared, unsigned int value, int linea, const char *file) {
int e = sem_init(sem,pshared,value);
if (e!=0) {
xperror(e, "Error in sem_init");
fprintf(stderr,"== %d == Line: %d, File: %s\n",getpid(),linea,file);
sleep(Thread_error_wait); // do not kill immediately other threads
exit(1);
}
return e;
}
int xsem_post(sem_t *sem, int linea, const char *file) {
int e = sem_post(sem);
if (e!=0) {
xperror(e, "Error in sem_post");
fprintf(stderr,"== %d == Line: %d, File: %s\n",getpid(),linea,file);
sleep(Thread_error_wait); // do not kill immediately other threads
exit(1);
}
return e;
}
int xsem_wait(sem_t *sem, int linea, const char *file) {
int e = sem_wait(sem);
if (e!=0) {
xperror(e, "Error in sem_wait");
fprintf(stderr,"== %d == Line: %d, File: %s\n",getpid(),linea,file);
sleep(Thread_error_wait); // do not kill immediately other threads
exit(1);
}
return e;
}
int xsem_destroy(sem_t *sem, int linea, const char *file) {
int e = sem_destroy(sem);
if (e!=0) {
xperror(e, "Error in sem_destroy");
fprintf(stderr,"== %d == Line: %d, File: %s\n",getpid(),linea,file);
sleep(Thread_error_wait); // do not kill immediately other threads
exit(1);
}
return e;
}