]> git.donarmstrong.com Git - rsem.git/blob - my_assert.h
Imported Upstream version 1.2.17
[rsem.git] / my_assert.h
1 #ifndef MY_ASSERT_H
2 #define MY_ASSERT_H
3
4 #include<cstdio>
5 #include<cstring>
6 #include<cstdlib>
7 #include<cerrno>
8 #include<string>
9 #include<sstream>
10 #include<iomanip>
11
12 inline std::string itos(int i) {
13   std::ostringstream strout;
14   strout<<i;
15   return strout.str();
16 }
17
18 // n : number of significant digits
19 inline std::string ftos(double f, int n) {
20   std::ostringstream strout;
21   strout<<std::setprecision(n)<<f;
22   return strout.str();
23 }
24
25 inline std::string ctos(char c) {
26   return std::string(1, c);
27 }
28
29 inline std::string cstrtos(const char* s) {
30   return std::string(s);
31 }
32
33
34 #define general_assert(expr, errmsg) if (!(expr)) general_report((errmsg), false)
35 #define general_assert_1(expr, errmsg) if (!(expr)) general_report((errmsg), true)
36
37 inline void general_report(const std::string& errmsg, bool putEnter) {
38         if (putEnter) printf("\n");
39         fprintf(stderr, "%s\n", errmsg.c_str());
40         exit(-1);
41 }
42
43 #define pthread_assert(rc, func_name, errmsg) if ((rc) != 0) pthread_report((rc), (func_name), (errmsg))
44
45 inline void pthread_report(int rc, const std::string& func_name, const std::string& errmsg) {
46         fprintf(stderr, "%s\n", errmsg.c_str());
47
48         if (func_name == "pthread_create") {
49                 switch(rc) {
50                 case EAGAIN:
51                         fprintf(stderr, "Error code: EAGAIN. Insufficient resources to create another thread, or a system-imposed limit on the number of threads was encountered.\n");
52                         break;
53                 case EINVAL:
54                         fprintf(stderr, "Error code: EINVAL. Invalid settings in attr.\n");
55                         break;
56                 case EPERM:
57                         fprintf(stderr, "Error code: EPERM. No permission to set the scheduling policy and parameters specified in attr.\n");
58                         break;
59                 default: fprintf(stderr, "Unknown error code: %d.\n", rc);
60                 }
61         } else if (func_name == "pthread_join") {
62                 switch(rc) {
63                 case EDEADLK:
64                         fprintf(stderr, "Error code: EDEADLK. A deadlock was detected (e.g., two threads tried to join with each other); or thread_id specifies the calling thread.\n");
65                         break;
66                 case EINVAL:
67                         fprintf(stderr, "Error code: EINVAL. The implementation has detected that the value specified by thread_id does not refer to a joinable thread.\n");
68                         break;
69                 case ESRCH:
70                         fprintf(stderr, "Error code: ESRCH. No thread with thread_id could be found.\n");
71                         break;
72                 default: fprintf(stderr, "Unknown error code: %d.\n", rc);
73                 }
74         } else if (func_name == "pthread_mutex_lock") {
75                 switch(rc) {
76                 case EAGAIN:
77                         fprintf(stderr, "Error code: EAGAIN. The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded.\n");
78                         break;
79                 case EDEADLK:
80                         fprintf(stderr, "Error code: EDEADLK. The current thread already owns the mutex.\n");
81                         break;
82                 case EINVAL:
83                         fprintf(stderr, "Error code: EINVAL. The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT and the calling thread's priority is higher than the mutex's current priority ceiling. Or the value specified by mutex does not refer to an initialized mutex object.\n");
84                         break;
85                 default: fprintf(stderr, "Unknown error code: %d.\n", rc);
86                 }
87         } else if (func_name == "pthread_mutex_unlock") {
88                 switch(rc) {
89                 case EAGAIN:
90                         fprintf(stderr, "Error code: EAGAIN. The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded.\n");
91                         break;
92                 case EINVAL:
93                         fprintf(stderr, "Error code: EINVAL. The value specified by mutex does not refer to an initialized mutex object.\n");
94                         break;
95                 case EPERM:
96                         fprintf(stderr, "Error code: EPERM. The current thread does not own the mutex.\n");
97                         break;
98                 default: fprintf(stderr, "Unknown error code: %d.\n", rc);
99                 }
100         } else {
101                 fprintf(stderr, "Unknown function name: %s.\n", func_name.c_str());
102         }
103
104         exit(-1);
105 }
106
107 #endif