1 | /* |
---|
2 | * RTEMS multi-tasking support |
---|
3 | */ |
---|
4 | |
---|
5 | #ifdef HAVE_CONFIG_H |
---|
6 | #include "config.h" |
---|
7 | #endif |
---|
8 | |
---|
9 | #include <rpc/rpc.h> |
---|
10 | #include <rtems.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <pthread.h> |
---|
13 | #include <assert.h> |
---|
14 | |
---|
15 | /* |
---|
16 | * RPC variables for single-thread |
---|
17 | */ |
---|
18 | static struct _rtems_rpc_task_variables rpc_default = { |
---|
19 | -1, /* svc_maxfd */ |
---|
20 | {{0}}, /* svc_svc_fdset */ |
---|
21 | NULL, /* svc_xports */ |
---|
22 | 0, /* svc_xportssize */ |
---|
23 | 0, /* svc__svc_fdsetsize */ |
---|
24 | 0, /* svc__svc_fdset */ |
---|
25 | NULL, /* svc_svc_head */ |
---|
26 | 0, /* clnt_perror_buf */ |
---|
27 | 0, /* clnt_raw_private */ |
---|
28 | 0, /* call_rpc_private */ |
---|
29 | 0, /* svc_raw_private */ |
---|
30 | |
---|
31 | 0, /* svc_simple_proglst */ |
---|
32 | 0, /* svc_simple_pl */ |
---|
33 | 0, /* svc_simple_transp */ |
---|
34 | |
---|
35 | 0, /* rpcdname_default_domain */ |
---|
36 | 0 /* svc_auths_Auths */ |
---|
37 | }; |
---|
38 | |
---|
39 | /* |
---|
40 | * RPC values for initializing a new per-task set of variables |
---|
41 | */ |
---|
42 | static const struct _rtems_rpc_task_variables rpc_init = { |
---|
43 | -1, /* svc_maxfd */ |
---|
44 | {{0}}, /* svc_svc_fdset */ |
---|
45 | NULL, /* svc_xports */ |
---|
46 | 0, /* svc_xportssize */ |
---|
47 | 0, /* svc__svc_fdsetsize */ |
---|
48 | 0, /* svc__svc_fdset */ |
---|
49 | NULL, /* svc_svc_head */ |
---|
50 | 0, /* clnt_perror_buf */ |
---|
51 | 0, /* clnt_raw_private */ |
---|
52 | 0, /* call_rpc_private */ |
---|
53 | 0, /* svc_raw_private */ |
---|
54 | |
---|
55 | 0, /* svc_simple_proglst */ |
---|
56 | 0, /* svc_simple_pl */ |
---|
57 | 0, /* svc_simple_transp */ |
---|
58 | |
---|
59 | 0, /* rpcdname_default_domain */ |
---|
60 | 0 /* svc_auths_Auths */ |
---|
61 | }; |
---|
62 | |
---|
63 | /* |
---|
64 | * Per-task pointer to RPC data |
---|
65 | */ |
---|
66 | static pthread_once_t rtems_rpc_task_variable_once = PTHREAD_ONCE_INIT; |
---|
67 | static pthread_key_t rtems_rpc_task_variable_key; |
---|
68 | |
---|
69 | /* |
---|
70 | * Return the current task variable pointer. |
---|
71 | */ |
---|
72 | struct _rtems_rpc_task_variables *rtems_rpc_task_variables_get (void) |
---|
73 | { |
---|
74 | void *ptr = pthread_getspecific(rtems_rpc_task_variable_key); |
---|
75 | if (ptr == NULL) { |
---|
76 | ptr = &rpc_default; |
---|
77 | } |
---|
78 | return (struct _rtems_rpc_task_variables *) ptr; |
---|
79 | } |
---|
80 | |
---|
81 | /* |
---|
82 | * Key create function for task_variable_key. |
---|
83 | */ |
---|
84 | static void rtems_rpc_task_variable_make_key (void) |
---|
85 | { |
---|
86 | int eno = pthread_key_create(&rtems_rpc_task_variable_key, NULL); |
---|
87 | assert (eno == 0); |
---|
88 | /* |
---|
89 | * FIXME: Should have destructor which cleans up |
---|
90 | * all RPC stuff: |
---|
91 | * - Close all files |
---|
92 | * - Go through and free linked list elements |
---|
93 | * - Free other allocated memory (e.g. clnt_perror_buf) |
---|
94 | */ |
---|
95 | } |
---|
96 | |
---|
97 | /* |
---|
98 | * Set up per-task RPC variables |
---|
99 | */ |
---|
100 | int rtems_rpc_task_init (void) |
---|
101 | { |
---|
102 | struct _rtems_rpc_task_variables *tvp; |
---|
103 | int eno = 0; |
---|
104 | |
---|
105 | eno = pthread_once( |
---|
106 | &rtems_rpc_task_variable_once, |
---|
107 | rtems_rpc_task_variable_make_key |
---|
108 | ); |
---|
109 | assert (eno == 0); |
---|
110 | |
---|
111 | tvp = pthread_getspecific (rtems_rpc_task_variable_key); |
---|
112 | if (tvp == NULL) { |
---|
113 | tvp = malloc (sizeof *tvp); |
---|
114 | if (tvp == NULL) |
---|
115 | return RTEMS_NO_MEMORY; |
---|
116 | |
---|
117 | eno = pthread_setspecific (rtems_rpc_task_variable_key, (void *) tvp); |
---|
118 | if (eno != 0) { |
---|
119 | free (tvp); |
---|
120 | return RTEMS_INTERNAL_ERROR; |
---|
121 | } |
---|
122 | *tvp = rpc_init; |
---|
123 | } |
---|
124 | return RTEMS_SUCCESSFUL; |
---|
125 | } |
---|