summaryrefslogtreecommitdiff
path: root/libpromise/promise.c
blob: f7e24f42237e3ea21c709343f085b66b7e982f0d (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* libpromise/promise.c - Promises for libcr
 *
 * Copyright (C) 2024  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-Licence-Identifier: AGPL-3.0-or-later
 */

#include <assert.h>  /* for assert() */
#include <stddef.h>  /* for size_t */
#include <stdbool.h>

#include <libmisc/vcall.h>

/* Header *********************************************************************/

struct promise_vtable;
struct promise_queue;
struct __promise;

typedef struct __promise {
	struct promise_vtable   *vtable;

	/* queue */
	struct promise_queue    *queue_root;
	struct __promise        *queue_front, *queue_rear;

	struct {
		cid_t                    cid;
		size_t                   cnt;
		struct __promise       **vec;
	}                        waiter;

	bool                     resolved;
} implements_promise;

struct promise_vtable {
	void                     (*on_complete)(implements_promise *);
	bool                     (*would_block)(implements_promise *);
};

struct promise_queue {
	implements_promise      *front, *rear;
};

/* consumer */
void    promise_push_to_rear(implements_promise *, struct promise_queue *);
void    promise_await(implements_promise *);
size_t  promise_select_v(size_t arg_cnt, implements_promise *arg_vec[]);
#define promise_select_l(...) ({ \
	implements_promise *args[] = { __VA_ARGS__ }; \
	promise_select_v(sizeof(args)/sizeof(args[0]), args); \
})

/* producer */
void    promise_resolve(implements_promise *);

/* Impl ***********************************************************************/

/* Linked-list operations ==========================================*/

void promise_push_to_rear(implements_promise *item, struct promise_queue *root) {
	assert(item);
	assert(root);

	item->root = root;
	item->front = root->rear;
	item->rear = NULL;
	if (root->rear)
		root->rear->rear = item;
	else
		root->front = item;
	root->rear = item;
}

static void promise_remove(implements_promise_queueitem *item) {
	assert(item);
	assert(item->root);

	struct promise_queue *root = item->root;

	if (item->front)
		item->front->rear = item->rear;
	else
		root->front = item->rear;
	if (item->rear)
		item->rear->front = item->front;
	else
		root->rear = item->front;
}

/* Resolve =========================================================*/

static inline void promise_remove_all(size_t arg_cnt, implements_promise *arg_vec[]) {
	for (size_t i = 0; i < arg_cnt; i++) {
		if (!arg_vec[i])
			continue;
		promise_remove(arg_vec[i]);
		arg_vec[i]->waiter = (typeof(arg_vec[i]->waiter)){0};
	}
}

void promise_resolve(implements_promise *arg) {
	assert(arg);
	bool enable = cr_disable_interrupts();
	assert(!arg->resolved);

	VCALL(arg, on_complete);
	arg->resolved = true;

	if (arg->waiter.cid) {
		typeof(arg->waiter) waiter = arg->waiter;
		promise_resolve_all(waiter->cnt, waiter->vec);
		cr_unpause(waiter->cid);
	}

	if (enable)
		cr_enable_interrupts();
}

/* Single-wait =====================================================*/

void promise_await(implements_promise *arg) {
	assert(arg);
	bool enable = cr_disable_interrupts();
	if (arg->resolved) {
		/* do nothing */
	} else if (!VCALL(arg, would_block)) {
		promise_resolve(arg);
	} else {
		implements_promise *vec[1] = { arg };
		arg->waiter = (typeof(arg->waiter)){
			.cid = cr_getcid();
			.cnt = 1;
			.vec = vec;
		};
		cr_pause_and_yield();
	}
	if (enable)
		cr_enable_interrupts();
	assert(arg->resolved);
}

/* Multi-wait ======================================================*/

static inline size_t pickone(size_t cnt) {
	long fair_cnt = (0x80000000L / cnt) * cnt;
	long rnd;
	do {
		rnd = random();
	} while (rnd >= fair_cnt);
	return rnd % cnt;
}

size_t promise_select_v(size_t arg_cnt, implements_promise *arg_vec[]) {
	size_t cnt_blocking = 0;
	size_t cnt_nonblock = 0;
	size_t cnt_default  = 0;

	size_t idx_default;
	bool wouldblock[arg_cnt];

	assert(arg_cnt);
	assert(arg_vec);

	cr_disable_interrupts();

	for (size_t i = 0; i < arg_cnt; i++) {
		if (arg_vec[i] == NULL) {
			cnt_default++;
			idx_default = i;
		} else if ( ((wouldblock[i] = (!arg_vec[i]->resolved) && VCALL(arg_vec[i], would_block))) )
			cnt_blocking++;
		else
			cnt_nonblock++;
	}

	if (cnt_nonblock) {
		size_t choice = pickone(cnt_nonblock);
		for (size_t i = 0, seen = 0; i < arg_cnt; i++) {
			if (arg_vec[i] && !wouldblock[i]) {
				if (seen == choice) {
					promise_resolve(arg_vec[i]);
					cr_enable_interrupts();
					return i;
				}
				seen++;
			}
		}
		__builtin_unreachable();
	}

	if (cnt_default) {
		return resolve_select(arg_cnt, arg_vec, idx_default);

	for (size_t i = 0, seen = 0; i < arg_cnt; i++) {
		arg_vec[i]->cid = cr_getcid();
		arg_vec[i]->cid = cr_getcid();
	}
	cr_pause_and_yield();
	for

		}