Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace, when possible, calls to bprintf() by calls to xbt_strdup().
[simgrid.git] / src / bindings / java / smx_context_java.c
1 /* context_java - implementation of context switching for java threads */
2
3 /* Copyright (c) 2009, 2010, 2012. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9
10 #include <xbt/function_types.h>
11 #include <simgrid/simix.h>
12 #include <xbt/ex.h>
13 #include "smx_context_java.h"
14 #include "jxbt_utilities.h"
15 #include "xbt/dynar.h"
16 extern JavaVM *__java_vm;
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
19
20 static smx_context_t
21 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
22                                     char **argv,
23                                     void_pfn_smxprocess_t cleanup_func,
24                                     void *data);
25
26 static void smx_ctx_java_free(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);
30 static void* smx_ctx_java_thread_run(void *data);
31 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
32 {
33   /* instantiate the context factory */
34   smx_ctx_base_factory_init(factory);
35
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;
46 }
47 smx_context_t smx_ctx_java_self(void)
48 {
49         return (smx_context_t)xbt_os_thread_get_extra_data();
50 }
51
52 static smx_context_t
53 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
54                                     char **argv,
55                                     void_pfn_smxprocess_t cleanup_func,
56                                     void* data)
57 {
58         xbt_ex_t e;
59         static int thread_amount=0;
60         smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
61         thread_amount++;
62   /* If the user provided a function for the process then use it
63      otherwise is the context for maestro */
64   if (code) {
65     if (argc == 0) {
66       context->jprocess = (jobject) code;
67     }
68     else {
69       context->jprocess = NULL;
70     }
71     context->super.cleanup_func = cleanup_func;
72     context->begin = xbt_os_sem_init(0);
73     context->end = xbt_os_sem_init(0);
74
75     context->super.argc = argc;
76     context->super.argv = argv;
77     context->super.code = code;
78
79     TRY {         
80        context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
81     } CATCH(e) {
82         RETHROWF("Failed to create context #%d. You may want to switch to Java coroutines to increase your limits (error: %s)."
83                  "See the Install section of simgrid-java documentation (in doc/install.html) for more on coroutines.",
84                         thread_amount);
85     }
86   }
87   else {
88         context->thread = NULL;
89     xbt_os_thread_set_extra_data(context);
90   }
91   context->super.data = data;
92   
93   return (smx_context_t) context;
94 }
95
96 static void* smx_ctx_java_thread_run(void *data) {
97   smx_ctx_java_t context = (smx_ctx_java_t)data;
98   xbt_os_thread_set_extra_data(context);
99   //Attach the thread to the JVM
100   JNIEnv *env;
101   jint error = (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
102   xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
103   context->jenv = get_current_thread_env();
104   //Wait for the first scheduling round to happen.
105   xbt_os_sem_acquire(context->begin);
106   //Create the "Process" object if needed.
107   if (context->super.argc > 0) {
108     (*(context->super.code))(context->super.argc, context->super.argv);
109   }
110   else {
111     smx_process_t process = SIMIX_process_self();
112     (*env)->SetLongField(env, context->jprocess, jprocess_field_Process_bind,
113                          (intptr_t)process);
114   }
115   xbt_assert((context->jprocess != NULL), "Process not created...");
116   //wait for the process to be able to begin
117   //TODO: Cache it
118         jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
119   jdouble startTime =  (*env)->GetDoubleField(env, context->jprocess, jprocess_field_Process_startTime);
120   if (startTime > MSG_get_clock()) {
121         MSG_process_sleep(startTime - MSG_get_clock());
122   }
123   //Execution of the "run" method.
124   jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
125   xbt_assert( (id != NULL), "Method not found...");
126   (*env)->CallVoidMethod(env, context->jprocess, id);
127   smx_ctx_java_stop((smx_context_t)context);
128
129   return NULL;
130 }
131
132 static void smx_ctx_java_free(smx_context_t context)
133 {
134   if (context) {
135     smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
136     if (ctx_java->thread) { /* We are not in maestro context */
137       xbt_os_thread_join(ctx_java->thread, NULL);
138       xbt_os_sem_destroy(ctx_java->begin);
139       xbt_os_sem_destroy(ctx_java->end);
140     }
141   }
142   smx_ctx_base_free(context);
143 }
144
145
146 void smx_ctx_java_stop(smx_context_t context)
147 {
148   smx_ctx_java_t ctx_java = (smx_ctx_java_t)context;
149   /* I am the current process and I am dying */
150   if (context->iwannadie) {
151     context->iwannadie = 0;
152     JNIEnv *env = get_current_thread_env();
153     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", xbt_strdup("Process killed :)"));
154     THROWF(cancel_error, 0, "process cancelled");
155   } else {
156     smx_ctx_base_stop(context);
157     /* detach the thread and kills it */
158     JNIEnv *env = ctx_java->jenv;
159     (*env)->DeleteGlobalRef(env,ctx_java->jprocess);
160     jint error = (*__java_vm)->DetachCurrentThread(__java_vm);
161     xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
162     xbt_os_sem_release(((smx_ctx_java_t)context)->end);
163     xbt_os_thread_exit(NULL);
164   }
165 }
166
167 static void smx_ctx_java_suspend(smx_context_t context)
168 {
169   smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
170   xbt_os_sem_release(ctx_java->end);
171   xbt_os_sem_acquire(ctx_java->begin);
172 }
173
174 // FIXME: inline those functions
175 static void smx_ctx_java_resume(smx_context_t new_context)
176 {
177   smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
178   xbt_os_sem_release(ctx_java->begin);
179   xbt_os_sem_acquire(ctx_java->end);
180 }
181
182 static void smx_ctx_java_runall(void)
183 {
184   xbt_dynar_t processes = SIMIX_process_get_runnable();
185   smx_process_t process;
186   unsigned int cursor;
187   xbt_dynar_foreach(processes, cursor, process) {
188     smx_ctx_java_resume(SIMIX_process_get_context(process));
189   }
190 }