Thread.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "Thread.h"
17 
18 #ifndef _WIN32
19 #include <pthread.h>
20 #endif
21 
22 //#define DEBUG_MUTEX_LOCKER 1
23 
24 #include <iostream>
25 
26 Thread::Thread(Type type, QObject *parent) :
27  QThread(parent),
28  m_type(type)
29 {
30  setStackSize(512 * 1024);
31 }
32 
33 void
35 {
36  QThread::start();
37 
38 #ifndef _WIN32
39  struct sched_param param;
40  ::memset(&param, 0, sizeof(param));
41 
42  if (m_type == RTThread) {
43 
44  param.sched_priority = 5;
45 
46  if (::pthread_setschedparam(pthread_self(), SCHED_FIFO, &param)) {
47  ::perror("INFO: pthread_setschedparam to SCHED_FIFO failed");
48  }
49 
50  } else {
51 
52  if (::pthread_setschedparam(pthread_self(), SCHED_OTHER, &param)) {
53  ::perror("WARNING: pthread_setschedparam to SCHED_OTHER failed");
54  }
55  }
56 
57 #endif
58 }
59 
60 MutexLocker::MutexLocker(QMutex *mutex, const char *name) :
61  m_profiler(name, false),
62  m_printer(name),
63  m_locker(mutex)
64 {
65 #ifdef DEBUG_MUTEX_LOCKER
66  cerr << "... locked mutex " << mutex << endl;
67 #endif
68  m_profiler.end();
69 }
70 
72 {
73 }
74 
75 MutexLocker::Printer::Printer(const char *name) :
76  m_name(name)
77 {
78 #ifdef DEBUG_MUTEX_LOCKER
79  cerr << "MutexLocker: Locking \"" << m_name << "\" in "
80  << (void *)QThread::currentThreadId() << endl;
81 #else
82  (void)m_name;
83 #endif
84 }
85 
87 {
88 #ifdef DEBUG_MUTEX_LOCKER
89  cerr << "MutexLocker: Unlocking \"" << m_name
90  << "\" in " << (void *)QThread::currentThreadId() << endl;
91 #endif
92 }
93 
void end()
Definition: Profiler.cpp:205
Thread(Type type=NonRTThread, QObject *parent=0)
Definition: Thread.cpp:26
void start()
Definition: Thread.cpp:34
static QMutex mutex
Definition: Debug.cpp:30
MutexLocker(QMutex *mutex, const char *name)
Definition: Thread.cpp:60
Profiler m_profiler
Definition: Thread.h:59
Type
Definition: Thread.h:29
const char * m_name
Definition: Thread.h:56
Type m_type
Definition: Thread.h:40
Printer(const char *name)
Definition: Thread.cpp:75
~MutexLocker()
Definition: Thread.cpp:71