-
Notifications
You must be signed in to change notification settings - Fork 298
/
System_omc.c
1156 lines (1027 loc) · 26.6 KB
/
System_omc.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 part of OpenModelica.
*
* Copyright (c) 1998-2010, Linköpings University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
#if defined(_MSC_VER) || defined(__MINGW32__)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
#if defined(__linux__) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE 1
#endif
#include <ctype.h> /* for toupper */
#include <limits.h>
#include <stdlib.h>
#include "omc_msvc.h"
#include "omc_file.h"
#include "openmodelica.h"
#include "meta_modelica.h"
#include "ModelicaUtilities.h"
#define ADD_METARECORD_DEFINITIONS static
#include "OpenModelicaBootstrappingHeader.h"
#include "systemimpl.c"
extern void System_writeFile(const char* filename, const char* data)
{
if (SystemImpl__writeFile(filename, data))
MMC_THROW();
}
extern void System_appendFile(const char *filename, const char *data)
{
if (SystemImpl__appendFile(filename, data))
MMC_THROW();
}
extern char* System_readFile(const char* filename)
{
return SystemImpl__readFile(filename);
}
extern const char* System_stringReplace(const char* str, const char* source, const char* target)
{
char* res = _replace(str,source,target);
if (res == NULL)
MMC_THROW();
return res;
}
extern const char* System_makeC89Identifier(const char* str)
{
int i=0, len=strlen(str);
char *res = omc_alloc_interface.malloc_strdup(str);
if (!((res[0]>='a' && res[0]<='z') || (res[0]>='A' && res[0]<='Z'))) {
res[0] = '_';
}
for (i=1; i<len; i++) {
if (!((res[i]>='a' && res[i]<='z') || (res[i]>='A' && res[i]<='Z') || (res[i]>='0' && res[i]<='9'))) {
res[i] = '_';
}
}
return res;
}
extern int System_stringFind(const char* str, const char* searchStr)
{
const char *found = strstr(str, searchStr);
if (found == NULL)
return -1;
else
return found-str;
}
extern const char* System_stringFindString(const char* str, const char* searchStr)
{
const char *found = strstr(str, searchStr);
if (found == NULL)
MMC_THROW();
return strcpy(ModelicaAllocateString(strlen(found)), found);
}
extern void System_realtimeTick(int ix)
{
if (ix < 0 || ix >= NUM_USER_RT_CLOCKS) MMC_THROW();
rt_tick(ix);
}
extern double System_realtimeTock(int ix)
{
if (ix < 0 || ix >= NUM_USER_RT_CLOCKS) MMC_THROW();
return rt_tock(ix);
}
extern void System_realtimeClear(int ix)
{
if (ix < 0 || ix >= NUM_USER_RT_CLOCKS) MMC_THROW();
rt_clear(ix);
}
extern int System_realtimeNtick(int ix)
{
if (ix < 0 || ix >= NUM_USER_RT_CLOCKS) MMC_THROW();
return rt_ncall(ix);
}
extern const char* System_getCCompiler()
{
return cc;
}
extern const char* System_getCXXCompiler()
{
return cxx;
}
extern const char* System_getOMPCCompiler()
{
return omp_cc;
}
extern const char* System_getLinker()
{
return linker;
}
extern const char* System_getLDFlags()
{
return ldflags;
}
extern const char* System_getCFlags()
{
return cflags;
}
extern const char* System_trim(const char* str, const char* chars_to_remove)
{
return SystemImpl__trim(str,chars_to_remove);
}
extern const char* System_trimWhitespace(const char* str)
{
return SystemImpl__trim(str," \f\n\r\t\v");
}
extern const char* System_trimChar(const char* str, const char* char_to_remove)
{
if (!char_to_remove[0] || char_to_remove[1])
MMC_THROW();
return MMC_STRINGDATA(SystemImpl__trimChar(str,*char_to_remove));
}
extern const char* System_basename(const char* str)
{
const char *res = SystemImpl__basename(str);
return strcpy(ModelicaAllocateString(strlen(res)), res);
}
extern const char* System_dirname(const char* str)
{
char *cpy = omc_alloc_interface.malloc_strdup(str);
char *res = NULL;
#if defined(_MSC_VER)
char drive[_MAX_DRIVE], dir[_MAX_DIR], filename[_MAX_FNAME], extension[_MAX_EXT];
_splitpath(str, drive, dir, filename, extension);
sprintf(cpy, "%s/%s/",drive,dir);
res = cpy;
#else
res = dirname(cpy);
#endif
return res;
}
#if defined(OPENMODELICA_BOOTSTRAPPING_STAGE_1)
extern const char* System_getRTLibs()
{
return "DUMMY RT LIBS";
}
extern const char* System_getRTLibsSim()
{
return "DUMMY RT LIBS";
}
extern const char* System_getRTLibsFMU()
{
return "DUMMY RT LIBS";
}
extern const char* System_getExeExt()
{
return ".exe";
}
extern const char* System_getDllExt()
{
return CONFIG_DLL_EXT;
}
extern const char* System_getMakeCommand()
{
return "make";
}
extern const char* System_os()
{
return "Windows_NT";
}
extern const char* System_configureCommandLine()
{
return "Dummy configure";
}
extern const char* System_platform()
{
return "Dummy platform";
}
extern const char* System_pathDelimiter()
{
return "/";
}
extern const char* System_groupDelimiter()
{
return ";";
}
extern const char* System_getCorbaLibs()
{
return "Dummy CORBA libs";
}
extern void* System_getRuntimeLibs()
{
return "Dummy system libs";
}
const char* System_getTriple()
{
return "DEFAULT_TRIPLE";
}
#endif
extern int System_strncmp(const char *str1, const char *str2, int len)
{
int res= strncmp(str1,str2,len);
/* adrpo: 2010-10-07, return -1, 0, +1 so we can pattern match on it directly! */
if (res>0) res = 1;
else if (res<0) res = -1;
return res;
}
extern int System_strcmp(const char *str1, const char *str2)
{
int res = strcmp(str1,str2);
/* adrpo: 2010-10-07, return -1, 0, +1 so we can pattern match on it directly! */
if (res>0) res = 1;
else if (res<0) res = -1;
return res;
}
extern int System_strcmp_offset(const char *str1, int offset1, int length1, const char *str2, int offset2, int length2)
{
int n = length1 > length2 ? length1 : length2;
int res = strncmp(str1+offset1-1, str2+offset2-1, n);
if (res>0) res = 1;
else if (res<0) res = -1;
return res;
}
extern int System_getHasExpandableConnectors()
{
return hasExpandableConnectors;
}
extern void System_setHasExpandableConnectors(int b)
{
hasExpandableConnectors = b;
}
extern int System_getHasOverconstrainedConnectors()
{
return hasOverconstrainedConnectors;
}
extern void System_setHasOverconstrainedConnectors(int b)
{
hasOverconstrainedConnectors = b;
}
extern int System_getPartialInstantiation()
{
return isPartialInstantiation;
}
extern void System_setPartialInstantiation(int b)
{
isPartialInstantiation = b;
}
extern int System_getHasInnerOuterDefinitions()
{
return hasInnerOuterDefinitions;
}
extern void System_setHasInnerOuterDefinitions(int b)
{
hasInnerOuterDefinitions = b;
}
extern int System_getHasStreamConnectors()
{
return hasStreamConnectors;
}
extern void System_setHasStreamConnectors(int b)
{
hasStreamConnectors = b;
}
extern int System_getUsesCardinality()
{
return usesCardinality;
}
extern void System_setUsesCardinality(int b)
{
usesCardinality = b;
}
extern void* System_strtok(const char *str0, const char *delimit)
{
char *s;
void *res = mmc_mk_nil();
char *str = omc_alloc_interface.malloc_strdup(str0);
char *saveptr;
s=strtok_r(str,delimit,&saveptr);
if (s == NULL) {
return res;
}
res = mmc_mk_cons(mmc_mk_scon(s),res);
while ((s=strtok_r(NULL,delimit,&saveptr))) {
res = mmc_mk_cons(mmc_mk_scon(s),res);
}
return listReverse(res);
}
extern char* System_substring(const char *str, int start, int stop)
{
char* substring = NULL;
int startIndex = start;
int stopIndex = stop;
int len1 = strlen(str);
int len2 = 0;
void *res = NULL;
/* Check arguments */
if ( startIndex < 1 )
{
MMC_THROW();
}
if ( stopIndex == -999 )
{
stopIndex = startIndex;
} else if ( stopIndex < startIndex ) {
MMC_THROW();
} else if ( stopIndex > len1 ) {
MMC_THROW();
}
/* Allocate memory and copy string */
len2 = stopIndex - startIndex + 1;
substring = (char*)ModelicaAllocateString(len2);
strncpy(substring, &str[startIndex-1], len2);
substring[len2] = '\0';
return substring;
}
extern char* System_toupper(const char *str)
{
int i;
char* strToUpper = strcpy(ModelicaAllocateString(strlen(str)),str);
for (i = 0; i < strlen(strToUpper); i++)
{
strToUpper[i] = toupper(strToUpper[i]);
}
return strToUpper;
}
extern char* System_tolower(const char *str)
{
int i;
char* strToLower = strcpy(ModelicaAllocateString(strlen(str)),str);
for (i = 0; i < strlen(strToLower); i++)
{
strToLower[i] = tolower(strToLower[i]);
}
return strToLower;
}
const char* System_getClassnamesForSimulation()
{
if(class_names_for_simulation)
return strcpy(ModelicaAllocateString(strlen(class_names_for_simulation)),class_names_for_simulation);
else
return "{}";
}
void System_setClassnamesForSimulation(const char *class_names)
{
class_names_for_simulation = omc_alloc_interface.malloc_strdup(class_names);
}
extern double System_getVariableValue(double _timeStamp, void* _timeValues, void* _varValues)
{
double res = 0;
if (SystemImpl__getVariableValue(_timeStamp,_timeValues,_varValues,&res))
MMC_THROW();
return res;
}
extern void* System_getFileModificationTime(const char *fileName)
{
struct stat attrib; // create a file attribute structure
double elapsedTime; // the time elapsed as double
int result; // the result of the function call
if (stat( fileName, &attrib ) != 0) {
return mmc_mk_none();
} else {
return mmc_mk_some(mmc_mk_rcon(difftime(attrib.st_mtime, 0))); // the file modification time
}
}
#if defined(__MINGW32__) || defined(_MSC_VER)
void* System_moFiles(const char *directory)
{
void *res;
WIN32_FIND_DATA FileData;
BOOL more = TRUE;
char pattern[1024];
HANDLE sh;
sprintf(pattern, "%s\\*.mo", directory);
res = mmc_mk_nil();
sh = FindFirstFile(pattern, &FileData);
if (sh != INVALID_HANDLE_VALUE) {
while(more) {
if (strcmp(FileData.cFileName,"package.mo") != 0)
{
res = mmc_mk_cons(mmc_mk_scon(FileData.cFileName),res);
}
more = FindNextFile(sh, &FileData);
}
if (sh != INVALID_HANDLE_VALUE) FindClose(sh);
}
return res;
}
#else
void* System_moFiles(const char *directory)
{
int i,count;
void *res;
struct dirent **files;
select_from_dir = directory;
count = scandir(directory, &files, file_select_mo, NULL);
res = mmc_mk_nil();
for (i=0; i<count; i++)
{
res = mmc_mk_cons(mmc_mk_scon(files[i]->d_name),res);
free(files[i]);
}
return res;
}
#endif
#if defined(__MINGW32__) || defined(_MSC_VER)
void* System_mocFiles(const char *directory)
{
void *res;
WIN32_FIND_DATA FileData;
BOOL more = TRUE;
char pattern[1024];
HANDLE sh;
sprintf(pattern, "%s\\*.moc", directory);
res = mmc_mk_nil();
sh = FindFirstFile(pattern, &FileData);
if (sh != INVALID_HANDLE_VALUE) {
while(more) {
if (strcmp(FileData.cFileName,"package.moc") != 0)
{
res = mmc_mk_cons(mmc_mk_scon(FileData.cFileName),res);
}
more = FindNextFile(sh, &FileData);
}
if (sh != INVALID_HANDLE_VALUE) FindClose(sh);
}
return res;
}
#else
void* System_mocFiles(const char *directory)
{
int i,count;
void *res;
struct dirent **files;
select_from_dir = directory;
count = scandir(directory, &files, file_select_moc, NULL);
res = mmc_mk_nil();
for (i=0; i<count; i++)
{
res = mmc_mk_cons(mmc_mk_scon(files[i]->d_name),res);
free(files[i]);
}
return res;
}
#endif
extern int System_lookupFunction(int _inLibHandle, const char* _inFunc)
{
int res = SystemImpl__lookupFunction(_inLibHandle, _inFunc);
if (res == -1) MMC_THROW();
return res;
}
extern void System_freeFunction(int _inFuncHandle, int printDebug)
{
if (SystemImpl__freeFunction(_inFuncHandle, printDebug)) MMC_THROW();
}
extern void System_freeLibrary(int _inLibHandle, int printDebug)
{
if (SystemImpl__freeLibrary(_inLibHandle, printDebug)) MMC_THROW();
}
extern int System_userIsRoot()
{
return CONFIG_USER_IS_ROOT;
}
extern int System_getuid()
{
#if defined(__MINGW32__) || defined(_MSC_VER)
return 0;
#else
return getuid();
#endif
}
extern const char* System_readEnv(const char *envname)
{
char *envvalue = getenv(envname);
if (envvalue == NULL) MMC_THROW();
return strcpy(ModelicaAllocateString(strlen(envvalue)),envvalue);
}
extern void System_getCurrentDateTime(int* sec, int* min, int* hour, int* mday, int* mon, int* year)
{
time_t t;
struct tm* localTime;
time( &t );
localTime = localtime(&t);
*sec = localTime->tm_sec;
*min = localTime->tm_min;
*hour = localTime->tm_hour;
*mday = localTime->tm_mday;
*mon = localTime->tm_mon + 1;
*year = localTime->tm_year + 1900;
}
extern const char* System_getUUIDStr()
{
const char *res = SystemImpl__getUUIDStr();
return strcpy(ModelicaAllocateString(strlen(res)),res);
}
extern int System_loadLibrary(const char *name, int printDebug)
{
int res = SystemImpl__loadLibrary(name, printDebug);
if (res == -1) MMC_THROW();
return res;
}
#if defined(__MINGW32__) || defined(_MSC_VER)
void* System_subDirectories(const char *directory)
{
void *res;
WIN32_FIND_DATA FileData;
BOOL more = TRUE;
char pattern[1024];
HANDLE sh;
sprintf(pattern, "%s\\*.*", directory);
res = mmc_mk_nil();
sh = FindFirstFile(pattern, &FileData);
if (sh != INVALID_HANDLE_VALUE) {
while(more) {
if (strcmp(FileData.cFileName,"..") != 0 &&
strcmp(FileData.cFileName,".") != 0 &&
(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
res = mmc_mk_cons(mmc_mk_scon(FileData.cFileName),res);
}
more = FindNextFile(sh, &FileData);
}
if (sh != INVALID_HANDLE_VALUE) FindClose(sh);
}
return res;
}
#else
void* System_subDirectories(const char *directory)
{
int i,count;
void *res;
struct dirent **files;
select_from_dir = directory;
count = scandir(directory, &files, file_select_directories, NULL);
res = mmc_mk_nil();
for (i=0; i<count; i++)
{
res = mmc_mk_cons(mmc_mk_scon(files[i]->d_name),res);
free(files[i]);
}
return res;
}
#endif
extern void* System_regex(const char* str, const char* re, int maxn, int extended, int sensitive, int *nmatch)
{
void *res;
int i = 0;
#if !defined(_MSC_VER)
void *matches[maxn];
#else
void **matches = omc_alloc_interface.malloc(sizeof(void*)*maxn);
#endif
*nmatch = OpenModelica_regexImpl(str,re,maxn,extended,sensitive,mmc_mk_scon,(void**)&matches);
res = mmc_mk_nil();
for (i=maxn-1; i>=0; i--) {
res = mmc_mk_cons(matches[i],res);
}
#if defined(_MSC_VER)
GC_free(matches);
#endif
return res;
}
extern char* System_escapedString(char* str, int nl)
{
char *res = omc__escapedString(str,nl);
if (res == NULL) return str;
return res;
}
extern char* System_unescapedString(char* str)
{
char *res = SystemImpl__unescapedString(str);
if (res == NULL) return str;
return res;
}
extern char* System_unquoteIdentifier(char *str)
{
return SystemImpl__unquoteIdentifier(str);
}
extern char* System_getCurrentTimeStr()
{
time_t t;
struct tm* localTime;
char * dateStr;
time( &t );
localTime = localtime(&t);
dateStr = asctime(localTime);
if (dateStr)
return dateStr;
MMC_THROW();
}
void System_resetTimer()
{
/* reset the timer */
timerIntervalTime = 0;
timerCummulatedTime = 0;
timerTime = 0;
timerStackIdx = 0;
}
void System_startTimer()
{
/* start the timer if not already started */
if (!timerStackIdx)
{
rt_tick(RT_CLOCK_SPECIAL_STOPWATCH);
}
pushTimerStack();
}
void System_stopTimer()
{
popTimerStack();
}
double System_getTimerElapsedTime()
{
/* get the cummulated timer time */
return rt_tock(RT_CLOCK_SPECIAL_STOPWATCH) - timerStack[0];
}
double System_getTimerIntervalTime()
{
return timerIntervalTime;
}
double System_getTimerCummulatedTime()
{
return timerCummulatedTime;
}
int System_getTimerStackIndex()
{
return timerStackIdx;
}
extern void System_uriToClassAndPath(const char *uri, const char **scheme, char **name, char **path)
{
int res = SystemImpl__uriToClassAndPath(uri, scheme, name, path);
// TODO: Fix memory leak by using the external interface
if (res) MMC_THROW();
}
extern const char* System_modelicaPlatform()
{
return CONFIG_MODELICA_SPEC_PLATFORM;
}
extern const char* System_openModelicaPlatform()
{
return CONFIG_OPENMODELICA_SPEC_PLATFORM;
}
extern const char* System_gccDumpMachine()
{
return CONFIG_GCC_DUMPMACHINE;
}
extern const char* System_gccVersion()
{
return CONFIG_GCC_VERSION;
}
extern void System_getGCStatus(double *used, double *allocated)
{
*allocated = GC_get_heap_size();
*used = *allocated - GC_get_free_bytes();
}
extern const char* System_snprintff(const char *fmt, int len, double d)
{
char *buf;
if (len < 0) {
MMC_THROW();
}
buf = ModelicaAllocateString(len);
if (snprintf(buf,len,fmt,d) >= len) {
MMC_THROW();
}
return buf;
}
extern const char* System_sprintff(const char *fmt, double d)
{
char *buf;
const int buf_size = 20;
buf = ModelicaAllocateString(buf_size);
int len = snprintf(buf, buf_size, fmt, d);
if (len < 0) {
MMC_THROW();
}
if (len >= buf_size) {
buf = ModelicaAllocateString(len + 1);
snprintf(buf, len, fmt, d);
}
return buf;
}
extern const char* System_realpath(const char *path)
{
#if defined(__MINGW32__) || defined(_MSC_VER)
MULTIBYTE_TO_WIDECHAR_LENGTH(path, unicodePathLength);
MULTIBYTE_TO_WIDECHAR_VAR(path, unicodePath, unicodePathLength);
DWORD bufLen = 0;
bufLen = GetFullPathNameW(unicodePath, bufLen, NULL, NULL);
if (!bufLen) {
fprintf(stderr, "GetFullPathNameW failed. %lu\n", GetLastError());
MMC_THROW();
}
WCHAR unicodeFullPath[bufLen];
if (!GetFullPathNameW(unicodePath, bufLen, unicodeFullPath, NULL)) {
MULTIBYTE_OR_WIDECHAR_VAR_FREE(unicodePath);
fprintf(stderr, "GetFullPathNameW failed. %lu\n", GetLastError());
MMC_THROW();
}
MULTIBYTE_OR_WIDECHAR_VAR_FREE(unicodePath);
WIDECHAR_TO_MULTIBYTE_LENGTH(unicodeFullPath, bufferLength);
WIDECHAR_TO_MULTIBYTE_VAR(unicodeFullPath, buffer, bufferLength);
SystemImpl__toWindowsSeperators(buffer, bufferLength);
char *res = omc_alloc_interface.malloc_strdup(buffer);
MULTIBYTE_OR_WIDECHAR_VAR_FREE(buffer);
return res;
#else
char buf[PATH_MAX];
if (realpath(path, buf) == NULL) {
MMC_THROW();
}
return omc_alloc_interface.malloc_strdup(buf);
#endif
}
extern int System_fileIsNewerThan(const char *file1, const char *file2)
{
int res = SystemImpl__fileIsNewerThan(file1, file2);
if (res == -1)
MMC_THROW();
return res;
}
typedef void* voidp;
/* Work in progress: Threading support in OMC */
typedef struct thread_data {
pthread_mutex_t mutex;
modelica_metatype (*fn)(threadData_t*,modelica_metatype);
int fail;
int current;
int len;
void **commands;
void **status;
threadData_t *parent;
} thread_data;
static void* System_launchParallelTasksThread(void *in)
{
int exitstatus = 1;
int n;
thread_data *data = (thread_data*) in;
while (1) {
int fail = 1;
pthread_mutex_lock(&data->mutex);
n = data->current++;
pthread_mutex_unlock(&data->mutex);
if (data->fail || n >= data->len) break;
MMC_TRY_TOP()
threadData->parent = data->parent;
threadData->mmc_thread_work_exit = threadData->mmc_jumper;
data->status[n] = data->fn(threadData,data->commands[n]);
fail = 0;
MMC_CATCH_TOP()
if (fail) {
data->fail = 1;
}
}
return NULL;
}
static void* System_launchParallelTasksSerial(threadData_t *threadData, void *dataLst, modelica_metatype (*fn)(threadData_t *,modelica_metatype))
{
void *result = mmc_mk_nil();
while (!listEmpty(dataLst)) {
result = mmc_mk_cons(fn(threadData, MMC_CAR(dataLst)),result);
dataLst = MMC_CDR(dataLst);
}
return listReverse(dataLst);
}
extern void* System_launchParallelTasks(threadData_t *threadData, int numThreads, void *dataLst, modelica_metatype (*fn)(threadData_t *,modelica_metatype))
{
int len = listLength(dataLst), i;
void *result = mmc_mk_nil();
thread_data data = {0};
#if !defined(_MSC_VER)
void *commands[len];
void *status[len];
pthread_t th[numThreads];
int isInteger = 0;
#if defined(__MINGW32__)
/* adrpo: set thread stack size on Windows to 4MB */
pthread_attr_t attr;
if (pthread_attr_init(&attr))
{
const char *tok[1] = {strerror(errno)};
data.fail = 1;
c_add_message(NULL,5999,
ErrorType_scripting,
ErrorLevel_internal,
gettext("System.launchParallelTasks: failed to initialize the pthread attributes: %s"),
NULL,
0);
MMC_THROW_INTERNAL();
}
/* try to set a stack size of 4MB */
if (pthread_attr_setstacksize(&attr, 4194304))
{
/* did not work, try half 2MB */
if (pthread_attr_setstacksize(&attr, 2097152))
{
/* did not work, try half 1MB */
if (pthread_attr_setstacksize(&attr, 1048576))
{
const char *tok[1] = {strerror(errno)};
data.fail = 1;
c_add_message(NULL,5999,
ErrorType_scripting,
ErrorLevel_internal,
gettext("System.launchParallelTasks: failed to set the pthread stack size to 1MB: %s"),
NULL,
0);
MMC_THROW_INTERNAL();
}
}
}
#endif
#else /* MSVC */
void **commands = (void**) omc_alloc_interface.malloc(sizeof(void*)*len);
void **status = (void**) omc_alloc_interface.malloc(sizeof(void*)*len);
pthread_t *th = (pthread_t*) omc_alloc_interface.malloc(sizeof(pthread_t)*numThreads);
#endif
if (len == 0) {
return mmc_mk_nil();
} else if (numThreads == 1 || len == 1) {
return System_launchParallelTasksSerial(threadData,dataLst,fn);
}
/* Make sure we get nothing unexpected here */
memset(commands, 0, len*sizeof(void*));
memset(status, 0, len*sizeof(void*));
memset(th, 0, numThreads*sizeof(pthread_t));
pthread_mutex_init(&data.mutex,NULL);
data.fn = fn;
data.current = 0;
data.len = len;
data.commands = commands;
data.status = status;
data.fail = 0;
data.parent = threadData;
for (i=0; i<len; i++, dataLst = MMC_CDR(dataLst)) {
commands[i] = MMC_CAR(dataLst);
status[i] = 0; /* just in case */
}
numThreads = numThreads > len ? len : numThreads;
for (i=0; i<numThreads; i++) {
if (GC_pthread_create(&th[i],
#if defined(__MINGW32__)
&attr,
#else
NULL,