Skip to content

Commit 7fa6bd7

Browse files
afq984Chromeos LUCI
authored and
Chromeos LUCI
committed
cras_thread: Add non-fatal functions to acquire thread ctxs
The non-fatal variants make it easier to workaround callers on the incorrect thread. BUG=b:340129658 TEST=bazel test //... Change-Id: I772a30d8fbe3828e461a3bdebb68d95f1a9fc20e Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5528492 Tested-by: [email protected] <[email protected]> Reviewed-by: Yu-Hsuan Hsu <[email protected]> Commit-Queue: Li-Yu Yu <[email protected]>
1 parent baffbac commit 7fa6bd7

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

cras/server/cras_thread.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,36 @@
1414
static thread_local bool main_ctx_allowed = false;
1515
static thread_local bool audio_ctx_allowed = false;
1616

17+
static struct cras_main_ctx mctx = {};
18+
1719
struct cras_main_ctx* checked_main_ctx() {
1820
CRAS_CHECK(main_ctx_allowed);
1921

20-
static struct cras_main_ctx mctx = {};
2122
return &mctx;
2223
}
2324

25+
struct cras_main_ctx* get_main_ctx_or_null() {
26+
if (main_ctx_allowed) {
27+
return &mctx;
28+
}
29+
return NULL;
30+
}
31+
32+
static struct cras_audio_ctx actx = {};
33+
2434
struct cras_audio_ctx* checked_audio_ctx() {
2535
CRAS_CHECK(audio_ctx_allowed);
2636

27-
static struct cras_audio_ctx actx = {};
2837
return &actx;
2938
}
3039

40+
struct cras_audio_ctx* get_audio_ctx_or_null() {
41+
if (audio_ctx_allowed) {
42+
return &actx;
43+
}
44+
return NULL;
45+
}
46+
3147
__attribute__((constructor)) static void init() {
3248
main_ctx_allowed = audio_ctx_allowed = true;
3349
}

cras/server/cras_thread.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ struct cras_audio_ctx {
3030
// Otherwise aborts (SIGABRT) the program.
3131
struct cras_main_ctx* checked_main_ctx();
3232

33+
// Like checked_main_ctx() but returns NULL instead of SIGABRT-ing when
34+
// called from the wrong thread.
35+
//
36+
// Use this instead of checked_main_ctx() only when you need to workaround
37+
// unexpected callers gracefully.
38+
struct cras_main_ctx* get_main_ctx_or_null();
39+
3340
// Returns the audio thread context singleton if any of the following is true:
3441
// - The current thread is the audio thread
3542
// - The audio thread has not started yet.
@@ -38,6 +45,13 @@ struct cras_main_ctx* checked_main_ctx();
3845
// Otherwise aborts (SIGABRT) the program.
3946
struct cras_audio_ctx* checked_audio_ctx();
4047

48+
// Like checked_audio_ctx() but returns NULL instead of SIGABRT-ing when
49+
// called from the wrong thread.
50+
//
51+
// Use this instead of checked_audio_ctx() only when you need to workaround
52+
// unexpected callers gracefully.
53+
struct cras_audio_ctx* get_audio_ctx_or_null();
54+
4155
// Wrapper to create the audio thread.
4256
int cras_thread_create_audio(pthread_t* thread,
4357
const pthread_attr_t* attr,

cras/server/cras_thread_test.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
// be one the main thread.
1010
TEST(CrasThread, Checks) {
1111
// Both contexts are allowed on the main thread.
12+
EXPECT_TRUE(get_main_ctx_or_null());
13+
EXPECT_TRUE(get_audio_ctx_or_null());
1214
checked_main_ctx()->test_num = 1;
1315
checked_audio_ctx()->test_num = 2;
1416

1517
{
1618
pthread_t child_tid = {};
1719
auto audio_thread = [](void* data) -> void* {
20+
// main_ctx disallowed in the audio thread.
21+
EXPECT_FALSE(get_main_ctx_or_null());
1822
// audio_ctx allowed in the audio thread.
23+
EXPECT_TRUE(get_audio_ctx_or_null());
1924
EXPECT_EQ(checked_audio_ctx()->test_num, 2);
2025
return nullptr;
2126
};
@@ -24,8 +29,10 @@ TEST(CrasThread, Checks) {
2429
0);
2530
EXPECT_NE(pthread_self(), child_tid);
2631
// main_ctx allowed after creating the audio thread.
32+
EXPECT_TRUE(get_main_ctx_or_null());
2733
EXPECT_EQ(checked_main_ctx()->test_num, 1);
2834
// audio_ctx not allowed after creating the audio thread.
35+
EXPECT_FALSE(get_audio_ctx_or_null());
2936
EXPECT_EXIT(checked_audio_ctx(), testing::KilledBySignal(SIGABRT),
3037
"audio_ctx_allowed");
3138
pthread_join(child_tid, nullptr);

0 commit comments

Comments
 (0)