-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ok.c
More file actions
211 lines (188 loc) · 5.21 KB
/
Copy pathtest_ok.c
File metadata and controls
211 lines (188 loc) · 5.21 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
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
202
203
204
205
206
207
208
209
210
211
/* Luno compiled output */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <unistd.h>
#ifdef _WIN32
#include <windows.h>
typedef CRITICAL_SECTION luno_mutex_t;
typedef CONDITION_VARIABLE luno_cond_t;
typedef HANDLE luno_thread_t;
#define luno_mutex_init(m) InitializeCriticalSection(m)
#define luno_mutex_lock(m) EnterCriticalSection(m)
#define luno_mutex_unlock(m) LeaveCriticalSection(m)
#define luno_mutex_destroy(m) DeleteCriticalSection(m)
#define luno_cond_init(c) InitializeConditionVariable(c)
#define luno_cond_wait(c,m) SleepConditionVariableCS((c),(m),INFINITE)
#define luno_cond_signal(c) WakeConditionVariable(c)
#define luno_cond_broadcast(c) WakeAllConditionVariable(c)
#define luno_cond_destroy(c) ((void)0)
#define luno_thread_create(t,f,a) (*(t)=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(f),(a),0,NULL))
#define luno_thread_detach(t) CloseHandle(t)
#else
#include <pthread.h>
typedef pthread_mutex_t luno_mutex_t;
typedef pthread_cond_t luno_cond_t;
typedef pthread_t luno_thread_t;
#define luno_mutex_init(m) pthread_mutex_init(m,NULL)
#define luno_mutex_lock(m) pthread_mutex_lock(m)
#define luno_mutex_unlock(m) pthread_mutex_unlock(m)
#define luno_mutex_destroy(m) pthread_mutex_destroy(m)
#define luno_cond_init(c) pthread_cond_init(c,NULL)
#define luno_cond_wait(c,m) pthread_cond_wait((c),(m))
#define luno_cond_signal(c) pthread_cond_signal(c)
#define luno_cond_broadcast(c) pthread_cond_broadcast(c)
#define luno_cond_destroy(c) pthread_cond_destroy(c)
#define luno_thread_create(t,f,a) pthread_create((t),NULL,(f),(a))
#define luno_thread_detach(t) pthread_detach(t)
#endif
#ifdef _WIN32
#define luno_strdup(s) _strdup(s)
#else
#define luno_strdup(s) strdup(s)
#endif
typedef struct {
char* data;
int64_t len;
} LunoString;
LunoString luno_string_new(const char* s) {
LunoString r;
r.data = luno_strdup(s);
r.len = (int64_t)strlen(s);
return r;
}
void luno_string_free(LunoString* s) {
free(s->data);
s->data = NULL;
}
int64_t luno_print_string(LunoString s) {
printf("%s\n", s.data);
return 0;
}
int64_t luno_print_int(int64_t v) {
printf("%lld\n", (long long)v);
return 0;
}
int64_t luno_print_float(double v) {
printf("%g\n", v);
return 0;
}
int64_t luno_print_bool(bool v) {
printf("%s\n", v ? "true" : "false");
return 0;
}
typedef struct LunoFuture {
void* result;
int ready;
luno_mutex_t mtx;
luno_cond_t cv;
} LunoFuture;
LunoFuture* luno_future_new(void) {
LunoFuture* f = (LunoFuture*)malloc(sizeof(LunoFuture));
f->result = NULL;
f->ready = 0;
luno_mutex_init(&f->mtx);
luno_cond_init(&f->cv);
return f;
}
void luno_future_set(LunoFuture* f, void* val) {
luno_mutex_lock(&f->mtx);
f->result = val;
f->ready = 1;
luno_cond_broadcast(&f->cv);
luno_mutex_unlock(&f->mtx);
}
void* luno_future_await(LunoFuture* f) {
luno_mutex_lock(&f->mtx);
while (!f->ready) {
luno_cond_wait(&f->cv, &f->mtx);
}
luno_mutex_unlock(&f->mtx);
void* result = f->result;
luno_mutex_destroy(&f->mtx);
luno_cond_destroy(&f->cv);
free(f);
return result;
}
typedef struct {
void* (*fn)(void*);
void* args;
LunoFuture* future;
} LunoTask;
void* luno_task_run(void* arg) {
LunoTask* task = (LunoTask*)arg;
void* result = task->fn(task->args);
luno_future_set(task->future, result);
free(task);
return NULL;
}
LunoFuture* luno_spawn(void* (*fn)(void*), void* args) {
LunoFuture* fut = luno_future_new();
LunoTask* task = (LunoTask*)malloc(sizeof(LunoTask));
task->fn = fn;
task->args = args;
task->future = fut;
luno_thread_t thread;
luno_thread_create(&thread, luno_task_run, task);
luno_thread_detach(thread);
return fut;
}
typedef struct {
void** buffer;
int capacity;
int count;
int head;
int tail;
luno_mutex_t mtx;
luno_cond_t not_full;
luno_cond_t not_empty;
} LunoChan;
LunoChan* luno_chan_new(int capacity) {
LunoChan* ch = (LunoChan*)malloc(sizeof(LunoChan));
ch->buffer = (void**)malloc(sizeof(void*) * (size_t)capacity);
ch->capacity = capacity;
ch->count = 0;
ch->head = 0;
ch->tail = 0;
luno_mutex_init(&ch->mtx);
luno_cond_init(&ch->not_full);
luno_cond_init(&ch->not_empty);
return ch;
}
void luno_chan_send(LunoChan* ch, void* val) {
luno_mutex_lock(&ch->mtx);
while (ch->count >= ch->capacity) {
luno_cond_wait(&ch->not_full, &ch->mtx);
}
ch->buffer[ch->tail] = val;
ch->tail = (ch->tail + 1) % ch->capacity;
ch->count++;
luno_cond_signal(&ch->not_empty);
luno_mutex_unlock(&ch->mtx);
}
void* luno_chan_recv(LunoChan* ch) {
luno_mutex_lock(&ch->mtx);
while (ch->count <= 0) {
luno_cond_wait(&ch->not_empty, &ch->mtx);
}
void* val = ch->buffer[ch->head];
ch->head = (ch->head + 1) % ch->capacity;
ch->count--;
luno_cond_signal(&ch->not_full);
luno_mutex_unlock(&ch->mtx);
return val;
}
void _luno_main();
void _luno_main() {
int64_t x_0 = 5;
int64_t y_1 = x_0;
luno_print_string(luno_string_new(y_1));
}
int main(int argc, char** argv) {
(void)argc; (void)argv;
_luno_main();
return 0;
}