Jack2  1.9.9
JackMMCSS.cpp
1 /*
2  Copyright (C) 2004-2008 Grame
3 
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.
8 
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.
13 
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.
17 
18  */
19 
20 #include "JackMMCSS.h"
21 #include "JackError.h"
22 #include <assert.h>
23 #include <stdio.h>
24 
25 namespace Jack
26 {
27 
28 avSetMmThreadCharacteristics JackMMCSS::ffMMCSSFun1 = NULL;
29 avSetMmThreadPriority JackMMCSS::ffMMCSSFun2 = NULL;
30 avRevertMmThreadCharacteristics JackMMCSS::ffMMCSSFun3 = NULL;
31 JACK_HANDLE JackMMCSS::fAvrtDll;
32 
33 std::map<jack_native_thread_t, HANDLE> JackMMCSS::fHandleTable;
34 
35 JackMMCSS::JackMMCSS()
36 {
37  fAvrtDll = LoadJackModule("avrt.dll");
38 
39  if (fAvrtDll != NULL) {
40  ffMMCSSFun1 = (avSetMmThreadCharacteristics)GetJackProc(fAvrtDll, "AvSetMmThreadCharacteristicsA");
41  ffMMCSSFun2 = (avSetMmThreadPriority)GetJackProc(fAvrtDll, "AvSetMmThreadPriority");
42  ffMMCSSFun3 = (avRevertMmThreadCharacteristics)GetJackProc(fAvrtDll, "AvRevertMmThreadCharacteristics");
43  }
44 }
45 
46 JackMMCSS::~JackMMCSS()
47 {}
48 
49 int JackMMCSS::MMCSSAcquireRealTime(jack_native_thread_t thread)
50 {
51  if (fHandleTable.find(thread) != fHandleTable.end()) {
52  return 0;
53  }
54 
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());
66  }
67  }
68 
69  return -1;
70 }
71 
72 int JackMMCSS::MMCSSDropRealTime(jack_native_thread_t thread)
73 {
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");
80  }
81  return 0;
82  } else {
83  return -1;
84  }
85 }
86 
87 }
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:107