forked from ipdk-io/krnlmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
krnlmon_main.cc
82 lines (63 loc) · 2.01 KB
/
krnlmon_main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2022-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "krnlmon_main.h"
// Enable GNU extensions: pthread_setname_np()
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include "absl/synchronization/notification.h"
#include "switchlink/switchlink_main.h"
static pthread_t main_tid;
static pthread_t stop_tid;
extern "C" {
// Ensure that the functions passed to pthread_create() have C interfaces.
static void *krnlmon_main_wrapper(void *arg);
static void *krnlmon_stop_wrapper(void *arg);
}
static void *krnlmon_main_wrapper(void *arg) {
// Wait for stratum server to signal that it is ready.
auto ready_sync = static_cast<absl::Notification*>(arg);
ready_sync->WaitForNotification();
// Start switchlink.
switchlink_main();
return nullptr;
}
static void *krnlmon_stop_wrapper(void *arg) {
// Wait for stratum server to signal that it is done.
auto done_sync = static_cast<absl::Notification*>(arg);
done_sync->WaitForNotification();
// Stop switchlink.
switchlink_stop();
return nullptr;
}
static void print_strerror(const char *msg, int err) {
char errbuf[64];
printf("%s: %s\n", msg, strerror_r(err, errbuf, sizeof(errbuf)));
}
int krnlmon_create_main_thread(absl::Notification* ready_sync) {
int rc = pthread_create(&main_tid, NULL, &krnlmon_main_wrapper, ready_sync);
if (rc) {
print_strerror("Error creating switchlink_main thread", rc);
return -1;
}
rc = pthread_setname_np(main_tid, "switchlink_main");
if (rc) {
print_strerror("Error naming switchlink_main thread", rc);
}
return 0;
}
int krnlmon_create_shutdown_thread(absl::Notification* done_sync) {
int rc = pthread_create(&stop_tid, NULL, &krnlmon_stop_wrapper, done_sync);
if (rc) {
print_strerror("Error creating switchlink_stop thread", rc);
return -1;
}
rc = pthread_setname_np(stop_tid, "switchlink_stop");
if (rc) {
print_strerror("Error naming switchlink_stop thread", rc);
}
return 0;
}