Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Huge commit for java with simgrid3_5
[simgrid.git] / src / smx_context_java.c
1 /* context_java - implementation of context switching for java threads */
2
3 /* Copyright (c) 2009, 2010. 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 <simix/simix.h>
12 #include "smx_context_java.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
15
16 static smx_context_t
17 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
18                                     char **argv,
19                                     void_pfn_smxprocess_t cleanup_func,
20                                     void *data);
21
22 static void smx_ctx_java_free(smx_context_t context);
23 static void smx_ctx_java_start(smx_context_t context);
24 static void smx_ctx_java_stop(smx_context_t context);
25 static void smx_ctx_java_suspend(smx_context_t context);
26 static void smx_ctx_java_resume(smx_context_t new_context);
27 static void smx_ctx_java_runall(xbt_swag_t processes);
28
29 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
30 {
31   /* instantiate the context factory */
32   smx_ctx_base_factory_init(factory);
33
34   (*factory)->create_context = smx_ctx_java_factory_create_context;
35   /* Leave default behavior of (*factory)->finalize */
36   (*factory)->free = smx_ctx_java_free;
37   (*factory)->stop = smx_ctx_java_stop;
38   (*factory)->suspend = smx_ctx_java_suspend;
39   (*factory)->runall = (smx_pfn_context_runall_t)smx_ctx_java_runall;
40   (*factory)->name = "ctx_java_factory";
41 }
42
43 static smx_context_t
44 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
45                                     char **argv,
46                                     void_pfn_smxprocess_t cleanup_func,
47                                     void* data)
48 {
49   fprintf(stderr,"XXXX Create Context\n");
50   smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
51
52   /* If the user provided a function for the process then use it
53      otherwise is the context for maestro */
54   if (code) {
55     context->super.cleanup_func = cleanup_func;
56     context->jprocess = (jobject) code;
57     context->jenv = get_current_thread_env();
58     jprocess_start(((smx_ctx_java_t) context)->jprocess,
59                    get_current_thread_env());
60   }else{
61     smx_current_context = (smx_context_t)context;
62   }
63   context->super.data = data;
64   
65   return (smx_context_t) context;
66 }
67
68 static void smx_ctx_java_free(smx_context_t context)
69 {
70   if (context) {
71     smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
72
73     if (ctx_java->jprocess) {
74       jobject jprocess = ctx_java->jprocess;
75
76       ctx_java->jprocess = NULL;
77
78       /* if the java process is alive join it */
79       if (jprocess_is_alive(jprocess, get_current_thread_env()))
80         jprocess_join(jprocess, get_current_thread_env());
81     }
82   }
83
84   smx_ctx_base_free(context);
85 }
86
87 static void smx_ctx_java_stop(smx_context_t context)
88 {
89   jobject jprocess = NULL;
90   fprintf(stderr,"XXXX Context Stop\n");
91
92   smx_ctx_java_t ctx_java;
93
94   if (context->cleanup_func)
95     (*(context->cleanup_func)) (context->data);
96
97   ctx_java = (smx_ctx_java_t) context;
98
99   /*FIXME: is this really necessary? Seems to. */
100   if (smx_current_context->iwannadie) {
101     INFO0("I wannadie");
102     /* The maestro call xbt_context_stop() with an exit code set to one */
103     if (ctx_java->jprocess) {
104       /* if the java process is alive schedule it */
105       if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
106         jprocess_schedule(smx_current_context);
107         jprocess = ctx_java->jprocess;
108         ctx_java->jprocess = NULL;
109
110         /* interrupt the java process */
111         jprocess_exit(jprocess, get_current_thread_env());
112       }
113     }
114   } else {
115     /* the java process exits */
116     jprocess = ctx_java->jprocess;
117     ctx_java->jprocess = NULL;
118   }
119
120   /* delete the global reference associated with the java process */
121   jprocess_delete_global_ref(jprocess, get_current_thread_env());
122 }
123
124 static void smx_ctx_java_suspend(smx_context_t context)
125 {
126   jprocess_unschedule(context);
127 }
128
129 // FIXME: inline those functions
130 static void smx_ctx_java_resume(smx_context_t new_context)
131 {
132   fprintf(stderr,"XXXX Context Resume\n");
133   jprocess_schedule(new_context);
134 }
135
136 static void smx_ctx_java_runall(xbt_swag_t processes)
137 {
138   smx_process_t process;
139   smx_context_t old_context;
140   fprintf(stderr,"XXXX Run all\n");
141
142   while ((process = xbt_swag_extract(processes))) {
143     old_context = smx_current_context;
144     smx_current_context = SIMIX_process_get_context(process);
145     smx_ctx_java_resume(smx_current_context);
146     smx_current_context = old_context;
147   }
148   fprintf(stderr,"XXXX End of run all\n");
149 }