Skip to content

Commit

Permalink
Merge branch 'main' into socketopt
Browse files Browse the repository at this point in the history
Change-Id: Ica48103af77179eb1f1cd328d76d497cbb8f82ba
  • Loading branch information
haozheng-cobalt committed Feb 21, 2024
2 parents 50cc1a6 + 7c47c7f commit a941b31
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 53 deletions.
24 changes: 24 additions & 0 deletions cobalt/build/get_build_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Prints out the Cobalt Build ID for Buildbot."""

from cobalt.build import build_info


def main():
return build_info.get_build_id()


if __name__ == '__main__':
print(main())
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ TEST_P(PosixSocketSetOptionsTest, TryThemAllTCP) {
sizeof(true_val)),
0);
#if defined(__APPLE__)
// In tvOS, TCP_KEEPIDLE and SOL_TCP are not available.
// For reference:
// https://stackoverflow.com/questions/15860127/how-to-configure-tcp-keepalive-under-mac-os-
// https://stackoverflow.com/questions/15860127/how-to-configure-tcp-keepalive-under-mac-os-x
EXPECT_EQ(setsockopt(socket_fd, IPPROTO_TCP, TCP_KEEPALIVE, &period_seconds,
sizeof(period_seconds)),
0);
#else
#elif !defined(_WIN32)
// In Windows, the SOL_TCP and TCP_KEEPIDLE options are not available.
// For reference:
// https://stackoverflow.com/questions/8176821/how-to-set-the-keep-alive-interval-for-winsock
EXPECT_EQ(setsockopt(socket_fd, SOL_TCP, TCP_KEEPIDLE, &period_seconds,
sizeof(period_seconds)),
0);
Expand Down
34 changes: 34 additions & 0 deletions starboard/shared/win32/posix_emu/include/malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_MALLOC_H_
#define STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_MALLOC_H_

#include <../ucrt/malloc.h> // The Visual Studio version of this same file

#ifdef __cplusplus
extern "C" {
#endif

// https://pubs.opengroup.org/onlinepubs/9699919799/functions/free.html
// NOTE: Also declared in stdlib.h and implementation in stdlib.cc.
void sb_free(void* ptr);
#undef free
#define free sb_free

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_MALLOC_H_
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The Cobalt Authors. All Rights Reserved.
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,23 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_POSIX_FORCE_INCLUDE_H_
#define STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_POSIX_FORCE_INCLUDE_H_
#ifndef STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_STDLIB_H_
#define STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_STDLIB_H_

#if defined(STARBOARD)

#define free sb_free
#include <../ucrt/stdlib.h> // The Visual Studio version of this same file

#ifdef __cplusplus
extern "C" {
#endif

// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html
int posix_memalign(void** res, size_t alignment, size_t size);

// https://pubs.opengroup.org/onlinepubs/9699919799/functions/free.html
void sb_free(void* ptr);
#undef free
#define free sb_free

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // defined(STARBOARD)

#endif // STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_POSIX_FORCE_INCLUDE_H_
#endif // STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_STDLIB_H_
6 changes: 1 addition & 5 deletions starboard/shared/win32/posix_emu/mman.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

#include "starboard/configuration.h"

#ifdef __cplusplus
extern "C" {
#endif

void* mmap(void* addr, size_t len, int prot, int flags, int fd, off_t off) {
if (addr != NULL) {
Expand Down Expand Up @@ -104,6 +102,4 @@ int mprotect(void* addr, size_t len, int prot) {
return -1;
}

#ifdef __cplusplus
}
#endif
} // extern "C"
4 changes: 4 additions & 0 deletions starboard/shared/win32/posix_emu/pthread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <errno.h>
#include <pthread.h>

extern "C" {

int pthread_mutex_destroy(pthread_mutex_t* mutex) {
return 0;
}
Expand Down Expand Up @@ -51,3 +53,5 @@ int pthread_mutex_trylock(pthread_mutex_t* mutex) {
bool result = TryAcquireSRWLockExclusive(mutex);
return result ? 0 : EBUSY;
}

} // extern "C"
47 changes: 37 additions & 10 deletions starboard/shared/win32/posix_emu/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "starboard/common/log.h"
#include "starboard/types.h"

int gen_fd() {
static int gen_fd() {
static int fd = 100;
fd++;
if (fd == 0x7FFFFFFF) {
Expand All @@ -38,7 +38,7 @@ struct CriticalSection {
static std::map<int, SOCKET>* g_map_addr = nullptr;
static CriticalSection g_critical_section;

int handle_db_put(SOCKET socket_handle) {
static int handle_db_put(SOCKET socket_handle) {
EnterCriticalSection(&g_critical_section.critical_section_);
if (g_map_addr == nullptr) {
g_map_addr = new std::map<int, SOCKET>();
Expand All @@ -56,7 +56,7 @@ int handle_db_put(SOCKET socket_handle) {
return fd;
}

SOCKET handle_db_get(int fd, bool erase) {
static SOCKET handle_db_get(int fd, bool erase) {
EnterCriticalSection(&g_critical_section.critical_section_);
if (g_map_addr == nullptr) {
g_map_addr = new std::map<int, SOCKET>();
Expand All @@ -76,43 +76,70 @@ SOCKET handle_db_get(int fd, bool erase) {
return socket_handle;
}

extern "C" int sb_socket(int domain, int type, int protocol) {
///////////////////////////////////////////////////////////////////////////////
// Implementations below exposed externally in pure C for emulation.
///////////////////////////////////////////////////////////////////////////////

extern "C" {

int sb_socket(int domain, int type, int protocol) {
// On Windows, we must call WSAStartup() before using any other socket
// functions, such as setsockopt, and call WSACleanup() when we are done using
// the Winsock library before exiting. Other platforms don't have similar
// functions to initialize/cleanup their socket libraries.
WSAData wsaData;
int init_result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (init_result != 0) {
SB_DLOG(ERROR) << "Failed to initialize WinSock, error = " << init_result;
WSACleanup();
return -1;
}

// Sockets on Windows do not use *nix-style file descriptors
// socket() returns a handle to a kernel object instead
SOCKET socket_handle = socket(domain, type, protocol);
if (socket_handle == INVALID_SOCKET) {
// TODO: update errno with file operation error
int last_error = WSAGetLastError();
SB_DLOG(ERROR) << "Failed to create socket, last_error = " << last_error;
WSACleanup();
return -1;
}

return handle_db_put(socket_handle);
}

extern "C" int close(int fd) {
int close(int fd) {
SOCKET socket_handle = handle_db_get(fd, true);

if (socket_handle != INVALID_SOCKET) {
WSACleanup();
return closesocket(socket_handle);
}

// This is then a file handle, so use Windows `_close` API.
WSACleanup();
return _close(fd);
}

extern "C" int sb_setsockopt(int socket,
int level,
int option_name,
const void* option_value,
int option_len) {
int sb_setsockopt(int socket,
int level,
int option_name,
const void* option_value,
int option_len) {
int result =
setsockopt(socket, level, option_name,
reinterpret_cast<const char*>(option_value), option_len);
// TODO(b/321999529): Windows returns SOCKET_ERROR on failure. The specific
// error code can be retrieved by calling WSAGetLastError(), and Posix returns
// -1 on failure and sets errno to the error’s value.
if (result == SOCKET_ERROR) {
int last_error = WSAGetLastError();
SB_DLOG(ERROR) << "Failed to set " << option_name << " on socket " << socket
<< ", last_error = " << last_error;
return -1;
} else {
return 0;
}
}
} // extern "C"
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#undef free
#include <stdlib.h>

#include <errno.h>
#include <malloc.h>

// Undef alias to `free` and pull in the system-level header so we can use it
#undef free
#include <../ucrt/malloc.h>

// clang-format off
// The windows.h must be included before the synchapi.h
Expand Down Expand Up @@ -69,7 +72,13 @@ static void* take_aligned_pointer(void* p) {
return take_or_store(true /* take */, p);
}

extern "C" int posix_memalign(void** res, size_t alignment, size_t size) {
///////////////////////////////////////////////////////////////////////////////
// Implementations below exposed externally in pure C for emulation.
///////////////////////////////////////////////////////////////////////////////

extern "C" {

int posix_memalign(void** res, size_t alignment, size_t size) {
*res = _aligned_malloc(size, alignment);
store_aligned_pointer(*res);
if (*res != nullptr) {
Expand All @@ -78,14 +87,16 @@ extern "C" int posix_memalign(void** res, size_t alignment, size_t size) {
return ENOMEM;
}

extern "C" void sb_free(void* p) {
void sb_free(void* p) {
if (!p) {
return;
}
void* res = take_aligned_pointer(p);
if (res == p) {
_aligned_free(p);
} else {
free(p);
free(p); // This is using the system-level implementation.
}
}

} // extern "C"
6 changes: 1 addition & 5 deletions starboard/shared/win32/posix_emu/string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@

#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

char* strdup(const char* s1) {
return _strdup(s1);
}

#ifdef __cplusplus
}
#endif
} // extern "C"
6 changes: 1 addition & 5 deletions starboard/shared/win32/posix_emu/strings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
#include <string.h> // For _stricmp, _strnicmp
#include <strings.h>

#ifdef __cplusplus
extern "C" {
#endif

int strcasecmp(const char* s1, const char* s2) {
return _stricmp(s1, s2);
Expand All @@ -27,6 +25,4 @@ int strncasecmp(const char* s1, const char* s2, size_t n) {
return _strnicmp(s1, s2, n);
}

#ifdef __cplusplus
}
#endif
} // extern "C"
10 changes: 7 additions & 3 deletions starboard/shared/win32/posix_emu/time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#include "starboard/common/log.h"
#include "starboard/types.h"

extern "C" int gettimeofday(struct timeval* tp, void* tzp) {
extern "C" {

int gettimeofday(struct timeval* tp, void* tzp) {
if (tp == NULL) {
return -1;
}
Expand All @@ -44,7 +46,7 @@ extern "C" int gettimeofday(struct timeval* tp, void* tzp) {
return 0;
}

extern "C" int clock_gettime(clockid_t clock_id, struct timespec* tp) {
int clock_gettime(clockid_t clock_id, struct timespec* tp) {
// There are only Windows implementations for realtime and monotonic clocks.
// If CLOCK_PROCESS_CPUTIME_ID or CLOCK_THREAD_CPUTIME_ID are passed in,
// this will return -1. Code that tries to use one of those should either
Expand Down Expand Up @@ -98,9 +100,11 @@ extern "C" int clock_gettime(clockid_t clock_id, struct timespec* tp) {
return -1;
}

extern "C" struct tm* gmtime_r(const time_t* timer, struct tm* result) {
struct tm* gmtime_r(const time_t* timer, struct tm* result) {
if (gmtime_s(result, timer) != 0) {
return NULL;
}
return result;
}

} // extern "C"
2 changes: 1 addition & 1 deletion starboard/win/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ static_library("starboard_platform") {
"//starboard/shared/win32/mutex_release.cc",
"//starboard/shared/win32/once.cc",
"//starboard/shared/win32/posix_emu/mman.cc",
"//starboard/shared/win32/posix_emu/posix_memalign.cc",
"//starboard/shared/win32/posix_emu/pthread.cc",
"//starboard/shared/win32/posix_emu/socket.cc",
"//starboard/shared/win32/posix_emu/stdlib.cc",
"//starboard/shared/win32/posix_emu/string.cc",
"//starboard/shared/win32/posix_emu/strings.cc",
"//starboard/shared/win32/posix_emu/time.cc",
Expand Down
5 changes: 0 additions & 5 deletions starboard/win/shared/platform_configuration/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ config("platform_configuration") {
"_CRT_STDIO_ISO_WIDE_SPECIFIERS",
]

cflags = [
# Force include in every module
"/FI ../../starboard/shared/win32/posix_emu/include/posix_force_include.h",
]

include_dirs = [
# POSIX emulation headers - these need to come *before* system include_dirs.
"//starboard/shared/win32/posix_emu/include",
Expand Down
Loading

0 comments on commit a941b31

Please sign in to comment.