-
Notifications
You must be signed in to change notification settings - Fork 16
/
C700TimeThread.h
110 lines (101 loc) · 2.82 KB
/
C700TimeThread.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
//
// C700TimeThread.h
// C700
//
// Created by osoumen on 2016/01/03.
//
//
#ifndef C700_C700TimeThread_h
#define C700_C700TimeThread_h
#ifdef _MSC_VER
#include <Windows.h>
// QueryPerformanceCounter による時間計測(Windows)
typedef long long MSTime;
typedef LARGE_INTEGER OSTime;
inline MSTime calcusTime(const OSTime &end, const OSTime &st) {
LARGE_INTEGER nFreq;
QueryPerformanceFrequency(&nFreq);
return (MSTime)((end.QuadPart - st.QuadPart) * 1000000 / nFreq.QuadPart);
}
inline void getNowOSTime(OSTime &time) {
QueryPerformanceCounter(&time);
}
inline void operator += (OSTime &time, MSTime addus) {
LARGE_INTEGER nFreq;
QueryPerformanceFrequency(&nFreq);
time.QuadPart += (addus * nFreq.QuadPart) / 1000000;
}
inline void WaitMicroSeconds(MSTime usec) {
::Sleep(usec / 1000); // 現状1ms未満の箇所は無い
}
// Windows標準のスレッド処理
typedef HANDLE ThreadObject;
typedef LPTHREAD_START_ROUTINE ThreadFunc;
inline void ThreadCreate(HANDLE &obj, ThreadFunc start_routine, LPVOID arg) {
DWORD dwID;
obj = CreateThread(NULL, 0, start_routine, arg, 0, &dwID);
SetThreadPriority(obj, THREAD_PRIORITY_HIGHEST);
}
inline void ThreadJoin(HANDLE &obj) {
WaitForSingleObject(obj, INFINITE);
}
typedef CRITICAL_SECTION MutexObject;
inline void MutexInit(MutexObject &obj) {
InitializeCriticalSection(&obj);
}
inline void MutexDestroy(MutexObject &obj) {
DeleteCriticalSection(&obj);
}
inline void MutexLock(MutexObject &obj) {
EnterCriticalSection(&obj);
}
inline void MutexUnlock(MutexObject &obj) {
LeaveCriticalSection(&obj);
}
#else
// timeval による時間計測(unix系)
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
typedef long long MSTime;
typedef timeval OSTime;
inline MSTime calcusTime(const OSTime &end, const OSTime &st) {
return ((end.tv_sec - st.tv_sec) * 1e6 + (end.tv_usec - st.tv_usec));
}
inline void getNowOSTime(OSTime &time) {
gettimeofday(&time, NULL);
}
inline void operator += (OSTime &time, MSTime addus) {
time.tv_usec += addus;
if (time.tv_usec >= 1000000) {
time.tv_usec -= 1000000;
time.tv_sec++;
}
}
inline void WaitMicroSeconds(MSTime usec) {
usleep(usec);
}
// pthread によるスレッド、同期処理
typedef pthread_t ThreadObject;
typedef void *(*ThreadFunc)(void*);
inline void ThreadCreate(ThreadObject &obj, ThreadFunc start_routine, void *arg) {
pthread_create(&obj, NULL, start_routine, arg);
}
inline void ThreadJoin(ThreadObject &obj) {
pthread_join(obj, NULL);
}
typedef pthread_mutex_t MutexObject;
inline void MutexInit(MutexObject &obj) {
pthread_mutex_init(&obj, 0);
}
inline void MutexDestroy(MutexObject &obj) {
pthread_mutex_destroy(&obj);
}
inline void MutexLock(MutexObject &obj) {
pthread_mutex_lock(&obj);
}
inline void MutexUnlock(MutexObject &obj) {
pthread_mutex_unlock(&obj);
}
#endif
#endif