12 std::string itos(int i) {
13 std::ostringstream strout;
18 // n : number of significant digits
19 std::string ftos(double f, int n) {
20 std::ostringstream strout;
21 strout<<std::setprecision(n)<<f;
25 std::string ctos(char c) {
26 return std::string(1, c);
29 std::string cstrtos(const char* s) {
30 return std::string(s);
34 void general_assert(int expr, const std::string& errmsg) {
37 fprintf(stderr, "%s\n", errmsg.c_str());
41 void pthread_assert(int rc, const std::string& func_name, const std::string& errmsg) {
44 fprintf(stderr, "%s\n", errmsg.c_str());
46 if (func_name == "pthread_create") {
49 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 fprintf(stderr, "Error code: EINVAL. Invalid settings in attr.\n");
55 fprintf(stderr, "Error code: EPERM. No permission to set the scheduling policy and parameters specified in attr.\n");
57 default: fprintf(stderr, "Unknown error code: %d.\n", rc);
59 } else if (func_name == "pthread_join") {
62 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 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 fprintf(stderr, "Error code: ESRCH. No thread with thread_id could be found.\n");
70 default: fprintf(stderr, "Unknown error code: %d.\n", rc);
72 } else if (func_name == "pthread_mutex_lock") {
75 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 fprintf(stderr, "Error code: EDEADLK. The current thread already owns the mutex.\n");
81 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 default: fprintf(stderr, "Unknown error code: %d.\n", rc);
85 } else if (func_name == "pthread_mutex_unlock") {
88 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 fprintf(stderr, "Error code: EINVAL. The value specified by mutex does not refer to an initialized mutex object.\n");
94 fprintf(stderr, "Error code: EPERM. The current thread does not own the mutex.\n");
96 default: fprintf(stderr, "Unknown error code: %d.\n", rc);
99 fprintf(stderr, "Unknown function name: %s.\n", func_name.c_str());