Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill old $Id$ command dating from CVS
[simgrid.git] / src / simix / smx_context_thread.c
1 /* context_thread - implementation of context switching with native threads */
2
3 /* Copyright (c) 2004-2008 the SimGrid team. All right reserved */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/function_types.h"
9 #include "private.h"
10
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/smx_context_private.h"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
18
19 typedef struct s_smx_ctx_thread {
20   s_smx_ctx_base_t super;       /* Fields of super implementation */
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
27 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
28                                       void_f_pvoid_t cleanup_func, void* cleanup_arg);
29
30 static void smx_ctx_thread_free(smx_context_t context);
31 static void smx_ctx_thread_stop(smx_context_t context);
32 static void smx_ctx_thread_suspend(smx_context_t context);
33 static void smx_ctx_thread_resume(smx_context_t new_context);
34
35 static void *smx_ctx_thread_wrapper(void *param);
36
37 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory) {
38
39   smx_ctx_base_factory_init(factory);
40
41   (*factory)->create_context = smx_ctx_thread_factory_create_context;
42   /* Do not overload that method (*factory)->finalize */
43   (*factory)->free = smx_ctx_thread_free;
44   (*factory)->stop = smx_ctx_thread_stop;
45   (*factory)->suspend = smx_ctx_thread_suspend;
46   (*factory)->resume = smx_ctx_thread_resume;
47   (*factory)->name = "ctx_thread_factory";
48 }
49
50 static smx_context_t 
51 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
52                                       void_f_pvoid_t cleanup_func, void* cleanup_arg)
53 {
54   smx_ctx_thread_t context = (smx_ctx_thread_t)smx_ctx_base_factory_create_context_sized
55       (sizeof(s_smx_ctx_thread_t), code,argc,argv,cleanup_func,cleanup_arg);
56
57   /* If the user provided a function for the process then use it
58      otherwise is the context for maestro */
59   if(code){
60     context->begin = xbt_os_sem_init(0);
61     context->end = xbt_os_sem_init(0);
62
63
64     /* create and start the process */
65     /* NOTE: The first argument to xbt_os_thread_create used to be the process *
66      * name, but now the name is stored at SIMIX level, so we pass a null      */
67     context->thread =
68       xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, context);
69
70     /* wait the starting of the newly created process */
71     xbt_os_sem_acquire(context->end);
72   }
73     
74   return (smx_context_t)context;
75 }
76
77 static void smx_ctx_thread_free(smx_context_t pcontext)
78 {
79   smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
80
81   /* check if this is the context of maestro (it doesn't has a real thread) */  
82   if (context->thread) {
83     /* wait about the thread terminason */
84     xbt_os_thread_join(context->thread, NULL);
85     
86     /* destroy the synchronisation objects */
87     xbt_os_sem_destroy(context->begin);
88     xbt_os_sem_destroy(context->end);
89   }
90   
91   smx_ctx_base_free(pcontext);
92 }
93
94 static void smx_ctx_thread_stop(smx_context_t pcontext)
95 {
96
97   smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
98   
99   /* please no debug here: our procdata was already free'd */
100   smx_ctx_base_stop(pcontext);
101
102   /* signal to the maestro that it has finished */
103   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
104
105   /* exit */
106   /* We should provide return value in case other wants it */
107   xbt_os_thread_exit(NULL);     
108 }
109
110 static void *smx_ctx_thread_wrapper(void *param)
111 {
112   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
113
114   /* Tell the maestro we are starting, and wait for its green light */
115   xbt_os_sem_release(context->end);
116   xbt_os_sem_acquire(context->begin);
117
118   (context->super.code) (context->super.argc, context->super.argv);
119
120   smx_ctx_thread_stop((smx_context_t)context);
121   return NULL;
122 }
123
124 static void smx_ctx_thread_suspend(smx_context_t context) {
125   xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
126   xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
127 }
128
129 static void smx_ctx_thread_resume(smx_context_t new_context) {
130   xbt_os_sem_release(((smx_ctx_thread_t) new_context)->begin);
131   xbt_os_sem_acquire(((smx_ctx_thread_t) new_context)->end);
132 }