-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
64 lines (55 loc) · 2.07 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
#ifndef COMMON_H
#define COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "time.h"
typedef long long unsigned u64;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
typedef int64_t i64;
typedef int32_t i32;
typedef int16_t i16;
typedef int8_t i8;
typedef float f32;
typedef double f64;
#define BOOL_FMT(x) x ? "true" : "false"
#ifdef DEBUG
#define debug(fmt, ...) printf(fmt, __VA_ARGS__);
#else
#define debug(fmt, ...)
#endif
#define get_lock(lock) debug("%s:%s:%d aquiring lock: %s\n", __FILE__, __func__, __LINE__, #lock); pthread_mutex_lock((lock)); debug("%s:%s:%d aquired lock: %s\n", __FILE__, __func__, __LINE__, #lock);
#define release_lock(lock) debug("%s:%s:%d releasing lock: %s\n", __FILE__, __func__, __LINE__, #lock); pthread_mutex_unlock((lock)); debug("%s:%s:%d released lock: %s\n", __FILE__, __func__, __LINE__, #lock);
#define wait_for_lock(cond, lock) debug("%s:%s:%d waiting for signal: %s, %s\n", __FILE__, __func__, __LINE__, #cond, #lock); pthread_cond_wait((cond), (lock)); debug("%s:%s:%d got signal: %s, %s\n", __FILE__, __func__, __LINE__, #cond, #lock);
#define signal_to_locks(cond) debug("%s:%s:%d signaling: %s\n", __FILE__, __func__, __LINE__, #cond); pthread_cond_signal((cond)); debug("%s:%s:%d sent signal: %s\n", __FILE__, __func__, __LINE__, #cond);
#define broadcast_to_locks(cond) debug("%s:%s:%d signaling: %s\n", __FILE__, __func__, __LINE__, #cond); pthread_cond_broadcast((cond)); debug("%s:%s:%d broadcasted signal: %s\n", __FILE__, __func__, __LINE__, #cond);
#if defined (__x86_64__)
u64 common_rdtsc() {
u32 lo, hi;
__asm__ __volatile__ (
"xorl %%eax, %%eax\n"
"cpuid\n"
"rdtsc\n"
: "=a" (lo), "=d" (hi)
:
: "%ebx", "%ecx");
return (u64)hi << 32 | lo;
}
#else
u64 common_rdtsc() {
puts("This platform may not support this rdtsc!");
return -1;
}
#endif
u64 get_time_ms() {
struct timespec tms;
clock_gettime(CLOCK_MONOTONIC, &tms);
u64 millis = tms.tv_sec * 1000;
millis += tms.tv_nsec / 1000000;
return millis;
}
#endif