Jack2 1.9.10

JackMMCSS.cpp

00001 /*
00002  Copyright (C) 2004-2008 Grame
00003 
00004  This program is free software; you can redistribute it and/or modify
00005  it under the terms of the GNU Lesser General Public License as published by
00006  the Free Software Foundation; either version 2.1 of the License, or
00007  (at your option) any later version.
00008 
00009  This program is distributed in the hope that it will be useful,
00010  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  GNU Lesser General Public License for more details.
00013 
00014  You should have received a copy of the GNU Lesser General Public License
00015  along with this program; if not, write to the Free Software
00016  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 
00018  */
00019 
00020 #include "JackMMCSS.h"
00021 #include "JackError.h"
00022 #include <assert.h>
00023 #include <stdio.h>
00024 
00025 namespace Jack
00026 {
00027 
00028 avSetMmThreadCharacteristics JackMMCSS::ffMMCSSFun1 = NULL;
00029 avSetMmThreadPriority JackMMCSS::ffMMCSSFun2 = NULL;
00030 avRevertMmThreadCharacteristics JackMMCSS::ffMMCSSFun3 = NULL;
00031 JACK_HANDLE JackMMCSS::fAvrtDll;
00032 
00033 std::map<jack_native_thread_t, HANDLE> JackMMCSS::fHandleTable;
00034 
00035 JackMMCSS::JackMMCSS()
00036 {
00037     fAvrtDll = LoadJackModule("avrt.dll");
00038 
00039     if (fAvrtDll != NULL) {
00040         ffMMCSSFun1 = (avSetMmThreadCharacteristics)GetJackProc(fAvrtDll, "AvSetMmThreadCharacteristicsA");
00041         ffMMCSSFun2 = (avSetMmThreadPriority)GetJackProc(fAvrtDll, "AvSetMmThreadPriority");
00042         ffMMCSSFun3 = (avRevertMmThreadCharacteristics)GetJackProc(fAvrtDll, "AvRevertMmThreadCharacteristics");
00043     }
00044 }
00045 
00046 JackMMCSS::~JackMMCSS()
00047 {}
00048 
00049 int JackMMCSS::MMCSSAcquireRealTime(jack_native_thread_t thread)
00050 {
00051     if (fHandleTable.find(thread) != fHandleTable.end()) {
00052         return 0;
00053     }
00054 
00055     if (ffMMCSSFun1) {
00056         DWORD dummy = 0;
00057         HANDLE task = ffMMCSSFun1("Pro Audio", &dummy);
00058         if (task == NULL) {
00059             jack_error("AvSetMmThreadCharacteristics error : %d", GetLastError());
00060         } else if (ffMMCSSFun2(task, AVRT_PRIORITY_CRITICAL)) {
00061             fHandleTable[thread] = task;
00062             jack_log("AvSetMmThreadPriority success");
00063             return 0;
00064         } else {
00065             jack_error("AvSetMmThreadPriority error : %d", GetLastError());
00066         }
00067      }
00068 
00069      return -1;
00070 }
00071 
00072 int JackMMCSS::MMCSSDropRealTime(jack_native_thread_t thread)
00073 {
00074     if (fHandleTable.find(thread) != fHandleTable.end()) {
00075         HANDLE task = fHandleTable[thread];
00076         if (ffMMCSSFun3(task) == 0) {
00077             jack_error("AvRevertMmThreadCharacteristics error : %d", GetLastError());
00078         } else {
00079             jack_log("AvRevertMmThreadCharacteristics success");
00080         }
00081         return 0;
00082     } else {
00083         return -1;
00084     }
00085 }
00086 
00087 }