forked from pmem/miniasync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.c
120 lines (103 loc) · 2.63 KB
/
runtime.c
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
114
115
116
117
118
119
120
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2021-2022, Intel Corporation */
#include <stdlib.h>
#include "libminiasync/runtime.h"
#include "core/os_thread.h"
#include "core/os.h"
#include "core/util.h"
struct runtime_waker_data {
os_cond_t *cond;
os_mutex_t *lock;
};
static void
runtime_waker_wake(void *fdata)
{
struct runtime_waker_data *data = fdata;
os_mutex_lock(data->lock);
os_cond_signal(data->cond);
os_mutex_unlock(data->lock);
}
struct runtime {
os_cond_t cond;
os_mutex_t lock;
uint64_t spins_before_sleep;
struct timespec cond_wait_time;
};
struct runtime *
runtime_new(void)
{
struct runtime *runtime = malloc(sizeof(struct runtime));
if (runtime == NULL)
return NULL;
os_cond_init(&runtime->cond);
os_mutex_init(&runtime->lock);
runtime->spins_before_sleep = 1000;
runtime->cond_wait_time = (struct timespec){0, 1000000};
return runtime;
}
void
runtime_delete(struct runtime *runtime)
{
free(runtime);
}
static void
runtime_sleep(struct runtime *runtime)
{
os_mutex_lock(&runtime->lock);
struct timespec ts;
os_clock_gettime(CLOCK_REALTIME, &ts);
static const size_t nsec_in_sec = 1000000000ULL;
ts.tv_nsec += runtime->cond_wait_time.tv_nsec;
uint64_t secs = (uint64_t)ts.tv_nsec / nsec_in_sec;
ts.tv_nsec -= (long)(secs * nsec_in_sec);
ts.tv_sec += (long)(runtime->cond_wait_time.tv_sec + (long)secs);
os_cond_timedwait(&runtime->cond, &runtime->lock, &ts);
os_mutex_unlock(&runtime->lock);
}
void
runtime_wait_multiple(struct runtime *runtime, struct future *futs[],
size_t nfuts)
{
struct runtime_waker_data waker_data;
waker_data.cond = &runtime->cond;
waker_data.lock = &runtime->lock;
struct future_notifier notifier;
notifier.waker = (struct future_waker){&waker_data, runtime_waker_wake};
notifier.poller.ptr_to_monitor = NULL;
size_t ndone = 0;
for (;;) {
for (uint64_t i = 0; i < runtime->spins_before_sleep; ++i) {
for (uint64_t f = 0; f < nfuts; ++f) {
struct future *fut = futs[f];
if (fut->context.state == FUTURE_STATE_COMPLETE)
continue;
if (future_poll(fut, ¬ifier) ==
FUTURE_STATE_COMPLETE) {
ndone++;
}
switch (notifier.notifier_used) {
case FUTURE_NOTIFIER_POLLER:
/*
* TODO: if this is the only future
* being polled, use umwait/umonitor
* for power-optimized polling.
*/
break;
case FUTURE_NOTIFIER_WAKER:
case FUTURE_NOTIFIER_NONE:
/* nothing to do for wakers or none */
break;
};
}
if (ndone == nfuts)
return;
WAIT();
}
runtime_sleep(runtime);
}
}
void
runtime_wait(struct runtime *runtime, struct future *fut)
{
runtime_wait_multiple(runtime, &fut, 1);
}