tools/perf/util/threads.c

Source file repositories/reference/linux-study-clean/tools/perf/util/threads.c

File Facts

System
Linux kernel
Corpus path
tools/perf/util/threads.c
Extension
.c
Size
4781 bytes
Lines
191
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (hashmap__add(&table->shard, tid, res)) {
			/* Add failed. Assume a race so find other entry. */
			thread__put(res);
			res = NULL;
			if (hashmap__find(&table->shard, tid, &res))
				res = thread__get(res);
		} else {
			res = thread__get(res);
			*created = true;
		}
		if (res)
			__threads_table_entry__set_last_match(table, res);
	}
	up_write(&table->lock);
	return res;
}

void threads__remove_all_threads(struct threads *threads)
{
	for (int i = 0; i < THREADS__TABLE_SIZE; i++) {
		struct threads_table_entry *table = &threads->table[i];
		struct hashmap_entry *cur, *tmp;
		size_t bkt;

		down_write(&table->lock);
		__threads_table_entry__set_last_match(table, NULL);
		hashmap__for_each_entry_safe(&table->shard, cur, tmp, bkt) {
			struct thread *old_value;

			hashmap__delete(&table->shard, cur->key, /*old_key=*/NULL, &old_value);
			thread__put(old_value);
		}
		up_write(&table->lock);
	}
}

void threads__remove(struct threads *threads, struct thread *thread)
{
	struct threads_table_entry *table  = threads__table(threads, thread__tid(thread));
	struct thread *old_value;

	down_write(&table->lock);
	if (table->last_match && RC_CHK_EQUAL(table->last_match, thread))
		__threads_table_entry__set_last_match(table, NULL);

	hashmap__delete(&table->shard, thread__tid(thread), /*old_key=*/NULL, &old_value);
	thread__put(old_value);
	up_write(&table->lock);
}

int threads__for_each_thread(struct threads *threads,
			     int (*fn)(struct thread *thread, void *data),
			     void *data)
{
	for (int i = 0; i < THREADS__TABLE_SIZE; i++) {
		struct threads_table_entry *table = &threads->table[i];
		struct hashmap_entry *cur;
		size_t bkt;

		down_read(&table->lock);
		hashmap__for_each_entry(&table->shard, cur, bkt) {
			int rc = fn((struct thread *)cur->pvalue, data);

			if (rc != 0) {
				up_read(&table->lock);
				return rc;
			}
		}
		up_read(&table->lock);
	}
	return 0;

}

Annotation

Implementation Notes