-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpe_parallel
66 lines (57 loc) · 1.69 KB
/
pe_parallel
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
#ifndef PE_PARALLEL_
#define PE_PARALLEL_
#include "pe_base"
#include "pe_mod"
#include "pe_time"
#include "pe_persistance"
#if OS_TYPE_WIN
namespace pe {
enum {
PRIORITY_REALTIME = REALTIME_PRIORITY_CLASS,
PRIORITY_HIGH = HIGH_PRIORITY_CLASS,
PRIORITY_ABOVE_NORMAL = ABOVE_NORMAL_PRIORITY_CLASS,
PRIORITY_NORMAL = NORMAL_PRIORITY_CLASS,
PRIORITY_BELOW_NORMAL = BELOW_NORMAL_PRIORITY_CLASS,
PRIORITY_BACKGROUND = 0x00100000, // PROCESS_MODE_BACKGROUND_BEGIN,
PRIORITY_IDLE = IDLE_PRIORITY_CLASS,
};
static inline void SetProcessPriority(int priority) {
::SetPriorityClass(::GetCurrentProcess(), priority);
}
static inline void MakeSureProcessSingleton(const char* id) {
std::string mutex_name = "pe_mutex_prefix_";
mutex_name += id;
HANDLE hMutex = ::OpenMutex(MUTEX_ALL_ACCESS, FALSE, mutex_name.c_str());
if (hMutex) {
fprintf(stderr, "another process is running\n");
::CloseHandle(hMutex);
exit(-1);
return;
}
hMutex = ::CreateMutex(nullptr, TRUE, mutex_name.c_str());
if (::GetLastError() == ERROR_ALREADY_EXISTS) {
fprintf(stderr, "another process is running\n");
::CloseHandle(hMutex);
exit(-1);
return;
}
}
} // namespace pe
#endif // end OS_TYPE_WIN
#if ENABLE_OPENMP
namespace pe {
class OmpLock {
public:
OmpLock() { omp_init_lock(&locker_); }
~OmpLock() { omp_destroy_lock(&locker_); }
OmpLock(const OmpLock&) = delete;
OmpLock& operator=(const OmpLock&) = delete;
void lock() { omp_set_lock(&locker_); }
void unlock() { omp_unset_lock(&locker_); }
private:
omp_lock_t locker_;
};
using OmpGuard = std::lock_guard<pe::OmpLock>;
} // namespace pe
#endif
#endif