Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix build warnings on 32bits archs.
[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_start(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(void);
31 static void* smx_ctx_java_thread_run(void *data);
32 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
33 {
34   /* instantiate the context factory */
35   smx_ctx_base_factory_init(factory);
36
37   (*factory)->create_context = smx_ctx_java_factory_create_context;
38   /* Leave default behavior of (*factory)->finalize */
39   (*factory)->free = smx_ctx_java_free;
40   (*factory)->stop = smx_ctx_java_stop;
41   (*factory)->suspend = smx_ctx_java_suspend;
42   (*factory)->runall = smx_ctx_java_runall;
43   (*factory)->name = "ctx_java_factory";
44   //(*factory)->finalize = smx_ctx_base_factory_finalize;
45   (*factory)->self = smx_ctx_java_self;
46   (*factory)->get_data = smx_ctx_base_get_data;
47 }
48 smx_context_t smx_ctx_java_self(void)
49 {
50         return (smx_context_t)xbt_os_thread_get_extra_data();
51 }
52
53 static smx_context_t
54 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
55                                     char **argv,
56                                     void_pfn_smxprocess_t cleanup_func,
57                                     void* data)
58 {
59         xbt_ex_t e;
60         static int thread_amount=0;
61         smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
62         thread_amount++;
63   /* If the user provided a function for the process then use it
64      otherwise is the context for maestro */
65   if (code) {
66     if (argc == 0) {
67       context->jprocess = (jobject) code;
68     }
69     else {
70       context->jprocess = NULL;
71     }
72     context->super.cleanup_func = cleanup_func;
73     context->begin = xbt_os_sem_init(0);
74     context->end = xbt_os_sem_init(0);
75
76     context->super.argc = argc;
77     context->super.argv = argv;
78     context->super.code = code;
79
80     TRY {         
81        context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
82     } CATCH(e) {
83         RETHROWF("Failed to create context #%d. You may want to switch to Java coroutines to increase your limits (error: %s)."
84                  "See the Install section of simgrid-java documentation (in doc/install.html) for more on coroutines.",
85                         thread_amount);
86     }
87   }
88   else {
89         context->thread = NULL;
90     xbt_os_thread_set_extra_data(context);
91   }
92   context->super.data = data;
93   
94   return (smx_context_t) context;
95 }
96
97 static void* smx_ctx_java_thread_run(void *data) {
98   smx_ctx_java_t context = (smx_ctx_java_t)data;
99   xbt_os_thread_set_extra_data(context);
100   //Attach the thread to the JVM
101   JNIEnv *env;
102   jint error = (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
103   xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
104   context->jenv = get_current_thread_env();
105   //Wait for the first scheduling round to happen.
106   xbt_os_sem_acquire(context->begin);
107   //Create the "Process" object if needed.
108   if (context->super.argc > 0) {
109     (*(context->super.code))(context->super.argc, context->super.argv);
110   }
111   else {
112     smx_process_t process = SIMIX_process_self();
113     (*env)->SetLongField(env, context->jprocess, jprocess_field_Process_bind,
114                          (intptr_t)process);
115   }
116   xbt_assert((context->jprocess != NULL), "Process not created...");
117   //wait for the process to be able to begin
118   //TODO: Cache it
119         jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
120   jdouble startTime =  (*env)->GetDoubleField(env, context->jprocess, jprocess_field_Process_startTime);
121   if (startTime > MSG_get_clock()) {
122         MSG_process_sleep(startTime - MSG_get_clock());
123   }
124   //Execution of the "run" method.
125   jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
126   xbt_assert( (id != NULL), "Method not found...");
127   (*env)->CallVoidMethod(env, context->jprocess, id);
128   smx_ctx_java_stop((smx_context_t)context);
129
130   return NULL;
131 }
132
133 static void smx_ctx_java_free(smx_context_t context)
134 {
135   if (context) {
136     smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
137     if (ctx_java->thread) { /* We are not in maestro context */
138       xbt_os_thread_join(ctx_java->thread, NULL);
139       xbt_os_sem_destroy(ctx_java->begin);
140       xbt_os_sem_destroy(ctx_java->end);
141     }
142   }
143   smx_ctx_base_free(context);
144 }
145
146
147 void smx_ctx_java_stop(smx_context_t context)
148 {
149   smx_ctx_java_t ctx_java = (smx_ctx_java_t)context;
150   /* I am the current process and I am dying */
151   if (context->iwannadie) {
152         context->iwannadie = 0;
153         JNIEnv *env = get_current_thread_env();
154         jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process killed :)"));
155         THROWF(cancel_error, 0, "process cancelled");
156   }
157   else {
158     smx_ctx_base_stop(context);
159     /* detach the thread and kills it */
160     JNIEnv *env = ctx_java->jenv;
161     (*env)->DeleteGlobalRef(env,ctx_java->jprocess);
162     jint error = (*__java_vm)->DetachCurrentThread(__java_vm);
163     xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
164     xbt_os_sem_release(((smx_ctx_java_t)context)->end);
165     xbt_os_thread_exit(NULL);
166   }
167 }
168
169 static void smx_ctx_java_suspend(smx_context_t context)
170 {
171   smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
172   xbt_os_sem_release(ctx_java->end);
173   xbt_os_sem_acquire(ctx_java->begin);
174 }
175
176 // FIXME: inline those functions
177 static void smx_ctx_java_resume(smx_context_t new_context)
178 {
179   smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
180   xbt_os_sem_release(ctx_java->begin);
181   xbt_os_sem_acquire(ctx_java->end);
182 }
183
184 static void smx_ctx_java_runall(void)
185 {
186   xbt_dynar_t processes = SIMIX_process_get_runnable();
187   smx_process_t process;
188   unsigned int cursor;
189   xbt_dynar_foreach(processes, cursor, process) {
190     smx_ctx_java_resume(SIMIX_process_get_context(process));
191   }
192 }