Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New simplified API for the context factory [Cristian]
[simgrid.git] / src / simix / smx_context_thread.c
1 /* $Id$ */
2
3 /* context_thread - implementation of context switching with native threads */
4
5 /* Copyright (c) 2004-2008 the SimGrid team. All right reserved */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/function_types.h"
11 #include "private.h"
12
13 #include "portable.h"           /* loads context system definitions */
14 #include "xbt/swag.h"
15 #include "xbt/xbt_os_thread.h"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smx_context);
18
19 typedef struct s_smx_ctx_thread {
20   SMX_CTX_BASE_T;
21   xbt_os_thread_t thread;       /* a plain dumb thread (portable to posix or windows) */
22   xbt_os_sem_t begin;           /* this semaphore is used to schedule/yield the process  */
23   xbt_os_sem_t end;             /* this semaphore is used to schedule/unschedule the process   */
24 } s_smx_ctx_thread_t, *smx_ctx_thread_t;
25
26 static smx_context_t smx_ctx_thread_factory_create_context(xbt_main_func_t code);
27
28 static smx_context_t smx_ctx_thread_factory_create_master_context(void);
29
30 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory);
31
32 static void smx_ctx_thread_free(smx_context_t context);
33
34 static void smx_ctx_thread_start(smx_context_t context);
35
36 static void smx_ctx_thread_stop(int exit_code);
37
38 static void smx_ctx_thread_suspend(smx_context_t context);
39
40 static void smx_ctx_thread_resume(smx_context_t old_context,
41                                   smx_context_t new_context);
42
43 static void *smx_ctx_thread_wrapper(void *param);
44
45 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
46 {
47   *factory = xbt_new0(s_smx_context_factory_t, 1);
48
49   (*factory)->create_context = smx_ctx_thread_factory_create_context;
50   (*factory)->finalize = smx_ctx_thread_factory_finalize;
51   (*factory)->create_maestro_context = smx_ctx_thread_factory_create_master_context;
52   (*factory)->free = smx_ctx_thread_free;
53   (*factory)->start = smx_ctx_thread_start;
54   (*factory)->stop = smx_ctx_thread_stop;
55   (*factory)->suspend = smx_ctx_thread_suspend;
56   (*factory)->resume = smx_ctx_thread_resume;
57   (*factory)->name = "ctx_thread_factory";
58 }
59
60 static smx_context_t smx_ctx_thread_factory_create_master_context(void)
61 {
62   return (smx_context_t) xbt_new0(s_smx_ctx_thread_t, 1);
63 }
64
65 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory)
66 {
67   free(*factory);
68   *factory = NULL;
69   return 0;
70 }
71
72 static smx_context_t smx_ctx_thread_factory_create_context(xbt_main_func_t code)
73 {
74   smx_ctx_thread_t context = xbt_new0(s_smx_ctx_thread_t, 1);
75
76   context->code = code;
77   context->begin = xbt_os_sem_init(0);
78   context->end = xbt_os_sem_init(0);
79
80   return (smx_context_t)context;
81 }
82
83 static void smx_ctx_thread_free(smx_context_t pcontext)
84 {
85   smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
86
87   /* Check if this is the context of maestro (it doesn't has a real thread) */  
88   if (context->thread) {
89     /* wait about the thread terminason */
90     xbt_os_thread_join(context->thread, NULL);
91     free(context->thread);
92     
93     /* destroy the synchronisation objects */
94     xbt_os_sem_destroy(context->begin);
95     xbt_os_sem_destroy(context->end);
96   }
97     
98   /* finally destroy the context */
99   free(context);
100 }
101
102 static void smx_ctx_thread_start(smx_context_t context)
103 {
104   smx_ctx_thread_t ctx_thread = (smx_ctx_thread_t)context;
105
106   /* create and start the process */
107   /* NOTE: The first argument to xbt_os_thread_create used to be the process *
108    * name, but now the name is stored at simix level, so we pass a null      */
109   ctx_thread->thread =
110     xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, ctx_thread);
111
112   /* wait the starting of the newly created process */
113   xbt_os_sem_acquire(ctx_thread->end);
114 }
115
116 static void smx_ctx_thread_stop(int exit_code)
117 {
118   /* please no debug here: our procdata was already free'd */
119   if (simix_global->current_process->cleanup_func)
120     (*simix_global->current_process->cleanup_func) (simix_global->current_process->cleanup_arg);
121
122   /* signal to the maestro that it has finished */
123   xbt_os_sem_release(((smx_ctx_thread_t) simix_global->current_process->context)->end);
124
125   /* exit */
126   xbt_os_thread_exit(NULL);     /* We should provide return value in case other wants it */
127 }
128
129 static void *smx_ctx_thread_wrapper(void *param)
130 {
131   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
132
133   /* Tell the maestro we are starting, and wait for its green light */
134   xbt_os_sem_release(context->end);
135   xbt_os_sem_acquire(context->begin);
136
137   smx_ctx_thread_stop((context->code) (simix_global->current_process->argc, 
138                                        simix_global->current_process->argv));
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_resume(smx_context_t not_used, 
149                                   smx_context_t new_context)
150 {
151   xbt_os_sem_release(((smx_ctx_thread_t) new_context)->begin);
152   xbt_os_sem_acquire(((smx_ctx_thread_t) new_context)->end);
153 }