-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjo_clojure_async.h
1252 lines (1129 loc) · 42.6 KB
/
jo_clojure_async.h
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
// simple wrapper so we can blop it into the t_object generic container
struct jo_clojure_agent_t : jo_object {
node_idx_t state = K_DEFAULT_NODE;
node_idx_t exception = NIL_NODE;
node_idx_t validate = NIL_NODE;
node_idx_t error_handler = NIL_NODE;
node_idx_t error_mode = NIL_NODE;
node_idx_t msg_queue = new_node_atom(EMPTY_QUEUE_NODE);
};
typedef jo_alloc_t<jo_clojure_agent_t> jo_clojure_agent_alloc_t;
jo_clojure_agent_alloc_t jo_clojure_agent_alloc;
typedef jo_shared_ptr_t<jo_clojure_agent_t> jo_clojure_agent_ptr_t;
template<typename...A>
jo_clojure_agent_ptr_t new_agent(A...args) { return jo_clojure_agent_ptr_t(jo_clojure_agent_alloc.emplace(args...)); }
static node_idx_t new_node_agent(jo_clojure_agent_ptr_t agent, int flags=0) { return new_node_object(NODE_AGENT, agent.cast<jo_object>(), flags); }
typedef std::function<node_idx_t()> jo_task_t;
typedef jo_shared_ptr<jo_task_t> jo_task_ptr_t;
class jo_threadpool {
jo_vector<std::thread> pool;
typedef std::function<void()> task_t;
jo_mpmcq<task_t*, nullptr, 4096> tasks;
public:
jo_threadpool(int nr = 1) : pool(), tasks() {
while(nr-- > 0) {
std::thread t([this]() {
tmProfileThread(0,0,0);
while(true) {
task_t *task = tasks.pop();
if(task == nullptr) {
break;
}
(*task)();
delete task;
}
});
pool.emplace_back(std::move(t));
}
}
~jo_threadpool() {
tasks.close();
for(std::thread &t : pool) {
t.join();
}
pool.clear();
}
void add_task(jo_task_ptr_t pt) {
tasks.push(new task_t([pt]{(*pt.ptr)();}));
}
};
#define USE_THREADPOOL 1
static jo_semaphore thread_limiter(processor_count);
static jo_threadpool *thread_pool = new jo_threadpool(processor_count);
static jo_threadpool *thread_pool2 = new jo_threadpool(processor_count);
static node_idx_t node_swap(env_ptr_t env, node_idx_t atom_idx, node_idx_t f_idx, list_ptr_t args
, node_idx_t v_idx = NIL_NODE // validator
)
{
node_t *atom = get_node(atom_idx);
node_idx_t old_val, new_val;
if(env->tx.ptr) {
old_val = env->tx->read(atom_idx);
new_val = eval_list(env, args->push_front(f_idx, old_val));
if(v_idx != NIL_NODE) {
node_idx_t valid = eval_list(env, list_va(v_idx, new_val));
if(valid == FALSE_NODE) {
return new_node_exception("swap: validation failed");
} else if(get_node_type(valid) == NODE_EXCEPTION) {
return valid;
}
}
env->tx->write(atom_idx, new_val);
return new_val;
}
int count = 0;
do {
old_val = atom->t_atom.load();
while(old_val <= TX_HOLD_NODE) {
jo_yield_backoff(&count);
old_val = atom->t_atom.load();
}
new_val = eval_list(env, args->push_front(f_idx, old_val));
if(v_idx != NIL_NODE) {
node_idx_t valid = eval_va(env, v_idx, new_val);
if(valid == FALSE_NODE) {
return new_node_exception("swap: validation failed");
} else if(get_node_type(valid) == NODE_EXCEPTION) {
return valid;
}
}
if(atom->t_atom.compare_exchange_weak(old_val, new_val)) {
break;
}
jo_yield_backoff(&count);
atom_retries++;
} while(true);
return new_val;
}
static node_idx_t node_reset(env_ptr_t env, node_idx_t atom_idx, node_idx_t new_val, node_idx_t v_idx = NIL_NODE) {
if(v_idx != NIL_NODE) {
node_idx_t valid = eval_va(env, v_idx, new_val);
if(valid == FALSE_NODE) {
return new_node_exception("swap: validation failed");
} else if(get_node_type(valid) == NODE_EXCEPTION) {
return valid;
}
}
node_t *atom = get_node(atom_idx);
if(env->tx.ptr) {
env->tx->write(atom_idx, new_val);
} else {
node_idx_t old_val;
do {
old_val = atom->t_atom.load();
int count = 0;
while(old_val <= TX_HOLD_NODE) {
jo_yield_backoff(&count);
old_val = atom->t_atom.load();
}
} while(!atom->t_atom.compare_exchange_weak(old_val, new_val));
}
return new_val;
}
static bool node_compare_and_set(env_ptr_t env, node_idx_t atom_idx, node_idx_t old_val, node_idx_t new_val) {
node_t *atom = get_node(atom_idx);
if(env->tx.ptr) {
if(env->tx->read(atom_idx) == old_val) {
env->tx->write(atom_idx, new_val);
return true;
}
return false;
}
return atom->t_atom.compare_exchange_weak(old_val, new_val);
}
static node_idx_t node_deref(env_ptr_t env, node_idx_t atom_idx) {
node_t *atom = get_node(atom_idx);
if(env->tx.ptr) {
return env->tx->read(atom_idx);
}
node_idx_t ret = atom->t_atom.load();
int count = 0;
while(ret < 0) {
jo_yield_backoff(&count);
ret = atom->t_atom;
}
return ret;
}
static node_idx_t node_try_deref(env_ptr_t env, node_idx_t atom_idx) {
node_t *atom = get_node(atom_idx);
if(env->tx.ptr) {
return env->tx->read(atom_idx);
}
return atom->t_atom.load();
}
static node_idx_t native_thread_workers(env_ptr_t env, list_ptr_t args) {
delete thread_pool;
delete thread_pool2;
thread_pool = new jo_threadpool(get_node_int(args->first_value()));
thread_pool2 = new jo_threadpool(get_node_int(args->first_value()));
return NIL_NODE;
}
// (atom x)(atom x & options)
// Creates and returns an Atom with an initial value of x and zero or
// more options (in any order):
// :meta metadata-map
// :validator validate-fn
// If metadata-map is supplied, it will become the metadata on the
// atom. validate-fn must be nil or a side-effect-free fn of one
// argument, which will be passed the intended new state on any state
// change. If the new state is unacceptable, the validate-fn should
// return false or throw an exception.
static node_idx_t native_atom(env_ptr_t env, list_ptr_t args) {
if(args->size() < 1) {
warnf("(atom) requires at least 1 argument\n");
return NIL_NODE;
}
node_idx_t x_idx = args->first_value();
list_ptr_t options = args->rest();
node_idx_t meta_idx = NIL_NODE;
node_idx_t validator_idx = NIL_NODE;
for(list_t::iterator it(options); it; it++) {
if(get_node_type(*it) != NODE_KEYWORD) {
warnf("(atom) options must be keywords\n");
return NIL_NODE;
}
if(get_node_string(*it) == "meta") {
meta_idx = *++it;
} else if(get_node_string(*it) == "validator") {
validator_idx = *++it;
} else {
warnf("(atom) unknown option: %s\n", get_node_string(*it).c_str());
return NIL_NODE;
}
}
return new_node_atom(x_idx);//, meta_idx, validator_idx);
}
// (deref ref)(deref ref timeout-ms timeout-val)
// Also reader macro: @ref/@agent/@var/@atom/@delay/@future/@promise. Within a transaction,
// returns the in-transaction-value of ref, else returns the
// most-recently-committed value of ref. When applied to a var, agent
// or atom, returns its current state. When applied to a delay, forces
// it if not already forced. When applied to a future, will block if
// computation not complete. When applied to a promise, will block
// until a value is delivered. The variant taking a timeout can be
// used for blocking references (futures and promises), and will return
// timeout-val if the timeout (in milliseconds) is reached before a
// value is available. See also - realized?.
static node_idx_t native_deref(env_ptr_t env, list_ptr_t args) {
if(args->size() < 1) {
warnf("(deref) requires at least 1 argument\n");
return NIL_NODE;
}
node_idx_t ref_idx = eval_node(env, args->first_value());
node_idx_t timeout_ms_idx = eval_node(env, args->second_value());
node_idx_t timeout_val_idx = eval_node(env, args->third_value());
node_t *ref = get_node(ref_idx);
int type = ref->type;
if(type == NODE_VAR) {
} else if(type == NODE_ATOM || type == NODE_AGENT) {
if(env->tx.ptr) {
return env->tx->read(ref_idx);
} else {
node_idx_t ret = ref->t_atom.load();
int count = 0;
while(ret <= TX_HOLD_NODE) {
jo_yield_backoff(&count);
ret = ref->t_atom.load();
}
return ret;
}
} else if(type == NODE_DELAY) {
return eval_node(env, ref_idx);
} else if(type == NODE_FUTURE || type == NODE_PROMISE) {
node_idx_t ret = ref->t_atom.load();
if(timeout_ms_idx != NIL_NODE) {
double A = jo_time();
long long timeout_ms = get_node_int(timeout_ms_idx);
int count = 0;
while(ret < 0) {
if(jo_time() - A >= timeout_ms * 0.001) {
return timeout_val_idx;
}
jo_yield_backoff(&count);
ret = ref->t_atom;
}
} else {
int count = 0;
while(ret < 0) {
jo_yield_backoff(&count);
ret = ref->t_atom;
}
}
return ret;
}
return NODE_NIL;
}
// (swap! atom f)(swap! atom f x)(swap! atom f x y)(swap! atom f x y & args)
// Atomically swaps the value of atom to be:
// (apply f current-value-of-atom args). Note that f may be called
// multiple times, and thus should be free of side effects. Returns
// the value that was swapped in.
static node_idx_t native_swap_e(env_ptr_t env, list_ptr_t args) {
return node_swap(env, args->first_value(), args->second_value(), args->drop(2));
}
// (reset! atom newval)
// Sets the value of atom to newval without regard for the
// current value. Returns newval.
static node_idx_t native_reset_e(env_ptr_t env, list_ptr_t args) {
return node_reset(env, args->first_value(), args->second_value());
}
// (compare-and-set! atom oldval newval)
// Atomically sets the value of atom to newval if and only if the
// current value of the atom is identical to oldval. Returns true if
// set happened, else false
static node_idx_t native_compare_and_set_e(env_ptr_t env, list_ptr_t args) {
return node_compare_and_set(env, args->first_value(), args->second_value(), args->third_value()) ? TRUE_NODE : FALSE_NODE;
}
// (swap-vals! atom f)(swap-vals! atom f x)(swap-vals! atom f x y)(swap-vals! atom f x y & args)
// Atomically swaps the value of atom to be:
// (apply f current-value-of-atom args). Note that f may be called
// multiple times, and thus should be free of side effects.
// Returns [old new], the value of the atom before and after the swap.
static node_idx_t native_swap_vals_e(env_ptr_t env, list_ptr_t args) {
if(args->size() < 2) {
warnf("(swap-vals!) requires at least 2 arguments\n");
return NIL_NODE;
}
list_t::iterator it(args);
node_idx_t atom_idx = *it++;
node_idx_t f_idx = *it++;
list_ptr_t args2 = args->rest(it);
node_t *atom = get_node(atom_idx);
if(atom->type != NODE_ATOM) {
warnf("(swap-vals!) requires an atom\n");
return NIL_NODE;
}
node_t *f = get_node(f_idx);
if(!f->is_func() && !f->is_native_func()) {
warnf("(swap-vals!) requires a function\n");
return NIL_NODE;
}
node_idx_t old_val, new_val;
if(env->tx.ptr) {
old_val = env->tx->read(atom_idx);
new_val = eval_list(env, args2->push_front(f_idx, old_val));
env->tx->write(atom_idx, new_val);
vector_ptr_t ret = new_vector();
ret->push_back_inplace(old_val);
ret->push_back_inplace(new_val);
return new_node_vector(ret);
}
do {
old_val = atom->t_atom.load();
int count = 0;
while(old_val <= TX_HOLD_NODE) {
jo_yield_backoff(&count);
old_val = atom->t_atom.load();
}
new_val = eval_list(env, args2->push_front(f_idx, old_val));
} while(!atom->t_atom.compare_exchange_weak(old_val, new_val));
vector_ptr_t ret = new_vector();
ret->push_back_inplace(old_val);
ret->push_back_inplace(new_val);
return new_node_vector(ret);
}
// (reset-vals! atom newval)
// Sets the value of atom to newval. Returns [old new], the value of the
// atom before and after the reset.
static node_idx_t native_reset_vals_e(env_ptr_t env, list_ptr_t args) {
if(args->size() < 2) {
warnf("(reset-vals!) requires at least 2 arguments\n");
return NIL_NODE;
}
node_idx_t atom_idx = args->first_value();
node_t *atom = get_node(atom_idx);
if(atom->type != NODE_ATOM) {
warnf("(reset-vals!) requires an atom\n");
return NIL_NODE;
}
node_idx_t old_val, new_val = args->second_value();
if(env->tx.ptr) {
old_val = env->tx->read(atom_idx);
env->tx->write(atom_idx, new_val);
return new_node_vector(vector_va(old_val, new_val));
}
do {
old_val = atom->t_atom.load();
int count = 0;
while(old_val <= TX_HOLD_NODE) {
jo_yield_backoff(&count);
old_val = atom->t_atom.load();
}
} while(!atom->t_atom.compare_exchange_weak(old_val, new_val));
return new_node_vector(vector_va(old_val, new_val));
}
// (multi-swap! (atom f args) (atom f args) (atom f args) ...)
// Atomically swaps the values of atoms to be:
// (apply f current-value-of-atom args). Note that f may be called
// multiple times, and thus should be free of side effects. Returns
// the values that were swapped in.
// atoms are updated in the order they are specified.
static node_idx_t native_multi_swap_e(env_ptr_t env, list_ptr_t args) {
jo_vector<list_ptr_t> lists;
jo_vector<node_idx_t> old_vals(args->size());
jo_vector<node_idx_t> new_vals(args->size());
lists.reserve(args->size());
// Gather lists
for(list_t::iterator it(args); it; it++) {
list_ptr_t list = get_node_list(*it);
if(list->size() < 2) {
warnf("(multi-swap!) requires at least 2 arguments\n");
return NIL_NODE;
}
node_idx_t atom = list->first_value();
node_idx_t f = list->second_value();
if(get_node_type(atom) != NODE_ATOM) {
warnf("(multi-swap!) requires an atom\n");
return NIL_NODE;
}
if(!get_node(f)->is_func() && !get_node(f)->is_native_func()) {
warnf("(multi-swap!) requires a function\n");
return NIL_NODE;
}
lists.push_back(list);
}
env_ptr_t env2 = new_env(env);
env2->begin_transaction();
int count = 0;
do {
// First get the old values and calc new values
for(size_t i = 0; i < lists.size(); i++) {
list_ptr_t list = lists[i];
list_t::iterator it2(list);
node_idx_t atom_idx = *it2++;
node_idx_t f_idx = *it2++;
list_ptr_t args2 = list->rest(it2);
node_idx_t old_val = env2->tx->read(atom_idx);
node_idx_t new_val = eval_list(env2, args2->push_front(f_idx, old_val));
old_vals[i] = old_val;
new_vals[i] = new_val;
env2->tx->write(atom_idx, new_val);
}
if(env2->end_transaction()) {
break;
}
for(size_t i = 0; i < lists.size(); i++) {
old_vals[i] = NIL_NODE;
new_vals[i] = NIL_NODE;
}
jo_yield_backoff(&count);
} while(true);
// return a list of new values
list_ptr_t ret = new_list();
for(size_t i = 0; i < lists.size(); i++) {
ret->push_back_inplace(new_vals[i]);
}
return new_node_list(ret);
}
// (multi-reset! (atom new-val) (atom new-val) (atom new-val) ...)
// Atomically resets the values of atoms to be new-vals. Returns the
// values that were reset.
// atoms are updated in the order they are specified.
static node_idx_t native_multi_reset_e(env_ptr_t env, list_ptr_t args) {
jo_vector<list_ptr_t> lists;
jo_vector<node_idx_t> old_vals(args->size());
jo_vector<node_idx_t> new_vals(args->size());
lists.reserve(args->size());
// Gather lists
for(list_t::iterator it(args); it; it++) {
list_ptr_t list = get_node_list(*it);
if(list->size() < 2) {
warnf("(multi-reset!) requires at least 2 arguments\n");
return NIL_NODE;
}
node_idx_t atom = list->first_value();
if(get_node_type(atom) != NODE_ATOM) {
warnf("(multi-reset!) requires an atom\n");
return NIL_NODE;
}
lists.push_back(list);
}
env_ptr_t env2 = new_env(env);
env2->begin_transaction();
int count = 0;
do {
// First get the old values and calc new values
for(size_t i = 0; i < lists.size(); i++) {
list_ptr_t list = lists[i];
list_t::iterator it2(list);
node_idx_t atom_idx = *it2++;
node_idx_t new_val = *it2++;
env2->tx->write(atom_idx, new_val);
new_vals[i] = new_val;
}
if(env2->end_transaction()) {
break;
}
for(size_t i = 0; i < lists.size(); i++) {
new_vals[i] = NIL_NODE;
}
jo_yield_backoff(&count);
} while(true);
// return a list of new values
list_ptr_t ret = new_list();
for(size_t i = 0; i < lists.size(); i++) {
ret->push_back_inplace(new_vals[i]);
}
return new_node_list(ret);
}
// (multi-swap-vals! (atom f args) (atom f args) (atom f args) ...)
static node_idx_t native_multi_swap_vals_e(env_ptr_t env, list_ptr_t args) {
jo_vector<list_ptr_t> lists;
jo_vector<node_idx_t> old_vals(args->size());
jo_vector<node_idx_t> new_vals(args->size());
lists.reserve(args->size());
// Gather lists
for(list_t::iterator it(args); it; it++) {
list_ptr_t list = get_node_list(*it);
if(list->size() < 2) {
warnf("(multi-swap!) requires at least 2 arguments\n");
return NIL_NODE;
}
node_idx_t atom = list->first_value();
node_idx_t f = list->second_value();
if(get_node_type(atom) != NODE_ATOM) {
warnf("(multi-swap!) requires an atom\n");
return NIL_NODE;
}
if(!get_node(f)->is_func() && !get_node(f)->is_native_func()) {
warnf("(multi-swap!) requires a function\n");
return NIL_NODE;
}
lists.push_back(list);
}
env_ptr_t env2 = new_env(env);
env2->begin_transaction();
int count = 0;
do {
// First get the old values and calc new values
for(size_t i = 0; i < lists.size(); i++) {
list_ptr_t list = lists[i];
list_t::iterator it2(list);
node_idx_t atom_idx = *it2++;
node_idx_t f_idx = *it2++;
list_ptr_t args2 = list->rest(it2);
node_idx_t old_val = env2->tx->read(atom_idx);
node_idx_t new_val = eval_list(env2, args2->push_front(f_idx, old_val));
old_vals[i] = old_val;
new_vals[i] = new_val;
env2->tx->write(atom_idx, new_val);
}
if(env2->end_transaction()) {
break;
}
for(size_t i = 0; i < lists.size(); i++) {
old_vals[i] = NIL_NODE;
new_vals[i] = NIL_NODE;
}
jo_yield_backoff(&count);
} while(true);
// return a list of new values
list_ptr_t ret = new_list();
for(size_t i = 0; i < lists.size(); i++) {
vector_ptr_t ret2 = new_vector();
ret2->push_back_inplace(old_vals[i]);
ret2->push_back_inplace(new_vals[i]);
ret->push_back_inplace(new_node_vector(ret2));
}
return new_node_list(ret);
}
// (multi-reset-vals! (atom new-val) (atom new-val) (atom new-val) ...)
// Atomically resets the values of atoms to be new-vals. Returns the
// values that were reset.
// atoms are updated in the order they are specified.
static node_idx_t native_multi_reset_vals_e(env_ptr_t env, list_ptr_t args) {
jo_vector<list_ptr_t> lists;
jo_vector<node_idx_t> old_vals(args->size());
jo_vector<node_idx_t> new_vals(args->size());
lists.reserve(args->size());
// Gather lists
for(list_t::iterator it(args); it; it++) {
list_ptr_t list = get_node_list(*it);
if(list->size() < 2) {
warnf("(multi-reset!) requires at least 2 arguments\n");
return NIL_NODE;
}
node_idx_t atom = list->first_value();
if(get_node_type(atom) != NODE_ATOM) {
warnf("(multi-swap!) requires an atom\n");
return NIL_NODE;
}
lists.push_back(list);
}
env_ptr_t env2 = new_env(env);
env2->begin_transaction();
int count = 0;
do {
// First get the old values and calc new values
for(size_t i = 0; i < lists.size(); i++) {
list_ptr_t list = lists[i];
list_t::iterator it2(list);
node_idx_t atom_idx = *it2++;
node_idx_t new_val = *it2++;
node_idx_t old_val = env2->tx->read(atom_idx);
env2->tx->write(atom_idx, new_val);
old_vals[i] = old_val;
new_vals[i] = new_val;
}
if(env2->end_transaction()) {
break;
}
for(size_t i = 0; i < lists.size(); i++) {
old_vals[i] = NIL_NODE;
new_vals[i] = NIL_NODE;
}
jo_yield_backoff(&count);
} while(true);
// return a list of new values
list_ptr_t ret = new_list();
for(size_t i = 0; i < lists.size(); i++) {
vector_ptr_t ret2 = new_vector();
ret2->push_back_inplace(old_vals[i]);
ret2->push_back_inplace(new_vals[i]);
ret->push_back_inplace(new_node_vector(ret2));
}
return new_node_list(ret);
}
// (dosync & exprs)
// Runs the exprs (in an implicit do) in a transaction that encompasses
// exprs and any nested calls. Starts a transaction if none is already
// running on this thread. Any uncaught exception will abort the
// transaction and flow out of dosync. The exprs may be run more than
// once, but any effects on Atoms will be atomic.
static node_idx_t native_dosync(env_ptr_t env, list_ptr_t args) {
env_ptr_t env2 = new_env(env);
env2->begin_transaction();
node_idx_t ret;
int count = 0;
do {
ret = eval_node_list(env2, args);
if(env2->end_transaction()) {
break;
}
ret = NIL_NODE;
jo_yield_backoff(&count);
} while(true);
return ret;
}
// (future & body)
// Takes a body of expressions and yields a future object that will
// invoke the body in another thread, and will cache the result and
// return it on all subsequent calls to deref/@. If the computation has
// not yet finished, calls to deref/@ will block, unless the variant of
// deref with timeout is used. See also - realized?.
static node_idx_t native_future(env_ptr_t env, list_ptr_t args) {
if(args->size() < 1) {
warnf("(future) requires at least 1 argument\n");
return NIL_NODE;
}
node_idx_t f_idx = new_node(NODE_FUTURE, 0);
node_reset(env, f_idx, INV_NODE);
jo_task_ptr_t task = new jo_task_t([env,args,f_idx]() -> node_idx_t {
node_reset(env, f_idx, eval_node_list(env, args));
return NIL_NODE;
});
thread_pool->add_task(task);
return f_idx;
}
// (auto-future & body)
// like future, but deref is automatic
static node_idx_t native_auto_future(env_ptr_t env, list_ptr_t args) {
if(args->size() < 1) {
warnf("(auto-future) requires at least 1 argument\n");
return NIL_NODE;
}
node_idx_t f_idx = new_node(NODE_FUTURE, NODE_FLAG_AUTO_DEREF);
node_reset(env, f_idx, INV_NODE);
jo_task_ptr_t task = new jo_task_t([env,args,f_idx]() -> node_idx_t {
node_reset(env, f_idx, eval_node_list(env, args));
return NIL_NODE;
});
thread_pool->add_task(task);
return f_idx;
}
// (future-call f)
// Takes a function of no args and yields a future object that will
// invoke the function in another thread, and will cache the result and
// return it on all subsequent calls to deref/@. If the computation has
// not yet finished, calls to deref/@ will block, unless the variant
// of deref with timeout is used. See also - realized?.
static node_idx_t native_future_call(env_ptr_t env, list_ptr_t args) {
if(args->size() < 1) {
warnf("(future-call) requires at least 1 argument\n");
return NIL_NODE;
}
node_idx_t f_idx = new_node(NODE_FUTURE, 0);
node_reset(env, f_idx, INV_NODE);
jo_task_ptr_t task = new jo_task_t([env,args,f_idx]() -> node_idx_t {
node_reset(env, f_idx, eval_list(env, args));
return NIL_NODE;
});
thread_pool->add_task(task);
return f_idx;
}
// (auto-future-call f)
// like future-call, but deref is automatic
static node_idx_t native_auto_future_call(env_ptr_t env, list_ptr_t args) {
if(args->size() < 1) {
warnf("(auto-future-call) requires at least 1 argument\n");
return NIL_NODE;
}
node_idx_t f_idx = new_node(NODE_FUTURE, NODE_FLAG_AUTO_DEREF);
node_reset(env, f_idx, INV_NODE);
jo_task_ptr_t task = new jo_task_t([env,args,f_idx]() -> node_idx_t {
node_reset(env, f_idx, eval_list(env, args));
return NIL_NODE;
});
thread_pool->add_task(task);
return f_idx;
}
// (future-cancel f)
// Cancels the future, if possible.
static node_idx_t native_future_cancel(env_ptr_t env, list_ptr_t args) {
if(args->size() < 1) {
warnf("(future-cancel) requires at least 1 argument\n");
return NIL_NODE;
}
node_idx_t f_idx = args->first_value();
node_t *f = get_node(f_idx);
if(f->type != NODE_FUTURE) {
warnf("(future-cancel) requires a future\n");
return NIL_NODE;
}
// TODO? Currently this is a no-op.
return NIL_NODE;
}
// (future-cancelled? f)
// Returns true if future f is cancelled
static node_idx_t native_future_cancelled(env_ptr_t env, list_ptr_t args) {
if(args->size() < 1) {
warnf("(future-cancelled?) requires at least 1 argument\n");
return NIL_NODE;
}
node_idx_t f_idx = args->first_value();
node_t *f = get_node(f_idx);
if(f->type != NODE_FUTURE) {
warnf("(future-cancelled?) requires a future\n");
return NIL_NODE;
}
// TODO? Currently this is a no-op.
return FALSE_NODE;
}
// (future-done? f)
// Returns true if future f is done
static node_idx_t native_future_done(env_ptr_t env, list_ptr_t args) {
return node_try_deref(env, args->first_value()) >= 0 ? TRUE_NODE : FALSE_NODE;
}
static node_idx_t native_is_future(env_ptr_t env, list_ptr_t args) { return get_node_type(args->first_value()) == NODE_FUTURE ? TRUE_NODE : FALSE_NODE; }
static node_idx_t native_thread_sleep(env_ptr_t env, list_ptr_t args) {
double ms = get_node_float(args->first_value());
jo_sleep(ms / 1000.0);
return NIL_NODE;
}
// (io! & body)
// If an io! block occurs in a transaction, throws an
// IllegalStateException, else runs body in an implicit do. If the
// first expression in body is a literal string, will use that as the
// exception message.
static node_idx_t native_io(env_ptr_t env, list_ptr_t args) {
if(env->tx) {
warnf("(io!) cannot be used in a transaction\n");
return NIL_NODE;
}
return native_do(env, args);
}
// memoize
// Returns a memoized version of a referentially transparent function. The
// memoized version of the function keeps a cache of the mapping from arguments
// to results and, when calls with the same arguments are repeated often, has
// higher performance at the expense of higher memory use.
static node_idx_t native_memoize(env_ptr_t env, list_ptr_t args) {
node_idx_t f = args->first_value();
hash_map_ptr_t cache_map = new_hash_map();
node_idx_t cache_map_idx = new_node_hash_map(cache_map);
node_idx_t cache_idx = new_node_atom(cache_map_idx);
node_idx_t func_idx = new_node(NODE_NATIVE_FUNC, 0);
node_t *func = get_node(func_idx);
func->t_native_function = new native_func_t([f,cache_idx](env_ptr_t env, list_ptr_t args) -> node_idx_t {
node_idx_t args_idx = new_node_list(args);
node_idx_t C_idx = native_deref(env, list_va(cache_idx));
node_t *C = get_node(C_idx);
hash_map_ptr_t mem = C->as_hash_map();
if(mem->contains(args_idx, node_eq)) {
return mem->get(args_idx, node_eq);
}
node_idx_t ret = eval_list(env, args->push_front(f));
native_swap_e(env, list_va(cache_idx, env->get("assoc"), args_idx, ret));
return ret;
});
return func_idx;
}
// (pmap f coll)(pmap f coll & colls)
// Like map, except f is applied in parallel. Semi-lazy in that the
// parallel computation stays ahead of the consumption, but doesn't
// realize the entire result unless required. Only useful for
// computationally intensive functions where the time of f dominates
// the coordination overhead.
static node_idx_t native_pmap(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t f = *it++;
list_ptr_t ret = new_list();
ret->push_back_inplace(env->get("pmap-next"));
ret->push_back_inplace(f);
while(it) {
ret->push_back_inplace(eval_node(env, *it++));
}
return new_node_lazy_list(env, new_node_list(ret));
}
static node_idx_t native_pmap_next(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t f = *it++;
// pull off the first element of each list and call f with it
list_ptr_t next_list = new_list();
list_ptr_t arg_list = new_list();
arg_list->push_back_inplace(f);
next_list->push_back_inplace(env->get("pmap-next"));
next_list->push_back_inplace(f);
for(; it; it++) {
node_idx_t arg_idx = *it;
node_t *arg = get_node(arg_idx);
auto fr = arg->seq_first_rest();
if(!fr.third) return NIL_NODE;
arg_list->push_back_inplace(fr.first);
next_list->push_back_inplace(fr.second);
}
// call f with the args
node_idx_t ret = native_auto_future(env, list_va(new_node_list(arg_list)));
next_list->cons_inplace(ret);
return new_node_list(next_list);
}
// (pcalls & fns)
// Executes the no-arg fns in parallel, returning a lazy sequence of
// their values
static node_idx_t native_pcalls(env_ptr_t env, list_ptr_t args) {
return new_node_lazy_list(env, new_node_list(args->push_front(env->get("pcalls-next"))));
}
static node_idx_t native_pcalls_next(env_ptr_t env, list_ptr_t args) {
if(!args->size()) {
return NIL_NODE;
}
node_idx_t ret = native_auto_future_call(env, list_va(args->first_value()));
return new_node_list(args->rest()->push_front(ret, env->get("pcalls-next")));
}
// (pvalues & fns)
// Executes the no-arg fns in parallel, returning a lazy sequence of
// their values
static node_idx_t native_pvalues(env_ptr_t env, list_ptr_t args) {
return new_node_lazy_list(env, new_node_list(args->push_front(env->get("pvalues-next"))));
}
static node_idx_t native_pvalues_next(env_ptr_t env, list_ptr_t args) {
if(!args->size()) {
return NIL_NODE;
}
node_idx_t ret = native_auto_future(env, list_va(args->first_value()));
return new_node_list(args->rest()->push_front(ret, env->get("pvalues-next")));
}
// TODO: Implement promise with atoms so its STM compliant!
// (promise)
// Returns a promise object that can be read with deref/@, and set,
// once only, with deliver. Calls to deref/@ prior to delivery will
// block, unless the variant of deref with timeout is used. All
// subsequent derefs will return the same delivered value without
// blocking. See also - realized?.
static node_idx_t native_promise(env_ptr_t env, list_ptr_t args) {
node_idx_t ret_idx = new_node(NODE_PROMISE, 0);
node_reset(env, ret_idx, INV_NODE);
return ret_idx;
}
// (deliver promise val)
// Delivers the supplied value to the promise, releasing any pending
// derefs. A subsequent call to deliver on a promise will have no effect.
static node_idx_t native_deliver(env_ptr_t env, list_ptr_t args) {
node_compare_and_set(env, args->first_value(), INV_NODE, args->second_value());
return NIL_NODE;
}
// (locking x & body)
// Executes exprs in an implicit do, while holding the monitor of x.
// Will release the monitor of x in all circumstances.
static node_idx_t native_locking(env_ptr_t env, list_ptr_t args) {
node_idx_t x_idx = args->first_value();
node_t *atom = get_node(x_idx);
node_idx_t old_val;
size_t current_thread_id = thread_id;
// mutexes in transactions (does this even make sense?)
if(env->tx.ptr) {
// TODO
warnf("locking in transactions! Danger Will Robinson! TODO abort transaction if fail");
}
// lock the atom
int count = 0;
do {
old_val = atom->t_atom.load();
while(old_val <= TX_HOLD_NODE) {
// re-entrant support
if(atom->t_thread_id == current_thread_id) {
break;
}
jo_yield_backoff(&count);
old_val = atom->t_atom.load();
}
if(atom->t_atom.compare_exchange_weak(old_val, TX_HOLD_NODE)) {
break;
}
jo_yield_backoff(&count);
atom_retries++;
} while(true);
// re-entrant support
atom->t_thread_id = current_thread_id;
// do whatever it is that its locked for...
node_idx_t ret = eval_node_list(env, args->rest());
// unlock the atom
if(old_val > TX_HOLD_NODE) {
atom->t_atom.store(old_val);
}
return ret;
}
static node_idx_t native_atom_retries(env_ptr_t env, list_ptr_t args) {
return new_node_int(atom_retries);
}
static node_idx_t native_atom_retries_reset(env_ptr_t env, list_ptr_t args) {
atom_retries = 0;
return NIL_NODE;
}
static node_idx_t native_stm_retries(env_ptr_t env, list_ptr_t args) {
return new_node_int(stm_retries);
}
static node_idx_t native_stm_retries_reset(env_ptr_t env, list_ptr_t args) {
stm_retries = 0;
return NIL_NODE;
}