1 /* context_java - implementation of context switching for java threads */
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include <xbt/function_types.h>
11 #include <simix/simix.h>
12 #include "smx_context_java.h"
13 #include "simix/process_private.h"
14 #include "xbt/dynar.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
18 static smx_context_t smx_ctx_java_self(void);
20 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
22 void_pfn_smxprocess_t cleanup_func,
25 static void smx_ctx_java_free(smx_context_t context);
26 static void smx_ctx_java_start(smx_context_t context);
27 static void smx_ctx_java_stop(smx_context_t context);
28 static void smx_ctx_java_suspend(smx_context_t context);
29 static void smx_ctx_java_resume(smx_context_t new_context);
30 static void smx_ctx_java_runall(xbt_dynar_t processes);
31 static void* smx_ctx_java_get_data(smx_context_t context);
33 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
35 /* instantiate the context factory */
36 smx_ctx_base_factory_init(factory);
38 (*factory)->create_context = smx_ctx_java_factory_create_context;
39 /* Leave default behavior of (*factory)->finalize */
40 (*factory)->free = smx_ctx_java_free;
41 (*factory)->stop = smx_ctx_java_stop;
42 (*factory)->suspend = smx_ctx_java_suspend;
43 (*factory)->runall = smx_ctx_java_runall;
44 (*factory)->name = "ctx_java_factory";
45 //(*factory)->finalize = smx_ctx_base_factory_finalize;
46 (*factory)->self = smx_ctx_base_self;
47 (*factory)->get_data = smx_ctx_base_get_data;
48 (*factory)->get_thread_id = smx_ctx_base_get_thread_id;
51 static void* smx_ctx_java_get_data(smx_context_t context)
57 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
59 void_pfn_smxprocess_t cleanup_func,
62 fprintf(stderr,"XXXX Create Context\n");
63 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
65 /* If the user provided a function for the process then use it
66 otherwise is the context for maestro */
68 context->super.cleanup_func = cleanup_func;
69 context->jprocess = (jobject) code;
70 context->jenv = get_current_thread_env();
71 jprocess_start(((smx_ctx_java_t) context)->jprocess,
72 get_current_thread_env());
74 smx_current_context = (smx_context_t)context;
76 context->super.data = data;
78 return (smx_context_t) context;
81 static void smx_ctx_java_free(smx_context_t context)
84 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
86 if (ctx_java->jprocess) {
87 jobject jprocess = ctx_java->jprocess;
89 ctx_java->jprocess = NULL;
91 /* if the java process is alive join it */
92 if (jprocess_is_alive(jprocess, get_current_thread_env()))
93 jprocess_join(jprocess, get_current_thread_env());
97 smx_ctx_base_free(context);
100 static void smx_ctx_java_stop(smx_context_t context)
102 jobject jprocess = NULL;
103 fprintf(stderr,"XXXX Context Stop\n");
105 smx_ctx_java_t ctx_java;
107 if (context->cleanup_func)
108 (*(context->cleanup_func)) (context->data);
110 ctx_java = (smx_ctx_java_t) context;
112 /*FIXME: is this really necessary? Seems to. */
113 if (smx_current_context->iwannadie) {
115 /* The maestro call xbt_context_stop() with an exit code set to one */
116 if (ctx_java->jprocess) {
117 /* if the java process is alive schedule it */
118 if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
119 jprocess_schedule(smx_current_context);
120 jprocess = ctx_java->jprocess;
121 ctx_java->jprocess = NULL;
123 /* interrupt the java process */
124 jprocess_exit(jprocess, get_current_thread_env());
128 /* the java process exits */
129 jprocess = ctx_java->jprocess;
130 ctx_java->jprocess = NULL;
133 /* delete the global reference associated with the java process */
134 jprocess_delete_global_ref(jprocess, get_current_thread_env());
137 static void smx_ctx_java_suspend(smx_context_t context)
139 jprocess_unschedule(context);
142 // FIXME: inline those functions
143 static void smx_ctx_java_resume(smx_context_t new_context)
145 fprintf(stderr,"XXXX Context Resume\n");
146 jprocess_schedule(new_context);
149 static void smx_ctx_java_runall(xbt_dynar_t processes)
151 fprintf(stderr,"XXXX Run all\n");
152 printf("Affiche les %ld elements\n",xbt_dynar_length(processes));
153 smx_process_t process;
154 smx_context_t old_context;
157 xbt_dynar_foreach(processes, cursor, process) {
158 printf("process_name : %s\n",process->name);
159 old_context = smx_current_context;
160 smx_current_context = process->context;
161 smx_ctx_java_resume(smx_current_context);
162 smx_current_context = old_context;
164 xbt_dynar_reset(processes);
166 fprintf(stderr,"XXXX End of run all\n");