-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
os: add a convenient way to ignore certain system signals (#19639)
- Loading branch information
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[has_globals] | ||
module os | ||
|
||
#flag -lpthread | ||
#include <signal.h> | ||
|
||
fn C.pthread_self() usize | ||
|
||
// g_main_thread_id and is_main_thread can be used to determine if the current thread is the main thread. | ||
// if need to get the tid of the main thread, can use the global variable g_main_thread_id | ||
// instead of using thread_id() every time. | ||
__global g_main_thread_id = u64(C.pthread_self()) | ||
|
||
// is_main_thread returns whether the current thread is the main thread. | ||
pub fn is_main_thread() bool { | ||
return g_main_thread_id == u64(C.pthread_self()) | ||
} | ||
|
||
fn C.sigaddset(set &u32, signum int) int | ||
fn C.sigemptyset(set &u32) | ||
fn C.sigprocmask(how int, set &u32, oldset &u32) int | ||
|
||
fn signal_ignore_internal(args ...Signal) { | ||
mask1 := u32(0) | ||
C.sigemptyset(&mask1) | ||
for arg in args { | ||
C.sigaddset(&mask1, int(arg)) | ||
} | ||
C.sigprocmask(C.SIG_BLOCK, &mask1, unsafe { nil }) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
[has_globals] | ||
module os | ||
|
||
#flag -lpthread | ||
#include <signal.h> | ||
|
||
fn C.pthread_self() usize | ||
|
||
// g_main_thread_id and is_main_thread can be used to determine if the current thread is the main thread. | ||
// if need to get the tid of the main thread, can use the global variable g_main_thread_id | ||
// instead of using thread_id() every time. | ||
__global g_main_thread_id = u64(C.pthread_self()) | ||
|
||
// is_main_thread returns whether the current thread is the main thread. | ||
pub fn is_main_thread() bool { | ||
return g_main_thread_id == u64(C.pthread_self()) | ||
} | ||
|
||
[typedef] | ||
struct C.sigset_t {} | ||
|
||
fn C.sigaddset(set &C.sigset_t, signum int) int | ||
fn C.sigemptyset(set &C.sigset_t) | ||
fn C.sigprocmask(how int, set &C.sigset_t, oldset &C.sigset_t) int | ||
|
||
fn signal_ignore_internal(args ...Signal) { | ||
$if !android { | ||
mask1 := C.sigset_t{} | ||
C.sigemptyset(&mask1) | ||
for arg in args { | ||
C.sigaddset(&mask1, int(arg)) | ||
} | ||
C.sigprocmask(C.SIG_BLOCK, &mask1, unsafe { nil }) | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[has_globals] | ||
module os | ||
|
||
fn C.GetCurrentThreadId() u32 | ||
|
||
// g_main_thread_id and is_main_thread can be used to determine if the current thread is the main thread. | ||
// if need to get the tid of the main thread, can use the global variable g_main_thread_id | ||
// instead of using thread_id() every time. | ||
__global g_main_thread_id = u64(C.GetCurrentThreadId()) | ||
|
||
// is_main_thread returns whether the current thread is the main thread. | ||
pub fn is_main_thread() bool { | ||
return g_main_thread_id == u64(C.GetCurrentThreadId()) | ||
} | ||
|
||
// The windows platform does not need to be handled. | ||
fn signal_ignore_internal(args ...Signal) { | ||
} |