| 71 | }; |
| 72 | |
| 73 | class Thread { |
| 74 | static const int WAIT_USECONDS = 50000; |
| 75 | typedef void* (*wrapper_t)(void*); |
| 76 | |
| 77 | public: |
| 78 | Thread(void* (*wrapper)(void*), ThreadArg* arg, bool delete_arg, unsigned int num) : wrapper(wrapper), arg(arg), delete_arg(delete_arg), num(num) { |
| 79 | state = NOT_STARTED; |
| 80 | tid = 0; |
| 81 | memset(&tv_started, 0, sizeof(tv_started)); |
| 82 | memset(&tv_finished, 0, sizeof(tv_finished)); |
| 83 | pthread_mutex_init(&mp, NULL); |
| 84 | } |
| 85 | |
| 86 | void running() { |
| 87 | pthread_mutex_lock(&mp); |
| 88 | gettimeofday(&tv_started, NULL); |
| 89 | state = RUNNING; |
| 90 | tid = pthread_self(); |
| 91 | pthread_mutex_unlock(&mp); |
| 92 | } |
| 93 | |
| 94 | void finished() { |
| 95 | pthread_mutex_lock(&mp); |
| 96 | gettimeofday(&tv_finished, NULL); |
| 97 | state = FINISHED; |
| 98 | pthread_mutex_unlock(&mp); |
| 99 | } |
| 100 | |
| 101 | bool waitFinished() { |
| 102 | |
| 103 | |
| 104 | #ifdef _WIN32 |
| 105 | //x32 Windows definitions |
| 106 | Sleep(WAIT_USECONDS); |
| 107 | #else |
| 108 | //other platforms |
| 109 | usleep(WAIT_USECONDS); |
| 110 | #endif |
| 111 | |
| 112 | usleep(WAIT_USECONDS); |
| 113 | pthread_mutex_lock(&mp); |
| 114 | if (state == FINISHED) { |
| 115 | pthread_mutex_unlock(&mp); |
| 116 | return true; |
| 117 | } |
| 118 | pthread_mutex_unlock(&mp); |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | unsigned int getNumber() const {return num;} |
| 123 | pthread_t getThreadSelf() const {return tid;} |
| 124 | |
| 125 | long long duration() const { |
| 126 | pthread_mutex_lock(&mp); |
| 127 | long long tv_usec = (state == FINISHED ? (unsigned long long)(tv_finished.tv_sec - tv_started.tv_sec) * 1000000 + (tv_finished.tv_usec - tv_started.tv_usec) : -1); |
| 128 | pthread_mutex_unlock(&mp); |
| 129 | return tv_usec; |
| 130 | } |
nothing calls this directly
no outgoing calls
no test coverage detected