summaryrefslogtreecommitdiffstats
path: root/cpukit/librpc/src/rpc/rtems_rpc.c
blob: 91e2b0fcf3aaca59191d36c69136457b5342b8e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "rtems-rpc-config.h"

/*
 * RTEMS multi-tasking support
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <rpc/rpc.h>
#include <rtems.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>

/*
 * RPC variables for single-thread
 */
static struct _rtems_rpc_task_variables rpc_default = {
	-1,		/* svc_maxfd */
	{{0}},		/* svc_svc_fdset */
	NULL,		/* svc_xports */
	0,		/* svc_xportssize */
	0,		/* svc__svc_fdsetsize */		
	0,		/* svc__svc_fdset */
	NULL,		/* svc_svc_head */
	0,		/* clnt_perror_buf */
	0,		/* clnt_raw_private */
	0,		/* call_rpc_private */
	0,		/* svc_raw_private */

	0,		/* svc_simple_proglst */
	0,		/* svc_simple_pl */
	0,		/* svc_simple_transp */

	0,		/* rpcdname_default_domain */
	0		/* svc_auths_Auths */
};

/*
 * RPC values for initializing a new per-task set of variables
 */
static const struct _rtems_rpc_task_variables rpc_init = {
	-1,		/* svc_maxfd */
	{{0}},		/* svc_svc_fdset */
	NULL,		/* svc_xports */
	0,		/* svc_xportssize */
	0,		/* svc__svc_fdsetsize */		
	0,		/* svc__svc_fdset */
	NULL,		/* svc_svc_head */
	0,		/* clnt_perror_buf */
	0,		/* clnt_raw_private */
	0,		/* call_rpc_private */
	0,		/* svc_raw_private */

	0,		/* svc_simple_proglst */
	0,		/* svc_simple_pl */
	0,		/* svc_simple_transp */

	0,		/* rpcdname_default_domain */
	0		/* svc_auths_Auths */
};

/*
 * Per-task pointer to RPC data
 */
static pthread_once_t rtems_rpc_task_variable_once = PTHREAD_ONCE_INIT;
static pthread_key_t rtems_rpc_task_variable_key;

/*
 * Return the current task variable pointer.
 */
struct _rtems_rpc_task_variables *rtems_rpc_task_variables_get (void)
{
	void *ptr = pthread_getspecific(rtems_rpc_task_variable_key);
	if (ptr == NULL) {
		ptr = &rpc_default;
	}
	return (struct _rtems_rpc_task_variables *) ptr;
}

/*
 * Key create function for task_variable_key.
 */
static void rtems_rpc_task_variable_make_key (void)
{
	int eno = pthread_key_create(&rtems_rpc_task_variable_key, NULL);
	assert (eno == 0);
	/*
	 * FIXME: Should have destructor which cleans up
	 * all RPC stuff:
	 *  - Close all files
	 *  - Go through and free linked list elements
	 *  - Free other allocated memory (e.g. clnt_perror_buf)
	 */
}

/*
 * Set up per-task RPC variables
 */
int rtems_rpc_task_init (void)
{
	struct _rtems_rpc_task_variables *tvp;
	int eno = 0;

	eno = pthread_once(
		&rtems_rpc_task_variable_once,
		rtems_rpc_task_variable_make_key
	);
	assert (eno == 0);

	tvp = pthread_getspecific (rtems_rpc_task_variable_key);
	if (tvp == NULL) {
		tvp = malloc (sizeof *tvp);
		if (tvp == NULL)
			return RTEMS_NO_MEMORY;

		eno = pthread_setspecific (rtems_rpc_task_variable_key, (void *) tvp);
		if (eno != 0) {
			free (tvp);
			return RTEMS_INTERNAL_ERROR;
		}
		*tvp = rpc_init;
	}
	return RTEMS_SUCCESSFUL;
}