<commit_before>/**
 * @file
 * @brief
 *
 * @date 31.08.11
 * @author <NAME>
 */

#include <assert.h>
#include <embox/unit.h>
#include <kernel/task/task_table.h>
#include <util/idx_table.h>
#include <framework/mod/options.h>

EMBOX_UNIT_INIT(task_table_module_init);

#define MODOPS_TASK_TABLE_SIZE OPTION_GET(NUMBER, task_table_size)

UTIL_IDX_TABLE_DEF(struct task *, task_table, MODOPS_TASK_TABLE_SIZE);

/*
 * tasks are started from 1, util table is from 0;
 * doing 1+ translation to output, and 1- for input
 */
int task_table_add(struct task *tsk) {
	assert(tsk != NULL);
	return 1 + util_idx_table_add(task_table, tsk);
}

struct task * task_table_get(int tid) {
	assert(tid >= 0);
	return util_idx_table_get(task_table, tid - 1);
}

void task_table_del(int tid) {
	assert(tid > 0);
	util_idx_table_del(task_table, tid - 1);
}

int task_table_has_space(void) {
	return 0 <= util_idx_table_next_alloc(task_table);
}

int task_table_get_first(int since) {
	/* since should be decremented. i.e. task_foreach calls
 	 * __func__, it returns first task. Then task_foreach
	 * gets returned task id, increments it and calls __func__.
	 * Without decrement it will skip task following next returned one.
	 */
	assert(since >= 0);
	return 1 + util_idx_table_next_mark(task_table, since - 1, 1);
}

static int task_table_module_init(void) {
	UTIL_IDX_TABLE_INIT(task_table, MODOPS_TASK_TABLE_SIZE);<commit_msg>replace util_idx_table with bitmap in task_table<commit_after>/**
 * @file
 * @brief
 *
 * @date 31.08.11
 * @author <NAME>
 */

#include <assert.h>
#include <errno.h>
#include <embox/unit.h>
#include <kernel/task/task_table.h>
#include <util/bitmap.h>
#include <framework/mod/options.h>

EMBOX_UNIT_INIT(task_table_module_init);

#define TABLE_SZ OPTION_GET(NUMBER, task_table_size)

static struct task *task_table[TABLE_SZ];
static unsigned long task_table_bm[BITMAP_SIZE(TABLE_SZ)];

/*
 * tasks are started from 1, table from 0;
 * doing 1+ translation to output, and 1- for input
 */
#define TID2IDX(tid) (tid - 1)
#define IDX2TID(idx) (idx + 1)

int task_table_add(struct task *tsk) {
	int idx = bitmap_find_zero_bit(task_table_bm, TABLE_SZ, 0);
	assert(idx < TABLE_SZ);
	task_table[idx] = tsk;
	bitmap_set_bit(task_table_bm, idx);
	return IDX2TID(idx);
}

struct task *task_table_get(int tid) {
	int idx = TID2IDX(tid);
	assert(idx >= 0 && tid < TABLE_SZ);
	return bitmap_test_bit(task_table_bm, idx) ? task_table[idx] : NULL;
}

void task_table_del(int tid) {
	int idx = TID2IDX(tid);
	assert(idx >= 0 && tid < TABLE_SZ);
	bitmap_clear_bit(task_table_bm, idx);
}

int task_table_has_space(void) {
	int idx = bitmap_find_zero_bit(task_table_bm, TABLE_SZ, 0);
	return idx < TABLE_SZ;
}

int task_table_get_first(int since) {
	int idx = TID2IDX(since);
	int set_idx = bitmap_find_bit(task_table_bm, TABLE_SZ, idx);
	return set_idx < TABLE_SZ ? IDX2TID(set_idx) : -ENOENT;
}

static int task_table_module_init(void) {
	bitmap_clear_all(task_table_bm, TABLE_SZ);
	/* if TABLE_SZ % LONG_BIT != 0, then
	 * last bits should be set here, and shouldn't changed later
	 */
	static_assert(TABLE_SZ % LONG_BIT == 0);
{'fraction_non_alphanumeric': 0.12143543735982057, 'fraction_numerical': 0.014418455623197693, 'mean_word_length': 5.194444444444445, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3839661', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1200, 'n_words': 282}