]> git.donarmstrong.com Git - rsem.git/blob - my_assert.h
Modified 'convert-sam-for-rsem' so that this program will convert users' SAM/BAM...
[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 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 std::string ftos(double f, int n) {
20   std::ostringstream strout;
21   strout<<std::setprecision(n)<<f;
22   return strout.str();
23 }
24
25 std::string ctos(char c) {
26   return std::string(1, c);
27 }
28
29 std::string cstrtos(const char* s) {
30   return std::string(s);
31 }
32
33
34 void general_assert(int expr, const std::string& errmsg, bool putEnter = false) {
35         if (expr) return;
36
37         if (putEnter) printf("\n");
38         fprintf(stderr, "%s\n", errmsg.c_str());
39         exit(-1);
40 }
41
42 void pthread_assert(int rc, const std::string& func_name, const std::string& errmsg) {
43         if (rc == 0) return;
44
45         fprintf(stderr, "%s\n", errmsg.c_str());
46
47         if (func_name == "pthread_create") {
48                 switch(rc) {
49                 case EAGAIN:
50                         fprintf(stderr, "Error code: EAGAIN. Insufficient resources to create another thread, or a system-imposed limit on the number of threads was encountered.\n");
51                         break;
52                 case EINVAL:
53                         fprintf(stderr, "Error code: EINVAL. Invalid settings in attr.\n");
54                         break;
55                 case EPERM:
56                         fprintf(stderr, "Error code: EPERM. No permission to set the scheduling policy and parameters specified in attr.\n");
57                         break;
58                 default: fprintf(stderr, "Unknown error code: %d.\n", rc);
59                 }
60         } else if (func_name == "pthread_join") {
61                 switch(rc) {
62                 case EDEADLK:
63                         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");
64                         break;
65                 case EINVAL:
66                         fprintf(stderr, "Error code: EINVAL. The implementation has detected that the value specified by thread_id does not refer to a joinable thread.\n");
67                         break;
68                 case ESRCH:
69                         fprintf(stderr, "Error code: ESRCH. No thread with thread_id could be found.\n");
70                         break;
71                 default: fprintf(stderr, "Unknown error code: %d.\n", rc);
72                 }
73         } else if (func_name == "pthread_mutex_lock") {
74                 switch(rc) {
75                 case EAGAIN:
76                         fprintf(stderr, "Error code: EAGAIN. The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded.\n");
77                         break;
78                 case EDEADLK:
79                         fprintf(stderr, "Error code: EDEADLK. The current thread already owns the mutex.\n");
80                         break;
81                 case EINVAL:
82                         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");
83                         break;
84                 default: fprintf(stderr, "Unknown error code: %d.\n", rc);
85                 }
86         } else if (func_name == "pthread_mutex_unlock") {
87                 switch(rc) {
88                 case EAGAIN:
89                         fprintf(stderr, "Error code: EAGAIN. The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded.\n");
90                         break;
91                 case EINVAL:
92                         fprintf(stderr, "Error code: EINVAL. The value specified by mutex does not refer to an initialized mutex object.\n");
93                         break;
94                 case EPERM:
95                         fprintf(stderr, "Error code: EPERM. The current thread does not own the mutex.\n");
96                         break;
97                 default: fprintf(stderr, "Unknown error code: %d.\n", rc);
98                 }
99         } else {
100                 fprintf(stderr, "Unknown function name: %s.\n", func_name.c_str());
101         }
102
103         exit(-1);
104 }
105
106 #endif