try to use posix_fadvise, doesn't seem to reall work, though.
[vuplus_dvbapp] / lib / base / filepush.cpp
1 #include <lib/base/filepush.h>
2 #include <lib/base/eerror.h>
3 #include <errno.h>
4 #include <fcntl.h>
5
6 eFilePushThread::eFilePushThread()
7 {
8         m_stop = 0;
9         m_buf_start = m_buf_end = 0;
10 }
11
12 static void signal_handler(int x)
13 {
14 }
15
16 void eFilePushThread::thread()
17 {
18         off_t dest_pos = 0;
19         eDebug("FILEPUSH THREAD START");
20                 // this is a race. FIXME.
21         
22                 /* we set the signal to not restart syscalls, so we can detect our signal. */
23         struct sigaction act;
24         act.sa_handler = signal_handler; // no, SIG_IGN doesn't do it. we want to receive the -EINTR
25         act.sa_flags = 0;
26         sigaction(SIGUSR1, &act, 0);
27         
28         dest_pos = lseek(m_fd_dest, 0, SEEK_CUR);
29                 /* m_stop must be evaluated after each syscall. */
30         while (!m_stop)
31         {
32                         /* first try flushing the bufptr */
33                 if (m_buf_start != m_buf_end)
34                 {
35                                 // TODO: take care of boundaries.
36                         int w = write(m_fd_dest, m_buffer + m_buf_start, m_buf_end - m_buf_start);
37                         if (w <= 0)
38                         {
39                                 if (errno == -EINTR)
40                                         continue;
41                                 eDebug("eFilePushThread *write error* - not yet handled");
42                                 // ... we would stop the thread
43                         }
44
45                         posix_fadvise(m_fd_dest, dest_pos, w, POSIX_FADV_DONTNEED);
46
47                         dest_pos += w;
48 //                      printf("FILEPUSH: wrote %d bytes\n", w);
49                         m_buf_start += w;
50                         continue;
51                 }
52                         
53                         /* now fill our buffer. */
54                 m_buf_start = 0;
55                 m_buf_end = read(m_fd_source, m_buffer, sizeof(m_buffer));
56                 if (m_buf_end < 0)
57                 {
58                         m_buf_end = 0;
59                         if (errno == EINTR)
60                                 continue;
61                         eDebug("eFilePushThread *read error* - not yet handled");
62                 }
63                 if (m_buf_end == 0)
64                 {
65                         eDebug("FILEPUSH: end-of-file! (currently unhandled)");
66                         if (!lseek(m_fd_source, 0, SEEK_SET))
67                         {
68                                 eDebug("(looping)");
69                                 continue;
70                         }
71                         break;
72                 }
73 //              printf("FILEPUSH: read %d bytes\n", m_buf_end);
74         }
75         
76         eDebug("FILEPUSH THREAD STOP");
77 }
78
79 void eFilePushThread::start(int fd_source, int fd_dest)
80 {
81         m_fd_source = fd_source;
82         m_fd_dest = fd_dest;
83         m_stop = 0;
84         run();
85 }
86
87 void eFilePushThread::stop()
88 {
89         m_stop = 1;
90         sendSignal(SIGUSR1);
91         kill();
92 }