Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / include / simix / context.h
1 /* a fast and simple context switching library                              */
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 #ifndef _SIMIX_CONTEXT_H
10 #define _SIMIX_CONTEXT_H
11
12 #include "xbt/swag.h"
13 #include "xbt/parmap.h"
14 #include "simix/datatypes.h"
15 #include "simgrid_config.h"
16
17 SG_BEGIN_DECL()
18 /******************************** Context *************************************/
19 typedef struct s_smx_context *smx_context_t;
20 typedef struct s_smx_context_factory *smx_context_factory_t;
21
22 /* Process creation/destruction callbacks */
23 typedef void (*void_pfn_smxprocess_t) (smx_process_t);
24
25
26 /* The following function pointer types describe the interface that any context
27    factory should implement */
28
29
30 typedef smx_context_t(*smx_pfn_context_factory_create_context_t)
31   (xbt_main_func_t, int, char **, void_pfn_smxprocess_t, void* data);
32 typedef int (*smx_pfn_context_factory_finalize_t) (smx_context_factory_t*);
33 typedef void (*smx_pfn_context_free_t) (smx_context_t);
34 typedef void (*smx_pfn_context_start_t) (smx_context_t);
35 typedef void (*smx_pfn_context_stop_t) (smx_context_t);
36 typedef void (*smx_pfn_context_suspend_t) (smx_context_t context);
37 typedef void (*smx_pfn_context_runall_t) (void);
38 typedef smx_context_t (*smx_pfn_context_self_t) (void);
39 typedef void* (*smx_pfn_context_get_data_t) (smx_context_t context);
40
41 /* interface of the context factories */
42 typedef struct s_smx_context_factory {
43   const char *name;
44   smx_pfn_context_factory_create_context_t create_context;
45   smx_pfn_context_factory_finalize_t finalize;
46   smx_pfn_context_free_t free;
47   smx_pfn_context_stop_t stop;
48   smx_pfn_context_suspend_t suspend;
49   smx_pfn_context_runall_t runall;
50   smx_pfn_context_self_t self;
51   smx_pfn_context_get_data_t get_data;
52 } s_smx_context_factory_t;
53
54 /* Hack: let msg load directly the right factory */
55 typedef void (*smx_ctx_factory_initializer_t)(smx_context_factory_t*);
56 XBT_PUBLIC(smx_ctx_factory_initializer_t) smx_factory_initializer_to_use;
57 extern char* smx_context_factory_name;
58 extern int smx_context_stack_size;
59
60 #ifdef HAVE_THREAD_LOCAL_STORAGE
61 extern __thread smx_context_t smx_current_context;
62 #else
63 extern smx_context_t smx_current_context;
64 #endif
65
66 /* *********************** */
67 /* Context type definition */
68 /* *********************** */
69 /* the following function pointers types describe the interface that all context
70    concepts must implement */
71 /* each context type derive from this structure, so they must contain this structure
72  * at their beginning -- OOP in C :/ */
73 typedef struct s_smx_context {
74   s_xbt_swag_hookup_t hookup;
75   xbt_main_func_t code;
76   void_pfn_smxprocess_t cleanup_func;
77   void *data;   /* Here SIMIX stores the smx_process_t containing the context */
78   char **argv;
79   int argc;
80   int iwannadie:1;
81 } s_smx_ctx_base_t;
82
83 /* methods of this class */
84 XBT_PUBLIC(void) smx_ctx_base_factory_init(smx_context_factory_t *factory);
85 XBT_PUBLIC(int) smx_ctx_base_factory_finalize(smx_context_factory_t *factory);
86
87 XBT_PUBLIC(smx_context_t)
88 smx_ctx_base_factory_create_context_sized(size_t size,
89                                           xbt_main_func_t code, int argc,
90                                           char **argv,
91                                           void_pfn_smxprocess_t cleanup,
92                                           void* data);
93 XBT_PUBLIC(void) smx_ctx_base_free(smx_context_t context);
94 XBT_PUBLIC(void) smx_ctx_base_stop(smx_context_t context);
95 XBT_PUBLIC(smx_context_t) smx_ctx_base_self(void);
96 XBT_PUBLIC(void) *smx_ctx_base_get_data(smx_context_t context);
97
98 XBT_PUBLIC(xbt_dynar_t) SIMIX_process_get_runnable(void);
99 XBT_PUBLIC(smx_process_t) SIMIX_process_from_PID(int PID);
100
101 /* parallelism */
102 XBT_PUBLIC(int) SIMIX_context_is_parallel(void);
103 XBT_PUBLIC(int) SIMIX_context_get_nthreads(void);
104 XBT_PUBLIC(void) SIMIX_context_set_nthreads(int nb_threads);
105 XBT_PUBLIC(int) SIMIX_context_get_parallel_threshold(void);
106 XBT_PUBLIC(void) SIMIX_context_set_parallel_threshold(int threshold);
107 XBT_PUBLIC(e_xbt_parmap_mode_t) SIMIX_context_get_parallel_mode(void);
108 XBT_PUBLIC(void) SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode);
109
110 SG_END_DECL()
111
112 #endif                          /* !_XBT_CONTEXT_H */