Created
November 13, 2018 00:30
-
-
Save dmaclach/76401f489ac27768edc776116824d247 to your computer and use it in GitHub Desktop.
Sample code for https://openradar.appspot.com/radar?id=5030997057863680
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// signaltest | |
// | |
// Created by Dave MacLachlan on 11/12/18. | |
// Copyright © 2018 Dave MacLachlan. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <pthread.h> | |
int theSignal = SIGUSR1; | |
const char *theSignalName = "SIGUSR1"; | |
void print_sigs(const char *source) { | |
sigset_t sigmask; | |
sigemptyset(&sigmask); | |
pthread_sigmask(SIG_BLOCK, NULL, &sigmask); | |
pthread_t thread = pthread_self(); | |
struct sigaction action; | |
sigaction(theSignal, NULL, &action); | |
printf("%s: %p %s thread: %s proc: %s\n", source, thread, theSignalName, | |
sigismember(&sigmask, theSignal) ? "BLOCKED" : "UNBLOCKED", | |
action.sa_handler == SIG_IGN ? "Ignored" : (action.sa_handler == SIG_DFL ? "Default" : "Other")); | |
} | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
signal(theSignal, SIG_IGN); | |
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); | |
dispatch_queue_t mainQueue = dispatch_get_main_queue(); | |
dispatch_source_t signalSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, theSignal, 0, mainQueue); | |
dispatch_source_set_event_handler(signalSource, ^(void) { | |
printf("Received Signal\n"); | |
print_sigs("Signal"); | |
}); | |
dispatch_source_set_registration_handler(signalSource, ^{ | |
printf("Source Registered\n"); | |
}); | |
dispatch_source_set_cancel_handler(signalSource, ^{ | |
printf("Source Cancelled\n"); | |
}); | |
dispatch_resume(signalSource); | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1000000000), globalQueue, ^{ | |
printf("Raising From Global Queue\n"); | |
print_sigs("globalQueue"); | |
raise(theSignal); | |
printf("Raised From Global Queue\n"); | |
}); | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2000000000), mainQueue, ^{ | |
printf("Raising From Main Queue\n"); | |
print_sigs("mainQueue"); | |
raise(theSignal); | |
printf("Raised From Main Queue\n"); }); | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 6000000000), mainQueue, ^{ | |
dispatch_cancel(signalSource); | |
exit(0); | |
}); | |
print_sigs("CFRunLoopRun"); CFRunLoopRun(); | |
//print_sigs("dispatch_main"); dispatch_main(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment