Skip to content

Commit

Permalink
Wait for pending API calls before exiting
Browse files Browse the repository at this point in the history
  • Loading branch information
Goshin committed Jul 26, 2020
1 parent 9fbc865 commit ae5f199
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
11 changes: 10 additions & 1 deletion ClientKit/Api.c
Expand Up @@ -17,7 +17,7 @@
#include "mach/mach_port.h"
#include "pthread.h"

static pthread_mutex_t* api_mutex;
static pthread_mutex_t* api_mutex = NULL;

bool get_platform_info(platform_info_t *info) {
memset(info, 0, sizeof(platform_info_t));
Expand Down Expand Up @@ -273,3 +273,12 @@ kern_return_t dis_associate_ssid(const char *ssid)
memcpy(dis.ssid, ssid, 32);
return ioctl_set(IOCTL_80211_DISASSOCIATE, &dis, sizeof(struct ioctl_disassociate));
}

void api_terminate(void) {
if (api_mutex) {
/* acquire API lock to wait for the pending API call */
pthread_mutex_lock(api_mutex);
pthread_mutex_unlock(api_mutex);
pthread_mutex_destroy(api_mutex);
}
}
2 changes: 2 additions & 0 deletions ClientKit/Api.h
Expand Up @@ -74,4 +74,6 @@ kern_return_t associate_ssid(const char *ssid, const char *pwd);

kern_return_t dis_associate_ssid(const char *ssid);

void api_terminate(void);

#endif /* Api_h */
7 changes: 6 additions & 1 deletion HeliPort/AppDelegate.swift
Expand Up @@ -16,7 +16,7 @@
import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {

checkRunPath()
Expand Down Expand Up @@ -110,4 +110,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
NSApp.terminate(nil)
#endif
}

func applicationWillTerminate(_ notification: Notification) {
Log.debug("Exit")
api_terminate()
}
}
2 changes: 1 addition & 1 deletion HeliPort/Appearance/StatusMenu.swift
Expand Up @@ -445,7 +445,7 @@ final class StatusMenu: NSMenu, NSMenuDelegate {
NSApplication.shared.orderFrontStandardAboutPanel()
NSApplication.shared.activate(ignoringOtherApps: true)
case NSLocalizedString("Quit HeliPort"):
exit(0)
NSApp.terminate(nil)
default:
Log.error("Invalid menu item clicked")
}
Expand Down
2 changes: 1 addition & 1 deletion HeliPort/Info.plist
Expand Up @@ -33,7 +33,7 @@
<key>NSSupportsAutomaticTermination</key>
<true/>
<key>NSSupportsSuddenTermination</key>
<true/>
<false/>
<key>SUFeedURL</key>
<string>https://heliport.bat-bat.workers.dev/https://github.com/zxystd/HeliPort/releases/latest/download/appcast.xml</string>
<key>SUPublicEDKey</key>
Expand Down

0 comments on commit ae5f199

Please sign in to comment.