<commit_before>/* Copyright 2013 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>
 */

#include <stdlib.h>
#include <stdio.h>

#include "ufibers.h"

void *shared = NULL;

static void *producer(void *data)
{
	shared = producer;
	printf("Producer: 0x%p -> shared\n", shared);
	return NULL;
}

static void *consumer(void *data)
{
	ufiber_t fid;

	ufiber_create(&fid, 0, producer, NULL);
	ufiber_join(fid, NULL);
	ufiber_unref(fid);
	printf("Consumer: 0x%p <- shared\n", shared);
	return shared;
}

int main(void)
{
	ufiber_t fid;
	void *val;

	ufiber_init();
	ufiber_create(&fid, 0, consumer, NULL);
	ufiber_join(fid, &val);
	ufiber_unref(fid);
	printf("Root:     0x%p <- child\n", val);

	ufiber_create(NULL, 0, consumer, NULL);
	ufiber_create(NULL, 0, producer, NULL);
	ufiber_create(NULL, 0, producer, NULL);
	ufiber_create(NULL, 0, producer, NULL);
	ufiber_create(NULL, 0, producer, NULL);

	ufiber_yeild();
	ufiber_yeild();
	ufiber_yeild();
	ufiber_yeild();
	ufiber_yeild();
}<commit_msg>Proper producer-consumer test/example<commit_after>/* Copyright 2013 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>
 */

#include <stdlib.h>
#include <stdio.h>

#include "ufibers.h"

#define BUF_LEN 8
#define NR_FIBERS 16

#define next(pos) (((pos) + 1) % BUF_LEN)

static long shared[BUF_LEN];
static size_t wpos = 0;
static size_t rpos = 0;

static void *producer(void *data)
{
	if ((long)data % 2 == 0)
		ufiber_yeild();

	while (next(wpos) == rpos)
		ufiber_yeild();

	shared[wpos] = (long) data;
	wpos = next(wpos);

	return NULL;
}

static void *consumer(void *data)
{
	while (rpos == wpos)
		ufiber_yeild();

	printf("read %ld\n", shared[rpos]);
	rpos = next(rpos);

	return NULL;
}

int main(void)
{
	ufiber_t fids[NR_FIBERS * 2];
	void *rv;

	ufiber_init();
	for (unsigned long i = 0; i < NR_FIBERS; i++)
		ufiber_create(&fids[i], 0, producer, (void*) i);
	for (int i = 0; i < NR_FIBERS; i++)
		ufiber_create(&fids[i + NR_FIBERS], 0, consumer, NULL);

	for (int i = 0; i < NR_FIBERS * 2; i++) {
		ufiber_join(fids[i], &rv);
		ufiber_unref(fids[i]);
	}

	return 0;
}
{'fraction_non_alphanumeric': 0.12641509433962264, 'fraction_numerical': 0.011320754716981131, 'mean_word_length': 4.249174917491749, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2569001', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1113, 'n_words': 384}