Logo AND Algorithmique Numérique Distribuée

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