Skip to content
This repository was archived by the owner on Mar 24, 2026. It is now read-only.

Commit 2216c59

Browse files
Privacy Sandbox Teamcopybara-github
authored andcommitted
feat: Copy event.h from B&A to common repo
This is part of a series of CLs to copy the KV BYOS clients from B&A to common repo. Bug: 411430242 Change-Id: I8bfab3ade62fd66003320da395907a9c8c378430 GitOrigin-RevId: 5a29336d8a1bd93f8e0ea3fef0364c6330557d52
1 parent bf55d9d commit 2216c59

10 files changed

Lines changed: 1099 additions & 0 deletions

File tree

src/core/event/BUILD.bazel

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("@rules_cc//cc:defs.bzl", "cc_library")
16+
17+
cc_library(
18+
name = "constants",
19+
hdrs = [
20+
"constants.h",
21+
],
22+
)
23+
24+
cc_library(
25+
name = "event_base",
26+
srcs = ["event_base.cc"],
27+
hdrs = [
28+
"event_base.h",
29+
],
30+
visibility = ["//visibility:public"],
31+
deps = [
32+
":constants",
33+
"@google_privacysandbox_servers_common//src/logger:request_context_logger",
34+
"@libevent//:event",
35+
],
36+
)
37+
38+
cc_library(
39+
name = "event",
40+
srcs = ["event.cc"],
41+
hdrs = [
42+
"event.h",
43+
],
44+
visibility = ["//visibility:public"],
45+
deps = [
46+
":constants",
47+
"@google_privacysandbox_servers_common//src/logger:request_context_logger",
48+
"@libevent//:event",
49+
],
50+
)

src/core/event/constants.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef SRC_CORE_EVENT_CONSTANTS_H_
18+
#define SRC_CORE_EVENT_CONSTANTS_H_
19+
20+
namespace privacy_sandbox::server_common {
21+
22+
// Libevent uses default priority as half of the number of priorities.
23+
// We needed 2 priorities to begin with (high and default) but configured 3
24+
// here for future use if we have to configure a low priority event.
25+
inline constexpr int kNumEventPriorities = 3;
26+
27+
} // namespace privacy_sandbox::server_common
28+
29+
#endif // SRC_CORE_EVENT_CONSTANTS_H_

src/core/event/event.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "src/core/event/event.h"
16+
17+
#include <utility>
18+
19+
#include "event2/event.h"
20+
#include "event2/event_struct.h"
21+
22+
namespace privacy_sandbox::server_common {
23+
24+
Event::Event(struct event_base* base, evutil_socket_t fd, int16_t event_type,
25+
Event::Callback event_callback, void* arg, int priority,
26+
struct timeval* event_timeout, OnDelete on_delete,
27+
bool add_to_loop)
28+
: priority_(priority),
29+
event_(event_new(base, fd, event_type, event_callback, arg)),
30+
on_delete_(std::move(on_delete)) {
31+
event_priority_set(event_, priority_);
32+
if (add_to_loop) {
33+
event_add(event_, event_timeout);
34+
}
35+
}
36+
37+
struct event* Event::get() { return event_; }
38+
Event::~Event() {
39+
if (event_) {
40+
if (on_delete_) {
41+
on_delete_(event_);
42+
}
43+
event_del(event_);
44+
event_free(event_);
45+
}
46+
}
47+
48+
} // namespace privacy_sandbox::server_common

src/core/event/event.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef SRC_CORE_EVENT_EVENT_H_
18+
#define SRC_CORE_EVENT_EVENT_H_
19+
20+
#include <functional>
21+
22+
#include "event2/event.h"
23+
#include "event2/event_struct.h"
24+
#include "src/core/event/constants.h"
25+
26+
namespace privacy_sandbox::server_common {
27+
28+
// Wraps the event used by libevent. This wrapper makes it easier to manage
29+
// lifecycle of the underlying event.
30+
class Event {
31+
public:
32+
using OnDelete = std::function<void(struct event*)>;
33+
// Arguments are documented here:
34+
// https://libevent.org/doc/event_8h.html#aed2307f3d9b38e07cc10c2607322d758
35+
using Callback = void (*)(/*fd or signal=*/int, /*events=*/int16_t,
36+
/*pointer to user provided data=*/void*);
37+
38+
explicit Event(struct event_base* base, evutil_socket_t fd,
39+
int16_t event_type, Callback event_callback, void* arg,
40+
int priority = kNumEventPriorities / 2,
41+
struct timeval* event_timeout = nullptr,
42+
OnDelete on_delete = nullptr, bool add_to_loop = true);
43+
struct event* get();
44+
virtual ~Event();
45+
46+
private:
47+
int priority_;
48+
struct event* event_ = nullptr;
49+
OnDelete on_delete_;
50+
};
51+
52+
} // namespace privacy_sandbox::server_common
53+
54+
#endif // SRC_CORE_EVENT_EVENT_H_

src/core/event/event_base.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "src/core/event/event_base.h"
16+
17+
#include "event2/event.h"
18+
#include "event2/thread.h"
19+
20+
namespace privacy_sandbox::server_common {
21+
22+
EventBase::EventBase(bool use_pthreads, int num_priorities) {
23+
if (use_pthreads) {
24+
evthread_use_pthreads();
25+
}
26+
event_base_ = event_base_new();
27+
event_base_priority_init(event_base_, num_priorities);
28+
if (server_common::log::PS_VLOG_IS_ON(10)) {
29+
event_enable_debug_mode();
30+
}
31+
}
32+
33+
EventBase::~EventBase() {
34+
if (event_base_ != nullptr) {
35+
event_base_free(event_base_);
36+
}
37+
}
38+
39+
struct event_base* EventBase::get() { return event_base_; }
40+
41+
} // namespace privacy_sandbox::server_common

src/core/event/event_base.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef SRC_CORE_EVENT_EVENT_BASE_H_
18+
#define SRC_CORE_EVENT_EVENT_BASE_H_
19+
20+
#include "event2/event.h"
21+
#include "event2/thread.h"
22+
#include "src/core/event/constants.h"
23+
#include "src/logger/request_context_logger.h"
24+
25+
namespace privacy_sandbox::server_common {
26+
27+
// Wrapper for the libevent structure to hold information and state for a
28+
// libevent dispatch loop.
29+
class EventBase {
30+
public:
31+
explicit EventBase(bool use_pthreads = true,
32+
int num_priorities = kNumEventPriorities);
33+
virtual ~EventBase();
34+
35+
// Gets the underlying event base data type.
36+
struct event_base* get();
37+
38+
private:
39+
struct event_base* event_base_ = nullptr;
40+
};
41+
42+
} // namespace privacy_sandbox::server_common
43+
44+
#endif // SRC_CORE_EVENT_EVENT_BASE_H_

third_party/cpp_deps.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ def cpp_dependencies():
185185
strip_prefix = "flatbuffers-24.3.25",
186186
urls = ["https://github.com/google/flatbuffers/archive/refs/tags/v24.3.25.zip"],
187187
)
188+
maybe(
189+
http_archive,
190+
name = "libevent",
191+
build_file = "//third_party:libevent.BUILD",
192+
patch_args = ["-p1"],
193+
patches = [
194+
"//third_party:libevent.patch",
195+
],
196+
sha256 = "8836ad722ab211de41cb82fe098911986604f6286f67d10dfb2b6787bf418f49",
197+
strip_prefix = "libevent-release-2.1.12-stable",
198+
urls = ["https://github.com/libevent/libevent/archive/refs/tags/release-2.1.12-stable.zip"],
199+
)
188200
_bazel_deps()
189201
_container_deps()
190202
_ts_js_deps()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
diff --git a/evconfig-private.h b/evconfig-private.h
2+
new file mode 100644
3+
index 0000000..31e6100
4+
--- /dev/null
5+
+++ b/evconfig-private.h
6+
@@ -0,0 +1,56 @@
7+
+/* evconfig-private.h. Generated from evconfig-private.h.in by configure. */
8+
+/* evconfig-private.h template - see "Configuration Header Templates" */
9+
+/* in AC manual. Kevin Bowling <kevin.bowling@kev009.com */
10+
+#ifndef EVCONFIG_PRIVATE_H_INCLUDED_
11+
+#define EVCONFIG_PRIVATE_H_INCLUDED_
12+
+
13+
+/* Enable extensions on AIX 3, Interix. */
14+
+#ifndef _ALL_SOURCE
15+
+# define _ALL_SOURCE 1
16+
+#endif
17+
+/* Enable GNU extensions on systems that have them. */
18+
+#ifndef _GNU_SOURCE
19+
+# define _GNU_SOURCE 1
20+
+#endif
21+
+/* Enable threading extensions on Solaris. */
22+
+#ifndef _POSIX_PTHREAD_SEMANTICS
23+
+# define _POSIX_PTHREAD_SEMANTICS 1
24+
+#endif
25+
+/* Enable extensions on HP NonStop. */
26+
+#ifndef _TANDEM_SOURCE
27+
+# define _TANDEM_SOURCE 1
28+
+#endif
29+
+/* Enable general extensions on Solaris. */
30+
+#ifndef __EXTENSIONS__
31+
+# define __EXTENSIONS__ 1
32+
+#endif
33+
+
34+
+/* Number of bits in a file offset, on hosts where this is settable. */
35+
+/* #undef _FILE_OFFSET_BITS */
36+
+/* Define for large files, on AIX-style hosts. */
37+
+/* #undef _LARGE_FILES */
38+
+
39+
+/* Define to 1 if on MINIX. */
40+
+#ifndef _MINIX
41+
+/* #undef _MINIX */
42+
+#endif
43+
+
44+
+/* Define to 2 if the system does not provide POSIX.1 features except with
45+
+ this defined. */
46+
+#ifndef _POSIX_1_SOURCE
47+
+/* #undef _POSIX_1_SOURCE */
48+
+#endif
49+
+
50+
+/* Define to 1 if you need to in order for `stat' and other things to work. */
51+
+#ifndef _POSIX_SOURCE
52+
+/* #undef _POSIX_SOURCE */
53+
+#endif
54+
+
55+
+/* Enable POSIX.2 extensions on QNX for getopt */
56+
+#ifdef __QNX__
57+
+# ifndef __EXT_POSIX2
58+
+# define __EXT_POSIX2
59+
+# endif
60+
+#endif
61+
+
62+
+#endif
63+
--
64+
2.43.0.472.g3155946c3a-goog

0 commit comments

Comments
 (0)