-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.h
executable file
·113 lines (90 loc) · 1.83 KB
/
common.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
111
112
113
//
// Created by Haifa Bogdan Adnan on 04/08/2018.
//
#ifndef COMMON_H
#define COMMON_H
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <regex>
#include <random>
#include <thread>
#include <mutex>
#include <chrono>
#include <atomic>
#include <cmath>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
#include <fcntl.h>
#include <getopt.h>
#include <microhttpd.h>
#ifdef __APPLE__
#include <dispatch/dispatch.h>
#else
#include <semaphore.h>
#endif
struct rk_sema {
#ifdef __APPLE__
dispatch_semaphore_t sem;
#else
sem_t sem;
#endif
};
static inline void
rk_sema_init(struct rk_sema *s, uint32_t value)
{
#ifdef __APPLE__
dispatch_semaphore_t *sem = &s->sem;
*sem = dispatch_semaphore_create(value);
#else
sem_init(&s->sem, 0, value);
#endif
}
static inline bool
rk_sema_wait(struct rk_sema *s, int timeout_sec)
{
#ifdef __APPLE__
return dispatch_semaphore_wait(s->sem, dispatch_time(DISPATCH_TIME_NOW, timeout_sec * 1000000000)) == 0; //timeout after 5 sec
#else
int r;
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) return false;
ts.tv_sec += timeout_sec;
while ((r = sem_timedwait(&s->sem, &ts)) == -1 && errno == EINTR)
continue;
return r != -1;
#endif
}
static inline void
rk_sema_post(struct rk_sema *s)
{
#ifdef __APPLE__
dispatch_semaphore_signal(s->sem);
#else
sem_post(&s->sem);
#endif
}
static inline void
rk_sema_destroy(struct rk_sema *s)
{
#ifdef __APPLE__
dispatch_release(s->sem);
#else
sem_destroy(&s->sem);
#endif
}
using namespace std;
#define LOG(msg) cout<<msg<<endl<<flush
#include <config.h>
#endif