Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move SIMIX comm requests to the top of the big switch
[simgrid.git] / src / simix / smx_context_thread.c
1 /* context_thread - implementation of context switching with native 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 #include "xbt/function_types.h"
10 #include "private.h"
11 #include "portable.h"           /* loads context system definitions */
12 #include "xbt/swag.h"
13 #include "xbt/xbt_os_thread.h"
14 #include "xbt_modinter.h"       /* prototype of os thread module's init/exit in XBT */
15 #include "simix/context.h"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
18
19 int smx_parallel_contexts = 0;
20
21 typedef struct s_smx_ctx_thread {
22   s_smx_ctx_base_t super;       /* Fields of super implementation */
23   xbt_os_thread_t thread;       /* a plain dumb thread (portable to posix or windows) */
24   xbt_os_sem_t begin;           /* this semaphore is used to schedule/yield the process  */
25   xbt_os_sem_t end;             /* this semaphore is used to schedule/unschedule the process   */
26 } s_smx_ctx_thread_t, *smx_ctx_thread_t;
27
28 static smx_context_t
29 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
30                                       char **argv,
31                                       void_pfn_smxprocess_t cleanup_func,
32                                       void *data);
33
34 static void smx_ctx_thread_free(smx_context_t context);
35 static void smx_ctx_thread_stop(smx_context_t context);
36 static void smx_ctx_thread_suspend(smx_context_t context);
37 static void smx_ctx_thread_resume(smx_context_t new_context);
38 static void smx_ctx_thread_runall_serial(xbt_dynar_t processes);
39 static void smx_ctx_thread_runall_parallel(xbt_dynar_t processes);
40 static smx_context_t smx_ctx_thread_self(void);
41
42 static void *smx_ctx_thread_wrapper(void *param);
43
44 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
45 {
46   smx_ctx_base_factory_init(factory);
47   VERB0("Activating thread context factory");
48
49   (*factory)->create_context = smx_ctx_thread_factory_create_context;
50   /* Do not overload that method (*factory)->finalize */
51   (*factory)->free = smx_ctx_thread_free;
52   (*factory)->stop = smx_ctx_thread_stop;
53   (*factory)->suspend = smx_ctx_thread_suspend;
54
55   if (smx_parallel_contexts)
56     (*factory)->runall = smx_ctx_thread_runall_parallel;
57   else
58     (*factory)->runall = smx_ctx_thread_runall_serial;
59
60   (*factory)->self = smx_ctx_thread_self;
61   (*factory)->name = "ctx_thread_factory";
62 }
63
64 static smx_context_t
65 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
66                                       char **argv,
67                                       void_pfn_smxprocess_t cleanup_func,
68                                       void *data)
69 {
70   smx_ctx_thread_t context = (smx_ctx_thread_t)
71       smx_ctx_base_factory_create_context_sized(sizeof(s_smx_ctx_thread_t),
72                                                 code, argc, argv,
73                                                 cleanup_func, data);
74
75   /* If the user provided a function for the process then use it
76      otherwise is the context for maestro */
77   if (code) {
78     context->begin = xbt_os_sem_init(0);
79     context->end = xbt_os_sem_init(0);
80     /* create and start the process */
81     /* NOTE: The first argument to xbt_os_thread_create used to be the process *
82     * name, but now the name is stored at SIMIX level, so we pass a null  */
83     context->thread =
84       xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, context, context);
85
86     /* wait the starting of the newly created process */
87     xbt_os_sem_acquire(context->end);
88
89   } else {
90     xbt_os_thread_set_extra_data(context);
91   }
92
93   return (smx_context_t) context;
94 }
95
96 static void smx_ctx_thread_free(smx_context_t pcontext)
97 {
98   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
99
100   /* check if this is the context of maestro (it doesn't has a real thread) */
101   if (context->thread) {
102     /* wait about the thread terminason */
103     xbt_os_thread_join(context->thread, NULL);
104
105     /* destroy the synchronisation objects */
106     xbt_os_sem_destroy(context->begin);
107     xbt_os_sem_destroy(context->end);
108   }
109
110   smx_ctx_base_free(pcontext);
111 }
112
113 static void smx_ctx_thread_stop(smx_context_t pcontext)
114 {
115   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
116
117   /* please no debug here: our procdata was already free'd */
118   smx_ctx_base_stop(pcontext);
119
120   /* signal to the maestro that it has finished */
121   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
122
123   /* exit */
124   /* We should provide return value in case other wants it */
125   xbt_os_thread_exit(NULL);
126 }
127
128 static void *smx_ctx_thread_wrapper(void *param)
129 {
130   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
131
132   /* Tell the maestro we are starting, and wait for its green light */
133   xbt_os_sem_release(context->end);
134   xbt_os_sem_acquire(context->begin);
135
136   (context->super.code) (context->super.argc, context->super.argv);
137
138   smx_ctx_thread_stop((smx_context_t) context);
139   return NULL;
140 }
141
142 static void smx_ctx_thread_suspend(smx_context_t context)
143 {
144   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
145   xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
146 }
147
148 static void smx_ctx_thread_runall_serial(xbt_dynar_t processes)
149 {
150   smx_process_t process;
151   unsigned int cursor;
152
153   xbt_dynar_foreach(processes, cursor, process) {
154     xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
155     xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
156   }
157   xbt_dynar_reset(processes);
158 }
159
160 static void smx_ctx_thread_runall_parallel(xbt_dynar_t processes)
161 {
162   unsigned int index;
163   smx_process_t process;
164
165   xbt_dynar_foreach(processes, index, process)
166     xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
167
168   xbt_dynar_foreach(processes, index, process) {
169      xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
170   }
171   xbt_dynar_reset(processes);
172 }
173
174 static smx_context_t smx_ctx_thread_self(void)
175 {
176   return (smx_context_t) xbt_os_thread_get_extra_data();
177 }