Fixes for alsa device reservation
[jack2.git] / windows / JackMMCSS.cpp
blobdc8897df2293f72d5101695a9fb1a2e0b43ae3e9
1 /*
2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "JackMMCSS.h"
21 #include "JackError.h"
22 #include <assert.h>
23 #include <stdio.h>
25 namespace Jack
28 avSetMmThreadCharacteristics JackMMCSS::ffMMCSSFun1 = NULL;
29 avSetMmThreadPriority JackMMCSS::ffMMCSSFun2 = NULL;
30 avRevertMmThreadCharacteristics JackMMCSS::ffMMCSSFun3 = NULL;
31 JACK_HANDLE JackMMCSS::fAvrtDll;
33 std::map<jack_native_thread_t, HANDLE> JackMMCSS::fHandleTable;
35 JackMMCSS::JackMMCSS()
37 fAvrtDll = LoadJackModule("avrt.dll");
39 if (fAvrtDll != NULL) {
40 ffMMCSSFun1 = (avSetMmThreadCharacteristics)GetJackProc(fAvrtDll, "AvSetMmThreadCharacteristicsA");
41 ffMMCSSFun2 = (avSetMmThreadPriority)GetJackProc(fAvrtDll, "AvSetMmThreadPriority");
42 ffMMCSSFun3 = (avRevertMmThreadCharacteristics)GetJackProc(fAvrtDll, "AvRevertMmThreadCharacteristics");
46 JackMMCSS::~JackMMCSS()
49 int JackMMCSS::MMCSSAcquireRealTime(jack_native_thread_t thread)
51 if (fHandleTable.find(thread) != fHandleTable.end()) {
52 return 0;
55 if (ffMMCSSFun1) {
56 DWORD dummy = 0;
57 HANDLE task = ffMMCSSFun1("Pro Audio", &dummy);
58 if (task == NULL) {
59 jack_error("AvSetMmThreadCharacteristics error : %d", GetLastError());
60 } else if (ffMMCSSFun2(task, AVRT_PRIORITY_CRITICAL)) {
61 fHandleTable[thread] = task;
62 jack_log("AvSetMmThreadPriority success");
63 return 0;
64 } else {
65 jack_error("AvSetMmThreadPriority error : %d", GetLastError());
69 return -1;
72 int JackMMCSS::MMCSSDropRealTime(jack_native_thread_t thread)
74 if (fHandleTable.find(thread) != fHandleTable.end()) {
75 HANDLE task = fHandleTable[thread];
76 if (ffMMCSSFun3(task) == 0) {
77 jack_error("AvRevertMmThreadCharacteristics error : %d", GetLastError());
78 } else {
79 jack_log("AvRevertMmThreadCharacteristics success");
81 return 0;
82 } else {
83 return -1;