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 <simgrid/simix.h>
12 #include "smx_context_java.h"
13 #include "xbt/dynar.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
17 static smx_context_t my_current_context = NULL;
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_suspend(smx_context_t context);
28 static void smx_ctx_java_resume(smx_context_t new_context);
29 static void smx_ctx_java_runall(void);
31 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
33 /* instantiate the context factory */
34 smx_ctx_base_factory_init(factory);
36 (*factory)->create_context = smx_ctx_java_factory_create_context;
37 /* Leave default behavior of (*factory)->finalize */
38 (*factory)->free = smx_ctx_java_free;
39 (*factory)->stop = smx_ctx_java_stop;
40 (*factory)->suspend = smx_ctx_java_suspend;
41 (*factory)->runall = smx_ctx_java_runall;
42 (*factory)->name = "ctx_java_factory";
43 //(*factory)->finalize = smx_ctx_base_factory_finalize;
44 (*factory)->self = smx_ctx_java_self;
45 (*factory)->get_data = smx_ctx_base_get_data;
48 smx_context_t smx_ctx_java_self(void)
50 return my_current_context;
54 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
56 void_pfn_smxprocess_t cleanup_func,
59 XBT_DEBUG("XXXX Create Context\n");
60 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
62 /* If the user provided a function for the process then use it
63 otherwise is the context for maestro */
65 context->super.cleanup_func = cleanup_func;
66 context->jprocess = (jobject) code;
67 context->jenv = get_current_thread_env();
68 jprocess_start(((smx_ctx_java_t) context)->jprocess,
69 get_current_thread_env());
72 my_current_context = (smx_context_t)context;
74 context->super.data = data;
76 return (smx_context_t) context;
79 static void smx_ctx_java_free(smx_context_t context)
82 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
84 if (ctx_java->jprocess) { /* the java process still exists */
85 jobject jprocess = ctx_java->jprocess;
86 ctx_java->jprocess = NULL;
89 XBT_DEBUG("The process still exists, making it exit now");
90 jprocess_exit(jprocess, get_current_thread_env());
92 /* it's dead now, remove it from the JVM */
93 jprocess_delete_global_ref(jprocess, get_current_thread_env());
97 smx_ctx_base_free(context);
101 void smx_ctx_java_stop(smx_context_t context)
103 xbt_assert(context == my_current_context,
104 "The context to stop must be the current one");
105 /* I am the current process and I am dying */
107 smx_ctx_base_stop(context);
109 XBT_DEBUG("I am dying");
111 /* suspend myself again, smx_ctx_java_free() will destroy me later
113 jprocess_unschedule(context);
114 XBT_DEBUG("Java stop finished");
117 static void smx_ctx_java_suspend(smx_context_t context)
119 jprocess_unschedule(context);
122 // FIXME: inline those functions
123 static void smx_ctx_java_resume(smx_context_t new_context)
125 XBT_DEBUG("XXXX Context Resume\n");
126 jprocess_schedule(new_context);
129 static void smx_ctx_java_runall(void)
131 xbt_dynar_t processes = SIMIX_process_get_runnable();
132 XBT_DEBUG("XXXX Run all\n");
133 smx_process_t process;
134 smx_context_t old_context;
136 xbt_dynar_foreach(processes, cursor, process) {
137 old_context = my_current_context;
138 my_current_context = SIMIX_process_get_context(process);
139 smx_ctx_java_resume(my_current_context);
140 my_current_context = old_context;
143 XBT_DEBUG("XXXX End of run all\n");