-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathcommon.h
133 lines (120 loc) · 5.01 KB
/
common.h
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
/*
* common.h
*
***************************************************************
* This program is part of the source code released for the book
* "Hands-on System Programming with Linux"
* (c) Author: Kaiwan N Billimoria
* Publisher: Packt
*
* From:
* 'Common' code.
****************************************************************
* Brief Description:
* This is the 'common' code that gets compiled into all binary
* executables.
*/
#ifndef __HOLSP_COMMON_H__
#define __HOLSP_COMMON_H__
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/*--- 'Regular' headers ---*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
/*---*/
/*--- Function prototypes ---*/
int show_blocked_signals(void);
int handle_err(int fatal, const char *fmt, ...);
int r_sleep(time_t sec, long nsec);
/*--- Macros ---*/
#define NON_FATAL 0
#define WARN(warnmsg, args...) do { \
handle_err(NON_FATAL, "!WARNING! %s:%s:%d: " warnmsg, \
__FILE__, __func__, __LINE__, ##args); \
} while(0)
#define FATAL(errmsg, args...) do { \
handle_err(EXIT_FAILURE, "FATAL:%s:%s:%d: " errmsg, \
__FILE__, __func__, __LINE__, ##args); \
} while(0)
/*------------------------ DELAY_LOOP --------------------------------*/
static inline void beep(int what)
{
char buf[1];
buf[0] = what;
(void)write(STDOUT_FILENO, buf, 1);
}
/*
* DELAY_LOOP macro
* @val : ASCII value to print
* @loop_count : times to loop around
*/
#define HZ 250
#define DELAY_LOOP(val,loop_count) \
{ \
int c=0, m; \
unsigned int for_index,inner_index; \
\
for(for_index=0;for_index<loop_count;for_index++) { \
beep((val)); \
c++; \
for(inner_index=0;inner_index<HZ*1000;inner_index++) \
for(m=0;m<30;m++); \
} \
/*printf("c=%d\n",c);*/ \
}
#define DELAY_LOOP_SILENT(loop_count) \
{ \
int c=0, m; \
unsigned int for_index,inner_index; \
\
for(for_index=0;for_index<loop_count;for_index++) { \
c++; \
for(inner_index=0;inner_index<HZ*1000;inner_index++) \
for(m=0;m<30;m++); \
} \
/*printf("c=%d\n",c);*/ \
}
/*-------------------- VPRINT : verbose print ----------------------------*/
/* IMP!
* The VPRINT macro depends on the caller having a (usually global) variable
* named 'gVerbose'. IFF it exists and is True, the printf gets emitted.
*/
#define VPRINT(msg, args...) do { \
if (gVerbose) \
printf("%s:%s:%d: " msg, \
__FILE__, __func__, __LINE__, ##args); \
} while(0)
/*------------------------ timerspecsub ----------------------------------*/
/*
* Macro: timerspecsub
* Perform (on struct timespec's): result=(a-b).
* Modified from the original 'timersub' macro defined in the <sys/time.h> header.
* From the original man page entry:
* "timersub() subtracts the time value in b from the time value in a, and
* places the result in the timeval pointed to by res. The result is
* normalized such that res->tv_usec (now becomes 'nsec') has a value in the
* range 0 to 999,999. (now 999,999,999)"
*/
#define timerspecsub(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
if ((result)->tv_nsec < 0) { \
--(result)->tv_sec; \
(result)->tv_nsec += 1000000000; \
} \
} while (0)
/*--------------- Mutex Lock and Unlock ----------------------------------*/
#define LOCK_MTX(mtx) do { \
int ret=0; \
if ((ret = pthread_mutex_lock(mtx))) \
FATAL("pthread_mutex_lock failed! [%d]\n", ret); \
} while(0)
#define UNLOCK_MTX(mtx) do { \
int ret=0; \
if ((ret = pthread_mutex_unlock(mtx))) \
FATAL("pthread_mutex_unlock failed! [%d]\n", ret); \
} while(0)
#endif