-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathjlapi.c
1088 lines (1008 loc) · 31.9 KB
/
jlapi.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
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
// This file is a part of Julia. License is MIT: https://julialang.org/license
/*
jlapi.c
miscellaneous functions for users of libjulia.so, to handle initialization
and the style of use where julia is not in control most of the time.
*/
#include "platform.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "julia.h"
#include "options.h"
#include "julia_assert.h"
#include "julia_internal.h"
#ifdef USE_TRACY
#include "tracy/TracyC.h"
#endif
#ifdef __cplusplus
#include <cfenv>
extern "C" {
#else
#include <fenv.h>
#endif
/**
* @brief Check if Julia is already initialized.
*
* Determine if Julia has been previously initialized
* via `jl_init` or `jl_init_with_image`.
*
* @return Returns 1 if Julia is initialized, 0 otherwise.
*/
JL_DLLEXPORT int jl_is_initialized(void)
{
return jl_main_module != NULL;
}
/**
* @brief Set Julia command line arguments.
*
* Allows setting the command line arguments for Julia,
* similar to arguments passed in the main function of a C program.
*
* @param argc The number of command line arguments.
* @param argv Array of command line arguments.
*/
JL_DLLEXPORT void jl_set_ARGS(int argc, char **argv)
{
if (jl_core_module != NULL) {
jl_array_t *args = (jl_array_t*)jl_get_global(jl_core_module, jl_symbol("ARGS"));
if (args == NULL) {
args = jl_alloc_vec_any(0);
JL_GC_PUSH1(&args);
jl_set_const(jl_core_module, jl_symbol("ARGS"), (jl_value_t*)args);
JL_GC_POP();
}
assert(jl_array_nrows(args) == 0);
jl_array_grow_end(args, argc);
int i;
for (i = 0; i < argc; i++) {
jl_value_t *s = (jl_value_t*)jl_cstr_to_string(argv[i]);
jl_array_ptr_set(args, i, s);
}
}
}
/**
* @brief Initialize Julia with a specified system image file.
*
* Initializes Julia by specifying the usr/bin directory where the Julia binary is
* and the path of a system image file (*.so). If the julia_bindir is NULL, the function
* attempts to guess the directory. The image_path is interpreted as a path to the system image
* file. A non-absolute path for the system image is considered relative to julia_bindir, or
* relative to the default Julia home directory. The default system image is typically
* something like ../lib/julia/sys.so.
*
* @param julia_bindir The usr/bin directory where the Julia binary is located, or NULL to guess.
* @param image_path The path of a system image file (*.so). Interpreted as relative to julia_bindir
* or the default Julia home directory if not an absolute path.
*/
JL_DLLEXPORT void jl_init_with_image(const char *julia_bindir,
const char *image_path)
{
if (jl_is_initialized())
return;
libsupport_init();
jl_options.julia_bindir = julia_bindir;
if (image_path != NULL)
jl_options.image_file = image_path;
else
jl_options.image_file = jl_get_default_sysimg_path();
julia_init(JL_IMAGE_JULIA_HOME);
jl_exception_clear();
}
/**
* @brief Initialize the Julia runtime.
*
* Initializes the Julia runtime without any specific system image.
* It must be called before any other Julia API functions.
*/
JL_DLLEXPORT void jl_init(void)
{
char *libbindir = NULL;
#ifdef _OS_WINDOWS_
libbindir = strdup(jl_get_libdir());
#else
(void)asprintf(&libbindir, "%s" PATHSEPSTRING ".." PATHSEPSTRING "%s", jl_get_libdir(), "bin");
#endif
if (!libbindir) {
printf("jl_init unable to find libjulia!\n");
abort();
}
jl_init_with_image(libbindir, jl_get_default_sysimg_path());
free(libbindir);
}
// HACK: remove this for Julia 1.8 (see <https://github.com/JuliaLang/julia/issues/40730>)
JL_DLLEXPORT void jl_init__threading(void)
{
jl_init();
}
// HACK: remove this for Julia 1.8 (see <https://github.com/JuliaLang/julia/issues/40730>)
JL_DLLEXPORT void jl_init_with_image__threading(const char *julia_bindir,
const char *image_relative_path)
{
jl_init_with_image(julia_bindir, image_relative_path);
}
static void _jl_exception_clear(jl_task_t *ct) JL_NOTSAFEPOINT
{
ct->ptls->previous_exception = NULL;
}
/**
* @brief Evaluate a Julia expression from a string.
*
* @param str A C string containing the Julia expression to be evaluated.
* @return A pointer to `jl_value_t` representing the result of the evaluation.
* Returns `NULL` if an error occurs during parsing or evaluation.
*/
JL_DLLEXPORT jl_value_t *jl_eval_string(const char *str)
{
jl_value_t *r;
jl_task_t *ct = jl_current_task;
JL_TRY {
const char filename[] = "none";
jl_value_t *ast = jl_parse_all(str, strlen(str),
filename, strlen(filename), 1);
JL_GC_PUSH1(&ast);
r = jl_toplevel_eval_in(jl_main_module, ast);
JL_GC_POP();
_jl_exception_clear(ct);
}
JL_CATCH {
ct->ptls->previous_exception = jl_current_exception(ct);
r = NULL;
}
return r;
}
/**
* @brief Get the current exception in the Julia context.
*
* @return A pointer to `jl_value_t` representing the current exception.
* Returns `NULL` if no exception is currently thrown.
*/
JL_DLLEXPORT jl_value_t *jl_current_exception(jl_task_t *ct) JL_GLOBALLY_ROOTED JL_NOTSAFEPOINT
{
jl_excstack_t *s = ct->excstack;
return s && s->top != 0 ? jl_excstack_exception(s, s->top) : jl_nothing;
}
/**
* @brief Check if an exception has occurred in the Julia context.
*
* @return A pointer to `jl_value_t` representing the exception that occurred.
* Returns `NULL` if no exception has occurred.
*/
JL_DLLEXPORT jl_value_t *jl_exception_occurred(void)
{
return jl_current_task->ptls->previous_exception;
}
/**
* @brief Clear the current exception in the Julia context.
*
*/
JL_DLLEXPORT void jl_exception_clear(void)
{
_jl_exception_clear(jl_current_task);
}
/**
* @brief Get the type name of a Julia value.
*
* @param v A pointer to `jl_value_t` representing the Julia value.
* @return A C string containing the name of the type.
*/
JL_DLLEXPORT const char *jl_typename_str(jl_value_t *v)
{
if (!jl_is_datatype(v))
return NULL;
return jl_symbol_name(((jl_datatype_t*)v)->name->name);
}
/**
* @brief Get the string representation of a Julia value's type.
*
* @param v A pointer to `jl_value_t` representing the Julia value.
* @return A C string describing the type of the value.
*/
JL_DLLEXPORT const char *jl_typeof_str(jl_value_t *v)
{
return jl_typename_str((jl_value_t*)jl_typeof(v));
}
/**
* @brief Get the element type of a Julia array.
*
* @param a A pointer to `jl_value_t` representing the Julia array.
* @return A pointer to the type of the array elements.
*/
JL_DLLEXPORT void *jl_array_eltype(jl_value_t *a)
{
return jl_tparam0(jl_typeof(a));
}
/**
* @brief Get the number of dimensions of a Julia array.
*
* Returns the rank (number of dimensions) of a Julia array.
*
* @param a A pointer to `jl_value_t` representing the Julia array.
* @return An integer representing the number of dimensions of the array.
*/
JL_DLLEXPORT int jl_array_rank(jl_value_t *a)
{
return jl_array_ndims(a);
}
/**
* @brief Get the size of a specific dimension of a Julia array.
*
* Returns the size (number of elements) of a specific dimension
* of a Julia array.
*
* @param a A pointer to `jl_array_t` representing the Julia array.
* @param d The dimension for which the size is requested.
* @return The size of the specified dimension of the array.
*/
JL_DLLEXPORT size_t jl_array_size(jl_array_t *a, int d)
{
// n.b this functions only use was to violate the vector abstraction, so we have to continue to emulate that
if (d >= jl_array_ndims(a))
return a->ref.mem->length;
return jl_array_dim(a, d);
}
/**
* @brief Get the C string pointer from a Julia string.
*
* @param s A pointer to `jl_value_t` representing the Julia string.
* @return A C string pointer containing the contents of the Julia string.
*/
JL_DLLEXPORT const char *jl_string_ptr(jl_value_t *s)
{
return jl_string_data(s);
}
/**
* @brief Call a Julia function with a specified number of arguments.
*
* @param f A pointer to `jl_function_t` representing the Julia function to call.
* @param args An array of pointers to `jl_value_t` representing the arguments.
* @param nargs The number of arguments in the array.
* @return A pointer to `jl_value_t` representing the result of the function call.
*/
JL_DLLEXPORT jl_value_t *jl_call(jl_function_t *f, jl_value_t **args, uint32_t nargs)
{
jl_value_t *v;
jl_task_t *ct = jl_current_task;
nargs++; // add f to args
JL_TRY {
jl_value_t **argv;
JL_GC_PUSHARGS(argv, nargs);
argv[0] = (jl_value_t*)f;
for (int i = 1; i < nargs; i++)
argv[i] = args[i - 1];
size_t last_age = ct->world_age;
ct->world_age = jl_get_world_counter();
v = jl_apply(argv, nargs);
ct->world_age = last_age;
JL_GC_POP();
_jl_exception_clear(ct);
}
JL_CATCH {
ct->ptls->previous_exception = jl_current_exception(ct);
v = NULL;
}
return v;
}
/**
* @brief Call a Julia function with no arguments.
*
* A specialized case of `jl_call` for simpler scenarios.
*
* @param f A pointer to `jl_function_t` representing the Julia function to call.
* @return A pointer to `jl_value_t` representing the result of the function call.
*/
JL_DLLEXPORT jl_value_t *jl_call0(jl_function_t *f)
{
jl_value_t *v;
jl_task_t *ct = jl_current_task;
JL_TRY {
JL_GC_PUSH1(&f);
size_t last_age = ct->world_age;
ct->world_age = jl_get_world_counter();
v = jl_apply_generic(f, NULL, 0);
ct->world_age = last_age;
JL_GC_POP();
_jl_exception_clear(ct);
}
JL_CATCH {
ct->ptls->previous_exception = jl_current_exception(ct);
v = NULL;
}
return v;
}
/**
* @brief Call a Julia function with one argument.
*
* A specialized case of `jl_call` for simpler scenarios.
*
* @param f A pointer to `jl_function_t` representing the Julia function to call.
* @param a A pointer to `jl_value_t` representing the argument to the function.
* @return A pointer to `jl_value_t` representing the result of the function call.
*/
JL_DLLEXPORT jl_value_t *jl_call1(jl_function_t *f, jl_value_t *a)
{
jl_value_t *v;
jl_task_t *ct = jl_current_task;
JL_TRY {
jl_value_t **argv;
JL_GC_PUSHARGS(argv, 2);
argv[0] = f;
argv[1] = a;
size_t last_age = ct->world_age;
ct->world_age = jl_get_world_counter();
v = jl_apply(argv, 2);
ct->world_age = last_age;
JL_GC_POP();
_jl_exception_clear(ct);
}
JL_CATCH {
ct->ptls->previous_exception = jl_current_exception(ct);
v = NULL;
}
return v;
}
/**
* @brief Call a Julia function with two arguments.
*
* A specialized case of `jl_call` for simpler scenarios.
*
* @param f A pointer to `jl_function_t` representing the Julia function to call.
* @param a A pointer to `jl_value_t` representing the first argument.
* @param b A pointer to `jl_value_t` representing the second argument.
* @return A pointer to `jl_value_t` representing the result of the function call.
*/
JL_DLLEXPORT jl_value_t *jl_call2(jl_function_t *f, jl_value_t *a, jl_value_t *b)
{
jl_value_t *v;
jl_task_t *ct = jl_current_task;
JL_TRY {
jl_value_t **argv;
JL_GC_PUSHARGS(argv, 3);
argv[0] = f;
argv[1] = a;
argv[2] = b;
size_t last_age = ct->world_age;
ct->world_age = jl_get_world_counter();
v = jl_apply(argv, 3);
ct->world_age = last_age;
JL_GC_POP();
_jl_exception_clear(ct);
}
JL_CATCH {
ct->ptls->previous_exception = jl_current_exception(ct);
v = NULL;
}
return v;
}
/**
* @brief Call a Julia function with three arguments.
*
* A specialized case of `jl_call` for simpler scenarios.
*
* @param f A pointer to `jl_function_t` representing the Julia function to call.
* @param a A pointer to `jl_value_t` representing the first argument.
* @param b A pointer to `jl_value_t` representing the second argument.
* @param c A pointer to `jl_value_t` representing the third argument.
* @return A pointer to `jl_value_t` representing the result of the function call.
*/
JL_DLLEXPORT jl_value_t *jl_call3(jl_function_t *f, jl_value_t *a,
jl_value_t *b, jl_value_t *c)
{
jl_value_t *v;
jl_task_t *ct = jl_current_task;
JL_TRY {
jl_value_t **argv;
JL_GC_PUSHARGS(argv, 4);
argv[0] = f;
argv[1] = a;
argv[2] = b;
argv[3] = c;
size_t last_age = ct->world_age;
ct->world_age = jl_get_world_counter();
v = jl_apply(argv, 4);
ct->world_age = last_age;
JL_GC_POP();
_jl_exception_clear(ct);
}
JL_CATCH {
ct->ptls->previous_exception = jl_current_exception(ct);
v = NULL;
}
return v;
}
/**
* @brief Get a field from a Julia object.
*
* @param o A pointer to `jl_value_t` representing the Julia object.
* @param fld A C string representing the name of the field to retrieve.
* @return A pointer to `jl_value_t` representing the value of the field.
*/
JL_DLLEXPORT jl_value_t *jl_get_field(jl_value_t *o, const char *fld)
{
jl_value_t *v;
jl_task_t *ct = jl_current_task;
JL_TRY {
jl_value_t *s = (jl_value_t*)jl_symbol(fld);
int i = jl_field_index((jl_datatype_t*)jl_typeof(o), (jl_sym_t*)s, 1);
v = jl_get_nth_field(o, i);
jl_exception_clear();
}
JL_CATCH {
ct->ptls->previous_exception = jl_current_exception(ct);
v = NULL;
}
return v;
}
/**
* @brief Begin an atomic signal-protected region.
*
* Marks the start of a region of code that should be protected
* from interruption by asynchronous signals.
*/
JL_DLLEXPORT void jl_sigatomic_begin(void)
{
JL_SIGATOMIC_BEGIN();
}
/**
* @brief End an atomic signal-protected region.
*
* Marks the end of a region of code protected from asynchronous signals.
* It should be used in conjunction with `jl_sigatomic_begin` to define signal-protected regions.
*/
JL_DLLEXPORT void jl_sigatomic_end(void)
{
jl_task_t *ct = jl_current_task;
if (ct->ptls->defer_signal == 0)
jl_error("sigatomic_end called in non-sigatomic region");
JL_SIGATOMIC_END();
}
/**
* @brief Check if Julia is running in debug build mode.
*
* @return Returns 1 if Julia is in debug build mode, 0 otherwise.
*/
JL_DLLEXPORT int jl_is_debugbuild(void) JL_NOTSAFEPOINT
{
#ifdef JL_DEBUG_BUILD
return 1;
#else
return 0;
#endif
}
/**
* @brief Check if Julia has been build with assertions enabled.
*
* @return Returns 1 if assertions are enabled, 0 otherwise.
*/
JL_DLLEXPORT int8_t jl_is_assertsbuild(void) JL_NOTSAFEPOINT {
#ifndef JL_NDEBUG
return 1;
#else
return 0;
#endif
}
/**
* @brief Check if Julia's memory debugging is enabled.
*
* @return Returns 1 if memory debugging is enabled, 0 otherwise.
*/
JL_DLLEXPORT int8_t jl_is_memdebug(void) JL_NOTSAFEPOINT {
#ifdef MEMDEBUG
return 1;
#else
return 0;
#endif
}
/**
* @brief Get the directory path of the Julia binary.
*
* @return A pointer to `jl_value_t` representing the directory path as a Julia string.
*/
JL_DLLEXPORT jl_value_t *jl_get_julia_bindir(void)
{
return jl_cstr_to_string(jl_options.julia_bindir);
}
/**
* @brief Get the path to the Julia binary.
*
* @return A pointer to `jl_value_t` representing the full path as a Julia string.
*/
JL_DLLEXPORT jl_value_t *jl_get_julia_bin(void)
{
return jl_cstr_to_string(jl_options.julia_bin);
}
/**
* @brief Get the path to the Julia system image file.
*
* @return A pointer to `jl_value_t` representing the system image file path as a Julia string.
*/
JL_DLLEXPORT jl_value_t *jl_get_image_file(void)
{
return jl_cstr_to_string(jl_options.image_file);
}
/**
* @brief Get the major version number of Julia.
*
* @return The major version number as an integer.
*/
JL_DLLEXPORT int jl_ver_major(void)
{
return JULIA_VERSION_MAJOR;
}
/**
* @brief Get the minor version number of Julia.
*
* @return The minor version number as an integer.
*/
JL_DLLEXPORT int jl_ver_minor(void)
{
return JULIA_VERSION_MINOR;
}
/**
* @brief Get the patch version number of Julia.
*
* @return The patch version number as an integer.
*/
JL_DLLEXPORT int jl_ver_patch(void)
{
return JULIA_VERSION_PATCH;
}
/**
* @brief Check if the current Julia version is a release version.
*
* @return Returns 1 if it is a release version, 0 otherwise.
*/
JL_DLLEXPORT int jl_ver_is_release(void)
{
return JULIA_VERSION_IS_RELEASE;
}
/**
* @brief Get the Julia version as a string.
*
* @return A C string containing the version information.
*/
JL_DLLEXPORT const char *jl_ver_string(void)
{
return JULIA_VERSION_STRING;
}
/**
* @brief Convert a Julia value to a tagged value.
*
* Converts a Julia value into its corresponding tagged value representation.
* Tagged values include additional metadata used internally by the Julia runtime.
*
* @param v A pointer to `jl_value_t` representing the Julia value.
* @return A pointer to `jl_taggedvalue_t` representing the tagged value.
*/
JL_DLLEXPORT jl_taggedvalue_t *(jl_astaggedvalue)(jl_value_t *v)
{
return jl_astaggedvalue(v);
}
/**
* @brief Convert a tagged value back to a Julia value.
*
* Converts a tagged value back into its original Julia value.
* It's the inverse operation of `jl_astaggedvalue`.
*
* @param v A pointer to `jl_taggedvalue_t` representing the tagged value.
* @return A pointer to `jl_value_t` representing the original Julia value.
*/
JL_DLLEXPORT jl_value_t *(jl_valueof)(jl_taggedvalue_t *v)
{
return jl_valueof(v);
}
/**
* @brief Get the type of a Julia value.
*
* @param v A pointer to `jl_value_t` representing the Julia value.
* @return A pointer to `jl_value_t` representing the type of the value.
*/
JL_DLLEXPORT jl_value_t *(jl_typeof)(jl_value_t *v)
{
return jl_typeof(v);
}
/**
* @brief Get the field types of a Julia value.
*
* @param v A pointer to `jl_value_t` representing the Julia value.
* @return A pointer to `jl_value_t` representing the field types.
*/
JL_DLLEXPORT jl_value_t *(jl_get_fieldtypes)(jl_value_t *v)
{
return (jl_value_t*)jl_get_fieldtypes((jl_datatype_t*)v);
}
/**
* @brief Check equality of two Julia values.
*
* @param a A pointer to `jl_value_t` representing the first Julia value.
* @param b A pointer to `jl_value_t` representing the second Julia value.
* @return Returns 1 if the values are equal, 0 otherwise.
*/
JL_DLLEXPORT int ijl_egal(jl_value_t *a, jl_value_t *b)
{
return jl_egal(a, b);
}
#ifndef __clang_gcanalyzer__
/**
* @brief Enter a state where concurrent garbage collection (GC) is considered unsafe.
*
* Marks the beginning of a code region where garbage collection operations are unsafe.
* Used to make it legal to access GC-managed state (almost anything)
*
* @return An `int8_t` state value representing the previous GC state.
*/
JL_DLLEXPORT int8_t (jl_gc_unsafe_enter)(void)
{
jl_task_t *ct = jl_current_task;
return jl_gc_unsafe_enter(ct->ptls);
}
/**
* @brief Leave the state where garbage collection is considered unsafe.
*
* Ends a code region where garbage collection was marked as unsafe.
* It restores the previous GC state using the state value returned by `jl_gc_unsafe_enter`.
*
* @param state The state value returned by `jl_gc_unsafe_enter` to restore the previous GC state.
*/
JL_DLLEXPORT void (jl_gc_unsafe_leave)(int8_t state)
{
jl_task_t *ct = jl_current_task;
jl_gc_unsafe_leave(ct->ptls, state);
}
/**
* @brief Enter a state where garbage collection (GC) is considered safe.
*
* Marks the beginning of a code region where garbage collection operations are safe.
* Used to enable GC in sections of code where it was previously marked as unsafe.
*
* @return An `int8_t` state value representing the previous GC state.
*/
JL_DLLEXPORT int8_t (jl_gc_safe_enter)(void)
{
jl_task_t *ct = jl_current_task;
return jl_gc_safe_enter(ct->ptls);
}
/**
* @brief Leave the state where garbage collection is considered safe.
*
* Ends a code region where garbage collection was marked as safe.
* It restores the previous GC state using the state value returned by `jl_gc_safe_enter`.
*
* @param state The state value returned by `jl_gc_safe_enter` to restore the previous GC state.
*/
JL_DLLEXPORT void (jl_gc_safe_leave)(int8_t state)
{
jl_task_t *ct = jl_current_task;
jl_gc_safe_leave(ct->ptls, state);
}
#endif
/**
* @brief Trigger a garbage collection safepoint in a GC-unsafe region.
*
* Triggers a safepoint for garbage collection. Used to
* ensure that the garbage collector can run at specific points in the code,
* particularly in long-running operations or loops.
*/
JL_DLLEXPORT void jl_gc_safepoint(void)
{
jl_task_t *ct = jl_current_task;
jl_gc_safepoint_(ct->ptls);
}
/**
* @brief Pause CPU execution for a brief moment.
*
* Used to pause the CPU briefly, typically to reduce power consumption
* or manage CPU resources more effectively in a tight loop or busy wait scenario.
*/
JL_DLLEXPORT void (jl_cpu_pause)(void)
{
jl_cpu_pause();
}
/**
* @brief Suspend CPU execution.
*
* Suspends CPU execution until a specific condition or event occurs.
*/
JL_DLLEXPORT void (jl_cpu_suspend)(void)
{
jl_cpu_suspend();
}
/**
* @brief Wake the CPU from a suspended state.
*
* Used to resume CPU execution after it has been suspended using `jl_cpu_suspend`.
*/
JL_DLLEXPORT void (jl_cpu_wake)(void)
{
jl_cpu_wake();
}
/**
* @brief Enable cumulative compile timing.
*/
JL_DLLEXPORT void jl_cumulative_compile_timing_enable(void)
{
// Increment the flag to allow reentrant callers to `@time`.
jl_atomic_fetch_add(&jl_measure_compile_time_enabled, 1);
}
/**
* @brief Disable cumulative compile timing.
*/
JL_DLLEXPORT void jl_cumulative_compile_timing_disable(void)
{
// Decrement the flag when done measuring, allowing other callers to continue measuring.
jl_atomic_fetch_add(&jl_measure_compile_time_enabled, -1);
}
/**
* @brief Get the cumulative compilation time in nanoseconds.
*
* @return The cumulative compilation time in nanoseconds.
*/
JL_DLLEXPORT uint64_t jl_cumulative_compile_time_ns(void)
{
return jl_atomic_load_relaxed(&jl_cumulative_compile_time);
}
/**
* @brief Get the cumulative recompilation time in nanoseconds.
*
* @return The cumulative recompilation time in nanoseconds.
*/
JL_DLLEXPORT uint64_t jl_cumulative_recompile_time_ns(void)
{
return jl_atomic_load_relaxed(&jl_cumulative_recompile_time);
}
/**
* @brief Enable per-task timing.
*/
JL_DLLEXPORT void jl_task_metrics_enable(void)
{
// Increment the flag to allow reentrant callers.
jl_atomic_fetch_add(&jl_task_metrics_enabled, 1);
}
/**
* @brief Disable per-task timing.
*/
JL_DLLEXPORT void jl_task_metrics_disable(void)
{
// Prevent decrementing the counter below zero
uint8_t enabled = jl_atomic_load_relaxed(&jl_task_metrics_enabled);
while (enabled > 0) {
if (jl_atomic_cmpswap(&jl_task_metrics_enabled, &enabled, enabled-1))
break;
}
}
/**
* @brief Retrieve floating-point environment constants.
*
* Populates an array with constants related to the floating-point environment,
* such as rounding modes and exception flags.
*
* @param ret An array of integers to be populated with floating-point environment constants.
*/
JL_DLLEXPORT void jl_get_fenv_consts(int *ret)
{
ret[0] = FE_INEXACT;
ret[1] = FE_UNDERFLOW;
ret[2] = FE_OVERFLOW;
ret[3] = FE_DIVBYZERO;
ret[4] = FE_INVALID;
ret[5] = FE_TONEAREST;
ret[6] = FE_UPWARD;
ret[7] = FE_DOWNWARD;
ret[8] = FE_TOWARDZERO;
}
// TODO: Windows binaries currently load msvcrt which doesn't have these C99 functions.
// the mingw compiler ships additional definitions, but only for use in C code.
// remove this when we switch to ucrt, make the version in openlibm portable,
// or figure out how to reexport the defs from libmingwex (see JuliaLang/julia#38466).
JL_DLLEXPORT int jl_get_fenv_rounding(void)
{
return fegetround();
}
/**
* @brief Set the floating-point rounding mode.
*
* @param i An integer representing the desired floating-point rounding mode.
See also "floating-point rounding" macros in `<fenv.h>`.
* @return An integer indicating the success or failure of setting the rounding mode.
*/
JL_DLLEXPORT int jl_set_fenv_rounding(int i)
{
return fesetround(i);
}
static int exec_program(char *program)
{
jl_task_t *ct = jl_current_task;
JL_TRY {
jl_load(jl_main_module, program);
}
JL_CATCH {
// TODO: It is possible for this output to be mangled due to `jl_print_backtrace`
// printing directly to STDERR_FILENO.
int shown_err = 0;
jl_printf(JL_STDERR, "error during bootstrap:\n");
jl_value_t *exc = jl_current_exception(ct);
jl_value_t *showf = jl_base_module ? jl_get_function(jl_base_module, "show") : NULL;
if (showf) {
jl_value_t *errs = jl_stderr_obj();
if (errs) {
if (jl_call2(showf, errs, exc)) {
jl_printf(JL_STDERR, "\n");
shown_err = 1;
}
}
}
if (!shown_err) {
jl_static_show((JL_STREAM*)STDERR_FILENO, exc);
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
}
jl_print_backtrace(); // written to STDERR_FILENO
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
return 1;
}
return 0;
}
static NOINLINE int true_main(int argc, char *argv[])
{
jl_set_ARGS(argc, argv);
jl_function_t *start_client = jl_base_module ?
(jl_function_t*)jl_get_global(jl_base_module, jl_symbol("_start")) : NULL;
jl_task_t *ct = jl_current_task;
if (start_client) {
int ret = 1;
JL_TRY {
size_t last_age = ct->world_age;
ct->world_age = jl_get_world_counter();
jl_value_t *r = jl_apply(&start_client, 1);
if (jl_typeof(r) != (jl_value_t*)jl_int32_type)
jl_type_error("typeassert", (jl_value_t*)jl_int32_type, r);
ret = jl_unbox_int32(r);
ct->world_age = last_age;
}
JL_CATCH {
jl_no_exc_handler(jl_current_exception(ct), ct);
}
return ret;
}
// run program if specified, otherwise enter REPL
if (argc > 0) {
if (strcmp(argv[0], "-")) {
return exec_program(argv[0]);
}
}
jl_printf(JL_STDOUT, "WARNING: Base._start not defined, falling back to economy mode repl.\n");
if (!jl_errorexception_type)
jl_printf(JL_STDOUT, "WARNING: jl_errorexception_type not defined; any errors will be fatal.\n");
while (!ios_eof(ios_stdin)) {
char *volatile line = NULL;
JL_TRY {
ios_puts("\njulia> ", ios_stdout);
ios_flush(ios_stdout);
line = ios_readline(ios_stdin);
jl_value_t *val = (jl_value_t*)jl_eval_string(line);
JL_GC_PUSH1(&val);
if (jl_exception_occurred()) {
jl_printf(JL_STDERR, "error during run:\n");
jl_static_show(JL_STDERR, jl_exception_occurred());
jl_exception_clear();
}
else if (val) {
jl_static_show(JL_STDOUT, val);
}
JL_GC_POP();
jl_printf(JL_STDOUT, "\n");
free(line);
line = NULL;
jl_process_events();
}
JL_CATCH {
if (line) {
free(line);
line = NULL;
}
jl_printf((JL_STREAM*)STDERR_FILENO, "\nparser error:\n");
jl_static_show((JL_STREAM*)STDERR_FILENO, jl_current_exception(ct));
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
jl_print_backtrace(); // written to STDERR_FILENO
}
}
return 0;
}
static void lock_low32(void)
{
#if defined(_OS_WINDOWS_) && defined(_P64) && defined(JL_DEBUG_BUILD)
// Prevent usage of the 32-bit address space on Win64, to catch pointer cast errors.
char *const max32addr = (char*)0xffffffffL;
SYSTEM_INFO info;
MEMORY_BASIC_INFORMATION meminfo;
GetNativeSystemInfo(&info);
memset(&meminfo, 0, sizeof(meminfo));
meminfo.BaseAddress = info.lpMinimumApplicationAddress;
while ((char*)meminfo.BaseAddress < max32addr) {
size_t nbytes = VirtualQuery(meminfo.BaseAddress, &meminfo, sizeof(meminfo));
assert(nbytes == sizeof(meminfo));
if (meminfo.State == MEM_FREE) { // reserve all free pages in the first 4GB of memory
char *first = (char*)meminfo.BaseAddress;
char *last = first + meminfo.RegionSize;
if (last > max32addr)
last = max32addr;
// adjust first up to the first allocation granularity boundary
// adjust last down to the last allocation granularity boundary