Skip to content

Commit 28261e2

Browse files
committed
Fix windows initialization of the jump buffer using pthread_once
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@17232 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent d599e36 commit 28261e2

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

Compiler/Template/CodegenC.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4564,6 +4564,7 @@ case FUNCTION(__) then
45644564
<%functionArguments |> var => '<%funArgDefinition(var)%>;' ;separator="\n"%>
45654565
<%if outVars then '<%retType%> out;'%>
45664566
<%functionArguments |> arg => readInVar(arg) ;separator="\n"%>
4567+
MMC_INIT();
45674568
MMC_TRY_TOP()
45684569
<%if outVars then "out = "%>omc_<%fname%>(<%functionArguments |> var => funArgName(var) ;separator=", "%>);
45694570
MMC_CATCH_TOP(return 1)
@@ -4894,6 +4895,7 @@ case efn as EXTERNAL_FUNCTION(__) then
48944895
<%funArgs |> VARIABLE(__) => '<%expTypeArrayIf(ty)%> <%contextCref(name,contextFunction)%>;' ;separator="\n"%>
48954896
<%if outVars then '<%retType%> out;'%>
48964897
<%funArgs |> arg as VARIABLE(__) => readInVar(arg) ;separator="\n"%>
4898+
MMC_INIT();
48974899
MMC_TRY_TOP()
48984900
<%if outVars then "out = "%>omc_<%fname%>(<%funArgs |> VARIABLE(__) => contextCref(name,contextFunction) ;separator=", "%>);
48994901
MMC_CATCH_TOP(return 1)

Compiler/runtime/System_omc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,8 @@ extern int System_forkAvailable()
715715
#endif
716716
}
717717

718-
static int System_forkCallJoin(int *statuses, int *ids, int *numWorking, int *working)
718+
#if 1
719+
static int System_forkCallJoin(int *statuses, int *ids, int *working)
719720
{
720721
while (1) {
721722
int status = 1, id, i;
@@ -730,7 +731,7 @@ static int System_forkCallJoin(int *statuses, int *ids, int *numWorking, int *wo
730731
} else {
731732
status = 0;
732733
}
733-
for (i=0; i<numWorking; i++) {
734+
for (i=0; 1; i++) {
734735
int idw = working[i];
735736
if (ids[idw] == id) {
736737
statuses[idw] = status;
@@ -741,7 +742,6 @@ static int System_forkCallJoin(int *statuses, int *ids, int *numWorking, int *wo
741742
}
742743
}
743744

744-
#if 1
745745
extern void* System_forkCall(int numThreads, void *dataLst, void (*fn)(void*))
746746
{
747747
#if defined(__MINGW32__) || defined(_MSC_VER)
@@ -764,7 +764,7 @@ extern void* System_forkCall(int numThreads, void *dataLst, void (*fn)(void*))
764764
int thisNum;
765765
/* Dynamic workload. Once we reach max number of threads, wait for one to finish. Else spawn new ones and join all at the end. */
766766
if (numWorking == numThreads) {
767-
thisNum = System_forkCallJoin(status, ids, &numWorking, working);
767+
thisNum = System_forkCallJoin(status, ids, working);
768768
} else {
769769
thisNum = numWorking++;
770770
}
@@ -787,7 +787,7 @@ extern void* System_forkCall(int numThreads, void *dataLst, void (*fn)(void*))
787787
}
788788
}
789789
while (numWorking) {
790-
System_forkCallJoin(status, ids, &numWorking, working);
790+
System_forkCallJoin(status, ids, working);
791791
numWorking--;
792792
}
793793
for (i=len-1; i>=0; i--) {

SimulationRuntime/c/meta/meta_modelica.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,3 +826,12 @@ void* boxptr_valueHashMod(void *p, void *mod)
826826
{
827827
return mmc_mk_icon(mmc_prim_hash(p) % (unsigned long) mmc_unbox_integer(mod));
828828
}
829+
830+
pthread_once_t mmc_init_once = PTHREAD_ONCE_INIT;
831+
void mmc_init()
832+
{
833+
pthread_key_create(&mmc_jumper,NULL);
834+
init_metamodelica_segv_handler();
835+
mmc_GC_init(mmc_GC_settings_default);
836+
MMC_INIT_STACK_OVERFLOW();
837+
}

SimulationRuntime/c/meta/meta_modelica.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,11 @@ struct record_description {
632632
#include <pthread.h>
633633

634634
void mmc_catch_dummy_fn();
635-
#if 1
636635

637636
extern pthread_key_t mmc_jumper;
638-
#define MMC_INIT() pthread_key_create(&mmc_jumper,NULL);init_metamodelica_segv_handler();mmc_GC_init(mmc_GC_settings_default);MMC_INIT_STACK_OVERFLOW();
637+
extern pthread_once_t mmc_init_once;
638+
extern void mmc_init();
639+
#define MMC_INIT() pthread_once(&mmc_init_once,mmc_init)
639640
#define MMC_TRY_INTERNAL(X) { jmp_buf new_mmc_jumper, *old_jumper; old_jumper = (jmp_buf*)pthread_getspecific(X); pthread_setspecific(X,&new_mmc_jumper); if (setjmp(new_mmc_jumper) == 0) {
640641
#define MMC_TRY() MMC_TRY_INTERNAL(mmc_jumper)
641642

@@ -652,8 +653,6 @@ extern pthread_key_t mmc_jumper;
652653
#define MMC_TRY_TOP() MMC_TRY()
653654
#define MMC_CATCH_TOP(X) pthread_setspecific(mmc_jumper,old_jumper);} else {pthread_setspecific(mmc_jumper,old_jumper);X;}}
654655

655-
#endif
656-
657656
#if defined(__cplusplus)
658657
}
659658
#endif

0 commit comments

Comments
 (0)