Skip to content

Commit 9208a63

Browse files
committed
Implement _dispatch_pp_to_nice
1 parent aabf51d commit 9208a63

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/shims/priority.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,46 @@ _dispatch_qos_to_pp(dispatch_qos_t qos)
210210
return pp | _PTHREAD_PRIORITY_PRIORITY_MASK;
211211
}
212212

213+
214+
#if defined(__linux__)
215+
// These presets roughly match the `android.os.Process' constants
216+
// used for `setThreadPriority()'.
217+
//
218+
// Be aware that with the Completely Fair Scheduler (CFS) the weight is computed
219+
// as 1024 / (1.25) ^ (nice) where nice is in the range -20 to 19.
220+
// This means that nice is not a linear scale.
221+
#define DISPATCH_NICE_BACKGROUND 10
222+
#define DISPATCH_NICE_UTILITY 2
223+
#define DISPATCH_NICE_DEFAULT 0
224+
// Note that you might not have permission to increase the priority
225+
// of a thread beyond the default priority.
226+
#define DISPATCH_NICE_USER_INITIATED -2
227+
#define DISPATCH_NICE_USER_INTERACTIVE -4
228+
229+
DISPATCH_ALWAYS_INLINE
230+
static inline int _dispatch_pp_to_nice(pthread_priority_t pp)
231+
{
232+
// FIXME: What about relative priorities?
233+
uint32_t qos = _dispatch_qos_from_pp(pp);
234+
235+
switch (qos) {
236+
case DISPATCH_QOS_BACKGROUND:
237+
return DISPATCH_NICE_BACKGROUND;
238+
case DISPATCH_QOS_UTILITY:
239+
return DISPATCH_NICE_UTILITY;
240+
case DISPATCH_QOS_DEFAULT:
241+
return DISPATCH_NICE_DEFAULT;
242+
case DISPATCH_QOS_USER_INITIATED:
243+
return DISPATCH_NICE_USER_INITIATED;
244+
case DISPATCH_QOS_USER_INTERACTIVE:
245+
return DISPATCH_NICE_USER_INTERACTIVE;
246+
}
247+
248+
return DISPATCH_NICE_DEFAULT;
249+
}
250+
#endif // defined(__linux__)
251+
252+
213253
// including maintenance
214254
DISPATCH_ALWAYS_INLINE
215255
static inline bool

0 commit comments

Comments
 (0)