From d8a8a8de00ca28879eb865cd4f25e362570a910e Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Mon, 5 Mar 2018 17:58:22 -0500 Subject: lib: some frr_pthread fixes * Use atomic fixed-width thread identifiers * Add ability to change thread name at runtime Signed-off-by: Quentin Young --- lib/frr_pthread.h | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'lib/frr_pthread.h') diff --git a/lib/frr_pthread.h b/lib/frr_pthread.h index 2cc50196a8..3990e2a4ce 100644 --- a/lib/frr_pthread.h +++ b/lib/frr_pthread.h @@ -1,6 +1,6 @@ /* * Utilities and interfaces for managing POSIX threads within FRR. - * Copyright (C) 2017 Cumulus Networks + * Copyright (C) 2017 Cumulus Networks, Inc. * * 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 @@ -32,14 +32,19 @@ struct frr_pthread; struct frr_pthread_attr; struct frr_pthread_attr { - int id; + _Atomic uint32_t id; void *(*start)(void *); int (*stop)(struct frr_pthread *, void **); - const char *name; }; struct frr_pthread { + /* + * Mutex protecting this structure. Must be taken for reading some + * fields, denoted by a 'Requires: mtx'. + */ + pthread_mutex_t mtx; + /* pthread id */ pthread_t thread; @@ -73,8 +78,17 @@ struct frr_pthread { * Fake thread-specific storage. No constraints on usage. Helpful when * creating reentrant pthread implementations. Can be used to pass * argument to pthread entry function. + * + * Requires: mtx */ void *data; + + /* + * Human-readable thread name. + * + * Requires: mtx + */ + char *name; }; extern struct frr_pthread_attr frr_pthread_attr_default; @@ -107,9 +121,19 @@ void frr_pthread_finish(void); * frr_pthread will cause them to run on that pthread. * * @param attr - the thread attributes + * @param name - Human-readable name * @return the created frr_pthread upon success, or NULL upon failure */ -struct frr_pthread *frr_pthread_new(struct frr_pthread_attr *attr); +struct frr_pthread *frr_pthread_new(struct frr_pthread_attr *attr, + const char *name); + +/* + * Changes the name of the frr_pthread. + * + * @param fpt - the frr_pthread to operate on + * @param name - Human-readable name + */ +void frr_pthread_set_name(struct frr_pthread *fpt, const char *name); /* * Destroys an frr_pthread. @@ -198,6 +222,6 @@ void frr_pthread_yield(void); * * @return unique identifier */ -unsigned int frr_pthread_get_id(void); +uint32_t frr_pthread_get_id(void); #endif /* _FRR_PTHREAD_H */ -- cgit v1.2.3