summaryrefslogtreecommitdiffstats
path: root/Src/Multiplexer/ThreadReadHdd.h
blob: 74996fd107518e4c65a5bc3691d32c615a192a60 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Video On Demand Samples
 *
 * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You may also obtain this software under a propriety license from Microchip.
 * Please contact Microchip for further information.
 *
 */

/*----------------------------------------------------------*/
/*! \file
 *  \brief  This file contains the CThreadReadHdd class.
 */
/*----------------------------------------------------------*/
#ifndef THREADREADHDD_H
#define	THREADREADHDD_H

#include <unistd.h>
#include "SafeVector.h"
#include <pthread.h>
#include <assert.h>
#include "Console.h"
#include "StreamList.h"



/*----------------------------------------------------------*/
/*! \brief Class creating a thread to call stream's reading function.
 */
/*----------------------------------------------------------*/
class CThreadReadHdd
{
private:
    pthread_t m_hThread;
    bool m_bRunning;
    CStreamList *m_StreamList;

public:    
    
    CThreadReadHdd() : m_hThread(0), m_bRunning(false), m_StreamList(NULL)
    {
    }
    
    
    /*----------------------------------------------------------*/
    /*! \brief thread reading from all stream lists
     */
    /*----------------------------------------------------------*/
    static void *Run(void *pInst) 
    {
        assert(NULL != pInst);
        
        CThreadReadHdd *that = (CThreadReadHdd *)pInst;
        /* set thread to max priority: */
        int ret;
        pthread_t this_thread = pthread_self();
        struct sched_param params;
        params.sched_priority = sched_get_priority_max(SCHED_FIFO);
        ret = pthread_setschedparam(this_thread, SCHED_FIFO, &params);
        if (ret != 0) {
            ConsolePrintf( PRIO_ERROR, "ThreadReadHdd_setschedparam(SCHED_FIFO) failed\n");
        }
        // Now verify the change in thread priority
        int policy = 0;
        ret = pthread_getschedparam(this_thread, &policy, &params);
        if (ret != 0) {
            ConsolePrintf( PRIO_ERROR, "ThreadReadHdd_getschedparam() failed\n");
        }
        
        while (that->m_bRunning)
        {
            if ( !that->m_StreamList->ReadHdd() )           // buffer full?
                usleep(50000);                              // we have 300ms buffer size, so it's save to sleep 50ms
        }
        pthread_exit(0);
    }
    
    
    
    
    /*----------------------------------------------------------*/
    /*! \brief start reading thread
     */
    /*----------------------------------------------------------*/
    void Start()
    {
        if ( m_bRunning )
            return;
            
        if ( NULL == m_StreamList )
            return;
            
        struct sched_param param;
        pthread_attr_t tattr;
        pthread_attr_init(&tattr);
        pthread_attr_getschedparam(&tattr, &param);
        pthread_attr_setschedpolicy(&tattr, SCHED_RR);
        param.sched_priority = sched_get_priority_max(SCHED_RR);
        pthread_attr_setschedparam(&tattr, &param);
        m_bRunning = true;
        if ( 0 != pthread_create(&m_hThread, &tattr, Run, this) )
            ConsolePrintf( PRIO_ERROR, RED"! failed to create thread"RESETCOLOR"\n");
    }
    
    
    
    
    /*----------------------------------------------------------*/
    /*! \brief stop reading thread
     */
    /*----------------------------------------------------------*/
    void Stop()
    {
        if ( m_bRunning )
        {
            m_bRunning = false;
            void *pVal;
            pthread_join(m_hThread, &pVal);
            m_hThread = 0;
        }        
    }
    
public:
    /*----------------------------------------------------------*/
    /*! \brief add a stream list to read thread
     *  \param StreamList - stream list to add for reading
     *  \return true, if stream list was added successfully
     */
    /*----------------------------------------------------------*/
    void AddStreamList(CStreamList *streamList)
    {
        Stop();
        m_StreamList = streamList;
        Start();
    }
    
    
    
    
    /*----------------------------------------------------------*/
    /*! \brief remove a stream list from read thread
     */
    /*----------------------------------------------------------*/
    void RemoveStreamList(CStreamList *streamList)
    {
        Stop();
        m_StreamList = NULL;
    }
};
    
    
    
    


#endif