acpica.library: Add AcpiScanTables() Hook based helper
[AROS.git] / rom / dos / internalflush.c
blob55bc311f4fa3227a72b31ebac865dcfa0e17290d
1 /*
2 Copyright © 2002-2013, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <exec/types.h>
7 #include <proto/dos.h>
9 #define DEBUG 0
10 #include <aros/debug.h>
12 #include "dos_intern.h"
14 LONG InternalFlush( struct FileHandle *fh, struct DosLibrary *DOSBase )
16 LONG position, pos;
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)
31 Delay(1);
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. */
37 position = 0;
38 /* Remember current position */
39 pos = fh->fh_Pos;
41 while( position < pos )
43 LONG size;
45 size = Write( MKBADDR(fh), BADDR(fh->fh_Buf) + position, fh->fh_Pos - position );
47 /* An error happened? No success. */
48 if( size < 0 )
50 fh->fh_Flags &= ~(FHF_WRITE | FHF_FLUSHING);
52 return FALSE;
55 position += size;
58 /* Reset the buffer. */
59 if (fh->fh_Pos == pos)
60 fh->fh_Pos = 0;
61 else
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",
70 pos, fh->fh_Pos,
71 fh->fh_Pos, fh->fh_Buf
72 ));
73 int i, len = fh->fh_Pos - pos;
74 char *dest = BADDR(fh->fh_Buf);
75 const char *src = dest + pos;
76 fh->fh_Pos -= pos;
77 for (i = 0; i < len; i++)
78 *dest++ = *src++;
81 fh->fh_Flags &= ~FHF_FLUSHING;
83 return TRUE;