Thread.cpp

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "Thread.h"
00017 
00018 #ifndef _WIN32
00019 #include <pthread.h>
00020 #endif
00021 
00022 //#define DEBUG_MUTEX_LOCKER 1
00023 
00024 #include <iostream>
00025 
00026 Thread::Thread(Type type, QObject *parent) :
00027     QThread(parent),
00028     m_type(type)
00029 {
00030     setStackSize(512 * 1024);
00031 }
00032 
00033 void
00034 Thread::start()
00035 {
00036     QThread::start();
00037 
00038 #ifndef _WIN32
00039     struct sched_param param;
00040     ::memset(&param, 0, sizeof(param));
00041 
00042     if (m_type == RTThread) {
00043 
00044         param.sched_priority = 5;
00045 
00046         if (::pthread_setschedparam(pthread_self(), SCHED_FIFO, &param)) {
00047             ::perror("INFO: pthread_setschedparam to SCHED_FIFO failed");
00048         }
00049 
00050     } else {
00051 
00052         if (::pthread_setschedparam(pthread_self(), SCHED_OTHER, &param)) {
00053             ::perror("WARNING: pthread_setschedparam to SCHED_OTHER failed");
00054         }
00055     }        
00056 
00057 #endif
00058 }
00059 
00060 MutexLocker::MutexLocker(QMutex *mutex, const char *name) :
00061     m_printer(name),
00062     m_locker(mutex)
00063 {
00064 #ifdef DEBUG_MUTEX_LOCKER
00065     std::cerr << "... locked mutex " << mutex << std::endl;
00066 #endif
00067 }
00068 
00069 MutexLocker::~MutexLocker()
00070 {
00071 }
00072 
00073 MutexLocker::Printer::Printer(const char *name) :
00074     m_name(name)
00075 {
00076 #ifdef DEBUG_MUTEX_LOCKER
00077     std::cerr << "MutexLocker: Locking   \"" << m_name << "\" in "
00078               << (void *)QThread::currentThreadId() << std::endl;
00079 #endif
00080 }
00081 
00082 MutexLocker::Printer::~Printer()
00083 {
00084 #ifdef DEBUG_MUTEX_LOCKER
00085     std::cerr << "MutexLocker: Unlocking \"" << m_name
00086               << "\" in " << (void *)QThread::currentThreadId() << std::endl;
00087 #endif
00088 }
00089 

Generated on Wed Feb 20 15:45:29 2008 for SonicVisualiser by  doxygen 1.5.1