File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test.c
Revision 1.15: download - view: text, annotated - select for diffs - revision graph
Sat Feb 25 15:55:00 2023 UTC (14 months, 3 weeks ago) by misho
Branches: MAIN
CVS tags: sched8_3, sched8_2, sched8_1, sched8_0, sched7_9, sched7_8, sched7_7, sched7_6, sched7_5, SCHED8_2, SCHED8_1, SCHED8_0, SCHED7_9, SCHED7_8, SCHED7_7, SCHED7_6, SCHED7_5, SCHED7_4, HEAD
version 7.4

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "../inc/config.h"
#include <aitsched.h>

intptr_t Kill[1];
#ifdef AIO_SUPPORT
struct iovec iv[3], wiv[3], riv[3];
#endif

void *event(sched_task_t *arg)
{
	printf("Event::\n");
	taskExit(arg, NULL);
}

void *regular(sched_task_t *arg)
{
	printf("Task::\n");
	taskExit(arg, NULL);
}

void *timer(sched_task_t *arg)
{
	printf("Timer %p sec::\n", TASK_ARG(arg));
	taskExit(arg, NULL);
}

void *r(sched_task_t *arg)
{
	printf("read:: %ld bytes wait\n", (long) TASK_RET(arg));
	Kill[0]++;
	taskExit(arg, NULL);
}

void *w(sched_task_t *arg)
{
	printf("write:: %ld bytes wait\n", (long) TASK_RET(arg));
	taskExit(arg, NULL);
}

void *once(sched_task_t *arg)
{
	printf("once::\n");
	taskExit(arg, NULL);
}

#ifdef AIO_SUPPORT
void *aioread(sched_task_t *arg);
void *aiowrite(sched_task_t *arg)
{
	char *ole = malloc(BUFSIZ);

	printf("AIO write[%d]:: %d bytes\n%p\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
			TASK_DATA(arg));
	free(TASK_DATA(arg));

	memset(ole, 0, BUFSIZ);
	schedAIORead(TASK_ROOT(arg), aioread, NULL, TASK_FD(arg), ole, BUFSIZ - 1, -1);
	taskExit(arg, NULL);
}

void *aioread(sched_task_t *arg)
{
	char *ole = malloc(BUFSIZ);
	int len;

	printf("AIO read[%d]:: %d bytes\n%s\n-------\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
			(char*) TASK_DATA(arg));

	if (TASK_ARG(arg)) {
		len = strlcpy(ole, "++++++BAHURA OR CULTURE .... A CULTURE OR BAHURA :-)\n", BUFSIZ);
		printf("sched Write len=%d %p\n", len, ole);
		schedAIOWrite(TASK_ROOT(arg), aiowrite, TASK_ARG(arg), TASK_FD(arg), ole,  len, -1);
				
	}
	free(TASK_DATA(arg));
	taskExit(arg, NULL);
}

void *aiobulkread(sched_task_t *arg)
{
	struct iovec *iv = TASK_DATA(arg);
	register int i;

	printf("aioBULKread::\n");
	for (i = 0; i < 3; i++) {
		printf("%d) rlen[%d]=%s\n---\n", i, iv[i].iov_len, (char*) iv[i].iov_base);
		free(iv[i].iov_base);
	}

	taskExit(arg, NULL);
}

void *aiobulkwrite(sched_task_t *arg)
{
	struct iovec *iv = TASK_DATA(arg);
	register int i;

	printf("aioBULKwrite::\n");
	for (i = 0; i < 3; i++) {
		printf("%d) wlen=%d\n", i, iv[i].iov_len);
		free(iv[i].iov_base);
	}

	taskExit(arg, NULL);
}
#endif

void *thr(sched_task_t *arg)
{
	printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
	taskExit(arg, 42);
}

void *thr4kill(sched_task_t *arg)
{
	char blah[BUFSIZ];

	printf("THREAD!!! tid (%lx):: %s\n", TASK_VAL(arg), __func__);

	read(0, blah, sizeof blah);
	printf("never see!!! (%lx):: %s (%d == %d)\n", TASK_VAL(arg), (char*) TASK_ARG(arg), TASK_TYPE(arg), taskTHREAD);
	taskExit(arg, 0);
}

void sig(int s)
{
	switch (s) {
		case SIGTERM:
		case SIGINT:
			printf("I'm in switch case %d\n", s);
			Kill[0]++;
			break;
		case SIGHUP:
			printf("Test SIGHUP\n");
			break;
	}
}

void *sigt(sched_task_t *arg)
{
	int s = TASK_VAL(arg);

	printf("Received signal #%d\n", s);

	sig(s);

	schedSignalSelf(arg);
	taskExit(arg, NULL);
}

int
main(int argc, char **argv)
{
	sched_root_task_t *root;
	int f, fd;
	struct sockaddr_in sin;
	struct timespec ts = { 20, 0 };
	struct timespec p = { 0, 10000000 };
//	struct sigaction sa;
	sched_task_t *t;
#ifdef AIO_SUPPORT
	char *ole = malloc(BUFSIZ);
	register int i;
#endif

	/*
	sa.sa_handler = sig;
	sigemptyset(&sa.sa_mask);
	sigaction(SIGTERM, &sa, NULL);
	*/

	f = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (f == -1)
		return 1;
#ifndef __linux__
	sin.sin_len = sizeof sin;
#endif
	sin.sin_family = AF_INET;
	sin.sin_port = htons(2345);
	sin.sin_addr.s_addr = INADDR_ANY;
	if (bind(f, (struct sockaddr*) &sin, sizeof sin) == -1)
		return 1;

	fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
	if (fd == -1)
		return 1;
	printf("fd=%d\n", fd);

	root = schedBegin();
	if (!root) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 1;
	}

	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 4;
	} else
		ts.tv_sec = 15;
	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 4;
	} else
		ts.tv_sec = 10;

	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 2;
	}

	if (!schedTask(root, regular, "piuk", 1111, NULL, 0)) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 3;
	}

	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 4;
	}

	if (!(t = schedRead(root, r, "rrr_test", f, NULL, 0))) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 5;
	}
	if (!schedRead(root, r, "rrr", f, NULL, 0)) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 5;
	}
	schedCancel(t);

	if (!schedWrite(root, w, "www", f, NULL, 0)) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 6;
	}

	if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 7;
	}
	if (!schedThread(root, thr, "mdaaaa this is thread task", 8192, NULL, 0)) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		printf("stack is too small\n");
	}
	if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 131072, NULL, 0)) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 7;
	}
	if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, NULL, 0)) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 7;
	}
	printf("~~~try to cancel tid = %lx\n", TASK_VAL(t));
	schedCancel(t);
	if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 0, NULL, 0)) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 7;
	}
	if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		return 7;
	}
	if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 4096, NULL, 0)) {
		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
		printf("stack is too small\n");
	}
	sleep(1);
	schedCancel(t);

#ifdef AIO_SUPPORT
	memset(ole, 0, BUFSIZ);
	if (!schedAIORead(root, aioread, (void*) f, fd, ole, BUFSIZ - 1, 0))
		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());


	iv[0].iov_len = 5;
	iv[1].iov_len = 2;
	iv[2].iov_len = 50;
	for (i = 0; i < 3; i++)
		iv[i].iov_base = malloc(iv[i].iov_len);
	if (!schedLIORead(root, aiobulkread, NULL, fd, iv, 3, 0))
		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
	fsync(fd);
	for (i = 0; i < 3; i++) {
		wiv[i].iov_len = 100;
		wiv[i].iov_base = malloc(wiv[i].iov_len);
	}
	strlcpy(wiv[0].iov_base, "12345678900000000000000000\n", wiv[0].iov_len);
	wiv[0].iov_len = strlen(wiv[0].iov_base) + 1;
	strlcpy(wiv[1].iov_base, "222222222222222222222222\n", wiv[1].iov_len);
	wiv[1].iov_len = strlen(wiv[1].iov_base) + 1;
	strlcpy(wiv[2].iov_base, "333\n", wiv[2].iov_len);
	wiv[2].iov_len = strlen(wiv[2].iov_base) + 1;
	if (!schedLIOWrite(root, aiobulkwrite, NULL, fd, wiv, 3, 0))
		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());

	for (i = 0; i < 3; i++) {
		riv[i].iov_len = 5;
		riv[i].iov_base = malloc(riv[i].iov_len + 1);
		memset(riv[i].iov_base, 0, riv[i].iov_len + 1);
	}
	if (!schedLIORead(root, aiobulkread, NULL, fd, riv, 3, 0))
		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
#endif

	schedCallOnce(root, once, "000000", 42, NULL, 0);

	printf("add signals\n");
	schedSignal(root, sigt, NULL, SIGHUP, NULL, 0);
	schedSignal(root, sigt, NULL, SIGTERM, NULL, 0);
	schedSignal(root, sigt, NULL, SIGINT, NULL, 0);

	schedPolling(root, &p, NULL);
	schedSignalDispatch(root, 42);
	schedRun(root, Kill);
	schedSignalDispatch(root, 0);
	schedEnd(&root);
	sleep(1);

#ifdef AIO_SUPPORT
	for (i = 0; i < 3; i++)
		free(iv[i].iov_base);
#endif

	close(fd);
	close(f);
	return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>