1 /* context_thread - implementation of context switching with native threads */
3 /* Copyright (c) 2004-2008 the SimGrid team. All right reserved */
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. */
8 #include "xbt/function_types.h"
11 #include "portable.h" /* loads context system definitions */
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"
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
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;
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);
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);
35 static void *smx_ctx_thread_wrapper(void *param);
37 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory) {
39 smx_ctx_base_factory_init(factory);
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";
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)
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);
57 /* If the user provided a function for the process then use it
58 otherwise is the context for maestro */
60 context->begin = xbt_os_sem_init(0);
61 context->end = xbt_os_sem_init(0);
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 */
68 xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, context);
70 /* wait the starting of the newly created process */
71 xbt_os_sem_acquire(context->end);
74 return (smx_context_t)context;
77 static void smx_ctx_thread_free(smx_context_t pcontext)
79 smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
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);
86 /* destroy the synchronisation objects */
87 xbt_os_sem_destroy(context->begin);
88 xbt_os_sem_destroy(context->end);
91 smx_ctx_base_free(pcontext);
94 static void smx_ctx_thread_stop(smx_context_t pcontext)
97 smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
99 /* please no debug here: our procdata was already free'd */
100 smx_ctx_base_stop(pcontext);
102 /* signal to the maestro that it has finished */
103 xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
106 /* We should provide return value in case other wants it */
107 xbt_os_thread_exit(NULL);
110 static void *smx_ctx_thread_wrapper(void *param)
112 smx_ctx_thread_t context = (smx_ctx_thread_t) param;
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);
118 (context->super.code) (context->super.argc, context->super.argv);
120 smx_ctx_thread_stop((smx_context_t)context);
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);
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);