app_darwin.m 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #import <Cocoa/Cocoa.h>
  2. #import "AppDelegate.h"
  3. #import "app_darwin.h"
  4. void run() {
  5. @autoreleasepool {
  6. [NSApplication sharedApplication];
  7. AppDelegate *appDelegate = [[AppDelegate alloc] init];
  8. [NSApp setDelegate:appDelegate];
  9. [NSApp run];
  10. }
  11. }
  12. // killOtherInstances kills all other instances of the app currently
  13. // running. This way we can ensure that only the most recently started
  14. // instance of Ollama is running
  15. void killOtherInstances() {
  16. pid_t pid = getpid();
  17. NSArray *all = [[NSWorkspace sharedWorkspace] runningApplications];
  18. NSMutableArray *apps = [NSMutableArray array];
  19. for (NSRunningApplication *app in all) {
  20. if ([app.bundleIdentifier isEqualToString:[[NSBundle mainBundle] bundleIdentifier]] ||
  21. [app.bundleIdentifier isEqualToString:@"ai.ollama.ollama"] ||
  22. [app.bundleIdentifier isEqualToString:@"com.electron.ollama"]) {
  23. if (app.processIdentifier != pid) {
  24. [apps addObject:app];
  25. }
  26. }
  27. }
  28. for (NSRunningApplication *app in apps) {
  29. kill(app.processIdentifier, SIGTERM);
  30. }
  31. for (NSRunningApplication *app in apps) {
  32. while (!app.terminated) {
  33. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  34. }
  35. }
  36. }