Loading...
sys/OSMemoryNotification.c Libc-997.90.3 Libc-583
--- Libc/Libc-997.90.3/sys/OSMemoryNotification.c
+++ Libc/Libc-583/sys/OSMemoryNotification.c
@@ -27,8 +27,6 @@
 #include <notify.h>
 #include <stdlib.h>
 #include <sys/time.h>
-#include <dispatch/dispatch.h>
-#include <dispatch/private.h>
 
 const char *kOSMemoryNotificationName = "com.apple.system.memorystatus";
 
@@ -71,6 +69,7 @@
 int
 OSMemoryNotificationWait(OSMemoryNotificationRef note, OSMemoryNotificationLevel *level)
 {
+
 	return OSMemoryNotificationTimedWait(note, level, NULL);
 }
 
@@ -96,40 +95,49 @@
 int
 OSMemoryNotificationTimedWait(OSMemoryNotificationRef note, OSMemoryNotificationLevel *level, const struct timeval *abstime)
 {
-    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
-    dispatch_retain(sema);
-    
-    dispatch_source_t memoryNotificationSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_VM, 0, DISPATCH_VM_PRESSURE, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
-    dispatch_source_set_event_handler(memoryNotificationSource, ^{
-        dispatch_semaphore_signal(sema);
-        dispatch_release(sema);
-    });
+	mach_msg_empty_rcv_t msg = {{0}};
+	mach_msg_return_t ret;
+	uint64_t val;
 
-    dispatch_resume(memoryNotificationSource);
-    
-    dispatch_time_t waitTime = DISPATCH_TIME_FOREVER;
-    if (abstime) {
-        mach_msg_timeout_t timeout = abs_to_timeout(abstime);
-        waitTime = dispatch_time(DISPATCH_TIME_NOW, timeout * 1e6);
-    }
-    
-    long ret = dispatch_semaphore_wait(sema, waitTime);
+	if (abstime) {
+		mach_msg_timeout_t timeout = abs_to_timeout(abstime);
 
-    dispatch_release(sema);
-    dispatch_release(memoryNotificationSource);
+		if (0 == timeout)
+			return ETIMEDOUT;
 
-    if (ret != 0) {
-        return ETIMEDOUT;
-    }
-    
-    if (level) {
-        *level = OSMemoryNotificationLevelUrgent;
-    }
-    
-    return 0;
+		ret = mach_msg(&msg.header, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0, sizeof msg, note->port, timeout, MACH_PORT_NULL);
+
+		if (MACH_RCV_TIMED_OUT == ret) {
+			return ETIMEDOUT;
+		} 
+	} else {
+		ret = mach_msg(&msg.header, MACH_RCV_MSG, 0, sizeof msg, note->port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
+	}
+
+	if (MACH_MSG_SUCCESS != ret)
+		return EINVAL;
+
+	if (NOTIFY_STATUS_OK != notify_get_state(note->token, &val))
+		return EINVAL;
+
+	*level = (OSMemoryNotificationLevel)val;
+
+	return 0;
 }
 
 OSMemoryNotificationLevel OSMemoryNotificationCurrentLevel(void)
 {
-	return OSMemoryNotificationLevelNormal;
+	uint64_t val;
+	int token;
+
+	if (NOTIFY_STATUS_OK != notify_register_check(kOSMemoryNotificationName, &token))
+		return OSMemoryNotificationLevelAny;
+
+	if (NOTIFY_STATUS_OK != notify_get_state(token, &val))
+		return OSMemoryNotificationLevelAny;
+
+	if (NOTIFY_STATUS_OK != notify_cancel(token))
+		return OSMemoryNotificationLevelAny;
+
+	return (OSMemoryNotificationLevel)val;
 }