-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcommon.h
More file actions
101 lines (84 loc) · 2.61 KB
/
common.h
File metadata and controls
101 lines (84 loc) · 2.61 KB
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
#ifndef COMMON_H
#define COMMON_H
#include <stdio.h>
#include <semaphore.h>
#include <errno.h>
#if __has_include ("tracy/TracyC.h")
#include "tracy/TracyC.h"
#endif
#include "workqueue.h"
#ifdef TRACY_ENABLE
#define TracyCMessageFmt(fmt, ...) do { \
char *msg; \
int len = asprintf(&msg, fmt, ##__VA_ARGS__); \
TracyCMessage(msg, len); free(msg); \
} while (0)
#else
#define TracyCMessageFmt(fmt, ...)
#endif
#ifdef TRACY_ENABLE
typedef struct Mutex {
TracyCLockCtx tracyCtx;
pthread_mutex_t mutex;
} Mutex;
#else
typedef pthread_mutex_t Mutex;
#endif
typedef struct ThreadControlBlock {
int index;
pid_t _Atomic tid;
pthread_t pthread;
WorkQueue* workQueue;
// Used for -serially and for profiling & diagnostics.
WorkQueueItem currentItem;
Mutex currentItemMutex;
// Used for managing the threadpool.
clockid_t clockid;
int64_t _Atomic currentItemStartTimestamp;
bool _Atomic wasObservedAsBlocked;
// We may deactivate (block on semaphore indefinitely) threads if
// they got caught on some I/O-bound task and there are enough
// non-benched threads to utilize the CPUs.
bool _Atomic isDeactivated;
sem_t reactivate;
// Current match being constructed (if applicable).
Match* currentMatch;
AtomicallyVersion* currentAtomicallyVersion;
// If running in subscription
int inSubscription;
// FOR DEBUGGING:
int _Atomic _allocs;
int _Atomic _frees;
} ThreadControlBlock;
#define THREADS_MAX 100
extern ThreadControlBlock threads[THREADS_MAX];
extern int _Atomic threadCount;
extern __thread ThreadControlBlock* self;
static inline int64_t timestamp_get(clockid_t clk_id) {
// Returns timestamp in nanoseconds.
struct timespec ts;
if (clock_gettime(clk_id, &ts)) {
perror("can't even get the time :'-(");
}
return (int64_t)ts.tv_sec * 1000000000 + (int64_t)ts.tv_nsec;
}
#ifdef TRACY_ENABLE
#define mutexInit(m) do { \
pthread_mutex_init(&((m)->mutex), NULL); \
TracyCLockAnnounce((m)->tracyCtx); \
} while (0)
#define mutexLock(m) do { \
TracyCLockBeforeLock((m)->tracyCtx); \
pthread_mutex_lock(&((m)->mutex)); \
TracyCLockAfterLock((m)->tracyCtx); \
} while (0)
#define mutexUnlock(m) do { \
pthread_mutex_unlock(&((m)->mutex)); \
TracyCLockAfterUnlock((m)->tracyCtx); \
} while (0)
#else
#define mutexInit(m) pthread_mutex_init(m, NULL)
#define mutexLock pthread_mutex_lock
#define mutexUnlock pthread_mutex_unlock
#endif
#endif