This is useful to compare the current thread against another one.
#pragma once
+#include <stdbool.h>
+
#define THREAD_NAME_MAX 16
typedef struct reactor_t reactor_t;
// |thread| may not be NULL.
void thread_stop(thread_t *thread);
+// Returns true if the current thread is the same as the one represented by |thread|.
+// |thread| may not be NULL.
+bool thread_is_self(const thread_t *thread);
+
// Returns the name of the given |thread|. |thread| may not be NULL.
const char *thread_name(const thread_t *thread);
reactor_stop(thread->reactor);
}
+bool thread_is_self(const thread_t *thread) {
+ assert(thread != NULL);
+ return !!pthread_equal(pthread_self(), thread->pthread);
+}
+
reactor_t *thread_get_reactor(const thread_t *thread) {
assert(thread != NULL);
return thread->reactor;
ASSERT_FALSE(FD_ISSET(wait_fd, &read_fds));
thread_free(thread);
}
+
+static void thread_is_self_fn(void *context) {
+ thread_t *thread = (thread_t *)context;
+ EXPECT_TRUE(thread_is_self(thread));
+}
+
+TEST(ThreadTest, test_thread_is_self) {
+ thread_t *thread = thread_new("test_thread");
+ thread_post(thread, thread_is_self_fn, thread);
+ thread_free(thread);
+}
+
+TEST(ThreadTest, test_thread_is_not_self) {
+ thread_t *thread = thread_new("test_thread");
+ EXPECT_FALSE(thread_is_self(thread));
+ thread_free(thread);
+}