~ubuntu-branches/ubuntu/utopic/drumkv1/utopic-proposed

« back to all changes in this revision

Viewing changes to src/drumkv1_sched.cpp

  • Committer: Package Import Robot
  • Author(s): Jaromír Mikeš
  • Date: 2014-04-11 19:21:51 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20140411192151-ebtxr7f21agkotlg
Tags: 0.4.1-1
* New upstream release.
* Fix syntax for Keywords entry in desktop file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// drumkv1_sched.cpp
 
2
//
 
3
/****************************************************************************
 
4
   Copyright (C) 2012-2014, rncbc aka Rui Nuno Capela. All rights reserved.
 
5
 
 
6
   This program is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU General Public License
 
8
   as published by the Free Software Foundation; either version 2
 
9
   of the License, or (at your option) any later version.
 
10
 
 
11
   This program is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
   GNU General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU General Public License along
 
17
   with this program; if not, write to the Free Software Foundation, Inc.,
 
18
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
19
 
 
20
*****************************************************************************/
 
21
 
 
22
#include "drumkv1_sched.h"
 
23
 
 
24
#include <QThread>
 
25
#include <QMutex>
 
26
#include <QWaitCondition>
 
27
 
 
28
#include <stdint.h>
 
29
 
 
30
 
 
31
//-------------------------------------------------------------------------
 
32
// drumkv1_sched_thread - worker/schedule thread decl.
 
33
//
 
34
 
 
35
class drumkv1_sched_thread : public QThread
 
36
{
 
37
public:
 
38
 
 
39
        // ctor.
 
40
        drumkv1_sched_thread(uint32_t nsize = 8);
 
41
 
 
42
        // dtor.
 
43
        ~drumkv1_sched_thread();
 
44
 
 
45
        // schedule processing and wake from wait condition.
 
46
        void schedule(drumkv1_sched *sched);
 
47
 
 
48
protected:
 
49
 
 
50
        // main thread executive.
 
51
        void run();
 
52
 
 
53
private:
 
54
 
 
55
        // sync queue instance reference.
 
56
        uint32_t m_nsize;
 
57
        uint32_t m_nmask;
 
58
 
 
59
        drumkv1_sched **m_items;
 
60
 
 
61
        volatile uint32_t m_iread;
 
62
        volatile uint32_t m_iwrite;
 
63
 
 
64
        // whether the thread is logically running.
 
65
        volatile bool m_running;
 
66
 
 
67
        // thread synchronization objects.
 
68
        QMutex m_mutex;
 
69
        QWaitCondition m_cond;
 
70
};
 
71
 
 
72
 
 
73
static drumkv1_sched_thread *g_sched_thread   = NULL;
 
74
static uint32_t              g_sched_refcount = 0;
 
75
 
 
76
 
 
77
//-------------------------------------------------------------------------
 
78
// drumkv1_sched_thread - worker/schedule thread impl.
 
79
//
 
80
 
 
81
// ctor.
 
82
drumkv1_sched_thread::drumkv1_sched_thread ( uint32_t nsize ) : QThread()
 
83
{
 
84
        m_nsize = (4 << 1);
 
85
        while (m_nsize < nsize)
 
86
                m_nsize <<= 1;
 
87
        m_nmask = (m_nsize - 1);
 
88
        m_items = new drumkv1_sched * [m_nsize];
 
89
 
 
90
        m_iread  = 0;
 
91
        m_iwrite = 0;
 
92
 
 
93
        ::memset(m_items, 0, m_nsize * sizeof(drumkv1_sched *));
 
94
 
 
95
        m_running = false;
 
96
}
 
97
 
 
98
 
 
99
// dtor.
 
100
drumkv1_sched_thread::~drumkv1_sched_thread (void)
 
101
{
 
102
        // fake sync and wait 
 
103
        if (m_running && isRunning()) do {
 
104
                if (m_mutex.tryLock()) {
 
105
                        m_running = false;
 
106
                        m_cond.wakeAll();
 
107
                        m_mutex.unlock();
 
108
                }
 
109
        } while (!wait(100));
 
110
 
 
111
        delete [] m_items;
 
112
}
 
113
 
 
114
 
 
115
// schedule processing and wake from wait condition.
 
116
void drumkv1_sched_thread::schedule ( drumkv1_sched *sched )
 
117
{
 
118
        if (!sched->sync_wait()) {
 
119
                const uint32_t w = (m_iwrite + 1) & m_nmask;
 
120
                if (w != m_iread) {
 
121
                        m_items[m_iwrite] = sched;
 
122
                        m_iwrite = w;
 
123
                }
 
124
        }
 
125
 
 
126
        if (m_mutex.tryLock()) {
 
127
                m_cond.wakeAll();
 
128
                m_mutex.unlock();
 
129
        }
 
130
}
 
131
 
 
132
 
 
133
// main thread executive.
 
134
void drumkv1_sched_thread::run (void)
 
135
{
 
136
        m_mutex.lock();
 
137
 
 
138
        m_running = true;
 
139
 
 
140
        while (m_running) {
 
141
                // do whatever we must...
 
142
                uint32_t r = m_iread;
 
143
                while (r != m_iwrite) {
 
144
                        drumkv1_sched *sched = m_items[r];
 
145
                        if (sched) {
 
146
                                sched->sync_process();
 
147
                                m_items[r] = NULL;
 
148
                        }
 
149
                        ++r &= m_nmask;
 
150
                }
 
151
                m_iread = r;
 
152
                // wait for sync...
 
153
                m_cond.wait(&m_mutex);
 
154
        }
 
155
 
 
156
        m_mutex.unlock();
 
157
}
 
158
 
 
159
 
 
160
//-------------------------------------------------------------------------
 
161
// drumkv1_sched - worker/scheduled stuff (pure virtual).
 
162
//
 
163
 
 
164
// ctor.
 
165
drumkv1_sched::drumkv1_sched (void) : m_sync_wait(false)
 
166
{
 
167
        if (++g_sched_refcount == 1 && g_sched_thread == NULL) {
 
168
                g_sched_thread = new drumkv1_sched_thread();
 
169
                g_sched_thread->start();
 
170
        }
 
171
}
 
172
 
 
173
 
 
174
// dtor (virtual).
 
175
drumkv1_sched::~drumkv1_sched (void)
 
176
{
 
177
        if (--g_sched_refcount == 0 && g_sched_thread) {
 
178
                delete g_sched_thread;
 
179
                g_sched_thread = NULL;
 
180
        }
 
181
}
 
182
 
 
183
 
 
184
// schedule process.
 
185
void drumkv1_sched::schedule (void)
 
186
{
 
187
        if (g_sched_thread)
 
188
                g_sched_thread->schedule(this);
 
189
}
 
190
 
 
191
 
 
192
// test-and-set.
 
193
bool drumkv1_sched::sync_wait (void)
 
194
{
 
195
        const bool sync_wait = m_sync_wait;
 
196
 
 
197
        if (!sync_wait)
 
198
                m_sync_wait = true;
 
199
 
 
200
        return sync_wait;
 
201
}
 
202
 
 
203
 
 
204
// scheduled processor.
 
205
void drumkv1_sched::sync_process (void)
 
206
{
 
207
        process();
 
208
 
 
209
        m_sync_wait = false;
 
210
}
 
211
 
 
212
 
 
213
// end of drumkv1_sched.cpp