]> git.saurik.com Git - apple/libpthread.git/blobdiff - src/qos.c
libpthread-137.1.1.tar.gz
[apple/libpthread.git] / src / qos.c
index e9c999398efa9604518351259b0c7c46190b94dd..485b93bc0373447cc177d1ed8a88973356f80c6b 100644 (file)
--- a/src/qos.c
+++ b/src/qos.c
@@ -42,8 +42,8 @@ static pthread_priority_t _main_qos = QOS_CLASS_UNSPECIFIED;
 struct pthread_override_s
 {
        uint32_t sig;
-       pthread_t pthread;
        mach_port_t kthread;
+       pthread_t pthread;
        pthread_priority_t priority;
        bool malloced;
 };
@@ -344,6 +344,20 @@ pthread_set_fixedpriority_self(void)
        }
 }
 
+int
+pthread_set_timeshare_self(void)
+{
+       if (!(__pthread_supported_features & PTHREAD_FEATURE_BSDTHREADCTL)) {
+               return ENOTSUP;
+       }
+       
+       if (__pthread_supported_features & PTHREAD_FEATURE_SETSELF) {
+               return _pthread_set_properties_self(_PTHREAD_SET_SELF_TIMESHARE_FLAG, 0, 0);
+       } else {
+               return ENOTSUP;
+       }
+}
+
 
 pthread_override_t
 pthread_override_qos_class_start_np(pthread_t __pthread,  qos_class_t __qos_class, int __relative_priority)
@@ -383,7 +397,7 @@ pthread_override_qos_class_start_np(pthread_t __pthread,  qos_class_t __qos_clas
        }
 
        if (res == 0) {
-               res = __bsdthread_ctl(BSDTHREAD_CTL_QOS_OVERRIDE_START, rv->kthread, rv->priority, 0);
+               res = __bsdthread_ctl(BSDTHREAD_CTL_QOS_OVERRIDE_START, rv->kthread, rv->priority, (uintptr_t)rv);
 
                if (res != 0) {
                        mach_port_mod_refs(mach_task_self(), rv->kthread, MACH_PORT_RIGHT_SEND, -1);
@@ -415,7 +429,7 @@ pthread_override_qos_class_end_np(pthread_override_t override)
        override->sig = PTHREAD_OVERRIDE_SIG_DEAD;
 
        /* Always consumes (and deallocates) the pthread_override_t object given. */
-       res = __bsdthread_ctl(BSDTHREAD_CTL_QOS_OVERRIDE_END, override->kthread, 0, 0);
+       res = __bsdthread_ctl(BSDTHREAD_CTL_QOS_OVERRIDE_END, override->kthread, (uintptr_t)override, 0);
        if (res == -1) { res = errno; }
 
        /* EFAULT from the syscall means we underflowed. Crash here. */
@@ -446,7 +460,8 @@ pthread_override_qos_class_end_np(pthread_override_t override)
 int
 _pthread_override_qos_class_start_direct(mach_port_t thread, pthread_priority_t priority)
 {
-       int res = __bsdthread_ctl(BSDTHREAD_CTL_QOS_OVERRIDE_START, thread, priority, 0);
+       // use pthread_self as the default per-thread memory allocation to track the override in the kernel
+       int res = __bsdthread_ctl(BSDTHREAD_CTL_QOS_OVERRIDE_START, thread, priority, (uintptr_t)pthread_self());
        if (res == -1) { res = errno; }
        return res;
 }
@@ -454,7 +469,8 @@ _pthread_override_qos_class_start_direct(mach_port_t thread, pthread_priority_t
 int
 _pthread_override_qos_class_end_direct(mach_port_t thread)
 {
-       int res = __bsdthread_ctl(BSDTHREAD_CTL_QOS_OVERRIDE_END, thread, 0, 0);
+       // use pthread_self as the default per-thread memory allocation to track the override in the kernel
+       int res = __bsdthread_ctl(BSDTHREAD_CTL_QOS_OVERRIDE_END, thread, (uintptr_t)pthread_self(), 0);
        if (res == -1) { res = errno; }
        return res;
 }
@@ -475,6 +491,36 @@ _pthread_workqueue_override_reset(void)
        return res;
 }
 
+int
+_pthread_workqueue_asynchronous_override_add(mach_port_t thread, pthread_priority_t priority, void *resource)
+{
+       int res = __bsdthread_ctl(BSDTHREAD_CTL_QOS_DISPATCH_ASYNCHRONOUS_OVERRIDE_ADD, thread, priority, (uintptr_t)resource);
+       if (res == -1) { res = errno; }
+       return res;
+}
+
+int
+_pthread_workqueue_asynchronous_override_reset_self(void *resource)
+{
+       int res = __bsdthread_ctl(BSDTHREAD_CTL_QOS_DISPATCH_ASYNCHRONOUS_OVERRIDE_RESET,
+                                                         0 /* !reset_all */,
+                                                         (uintptr_t)resource,
+                                                         0);
+       if (res == -1) { res = errno; }
+       return res;
+}
+
+int
+_pthread_workqueue_asynchronous_override_reset_all_self(void)
+{
+       int res = __bsdthread_ctl(BSDTHREAD_CTL_QOS_DISPATCH_ASYNCHRONOUS_OVERRIDE_RESET,
+                                                         1 /* reset_all */,
+                                                         0,
+                                                         0);
+       if (res == -1) { res = errno; }
+       return res;
+}
+
 int
 posix_spawnattr_set_qos_class_np(posix_spawnattr_t * __restrict __attr, qos_class_t __qos_class)
 {