Chris@109: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@109: 
Chris@109: /*
Chris@109:     Sonic Visualiser
Chris@109:     An audio file viewer and annotation editor.
Chris@109:     Centre for Digital Music, Queen Mary, University of London.
Chris@109:     This file copyright 2006 Chris Cannam.
Chris@109:     
Chris@109:     This program is free software; you can redistribute it and/or
Chris@109:     modify it under the terms of the GNU General Public License as
Chris@109:     published by the Free Software Foundation; either version 2 of the
Chris@109:     License, or (at your option) any later version.  See the file
Chris@109:     COPYING included with this distribution for more information.
Chris@109: */
Chris@109: 
Chris@109: #include "Thread.h"
Chris@109: 
Chris@109: #ifndef _WIN32
Chris@109: #include <pthread.h>
Chris@109: #endif
Chris@109: 
Chris@244: //#define DEBUG_MUTEX_LOCKER 1
Chris@244: 
Chris@244: #include <iostream>
Chris@244: 
Chris@109: Thread::Thread(Type type, QObject *parent) :
Chris@109:     QThread(parent),
Chris@109:     m_type(type)
Chris@109: {
Chris@109:     setStackSize(512 * 1024);
Chris@109: }
Chris@109: 
Chris@109: void
Chris@109: Thread::start()
Chris@109: {
Chris@109:     QThread::start();
Chris@109: 
Chris@109: #ifndef _WIN32
Chris@109:     struct sched_param param;
Chris@109:     ::memset(&param, 0, sizeof(param));
Chris@109: 
Chris@109:     if (m_type == RTThread) {
Chris@109: 
Chris@109:         param.sched_priority = 5;
Chris@109: 
Chris@109:         if (::pthread_setschedparam(pthread_self(), SCHED_FIFO, &param)) {
Chris@109:             ::perror("INFO: pthread_setschedparam to SCHED_FIFO failed");
Chris@109:         }
Chris@109: 
Chris@109:     } else {
Chris@109: 
Chris@109:         if (::pthread_setschedparam(pthread_self(), SCHED_OTHER, &param)) {
Chris@109:             ::perror("WARNING: pthread_setschedparam to SCHED_OTHER failed");
Chris@109:         }
Chris@109:     }        
Chris@109: 
Chris@109: #endif
Chris@109: }
Chris@109: 
Chris@244: MutexLocker::MutexLocker(QMutex *mutex, const char *name) :
Chris@408:     m_profiler(name, false),
Chris@244:     m_printer(name),
Chris@244:     m_locker(mutex)
Chris@244: {
Chris@244: #ifdef DEBUG_MUTEX_LOCKER
Chris@244:     std::cerr << "... locked mutex " << mutex << std::endl;
Chris@244: #endif
Chris@408:     m_profiler.end();
Chris@244: }
Chris@244: 
Chris@244: MutexLocker::~MutexLocker()
Chris@244: {
Chris@244: }
Chris@244: 
Chris@244: MutexLocker::Printer::Printer(const char *name) :
Chris@244:     m_name(name)
Chris@244: {
Chris@244: #ifdef DEBUG_MUTEX_LOCKER
Chris@244:     std::cerr << "MutexLocker: Locking   \"" << m_name << "\" in "
Chris@244:               << (void *)QThread::currentThreadId() << std::endl;
Chris@244: #endif
Chris@244: }
Chris@244: 
Chris@244: MutexLocker::Printer::~Printer()
Chris@244: {
Chris@244: #ifdef DEBUG_MUTEX_LOCKER
Chris@244:     std::cerr << "MutexLocker: Unlocking \"" << m_name
Chris@244:               << "\" in " << (void *)QThread::currentThreadId() << std::endl;
Chris@244: #endif
Chris@244: }
Chris@244: