diff options
author | Chirag Shah <chirag@cumulusnetworks.com> | 2020-04-23 01:09:15 +0200 |
---|---|---|
committer | Quentin Young <qlyoung@cumulusnetworks.com> | 2020-05-08 21:50:11 +0200 |
commit | 0edcb505a347c8dde01c9217f627715273d0ed0f (patch) | |
tree | 5d0ba780239ab5b85bb266e4d1f96eeb6cde2631 /lib/northbound_grpc.cpp | |
parent | Merge pull request #6348 from dslicenc/frr-reload-bfd (diff) | |
download | frr-0edcb505a347c8dde01c9217f627715273d0ed0f.tar.xz frr-0edcb505a347c8dde01c9217f627715273d0ed0f.zip |
lib: use frr_pthread to spawn grpc pthread
start grpc thread with frr_pthread library
callbacks to integrate with rcu infrastructure.
If a thread is created using native pthread callbacks
and if zlog is used then it leads to crash.
Signed-off-by: Chirag Shah <chirag@cumulusnetworks.com>
Diffstat (limited to 'lib/northbound_grpc.cpp')
-rw-r--r-- | lib/northbound_grpc.cpp | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/lib/northbound_grpc.cpp b/lib/northbound_grpc.cpp index 66bf05c1a..fd4101d67 100644 --- a/lib/northbound_grpc.cpp +++ b/lib/northbound_grpc.cpp @@ -28,6 +28,7 @@ #include "lib_errors.h" #include "northbound.h" #include "northbound_db.h" +#include "frr_pthread.h" #include <iostream> #include <sstream> @@ -36,6 +37,8 @@ #define GRPC_DEFAULT_PORT 50051 +static void *grpc_pthread_start(void *arg); + /* * NOTE: we can't use the FRR debugging infrastructure here since it uses * atomics and C++ has a different atomics API. Enable gRPC debugging @@ -43,7 +46,13 @@ */ static bool nb_dbg_client_grpc = 1; -static pthread_t grpc_pthread; +static struct frr_pthread *fpt; + +/* Default frr_pthread attributes */ +static const struct frr_pthread_attr attr = { + .start = grpc_pthread_start, + .stop = NULL, +}; class NorthboundImpl final : public frr::Northbound::Service { @@ -844,10 +853,13 @@ class NorthboundImpl final : public frr::Northbound::Service static void *grpc_pthread_start(void *arg) { - unsigned long *port = static_cast<unsigned long *>(arg); + struct frr_pthread *fpt = static_cast<frr_pthread *>(arg); + unsigned long *port = static_cast<unsigned long *>(fpt->data); NorthboundImpl service; std::stringstream server_address; + frr_pthread_set_name(fpt); + server_address << "0.0.0.0:" << *port; grpc::ServerBuilder builder; @@ -867,19 +879,24 @@ static void *grpc_pthread_start(void *arg) static int frr_grpc_init(unsigned long *port) { + fpt = frr_pthread_new(&attr, "frr-grpc", "frr-grpc"); + fpt->data = static_cast<void *>(port); + /* Create a pthread for gRPC since it runs its own event loop. */ - if (pthread_create(&grpc_pthread, NULL, grpc_pthread_start, port)) { + if (frr_pthread_run(fpt, NULL) < 0) { flog_err(EC_LIB_SYSTEM_CALL, "%s: error creating pthread: %s", __func__, safe_strerror(errno)); return -1; } - pthread_detach(grpc_pthread); + pthread_detach(fpt->thread); return 0; } static int frr_grpc_finish(void) { + if (fpt) + frr_pthread_destroy(fpt); // TODO: cancel the gRPC pthreads gracefully. return 0; |