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"
16 /******************************** Context *************************************/
17 typedef struct s_smx_context *smx_context_t;
18 typedef struct s_smx_context_factory *smx_context_factory_t;
20 /* Process creation/destruction callbacks */
21 typedef void (*void_pfn_smxprocess_t) (smx_process_t);
24 /* The following function pointer types describe the interface that any context
25 factory should implement */
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 typedef int (*smx_pfn_context_get_thread_id) (void);
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 smx_pfn_context_get_thread_id get_thread_id;
52 } s_smx_context_factory_t;
56 /* Hack: let msg load directly the right factory */
57 typedef void (*smx_ctx_factory_initializer_t)(smx_context_factory_t*);
58 extern smx_ctx_factory_initializer_t smx_factory_initializer_to_use;
59 extern char* smx_context_factory_name;
60 extern int smx_context_stack_size;
61 extern smx_context_t smx_current_context;
63 /* *********************** */
64 /* Context type definition */
65 /* *********************** */
66 /* the following function pointers types describe the interface that all context
67 concepts must implement */
68 /* each context type derive from this structure, so they must contain this structure
69 * at their begining -- OOP in C :/ */
70 typedef struct s_smx_context {
71 s_xbt_swag_hookup_t hookup;
75 void_pfn_smxprocess_t cleanup_func;
77 void *data; /* Here SIMIX stores the smx_process_t containing the context */
80 /* methods of this class */
81 void smx_ctx_base_factory_init(smx_context_factory_t *factory);
82 int smx_ctx_base_factory_finalize(smx_context_factory_t *factory);
85 smx_ctx_base_factory_create_context_sized(size_t size,
86 xbt_main_func_t code, int argc,
88 void_pfn_smxprocess_t cleanup,
90 void smx_ctx_base_free(smx_context_t context);
91 void smx_ctx_base_stop(smx_context_t context);
92 smx_context_t smx_ctx_base_self(void);
93 void *smx_ctx_base_get_data(smx_context_t context);
94 int smx_ctx_base_get_thread_id(void);
97 void SIMIX_context_set_nthreads(int nb_threads);
98 int SIMIX_context_get_nthreads(void);
99 int SIMIX_context_is_parallel(void);
103 #endif /* !_XBT_CONTEXT_H */