Repository navigation
Expand file tree
/
Copy pathminicoro.c
More file actions
88 lines (73 loc) · 2.15 KB
/
Copy pathminicoro.c
File metadata and controls
88 lines (73 loc) · 2.15 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
#define MINICORO_IMPL
// Ante passes effect arguments and results through the message field instead of the storage buffer
#define MCO_DEFAULT_STORAGE_SIZE 0
#include <stdio.h>
#include <stdlib.h>
#include "minicoro.h"
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <direct.h>
#endif
mco_coro* mco_coro_init(void (*effectful_fn)(mco_coro* co), void* user_data) {
mco_coro* co;
mco_desc desc = mco_desc_init(effectful_fn, 0);
desc.user_data = user_data;
mco_result res = mco_create(&co, &desc);
assert(res == MCO_SUCCESS);
return co;
}
void* mco_coro_get_user_data(mco_coro* k) {
return mco_get_user_data(k);
}
void mco_coro_free(mco_coro* k) {
mco_result res = mco_destroy(k);
assert(res == MCO_SUCCESS);
}
bool mco_coro_is_suspended(mco_coro* k) {
return mco_status(k) == MCO_SUSPENDED;
}
void mco_coro_set_message(mco_coro* k, void* message) {
k->message = message;
}
void* mco_coro_get_message(mco_coro* k) {
return k->message;
}
void mco_coro_suspend(mco_coro* k) {
mco_result res = mco_yield(k);
assert(res == MCO_SUCCESS);
}
void mco_coro_resume(mco_coro* k) {
mco_result res = mco_resume(k);
assert(res == MCO_SUCCESS);
}
mco_coro* mco_coro_running(void) {
return mco_running();
}
// When a function returns in order to unwind, this holds the frame of the handle expression
// being exited. It is NULL otherwise.
MCO_THREAD_LOCAL void* ante_abort_target = NULL;
// For code which cannot refer to a thread-local variable
void** ante_abort_marker(void) {
return &ante_abort_target;
}
/* Used by Ante's stdlib to write to stdout and stderr portably */
void ante_write_stdout(const char* data, size_t length) {
fwrite(data, 1, length, stdout);
}
void ante_write_stderr(const char* data, size_t length) {
fwrite(data, 1, length, stderr);
}
int ante_create_dir(const char* path) {
#ifdef _WIN32
if (_mkdir(path) == 0) return 0;
#else
if (mkdir(path, 0777) == 0) return 0;
#endif
if (errno != EEXIST) return -1;
struct stat info;
return stat(path, &info) == 0 && (info.st_mode & S_IFMT) == S_IFDIR ? 0 : -1;
}