xref: /freebsd/tools/test/stress2/misc/sem_post.sh (revision bfcc09ddd422c95a1a2e4e794b63ee54c4902398)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2019 Dell EMC Isilon
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# No problems seen.
31
32cat > /tmp/sem_post.c <<EOF
33#include <err.h>
34#include <pthread.h>
35#include <semaphore.h>
36#include <signal.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <time.h>
41#include <unistd.h>
42
43static sem_t semaphore;
44
45static void *
46threadfunc(void *data __unused) {
47
48	for (;;) {
49		usleep(arc4random() % 100);
50		if (sem_wait(&semaphore) == -1)
51			err(1, "sem_wait");
52		usleep(arc4random() % 100);
53		if (arc4random() % 100 < 3)
54			sleep(1);
55		if (sem_post(&semaphore) == -1)
56			err(1, "sem_post");
57	}
58
59	return (NULL);
60}
61
62int
63main(void) {
64	pthread_t mythread;
65	time_t start;
66	int r;
67
68	// initialize semaphore and set value to 1
69	if (sem_init(&semaphore, 0, 1) == -1)
70		err(1, "sem_init");
71
72	r = pthread_create(&mythread, NULL, threadfunc, NULL);
73	if (r != 0)
74		errc(1, r, "pthread_create");
75
76	usleep(50);
77	alarm(300);
78	start = time(NULL);
79	while (time(NULL) - start < 120) {
80		usleep(arc4random() % 100);
81		if (sem_wait(&semaphore) == -1)
82			err(1, "sem_wait");
83		usleep(arc4random() % 100);
84		if (arc4random() % 100 < 3)
85			sleep(1);
86		if (sem_post(&semaphore) == -1)
87			err(1, "sem_post");
88	}
89	if ((r = pthread_kill(mythread, SIGINT)) != 0)
90		errc(1, r, "pthread_kill");
91	if ((r = pthread_join(mythread, NULL)) != 0)
92		errc(1, r, "pthread_join");
93
94	return (0);
95}
96EOF
97cc -o /tmp/sem_post -Wall -Wextra -O2 /tmp/sem_post.c -lpthread || exit 1
98(cd ../testcases/swap; ./swap -t 3m -i 30 -h -l 100) &
99sleep 20
100for i in `jot 128`; do
101	/tmp/sem_post > /dev/null &
102done
103while pgrep -q sem_post; do sleep .5; done
104while pkill swap; do :; done
105wait
106rm -f /tmp/sem_post /tmp/sem_post.c
107exit 0
108