]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/mk_timer.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / osfmk / kern / mk_timer.c
index f968d1898e58cd9721f0abb3aced183aa6319553..883a1e31b745c139ffe62a72140019b09e0ae52b 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
- * 
+ *
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * unlawful or unlicensed copies of an Apple operating system, or to
  * circumvent, violate, or enable the circumvention or violation of, any
  * terms of an Apple operating system software license agreement.
- * 
+ *
  * Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this file.
- * 
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
@@ -22,7 +22,7 @@
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
 
 #include <ipc/ipc_space.h>
 
+#include <kern/lock_group.h>
 #include <kern/mk_timer.h>
 #include <kern/thread_call.h>
 
-static zone_t          mk_timer_zone;
+static zone_t           mk_timer_zone;
 
 static mach_port_qos_t mk_timer_qos = {
-       FALSE, TRUE, 0, sizeof (mk_timer_expire_msg_t)
+       FALSE, TRUE, 0, sizeof(mk_timer_expire_msg_t)
 };
 
-static void    mk_timer_expire(
-                               void                    *p0,
-                               void                    *p1);
+static void     mk_timer_expire(
+       void                    *p0,
+       void                    *p1);
 
 mach_port_name_t
 mk_timer_create_trap(
        __unused struct mk_timer_create_trap_args *args)
 {
-       mk_timer_t                      timer;
-       ipc_space_t                     myspace = current_space();
-       mach_port_name_t        name = MACH_PORT_NULL;
-       ipc_port_t                      port;
-       kern_return_t           result;
+       mk_timer_t                      timer;
+       ipc_space_t                     myspace = current_space();
+       mach_port_name_t        name = MACH_PORT_NULL;
+       ipc_port_t                      port;
+       kern_return_t           result;
 
        timer = (mk_timer_t)zalloc(mk_timer_zone);
-       if (timer == NULL)
-               return (MACH_PORT_NULL);
+       if (timer == NULL) {
+               return MACH_PORT_NULL;
+       }
 
        result = mach_port_allocate_qos(myspace, MACH_PORT_RIGHT_RECEIVE,
-                                       &mk_timer_qos, &name);
-       if (result == KERN_SUCCESS)
+           &mk_timer_qos, &name);
+       if (result == KERN_SUCCESS) {
                result = ipc_port_translate_receive(myspace, name, &port);
+       }
 
        if (result != KERN_SUCCESS) {
                zfree(mk_timer_zone, timer);
 
-               return (MACH_PORT_NULL);
+               return MACH_PORT_NULL;
        }
 
        simple_lock_init(&timer->lock, 0);
@@ -92,28 +95,29 @@ mk_timer_create_trap(
        ip_reference(port);
        ip_unlock(port);
 
-       return (name);
+       return name;
 }
 
 void
 mk_timer_port_destroy(
-       ipc_port_t                      port)
+       ipc_port_t                      port)
 {
-       mk_timer_t                      timer = NULL;
+       mk_timer_t                      timer = NULL;
 
        ip_lock(port);
        if (ip_kotype(port) == IKOT_TIMER) {
                timer = (mk_timer_t)port->ip_kobject;
                assert(timer != NULL);
                ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE);
-               simple_lock(&timer->lock);
+               simple_lock(&timer->lock, LCK_GRP_NULL);
                assert(timer->port == port);
        }
        ip_unlock(port);
 
        if (timer != NULL) {
-               if (thread_call_cancel(&timer->call_entry))
+               if (thread_call_cancel(&timer->call_entry)) {
                        timer->active--;
+               }
                timer->is_armed = FALSE;
 
                timer->is_dead = TRUE;
@@ -132,7 +136,7 @@ mk_timer_port_destroy(
 void
 mk_timer_init(void)
 {
-       int                     s = sizeof (mk_timer_data_t);
+       int                     s = sizeof(mk_timer_data_t);
 
        assert(!(mk_timer_zone != NULL));
 
@@ -143,13 +147,13 @@ mk_timer_init(void)
 
 static void
 mk_timer_expire(
-       void                    *p0,
-       __unused void           *p1)
+       void                    *p0,
+       __unused void           *p1)
 {
-       mk_timer_t                      timer = p0;
-       ipc_port_t                      port;
+       mk_timer_t                      timer = p0;
+       ipc_port_t                      port;
 
-       simple_lock(&timer->lock);
+       simple_lock(&timer->lock, LCK_GRP_NULL);
 
        if (timer->active > 1) {
                timer->active--;
@@ -162,7 +166,7 @@ mk_timer_expire(
        assert(timer->active == 1);
 
        while (timer->is_armed && timer->active == 1) {
-               mk_timer_expire_msg_t           msg;
+               mk_timer_expire_msg_t           msg;
 
                timer->is_armed = FALSE;
                simple_unlock(&timer->lock);
@@ -176,9 +180,9 @@ mk_timer_expire(
 
                msg.unused[0] = msg.unused[1] = msg.unused[2] = 0;
 
-               (void) mach_msg_send_from_kernel_proper(&msg.header, sizeof (msg));
+               (void) mach_msg_send_from_kernel_proper(&msg.header, sizeof(msg));
 
-               simple_lock(&timer->lock);
+               simple_lock(&timer->lock, LCK_GRP_NULL);
        }
 
        if (--timer->active == 0 && timer->is_dead) {
@@ -201,32 +205,32 @@ mk_timer_expire(
  *
  *
  * Returns:        0                      Success
- *                !0                      Not success           
+ *                !0                      Not success
  *
  */
 kern_return_t
 mk_timer_destroy_trap(
        struct mk_timer_destroy_trap_args *args)
 {
-       mach_port_name_t        name = args->name;
-       ipc_space_t                     myspace = current_space();
-       ipc_port_t                      port;
-       kern_return_t           result;
+       mach_port_name_t        name = args->name;
+       ipc_space_t                     myspace = current_space();
+       ipc_port_t                      port;
+       kern_return_t           result;
 
        result = ipc_port_translate_receive(myspace, name, &port);
-       if (result != KERN_SUCCESS)
-               return (result);
+       if (result != KERN_SUCCESS) {
+               return result;
+       }
 
        if (ip_kotype(port) == IKOT_TIMER) {
                ip_unlock(port);
                result = mach_port_destroy(myspace, name);
-       }
-       else {
+       } else {
                ip_unlock(port);
                result = KERN_INVALID_ARGUMENT;
        }
 
-       return (result);
+       return result;
 }
 
 /*
@@ -239,26 +243,28 @@ mk_timer_destroy_trap(
  *
  *
  * Returns:        0                      Success
- *                !0                      Not success           
+ *                !0                      Not success
  *
  */
 
 static kern_return_t
-mk_timer_arm_trap_internal(mach_port_name_t name, uint64_t expire_time, uint64_t mk_leeway, uint64_t mk_timer_flags) {
-       mk_timer_t                      timer;
-       ipc_space_t                     myspace = current_space();
-       ipc_port_t                      port;
-       kern_return_t                   result;
+mk_timer_arm_trap_internal(mach_port_name_t name, uint64_t expire_time, uint64_t mk_leeway, uint64_t mk_timer_flags)
+{
+       mk_timer_t                      timer;
+       ipc_space_t                     myspace = current_space();
+       ipc_port_t                      port;
+       kern_return_t                   result;
 
        result = ipc_port_translate_receive(myspace, name, &port);
-       if (result != KERN_SUCCESS)
-               return (result);
+       if (result != KERN_SUCCESS) {
+               return result;
+       }
 
        if (ip_kotype(port) == IKOT_TIMER) {
                timer = (mk_timer_t)port->ip_kobject;
                assert(timer != NULL);
 
-               simple_lock(&timer->lock);
+               simple_lock(&timer->lock, LCK_GRP_NULL);
                assert(timer->port == port);
                ip_unlock(port);
 
@@ -277,14 +283,14 @@ mk_timer_arm_trap_internal(mach_port_name_t name, uint64_t expire_time, uint64_t
                                }
 
                                if (!thread_call_enter_delayed_with_leeway(
-                                       &timer->call_entry, NULL,
-                                       expire_time, mk_leeway, tcflags)) {
-
+                                           &timer->call_entry, NULL,
+                                           expire_time, mk_leeway, tcflags)) {
                                        timer->active++;
                                }
                        } else {
-                               if (!thread_call_enter1(&timer->call_entry, NULL))
+                               if (!thread_call_enter1(&timer->call_entry, NULL)) {
                                        timer->active++;
+                               }
                        }
                }
 
@@ -293,16 +299,18 @@ mk_timer_arm_trap_internal(mach_port_name_t name, uint64_t expire_time, uint64_t
                ip_unlock(port);
                result = KERN_INVALID_ARGUMENT;
        }
-       return (result);
+       return result;
 }
 
 kern_return_t
-mk_timer_arm_trap(struct mk_timer_arm_trap_args *args) {
+mk_timer_arm_trap(struct mk_timer_arm_trap_args *args)
+{
        return mk_timer_arm_trap_internal(args->name, args->expire_time, 0, MK_TIMER_NORMAL);
 }
 
 kern_return_t
-mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args *args) {
+mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args *args)
+{
        return mk_timer_arm_trap_internal(args->name, args->expire_time, args->mk_leeway, args->mk_timer_flags);
 }
 
@@ -316,51 +324,54 @@ mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args *args) {
  *
  *
  * Returns:        0                      Success
- *                !0                      Not success           
+ *                !0                      Not success
  *
  */
 kern_return_t
 mk_timer_cancel_trap(
        struct mk_timer_cancel_trap_args *args)
 {
-       mach_port_name_t        name = args->name;
-       mach_vm_address_t       result_time_addr = args->result_time; 
-       uint64_t                        armed_time = 0;
-       mk_timer_t                      timer;
-       ipc_space_t                     myspace = current_space();
-       ipc_port_t                      port;
-       kern_return_t           result;
+       mach_port_name_t        name = args->name;
+       mach_vm_address_t       result_time_addr = args->result_time;
+       uint64_t                        armed_time = 0;
+       mk_timer_t                      timer;
+       ipc_space_t                     myspace = current_space();
+       ipc_port_t                      port;
+       kern_return_t           result;
 
        result = ipc_port_translate_receive(myspace, name, &port);
-       if (result != KERN_SUCCESS)
-               return (result);
+       if (result != KERN_SUCCESS) {
+               return result;
+       }
 
        if (ip_kotype(port) == IKOT_TIMER) {
                timer = (mk_timer_t)port->ip_kobject;
                assert(timer != NULL);
-               simple_lock(&timer->lock);
+               simple_lock(&timer->lock, LCK_GRP_NULL);
                assert(timer->port == port);
                ip_unlock(port);
 
                if (timer->is_armed) {
                        armed_time = timer->call_entry.tc_call.deadline;
-                       if (thread_call_cancel(&timer->call_entry))
+                       if (thread_call_cancel(&timer->call_entry)) {
                                timer->active--;
+                       }
                        timer->is_armed = FALSE;
                }
 
                simple_unlock(&timer->lock);
-       }
-       else {
+       } else {
                ip_unlock(port);
                result = KERN_INVALID_ARGUMENT;
        }
 
-       if (result == KERN_SUCCESS)
-               if (    result_time_addr != 0                                                                           &&
-                               copyout((void *)&armed_time, result_time_addr,
-                                                               sizeof (armed_time)) != 0                                       )
+       if (result == KERN_SUCCESS) {
+               if (result_time_addr != 0 &&
+                   copyout((void *)&armed_time, result_time_addr,
+                   sizeof(armed_time)) != 0) {
                        result = KERN_FAILURE;
+               }
+       }
 
-       return (result);
+       return result;
 }