Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove useless operation.
[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) (xbt_dynar_t processes);
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 typedef int (*smx_pfn_context_get_thread_id) (void);
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   smx_pfn_context_get_thread_id get_thread_id;
53 } s_smx_context_factory_t;
54
55
56
57 /* Hack: let msg load directly the right factory */
58 typedef void (*smx_ctx_factory_initializer_t)(smx_context_factory_t*);
59 extern smx_ctx_factory_initializer_t smx_factory_initializer_to_use;
60 extern char* smx_context_factory_name;
61 extern int smx_context_stack_size;
62
63 #ifdef CONTEXT_THREADS
64 extern __thread smx_context_t smx_current_context;
65 #else
66 extern smx_context_t smx_current_context;
67 #endif
68
69 /* *********************** */
70 /* Context type definition */
71 /* *********************** */
72 /* the following function pointers types describe the interface that all context
73    concepts must implement */
74 /* each context type derive from this structure, so they must contain this structure
75  * at their begining -- OOP in C :/ */
76 typedef struct s_smx_context {
77   s_xbt_swag_hookup_t hookup;
78   xbt_main_func_t code;
79   int argc;
80   char **argv;
81   void_pfn_smxprocess_t cleanup_func;
82   int iwannadie:1;
83   void *data;   /* Here SIMIX stores the smx_process_t containing the context */
84 } s_smx_ctx_base_t;
85
86 /* methods of this class */
87 void smx_ctx_base_factory_init(smx_context_factory_t *factory);
88 int smx_ctx_base_factory_finalize(smx_context_factory_t *factory);
89
90 smx_context_t
91 smx_ctx_base_factory_create_context_sized(size_t size,
92                                           xbt_main_func_t code, int argc,
93                                           char **argv,
94                                           void_pfn_smxprocess_t cleanup,
95                                           void* data);
96 void smx_ctx_base_free(smx_context_t context);
97 void smx_ctx_base_stop(smx_context_t context);
98 smx_context_t smx_ctx_base_self(void);
99 void *smx_ctx_base_get_data(smx_context_t context);
100 int smx_ctx_base_get_thread_id(void);
101
102 /* parallelism */
103 XBT_INLINE void SIMIX_context_set_nthreads(int nb_threads);
104 XBT_INLINE int SIMIX_context_get_nthreads(void);
105 XBT_INLINE int SIMIX_context_is_parallel(void);
106 XBT_INLINE void SIMIX_context_set_parallel_threshold(int threshold);
107 XBT_INLINE int SIMIX_context_get_parallel_threshold(void);
108
109 SG_END_DECL()
110
111 #endif                          /* !_XBT_CONTEXT_H */