Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce the coupling between main library and ruby bindings
[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
12 #include "portable.h"           /* loads context system definitions */
13 #include "xbt/swag.h"
14 #include "xbt/xbt_os_thread.h"
15 #include "xbt_modinter.h"       /* prototype of os thread module's init/exit in XBT */
16 #include "simix/smx_context_private.h"
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
19
20 typedef struct s_smx_ctx_thread {
21   s_smx_ctx_base_t super;       /* Fields of super implementation */
22   xbt_os_thread_t thread;       /* a plain dumb thread (portable to posix or windows) */
23   xbt_os_sem_t begin;           /* this semaphore is used to schedule/yield the process  */
24   xbt_os_sem_t end;             /* this semaphore is used to schedule/unschedule the process   */
25 } s_smx_ctx_thread_t, *smx_ctx_thread_t;
26
27 static smx_context_t
28 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
29                                       char **argv,
30                                       void_f_pvoid_t cleanup_func,
31                                       void *cleanup_arg);
32
33 static void smx_ctx_thread_free(smx_context_t context);
34 static void smx_ctx_thread_stop(smx_context_t context);
35 static void smx_ctx_thread_suspend(smx_context_t context);
36 static void smx_ctx_thread_resume(smx_context_t new_context);
37
38 static void *smx_ctx_thread_wrapper(void *param);
39
40 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
41 {
42
43   smx_ctx_base_factory_init(factory);
44
45   (*factory)->create_context = smx_ctx_thread_factory_create_context;
46   /* Do not overload that method (*factory)->finalize */
47   (*factory)->free = smx_ctx_thread_free;
48   (*factory)->stop = smx_ctx_thread_stop;
49   (*factory)->suspend = smx_ctx_thread_suspend;
50   (*factory)->resume = smx_ctx_thread_resume;
51   (*factory)->name = "ctx_thread_factory";
52 }
53
54 static smx_context_t
55 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
56                                       char **argv,
57                                       void_f_pvoid_t cleanup_func,
58                                       void *cleanup_arg)
59 {
60   smx_ctx_thread_t context = (smx_ctx_thread_t)
61       smx_ctx_base_factory_create_context_sized(sizeof(s_smx_ctx_thread_t),
62                                                 code, argc, argv,
63                                                 cleanup_func, cleanup_arg);
64
65   /* If the user provided a function for the process then use it
66      otherwise is the context for maestro */
67   if (code) {
68     context->begin = xbt_os_sem_init(0);
69     context->end = xbt_os_sem_init(0);
70
71
72     /* create and start the process */
73     /* NOTE: The first argument to xbt_os_thread_create used to be the process *
74      * name, but now the name is stored at SIMIX level, so we pass a null      */
75     context->thread =
76         xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, context);
77
78     /* wait the starting of the newly created process */
79     xbt_os_sem_acquire(context->end);
80   }
81
82   return (smx_context_t) context;
83 }
84
85 static void smx_ctx_thread_free(smx_context_t pcontext)
86 {
87   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
88
89   /* check if this is the context of maestro (it doesn't has a real thread) */
90   if (context->thread) {
91     /* wait about the thread terminason */
92     xbt_os_thread_join(context->thread, NULL);
93
94     /* destroy the synchronisation objects */
95     xbt_os_sem_destroy(context->begin);
96     xbt_os_sem_destroy(context->end);
97   }
98
99   smx_ctx_base_free(pcontext);
100 }
101
102 static void smx_ctx_thread_stop(smx_context_t pcontext)
103 {
104
105   smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
106
107   /* please no debug here: our procdata was already free'd */
108   smx_ctx_base_stop(pcontext);
109
110   /* signal to the maestro that it has finished */
111   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
112
113   /* exit */
114   /* We should provide return value in case other wants it */
115   xbt_os_thread_exit(NULL);
116 }
117
118 static void *smx_ctx_thread_wrapper(void *param)
119 {
120   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
121
122   /* Tell the maestro we are starting, and wait for its green light */
123   xbt_os_sem_release(context->end);
124   xbt_os_sem_acquire(context->begin);
125
126   (context->super.code) (context->super.argc, context->super.argv);
127
128   smx_ctx_thread_stop((smx_context_t) context);
129   return NULL;
130 }
131
132 static void smx_ctx_thread_suspend(smx_context_t context)
133 {
134   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
135   xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
136 }
137
138 static void smx_ctx_thread_resume(smx_context_t new_context)
139 {
140   xbt_os_sem_release(((smx_ctx_thread_t) new_context)->begin);
141   xbt_os_sem_acquire(((smx_ctx_thread_t) new_context)->end);
142 }