Skip to content
This repository has been archived by the owner on Dec 1, 2020. It is now read-only.

Commit

Permalink
Add Windows support (#112)
Browse files Browse the repository at this point in the history
Added Windows specific files.
For now it is for i686 builds.

libtuv-DCO-1.0-Signed-off-by: Peter Gal [email protected]
  • Loading branch information
galpeter authored and yichoi committed Jun 19, 2018
1 parent d9ae9f2 commit dd02811
Show file tree
Hide file tree
Showing 40 changed files with 23,810 additions and 3 deletions.
20 changes: 20 additions & 0 deletions cmake/config/config_i686-windows.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
#
# 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.

set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR i686)

if(NOT DEFINED CMAKE_C_COMPILER)
set(CMAKE_C_COMPILER CL.exe)
endif()
2 changes: 1 addition & 1 deletion cmake/libtuv.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ set(COMMON_SRCFILES
${INCLUDE_ROOT}/uv-errno.h
${INCLUDE_ROOT}/uv-threadpool.h
${INCLUDE_ROOT}/uv-version.h
# ${SOURCE_ROOT}/fs-poll.c
${SOURCE_ROOT}/fs-poll.c
${SOURCE_ROOT}/heap-inl.h
${SOURCE_ROOT}/inet.c
${SOURCE_ROOT}/queue.h
Expand Down
19 changes: 19 additions & 0 deletions cmake/option/option_i686-windows.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2015 Samsung Electronics Co., Ltd.
#
# 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.

# linux common
include("cmake/option/option_windows_common.cmake")

# i686 specific

88 changes: 88 additions & 0 deletions cmake/option/option_windows_common.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Copyright 2015-2017 Samsung Electronics Co., Ltd.
#
# 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.


#set(CMAKE_C_FLAGS_DEBUG "-O0 -g -DDEBUG")
#set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")

# unix common source files
set(WIN_PATH "${SOURCE_ROOT}/win")

# test include
set(TUV_TEST_INCDIRS "${TEST_ROOT}")

#
# { TUV_CHANGES@20161129:
# It corresponds to uv.gyp's section "Windows" }
#
set(PLATFORM_SRCFILES
${INCLUDE_ROOT}/uv-win.h
${WIN_PATH}/async.c
${WIN_PATH}/atomicops-inl.h
${WIN_PATH}/core.c
${WIN_PATH}/detect-wakeup.c
${WIN_PATH}/dl.c
${WIN_PATH}/error.c
${WIN_PATH}/fs.c
${WIN_PATH}/fs-event.c
${WIN_PATH}/getaddrinfo.c
${WIN_PATH}/getnameinfo.c
${WIN_PATH}/handle.c
${WIN_PATH}/handle-inl.h
${WIN_PATH}/internal.h
${WIN_PATH}/loop-watcher.c
${WIN_PATH}/pipe.c
${WIN_PATH}/thread.c
${WIN_PATH}/poll.c
${WIN_PATH}/process.c
${WIN_PATH}/process-stdio.c
${WIN_PATH}/req.c
${WIN_PATH}/req-inl.h
${WIN_PATH}/signal.c
${WIN_PATH}/snprintf.c
${WIN_PATH}/stream.c
${WIN_PATH}/stream-inl.h
${WIN_PATH}/tcp.c
${WIN_PATH}/tty.c
${WIN_PATH}/timer.c
${WIN_PATH}/udp.c
${WIN_PATH}/util.c
${WIN_PATH}/winapi.c
${WIN_PATH}/winapi.h
${WIN_PATH}/winsock.c
${WIN_PATH}/winsock.h
)

set(TEST_MAINFILE "${TEST_ROOT}/runner_main.c")

set(TEST_UNITFILES
${TEST_ROOT}/test_idle.c
${TEST_ROOT}/test_timer.c
${TEST_ROOT}/test_timer_again.c
${TEST_ROOT}/test_fs.c
${TEST_ROOT}/test_cwd.c
${TEST_ROOT}/test_error.c
${TEST_ROOT}/test_tcp_open.c
${TEST_ROOT}/test_shutdown_eof.c
${TEST_ROOT}/echo_server.c
${TEST_ROOT}/test_getaddrinfo.c
${TEST_ROOT}/test_threadpool.c
${TEST_ROOT}/test_condvar.c
${TEST_ROOT}/test_active.c
${TEST_ROOT}/test_walk_handles.c
${TEST_ROOT}/test_async.c
)

# configuration values
set(CONFIG_FILESYSTEM 1)
26 changes: 26 additions & 0 deletions include/uv.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ extern "C" {

/* Expand this list if necessary. */
#define UV_ERRNO_MAP(XX) \
XX(E2BIG, "argument list too long") \
XX(EACCES, "permission denied") \
XX(EADDRINUSE, "address already in use") \
XX(EADDRNOTAVAIL, "address not available") \
XX(EAFNOSUPPORT, "address family not supported") \
XX(EAGAIN, "resource temporarily unavailable") \
XX(EAI_ADDRFAMILY, "address family not supported") \
Expand All @@ -99,21 +102,44 @@ extern "C" {
XX(EAI_SERVICE, "service not available for socket type") \
XX(EAI_SOCKTYPE, "socket type not supported") \
XX(EALREADY, "connection already in progress") \
XX(EBADF, "bad file descriptor") \
XX(EBUSY, "resource busy or locked") \
XX(ECANCELED, "operation canceled") \
XX(ECHARSET, "invalid Unicode character") \
XX(ECONNABORTED, "software caused connection abort") \
XX(ECONNREFUSED, "connection refused") \
XX(ECONNRESET, "connection reset by peer") \
XX(EEXIST, "file already exists") \
XX(EFAULT, "bad address in system call argument") \
XX(EHOSTUNREACH, "host is unreachable") \
XX(EINVAL, "invalid argument") \
XX(EIO, "i/o error") \
XX(EISCONN, "socket is already connected") \
XX(EISDIR, "illegal operation on a directory") \
XX(ELOOP, "too many symbolic links encountered") \
XX(EMFILE, "too many open files") \
XX(EMSGSIZE, "message too long") \
XX(ENAMETOOLONG, "name too long") \
XX(ENETUNREACH, "network is unreachable") \
XX(ENOBUFS, "no buffer space available") \
XX(ENOENT, "no such file or directory") \
XX(ENOMEM, "not enough memory") \
XX(ENOSPC, "no space left on device") \
XX(ENOSYS, "function not implemented") \
XX(ENOTCONN, "socket is not connected") \
XX(ENOTDIR, "not a directory") \
XX(ENOTEMPTY, "directory not empty") \
XX(ENOTSOCK, "socket operation on non-socket") \
XX(ENOTSUP, "operation not supported on socket") \
XX(EPERM, "operation not permitted") \
XX(EPIPE, "broken pipe") \
XX(ESRCH, "no such process") \
XX(EPROTONOSUPPORT, "protocol not supported") \
XX(EROFS, "read-only file system") \
XX(ETIMEDOUT, "connection timed out") \
XX(EXDEV, "cross-device link not permitted") \
XX(EOF, "end of file") \
XX(UNKNOWN, "unknown error") \

#define UV_HANDLE_TYPE_MAP(XX) \
XX(ASYNC, async) \
Expand Down
8 changes: 6 additions & 2 deletions src/uv-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* memset */
#include <unistd.h> /* usleep */

#if defined(_WIN32)
# include <malloc.h> /* malloc */
#else
# include <unistd.h> /* usleep */
# include <net/if.h> /* if_nametoindex */
#endif

Expand All @@ -67,7 +67,7 @@ static uv__allocator_t uv__allocator = {
free,
};

#if defined(__APPLE__)
#if defined(__APPLE__) || defined(_WIN32)
char* uv__strdup(const char* s) {
size_t len = strlen(s) + 1;
char* m = uv__malloc(len);
Expand Down Expand Up @@ -491,5 +491,9 @@ int uv_loop_close(uv_loop_t* loop) {

/* Pause the calling thread for a number of milliseconds. */
void uv_sleep(int msec) {
#ifdef _WIN32
Sleep(msec * 1000);
#else
usleep(msec * 1000);
#endif
}
103 changes: 103 additions & 0 deletions src/win/async.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include <assert.h>

#include "uv.h"
#include "internal.h"
#include "atomicops-inl.h"
#include "handle-inl.h"
#include "req-inl.h"


void uv_async_endgame(uv_loop_t* loop, uv_async_t* handle) {
if (handle->flags & UV__HANDLE_CLOSING &&
!handle->async_sent) {
assert(!(handle->flags & UV_HANDLE_CLOSED));
uv__handle_close(handle);
}
}


int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
uv_req_t* req;

uv__handle_init(loop, (uv_handle_t*) handle, UV_ASYNC);
handle->async_sent = 0;
handle->async_cb = async_cb;

req = &handle->async_req;
uv_req_init(loop, req);
req->type = UV_WAKEUP;
req->data = handle;

uv__handle_start(handle);

return 0;
}

int tuv_async_deinit(uv_loop_t* loop, uv_async_t* handle) {
/* No operation required */
return 0;
}

void uv_async_close(uv_loop_t* loop, uv_async_t* handle) {
if (!((uv_async_t*)handle)->async_sent) {
uv_want_endgame(loop, (uv_handle_t*) handle);
}

uv__handle_closing(handle);
}


int uv_async_send(uv_async_t* handle) {
uv_loop_t* loop = handle->loop;

if (handle->type != UV_ASYNC) {
/* Can't set errno because that's not thread-safe. */
return -1;
}

/* The user should make sure never to call uv_async_send to a closing */
/* or closed handle. */
assert(!(handle->flags & UV__HANDLE_CLOSING));

if (!uv__atomic_exchange_set(&handle->async_sent)) {
POST_COMPLETION_FOR_REQ(loop, &handle->async_req);
}

return 0;
}


void uv_process_async_wakeup_req(uv_loop_t* loop, uv_async_t* handle,
uv_req_t* req) {
assert(handle->type == UV_ASYNC);
assert(req->type == UV_WAKEUP);

handle->async_sent = 0;

if (handle->flags & UV__HANDLE_CLOSING) {
uv_want_endgame(loop, (uv_handle_t*)handle);
} else if (handle->async_cb != NULL) {
handle->async_cb(handle);
}
}
56 changes: 56 additions & 0 deletions src/win/atomicops-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#ifndef UV_WIN_ATOMICOPS_INL_H_
#define UV_WIN_ATOMICOPS_INL_H_

#include "uv.h"


/* Atomic set operation on char */
#ifdef _MSC_VER /* MSVC */

/* _InterlockedOr8 is supported by MSVC on x32 and x64. It is slightly less */
/* efficient than InterlockedExchange, but InterlockedExchange8 does not */
/* exist, and interlocked operations on larger targets might require the */
/* target to be aligned. */
#pragma intrinsic(_InterlockedOr8)

static char __declspec(inline) uv__atomic_exchange_set(char volatile* target) {
return _InterlockedOr8(target, 1);
}

#else /* GCC */

/* Mingw-32 version, hopefully this works for 64-bit gcc as well. */
static inline char uv__atomic_exchange_set(char volatile* target) {
const char one = 1;
char old_value;
__asm__ __volatile__ ("lock xchgb %0, %1\n\t"
: "=r"(old_value), "=m"(*target)
: "0"(one), "m"(*target)
: "memory");
return old_value;
}

#endif

#endif /* UV_WIN_ATOMICOPS_INL_H_ */
Loading

0 comments on commit dd02811

Please sign in to comment.