summaryrefslogtreecommitdiffstats
path: root/misc/cgo/test/sigprocmask.c
blob: e77ba5b08ea96de5283b62ddaac841a3c1cac69c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !windows

#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>

extern void IntoGoAndBack();

int CheckBlocked() {
	sigset_t mask;
	sigprocmask(SIG_BLOCK, NULL, &mask);
	return sigismember(&mask, SIGIO);
}

static void* sigthreadfunc(void* unused) {
	sigset_t mask;
	sigemptyset(&mask);
	sigaddset(&mask, SIGIO);
	sigprocmask(SIG_BLOCK, &mask, NULL);
	IntoGoAndBack();
	return NULL;
}

int RunSigThread() {
	int tries;
	pthread_t thread;
	int r;
	struct timespec ts;

	for (tries = 0; tries < 20; tries++) {
		r = pthread_create(&thread, NULL, &sigthreadfunc, NULL);
		if (r == 0) {
			return pthread_join(thread, NULL);
		}
		if (r != EAGAIN) {
			return r;
		}
		ts.tv_sec = 0;
		ts.tv_nsec = (tries + 1) * 1000 * 1000; // Milliseconds.
		nanosleep(&ts, NULL);
	}
	return EAGAIN;
}