Logo AND Algorithmique Numérique Distribuée

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