1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Synchronize Data on Disk with Memory |
---|
5 | * @ingroup libcsupport |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2008. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | /* Since we compile with strict ANSI we need to undef it to get |
---|
22 | * prototypes for extensions |
---|
23 | */ |
---|
24 | #undef __STRICT_ANSI__ |
---|
25 | int fdatasync(int); /* still not always prototyped */ |
---|
26 | |
---|
27 | |
---|
28 | #include <unistd.h> |
---|
29 | #include <stdio.h> |
---|
30 | |
---|
31 | #include <rtems.h> |
---|
32 | #include <rtems/score/percpu.h> |
---|
33 | |
---|
34 | /* XXX check standards -- Linux version appears to be void */ |
---|
35 | void _fwalk(struct _reent *, void *); |
---|
36 | |
---|
37 | |
---|
38 | static void sync_wrapper(FILE *f) |
---|
39 | { |
---|
40 | int fn = fileno(f); |
---|
41 | |
---|
42 | /* |
---|
43 | * There is no way to report errors here. So this is a best-effort approach. |
---|
44 | */ |
---|
45 | (void) fsync(fn); |
---|
46 | (void) fdatasync(fn); |
---|
47 | } |
---|
48 | |
---|
49 | /* iterate over all FILE *'s for this thread */ |
---|
50 | static bool sync_per_thread(Thread_Control *t, void *arg) |
---|
51 | { |
---|
52 | struct _reent *current_reent; |
---|
53 | struct _reent *this_reent; |
---|
54 | |
---|
55 | /* |
---|
56 | * The sync_wrapper() function will operate on the current thread's |
---|
57 | * reent structure so we will temporarily use that. |
---|
58 | */ |
---|
59 | this_reent = t->libc_reent; |
---|
60 | if ( this_reent ) { |
---|
61 | Thread_Control *executing = _Thread_Get_executing(); |
---|
62 | current_reent = executing->libc_reent; |
---|
63 | executing->libc_reent = this_reent; |
---|
64 | _fwalk (t->libc_reent, sync_wrapper); |
---|
65 | executing->libc_reent = current_reent; |
---|
66 | } |
---|
67 | |
---|
68 | return false; |
---|
69 | } |
---|
70 | |
---|
71 | /* |
---|
72 | * _global_impure_ptr is not prototyped in any .h files. |
---|
73 | * We have to extern it here. |
---|
74 | */ |
---|
75 | extern struct _reent * const _global_impure_ptr __ATTRIBUTE_IMPURE_PTR__; |
---|
76 | |
---|
77 | /** |
---|
78 | * This function operates by as follows: |
---|
79 | * for all threads |
---|
80 | * for all FILE * |
---|
81 | * fsync() |
---|
82 | * fdatasync() |
---|
83 | */ |
---|
84 | void sync(void) |
---|
85 | { |
---|
86 | |
---|
87 | /* |
---|
88 | * Walk the one used initially by RTEMS. |
---|
89 | */ |
---|
90 | _fwalk(_global_impure_ptr, sync_wrapper); |
---|
91 | |
---|
92 | /* |
---|
93 | * XXX Do we walk the one used globally by newlib? |
---|
94 | * XXX Do we need the RTEMS global one? |
---|
95 | */ |
---|
96 | |
---|
97 | /* |
---|
98 | * Now walk all the per-thread reentrancy structures. |
---|
99 | */ |
---|
100 | rtems_task_iterate(sync_per_thread, NULL); |
---|
101 | } |
---|