Debian Codegraph @

src/engine/locking.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2020 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include <engine/locking.h>
14
15 #include <config.h>
16
17 using namespace smooth::Threads;
18
19 using namespace BoCA;
20
21 Int freac::Locking::allocatedThreads = 0;
22
23 Array<Bool> freac::Locking::deviceLocked;
24 Array<Bool> freac::Locking::outputLocked;
25
26 Mutex freac::Locking::managementMutex;
27
28 Bool freac::Locking::AllocateThread()
29 {
30 /* Get number of available threads.
31 */
32 BoCA::Config *config = BoCA::Config::Get();
33
34 Bool enableParallel = config->GetIntValue(Config::CategoryResourcesID, Config::ResourcesEnableParallelConversionsID, Config::ResourcesEnableParallelConversionsDefault);
35 Int numberOfThreads = config->GetIntValue(Config::CategoryResourcesID, Config::ResourcesNumberOfConversionThreadsID, Config::ResourcesNumberOfConversionThreadsDefault);
36
37 if (!enableParallel) numberOfThreads = 1;
38 else if (numberOfThreads <= 1) numberOfThreads = CPU().GetNumCores() + (CPU().GetNumLogicalCPUs() - CPU().GetNumCores()) / 2;
39
40 /* Allocate a thread and return.
41 */
42 while (True)
43 {
44 if (allocatedThreads >= numberOfThreads) { S::System::System::Sleep(1); continue; }
45
46 Lock lock(managementMutex);
47
48 if (allocatedThreads < numberOfThreads)
49 {
50 allocatedThreads++;
51
52 return True;
53 }
54 }
55 }
56
57 Bool freac::Locking::FreeThread()
58 {
59 /* Free a thread and return.
60 */
61 Lock lock(managementMutex);
62
63 if (allocatedThreads)
64 {
65 allocatedThreads--;
66
67 return True;
68 }
69
70 return False;
71 }
72
73 Bool freac::Locking::LockDeviceForTrack(const Track &track)
74 {
75 /* Check if the track is on a locked device.
76 */
77 if (track.fileName.StartsWith("device://"))
78 {
79 String device = track.fileName.SubString(9, track.fileName.Tail(track.fileName.Length() - 9).Find("/"));
80 Lock lock(managementMutex);
81
82 if (deviceLocked.Get(device.ComputeCRC32())) return False;
83
84 deviceLocked.Add(True, device.ComputeCRC32());
85 }
86
87 /* Treat .cda files and CDDA volumes like CD tracks.
88 */
89 String filePath = File(track.fileName).GetFilePath();
90
91 #if defined __APPLE__
92 if (track.fileName.EndsWith(".aiff") && Directory(filePath).GetDirectoryPath() == "/Volumes" && File(String(filePath).Append("/.TOC.plist")).Exists())
93 #elif defined __HAIKU__
94 if (track.fileName.EndsWith(".wav") && filePath != NIL && Directory(filePath).GetDirectoryPath() == NIL)
95 #else
96 if (track.fileName.EndsWith(".cda"))
97 #endif
98 {
99 Lock lock(managementMutex);
100
101 if (deviceLocked.Get(filePath.ComputeCRC32())) return False;
102
103 deviceLocked.Add(True, filePath.ComputeCRC32());
104 }
105
106 return True;
107 }
108
109 Bool freac::Locking::UnlockDeviceForTrack(const Track &track)
110 {
111 /* Unlock track device if necessary.
112 */
113 if (track.fileName.StartsWith("device://"))
114 {
115 String device = track.fileName.SubString(9, track.fileName.Tail(track.fileName.Length() - 9).Find("/"));
116 Lock lock(managementMutex);
117
118 deviceLocked.Remove(device.ComputeCRC32());
119 }
120
121 /* Treat .cda files and CDDA volumes like CD tracks.
122 */
123 String filePath = File(track.fileName).GetFilePath();
124
125 #if defined __APPLE__
126 if (track.fileName.EndsWith(".aiff") && Directory(filePath).GetDirectoryPath() == "/Volumes" && File(String(filePath).Append("/.TOC.plist")).Exists())
127 #elif defined __HAIKU__
128 if (track.fileName.EndsWith(".wav") && filePath != NIL && Directory(filePath).GetDirectoryPath() == NIL)
129 #else
130 if (track.fileName.EndsWith(".cda"))
131 #endif
132 {
133 Lock lock(managementMutex);
134
135 deviceLocked.Remove(filePath.ComputeCRC32());
136 }
137
138 return True;
139 }
140
141 Bool freac::Locking::LockOutputForTrack(const Track &track)
142 {
143 /* Check if the track output file is currently locked.
144 */
145 if (track.outputFile != NIL)
146 {
147 Lock lock(managementMutex);
148
149 if (outputLocked.Get(track.outputFile.ComputeCRC32())) return False;
150
151 outputLocked.Add(True, track.outputFile.ComputeCRC32());
152 }
153
154 return True;
155 }
156
157 Bool freac::Locking::UnlockOutputForTrack(const Track &track)
158 {
159 /* Unlock track output file if necessary.
160 */
161 if (track.outputFile != NIL)
162 {
163 Lock lock(managementMutex);
164
165 outputLocked.Remove(track.outputFile.ComputeCRC32());
166 }
167
168 return True;
169 }