Skip to content

Commit

Permalink
- send threadData to dynamically loaded external function
Browse files Browse the repository at this point in the history
- do not fail in writeBuf if there is no buffer or there is no data
- update test BatchPlant_StandardWater


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@24304 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Jan 29, 2015
1 parent b68318f commit e10c650
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -5156,7 +5156,7 @@ template functionHeaderImpl(String fname, list<Variable> fargs, list<Variable> o
let inFnStr = if boolAnd(boxed,inFunc) then
<<
DLLExport
int in_<%fname%>(type_description * inArgs, type_description * outVar);
int in_<%fname%>(threadData_t *threadData, type_description * inArgs, type_description * outVar);
>>
match visibility
case PROTECTED(__) then
Expand Down Expand Up @@ -5663,13 +5663,13 @@ template generateInFunc(Text fname, list<Variable> functionArguments, list<Varia
::=
<<
DLLExport
int in_<%fname%>(type_description * inArgs, type_description * outVar)
int in_<%fname%>(threadData_t *threadData, type_description * inArgs, type_description * outVar)
{
if (!mmc_GC_state) mmc_GC_init();
//if (!mmc_GC_state) mmc_GC_init();
<%functionArguments |> var => '<%funArgDefinition(var)%>;' ;separator="\n"%>
<%outVars |> var => '<%funArgDefinition(var)%>;' ;separator="\n"%>
<%functionArguments |> arg => readInVar(arg) ;separator="\n"%>
MMC_TRY_TOP()
MMC_TRY_TOP_INTERNAL()
<%match outVars
case v::_ then '<%funArgName(v)%> = '
%>omc_<%fname%>(threadData<%functionArguments |> var => (", " + funArgName(var) )%><%List.restOrEmpty(outVars) |> var => (", &" + funArgName(var) )%>);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Util/DynLoad.mo
Expand Up @@ -47,7 +47,7 @@ of values."
input Boolean inPrintDebug;
output Values.Value outVal;

external "C" outVal=DynLoad_executeFunction(inFuncHandle,inValLst,inPrintDebug) annotation(Library = "omcruntime");
external "C" outVal=DynLoad_executeFunction(OpenModelica.threadData(),inFuncHandle,inValLst,inPrintDebug) annotation(Library = "omcruntime");
end executeFunction;

annotation(__OpenModelica_Interface="frontend");
Expand Down
13 changes: 8 additions & 5 deletions Compiler/runtime/Dynload.cpp
Expand Up @@ -43,8 +43,9 @@ extern "C" {

static void *type_desc_to_value(type_description *desc);
static int value_to_type_desc(void *value, type_description *desc);
static int execute_function(void *in_arg, void **out_arg,
int (* func)(type_description *,
static int execute_function(threadData_t* threadData, void *in_arg, void **out_arg,
int (* func)(threadData_t*,
type_description *,
type_description *), int printDebug);
static int parse_array(type_description *desc, void *arrdata, void *dimLst);
static void *value_to_mmc(void* value);
Expand Down Expand Up @@ -158,8 +159,10 @@ static int value_to_type_desc(void *value, type_description *desc)
return 0;
}

static int execute_function(void *in_arg, void **out_arg,
int (* func)(type_description *,
static int execute_function(threadData_t* threadData,
void *in_arg, void **out_arg,
int (* func)(threadData_t*,
type_description *,
type_description *), int printDebug)
{
type_description arglst[MMC_NUM_ARGS + 1], crashbuf[50], *arg = NULL;
Expand Down Expand Up @@ -199,7 +202,7 @@ static int execute_function(void *in_arg, void **out_arg,
fflush(stdout);
/* call our function pointer! */
try { /* Don't let external C functions throwing C++ exceptions kill OMC! */
retval = func(arglst, &retarg);
retval = func(threadData, arglst, &retarg);
} catch (...) {
retval = 1;
}
Expand Down
5 changes: 3 additions & 2 deletions Compiler/runtime/Dynload_omc.cpp
Expand Up @@ -43,20 +43,21 @@ extern "C" {
#include "Dynload.cpp"
#include "ModelicaUtilities.h"

extern void* DynLoad_executeFunction(int _inFuncHandle, void* _inValLst, int _inPrintDebug)
extern void* DynLoad_executeFunction(threadData_t* threadData, int _inFuncHandle, void* _inValLst, int _inPrintDebug)
{
modelica_ptr_t func = NULL;
int retval = -1;
void *retarg = NULL;
func = lookup_ptr(_inFuncHandle);
if (func == NULL) MMC_THROW();

retval = execute_function(_inValLst, &retarg, func->data.func.handle, _inPrintDebug);
retval = execute_function(threadData, _inValLst, &retarg, func->data.func.handle, _inPrintDebug);
if (retval) MMC_THROW();
return retarg;
}

extern void* omc_Absyn_pathString2(threadData_t*,void*,void*);

static const char* path_to_name(void* path, char del)
{
threadData_t *threadData = (threadData_t *) pthread_getspecific(mmc_thread_data_key);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/runtime/printimpl.c
Expand Up @@ -346,7 +346,7 @@ static int PrintImpl__writeBuf(threadData_t *threadData,const char* filename)
if (buf == NULL || buf[0]=='\0') {
/* nothing to write to file, just close it and return ! */
fclose(file);
return 1;
return 0;
}

/* write 1 element of size nfilled to file and check for errors */
Expand Down
2 changes: 1 addition & 1 deletion Compiler/runtime/systemimpl.h
Expand Up @@ -36,7 +36,7 @@ char* _replace(const char* source_str,
const char* search_str,
const char* replace_str);

typedef int (*function_t)(type_description*, type_description*);
typedef int (*function_t)(threadData_t*, type_description*, type_description*);

#if defined(_MSC_VER) /* no gettext for VS! */
#define gettext(str) str
Expand Down

0 comments on commit e10c650

Please sign in to comment.