forked from vsamtuc/tinyos3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kernel_streams.h
132 lines (92 loc) · 3.65 KB
/
kernel_streams.h
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
128
129
130
131
132
#ifndef __KERNEL_STREAMS_H
#define __KERNEL_STREAMS_H
#include "tinyos.h"
#include "kernel_dev.h"
/**
@file kernel_streams.h
@brief Support for I/O streams.
@defgroup streams Streams.
@ingroup kernel
@brief Support for I/O streams.
The stream model of tinyos3 is similar to the Unix model.
Streams are objects that are shared between processes.
Streams are accessed by file IDs (similar to file descriptors
in Unix).
The streams of each process are held in the file table of the
PCB of the process. The system calls generally use the API
of this file to access FCBs: @ref get_fcb, @ref FCB_reserve
and @ref FCB_unreserve.
Streams are connected to devices by virtue of a @c file_operations
object, which provides pointers to device-specific implementations
for read, write and close.
@{
*/
/** @brief The file control block.
A file control block provides a uniform object to the
system calls, and contains pointers to device-specific
functions.
*/
typedef struct file_control_block
{
uint refcount; /**< @brief Reference counter. */
void* streamobj; /**< @brief The stream object (e.g., a device) */
file_ops* streamfunc; /**< @brief The stream implementation methods */
rlnode freelist_node; /**< @brief Intrusive list node */
} FCB;
/**
@brief Initialization for files and streams.
This function is called at kernel startup.
*/
void initialize_files();
/**
@brief Increase the reference count of an fcb
@param fcb the fcb whose reference count will be increased
*/
void FCB_incref(FCB* fcb);
/**
@brief Decrease the reference count of the fcb.
If the reference count drops to 0, release the FCB, calling the
Close method and returning its return value.
If the reference count is still >0, return 0.
@param fcb the fcb whose reference count is decreased
@returns if the reference count is still >0, return 0, else return the value returned by the
`Close()` operation
*/
int FCB_decref(FCB* fcb);
/** @brief Acquire a number of FCBs and corresponding fids.
Given an array of fids and an array of pointers to FCBs of
size @ num, this function will check is available resources
in the current process PCB and FCB are available, and if so
it will fill the two arrays with the appropriate values.
If not, the state is unchanged (but the array contents
may have been overwritten).
If these resources are not needed, the operation can be
reversed by calling @ref FCB_unreserve.
@param num the number of resources to reserve.
@param fid array of size at least `num` of `Fid_t`.
@param fcb array of size at least `num` of `FCB*`.
@returns 1 for success and 0 for failure.
*/
int FCB_reserve(size_t num, Fid_t *fid, FCB** fcb);
/** @brief Release a number of FCBs and corresponding fids.
Given an array of fids of size @ num, this function will
return the fids to the free pool of the current process and
release the corresponding FCBs.
This is the opposite of operation @ref FCB_reserve.
Note that this is very different from closing open fids.
No I/O operation is performed by this function.
This function does not check its arguments for correctness.
Use only with arrays filled by a call to @ref FCB_reserve.
@param num the number of resources to unreserve.
@param fid array of size at least `num` of `Fid_t`.
@param fcb array of size at least `num` of `FCB*`.
*/
void FCB_unreserve(size_t num, Fid_t *fid, FCB** fcb);
/** @brief Translate an fid to an FCB.
This routine will return NULL if the fid is not legal.
@param fid the file ID to translate to a pointer to FCB
@returns a pointer to the corresponding FCB, or NULL.
*/
FCB* get_fcb(Fid_t fid);
/** @} */
#endif