2 Copyright © 2002-2013, The AROS Development Team. All rights reserved.
6 #include <exec/types.h>
10 #include <aros/debug.h>
12 #include "dos_intern.h"
14 LONG
InternalFlush( struct FileHandle
*fh
, struct DosLibrary
*DOSBase
)
18 /* Make sure the input parameters are sane. */
19 ASSERT_VALID_PTR( fh
);
20 ASSERT_VALID_PTR( BADDR(fh
->fh_Buf
) );
22 ASSERT(fh
->fh_End
> 0);
23 ASSERT(fh
->fh_Pos
>= 0);
24 ASSERT(fh
->fh_Pos
<= fh
->fh_End
);
26 /* If other task is flushing the buffer wait for completion.
27 Currently we just delay 1 tick, we assume task that is flushing will
28 be scheduled during first Delay() call so we have to wait only once.
30 while (fh
->fh_Flags
& FHF_FLUSHING
)
33 /* TODO: Is following line race free ? */
34 fh
->fh_Flags
|= FHF_FLUSHING
;
36 /* Write the data, in many pieces if the first one isn't enough. */
38 /* Remember current position */
41 while( position
< pos
)
45 size
= Write( MKBADDR(fh
), BADDR(fh
->fh_Buf
) + position
, fh
->fh_Pos
- position
);
47 /* An error happened? No success. */
50 fh
->fh_Flags
&= ~(FHF_WRITE
| FHF_FLUSHING
);
58 /* Reset the buffer. */
59 if (fh
->fh_Pos
== pos
)
63 /* If we are here it means that During Flush() characters were added
64 to the buffer. Try to remove only the part that was flushed.
65 This implementation is not fully race free but we try our best to
66 reduce data loss as much as possible.
68 D(bug("Characters written during Flush(); pos=%d, fh->fh_Pos=%d\n"
69 "\tfh->fh_Buf='%0.*s'\n",
71 fh
->fh_Pos
, fh
->fh_Buf
73 int i
, len
= fh
->fh_Pos
- pos
;
74 char *dest
= BADDR(fh
->fh_Buf
);
75 const char *src
= dest
+ pos
;
77 for (i
= 0; i
< len
; i
++)
81 fh
->fh_Flags
&= ~FHF_FLUSHING
;