1 /* a fast and simple context switching library */
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #ifndef _SIMIX_CONTEXT_H
10 #define _SIMIX_CONTEXT_H
13 #include "simix/datatypes.h"
14 #include "simgrid_config.h"
17 /******************************** Context *************************************/
18 typedef struct s_smx_context *smx_context_t;
19 typedef struct s_smx_context_factory *smx_context_factory_t;
21 /* Process creation/destruction callbacks */
22 typedef void (*void_pfn_smxprocess_t) (smx_process_t);
25 /* The following function pointer types describe the interface that any context
26 factory should implement */
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);
40 /* interface of the context factories */
41 typedef struct s_smx_context_factory {
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;
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;
61 #ifdef HAVE_THREAD_LOCAL_STORAGE
62 extern __thread smx_context_t smx_current_context;
64 extern smx_context_t smx_current_context;
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;
79 void_pfn_smxprocess_t cleanup_func;
81 void *data; /* Here SIMIX stores the smx_process_t containing the context */
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);
89 smx_ctx_base_factory_create_context_sized(size_t size,
90 xbt_main_func_t code, int argc,
92 void_pfn_smxprocess_t cleanup,
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);
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);
108 #endif /* !_XBT_CONTEXT_H */