Skip to content

Commit

Permalink
Fix windows initialization of the jump buffer using pthread_once
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@17232 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Sep 13, 2013
1 parent d599e36 commit 28261e2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4564,6 +4564,7 @@ case FUNCTION(__) then
<%functionArguments |> var => '<%funArgDefinition(var)%>;' ;separator="\n"%>
<%if outVars then '<%retType%> out;'%>
<%functionArguments |> arg => readInVar(arg) ;separator="\n"%>
MMC_INIT();
MMC_TRY_TOP()
<%if outVars then "out = "%>omc_<%fname%>(<%functionArguments |> var => funArgName(var) ;separator=", "%>);
MMC_CATCH_TOP(return 1)
Expand Down Expand Up @@ -4894,6 +4895,7 @@ case efn as EXTERNAL_FUNCTION(__) then
<%funArgs |> VARIABLE(__) => '<%expTypeArrayIf(ty)%> <%contextCref(name,contextFunction)%>;' ;separator="\n"%>
<%if outVars then '<%retType%> out;'%>
<%funArgs |> arg as VARIABLE(__) => readInVar(arg) ;separator="\n"%>
MMC_INIT();
MMC_TRY_TOP()
<%if outVars then "out = "%>omc_<%fname%>(<%funArgs |> VARIABLE(__) => contextCref(name,contextFunction) ;separator=", "%>);
MMC_CATCH_TOP(return 1)
Expand Down
10 changes: 5 additions & 5 deletions Compiler/runtime/System_omc.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,8 @@ extern int System_forkAvailable()
#endif
}

static int System_forkCallJoin(int *statuses, int *ids, int *numWorking, int *working)
#if 1
static int System_forkCallJoin(int *statuses, int *ids, int *working)
{
while (1) {
int status = 1, id, i;
Expand All @@ -730,7 +731,7 @@ static int System_forkCallJoin(int *statuses, int *ids, int *numWorking, int *wo
} else {
status = 0;
}
for (i=0; i<numWorking; i++) {
for (i=0; 1; i++) {
int idw = working[i];
if (ids[idw] == id) {
statuses[idw] = status;
Expand All @@ -741,7 +742,6 @@ static int System_forkCallJoin(int *statuses, int *ids, int *numWorking, int *wo
}
}

#if 1
extern void* System_forkCall(int numThreads, void *dataLst, void (*fn)(void*))
{
#if defined(__MINGW32__) || defined(_MSC_VER)
Expand All @@ -764,7 +764,7 @@ extern void* System_forkCall(int numThreads, void *dataLst, void (*fn)(void*))
int thisNum;
/* Dynamic workload. Once we reach max number of threads, wait for one to finish. Else spawn new ones and join all at the end. */
if (numWorking == numThreads) {
thisNum = System_forkCallJoin(status, ids, &numWorking, working);
thisNum = System_forkCallJoin(status, ids, working);
} else {
thisNum = numWorking++;
}
Expand All @@ -787,7 +787,7 @@ extern void* System_forkCall(int numThreads, void *dataLst, void (*fn)(void*))
}
}
while (numWorking) {
System_forkCallJoin(status, ids, &numWorking, working);
System_forkCallJoin(status, ids, working);
numWorking--;
}
for (i=len-1; i>=0; i--) {
Expand Down
9 changes: 9 additions & 0 deletions SimulationRuntime/c/meta/meta_modelica.c
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,12 @@ void* boxptr_valueHashMod(void *p, void *mod)
{
return mmc_mk_icon(mmc_prim_hash(p) % (unsigned long) mmc_unbox_integer(mod));
}

pthread_once_t mmc_init_once = PTHREAD_ONCE_INIT;
void mmc_init()
{
pthread_key_create(&mmc_jumper,NULL);
init_metamodelica_segv_handler();
mmc_GC_init(mmc_GC_settings_default);
MMC_INIT_STACK_OVERFLOW();
}
7 changes: 3 additions & 4 deletions SimulationRuntime/c/meta/meta_modelica.h
Original file line number Diff line number Diff line change
Expand Up @@ -632,10 +632,11 @@ struct record_description {
#include <pthread.h>

void mmc_catch_dummy_fn();
#if 1

extern pthread_key_t mmc_jumper;
#define MMC_INIT() pthread_key_create(&mmc_jumper,NULL);init_metamodelica_segv_handler();mmc_GC_init(mmc_GC_settings_default);MMC_INIT_STACK_OVERFLOW();
extern pthread_once_t mmc_init_once;
extern void mmc_init();
#define MMC_INIT() pthread_once(&mmc_init_once,mmc_init)
#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) {
#define MMC_TRY() MMC_TRY_INTERNAL(mmc_jumper)

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

#endif

#if defined(__cplusplus)
}
#endif
Expand Down

0 comments on commit 28261e2

Please sign in to comment.