---help---
select to enable all iotivity stack logs (DEBUG / INFO etc.)
+config LIBTUV
+ bool "libtuv"
+ default n
+ ---help---
+ enable libtuv
endif
endif
+ifeq ($(CONFIG_LIBTUV),y)
+include libtuv/source/tinyara/Make.defs
+include libtuv/source/unix/Make.defs
+include libtuv/source/Make.defs
+endif
+
ifeq ($(CONFIG_DM),y)
CFLAGS+=-I$(TOPDIR)/../framework/include/dm
endif
--- /dev/null
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "{}"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright {yyyy} {name of copyright owner}
+
+ 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.
+
+=============================================================================
+
+LICENCE of libuv
+
+libuv is part of the Node project: http://nodejs.org/
+libuv may be distributed alone under Node's license:
+
+====
+
+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.
+
+====
+
+This license applies to all parts of libuv that are not externally
+maintained libraries.
+
+The externally maintained libraries used by libuv are:
+
+ - tree.h (from FreeBSD), copyright Niels Provos. Two clause BSD license.
+
+ - inet_pton and inet_ntop implementations, contained in src/inet.c, are
+ copyright the Internet Systems Consortium, Inc., and licensed under the ISC
+ license.
+
+ - stdint-msvc2008.h (from msinttypes), copyright Alexander Chemeris. Three
+ clause BSD license.
+
+ - pthread-fixes.h, pthread-fixes.c, copyright Google Inc. and Sony Mobile
+ Communications AB. Three clause BSD license.
+
+ - android-ifaddrs.h, android-ifaddrs.c, copyright Berkeley Software Design
+ Inc, Kenneth MacKay and Emergya (Cloud4all, FP7/2007-2013, grant agreement
+ n° 289016). Three clause BSD license.
--- /dev/null
+/* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef QUEUE_H_
+#define QUEUE_H_
+
+typedef void *QUEUE[2];
+
+/* Private macros. */
+#define QUEUE_NEXT(q) (*(QUEUE **) &((*(q))[0]))
+#define QUEUE_PREV(q) (*(QUEUE **) &((*(q))[1]))
+#define QUEUE_PREV_NEXT(q) (QUEUE_NEXT(QUEUE_PREV(q)))
+#define QUEUE_NEXT_PREV(q) (QUEUE_PREV(QUEUE_NEXT(q)))
+
+/* Public macros. */
+#define QUEUE_DATA(ptr, type, field) \
+ ((type *) ((char *) (ptr) - ((char *) &((type *) 0)->field)))
+
+#define QUEUE_FOREACH(q, h) \
+ for ((q) = QUEUE_NEXT(h); (q) != (h); (q) = QUEUE_NEXT(q))
+
+#define QUEUE_EMPTY(q) \
+ ((const QUEUE *) (q) == (const QUEUE *) QUEUE_NEXT(q))
+
+#define QUEUE_HEAD(q) \
+ (QUEUE_NEXT(q))
+
+#define QUEUE_INIT(q) \
+ do { \
+ QUEUE_NEXT(q) = (q); \
+ QUEUE_PREV(q) = (q); \
+ } \
+ while (0)
+
+#define QUEUE_ADD(h, n) \
+ do { \
+ QUEUE_PREV_NEXT(h) = QUEUE_NEXT(n); \
+ QUEUE_NEXT_PREV(n) = QUEUE_PREV(h); \
+ QUEUE_PREV(h) = QUEUE_PREV(n); \
+ QUEUE_PREV_NEXT(h) = (h); \
+ } \
+ while (0)
+
+#define QUEUE_SPLIT(h, q, n) \
+ do { \
+ QUEUE_PREV(n) = QUEUE_PREV(h); \
+ QUEUE_PREV_NEXT(n) = (n); \
+ QUEUE_NEXT(n) = (q); \
+ QUEUE_PREV(h) = QUEUE_PREV(q); \
+ QUEUE_PREV_NEXT(h) = (h); \
+ QUEUE_PREV(q) = (n); \
+ } \
+ while (0)
+
+#define QUEUE_INSERT_HEAD(h, q) \
+ do { \
+ QUEUE_NEXT(q) = QUEUE_NEXT(h); \
+ QUEUE_PREV(q) = (h); \
+ QUEUE_NEXT_PREV(q) = (q); \
+ QUEUE_NEXT(h) = (q); \
+ } \
+ while (0)
+
+#define QUEUE_INSERT_TAIL(h, q) \
+ do { \
+ QUEUE_NEXT(q) = (h); \
+ QUEUE_PREV(q) = QUEUE_PREV(h); \
+ QUEUE_PREV_NEXT(q) = (q); \
+ QUEUE_PREV(h) = (q); \
+ } \
+ while (0)
+
+#define QUEUE_REMOVE(q) \
+ do { \
+ QUEUE_PREV_NEXT(q) = QUEUE_NEXT(q); \
+ QUEUE_NEXT_PREV(q) = QUEUE_PREV(q); \
+ } \
+ while (0)
+
+#endif /* QUEUE_H_ */
--- /dev/null
+/* 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.
+ */
+
+#ifndef __tuv_debuglog_header__
+#define __tuv_debuglog_header__
+
+#ifdef ENABLE_DEBUG_LOG
+
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern int tuv_debug_level;
+extern FILE *tuv_log_stream;
+extern const char *tuv_debug_prefix[4];
+
+#define TDBGLEV_ERR 1
+#define TDBGLEV_WARN 2
+#define TDBGLEV_INFO 3
+
+#define TUV_DLOG(lvl, ...) \
+ do { \
+ int errback = get_errno(); \
+ if (0 <= lvl && lvl <= tuv_debug_level && tuv_log_stream) { \
+ fprintf(tuv_log_stream, "[%s] ", tuv_debug_prefix[lvl]); \
+ fprintf(tuv_log_stream, __VA_ARGS__); \
+ fprintf(tuv_log_stream, "\n"); \
+ fflush(tuv_log_stream); \
+ } \
+ set_errno(errback); \
+ } while (0)
+#define TDLOG(...) TUV_DLOG(TDBGLEV_ERR, __VA_ARGS__)
+#define TDDLOG(...) TUV_DLOG(TDBGLEV_WARN, __VA_ARGS__)
+#define TDDDLOG(...) TUV_DLOG(TDBGLEV_INFO, __VA_ARGS__)
+
+/*
+ Use DLOG for errors, default you will see them
+ Use DDLOG for warnings, set tuv_debug_level=2 to see them
+ USE DDDLOG for informations, set tuv_debug_level=3 to see them
+*/
+
+#ifdef __cplusplus
+}
+#endif
+#else /* !ENABLE_DEBUG_LOG */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define TUV_DLOG(...)
+#define TDLOG(...)
+#define TDDLOG(...)
+#define TDDDLOG(...)
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* ENABLE_DEBUG_LOG */
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void InitDebugSettings(void);
+void ReleaseDebugSettings(void);
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* __tuv_debuglog_header__ */
--- /dev/null
+/* 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.
+ */
+
+/* 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_header__
+#define __uv_header__
+
+#include <stdint.h>
+#include <stddef.h>
+#include <sys/types.h>
+
+#include "queue.h"
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
+#define container_of(ptr, type, member) \
+ ((type *) ((char *) (ptr) - offsetof(type, member)))
+
+#include "uv__types.h" // types should be placed in the first
+#include "uv_platform.h" // platform should be placed before extension
+#include "uv_extension.h" // extension should be placed before others
+
+#include "uv__error.h"
+#include "uv__handle.h"
+#include "uv__loop.h"
+#include "uv__idle.h"
+#include "uv__timer.h"
+#include "uv__inet.h"
+
+#include "uv__poll.h"
+
+#include "uv__async.h"
+#include "uv__req.h" // req should be placed before fs
+#include "uv__fs.h"
+#include "uv__fd.h"
+#include "uv__stream.h"
+#include "uv__tcp.h"
+#include "uv__pipe.h"
+#include "uv__thread.h"
+
+#include "uv__threadpool.h"
+#include "uv__getaddrinfo.h"
+
+#include "uv__dir.h"
+#include "uv__util.h"
+
+#include "tuv_debuglog.h"
+
+#undef UV_HANDLE_PRIVATE_FIELDS
+#undef UV_ASYNC_PRIVATE_FIELDS
+#undef UV_LOOP_PRIVATE_FIELDS
+#undef UV_IDLE_PRIVATE_FIELDS
+#undef UV_TIMER_PRIVATE_FIELDS
+#undef UV_FS_PRIVATE_FIELDS
+#undef UV_REQ_TYPE_PRIVATE
+#undef UV_REQ_PRIVATE_FIELDS
+#undef UV_FS_REQ_PRIVATE_FIELDS
+#undef UV_WORK_PRIVATE_FIELDS
+#undef UV_IO_PRIVATE_PLATFORM_FIELDS
+#undef UV_TCP_PRIVATE_FIELDS
+#undef UV_STREAM_PRIVATE_FIELDS
+#undef UV_PIPE_PRIVATE_FIELDS
+#undef UV_CONNECT_PRIVATE_FIELDS
+#undef UV_WRITE_PRIVATE_FIELDS
+#undef UV_SHUTDOWN_PRIVATE_FIELDS
+#undef UV_GETADDRINFO_PRIVATE_FIELDS
+
+#endif // __uv_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__async_header__
+#define __uv__async_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//-----------------------------------------------------------------------------
+
+#define ACCESS_ONCE(type, var) \
+ (*(volatile type*) &(var))
+
+//-----------------------------------------------------------------------------
+
+struct uv_async_s {
+ UV_HANDLE_FIELDS UV_ASYNC_PRIVATE_FIELDS
+};
+
+int uv_async_init(uv_loop_t *, uv_async_t *async, uv_async_cb async_cb);
+int uv_async_send(uv_async_t *async);
+int uv_async_deinit(uv_loop_t *loop, uv_async_t *handle);
+
+//-----------------------------------------------------------------------------
+
+struct uv__io_s {
+ uv__io_cb cb;
+ void *pending_queue[2];
+ void *watcher_queue[2];
+ unsigned int pevents; /* Pending event mask i.e. mask at next tick. */
+ unsigned int events; /* Current event mask. */
+ int fd;
+ UV_IO_PRIVATE_PLATFORM_FIELDS
+};
+
+struct uv__async {
+ uv__async_cb cb;
+ uv__io_t io_watcher;
+ int wfd;
+};
+
+void uv__async_send(struct uv__async *wa);
+void uv__async_init(struct uv__async *wa);
+int uv__async_start(uv_loop_t *loop, struct uv__async *wa, uv__async_cb cb);
+void uv__async_stop(uv_loop_t *loop, struct uv__async *wa);
+
+void uv__async_close(uv_async_t *handle);
+int uv__async_make_pending(int *pending);
+
+void uv__io_init(uv__io_t *w, uv__io_cb cb, int fd);
+void uv__io_start(uv_loop_t *loop, uv__io_t *w, unsigned int events);
+void uv__io_stop(uv_loop_t *loop, uv__io_t *w, unsigned int events);
+void uv__io_close(uv_loop_t *loop, uv__io_t *w);
+
+void uv__io_feed(uv_loop_t *loop, uv__io_t *w);
+int uv__io_active(const uv__io_t *w, unsigned int events);
+void uv__io_poll(uv_loop_t *loop, int timeout); /* in milliseconds or -1 */
+
+//-----------------------------------------------------------------------------
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__async_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__dir_header__
+#define __uv__dir_header__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int uv_cwd(char *buffer, size_t *size);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__dir_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__error_header__
+#define __uv__error_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#include <errno.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined(DEBUG)
+#define ABORT() \
+ do { \
+ TDLOG("!!!!! ABORT !!!!!"); \
+ exit(-1); \
+ } while(0)
+#else
+#define ABORT() abort()
+#endif
+
+//-----------------------------------------------------------------------------
+// errorno < uv_errorno.h
+
+#define UV__EOF (-4095)
+#define UV__UNKNOWN (-4094)
+
+#define UV__EAI_ADDRFAMILY (-3000)
+#define UV__EAI_AGAIN (-3001)
+#define UV__EAI_BADFLAGS (-3002)
+#define UV__EAI_CANCELED (-3003)
+#define UV__EAI_FAIL (-3004)
+#define UV__EAI_FAMILY (-3005)
+#define UV__EAI_MEMORY (-3006)
+#define UV__EAI_NODATA (-3007)
+#define UV__EAI_NONAME (-3008)
+#define UV__EAI_OVERFLOW (-3009)
+#define UV__EAI_SERVICE (-3010)
+#define UV__EAI_SOCKTYPE (-3011)
+#define UV__EAI_BADHINTS (-3013)
+#define UV__EAI_PROTOCOL (-3014)
+
+#if defined(EAFNOSUPPORT)
+#define UV__EAFNOSUPPORT (-EAFNOSUPPORT)
+#else
+#define UV__EAFNOSUPPORT (-4089)
+#endif
+
+#if defined(EAGAIN)
+#define UV__EAGAIN (-EAGAIN)
+#else
+#define UV__EAGAIN (-4088)
+#endif
+
+#if defined(EBUSY)
+#define UV__EBUSY (-EBUSY)
+#else
+#define UV__EBUSY (-4082)
+#endif
+
+#if defined(ECANCELED)
+#define UV__ECANCELED (-ECANCELED)
+#else
+#define UV__ECANCELED (-4081)
+#endif
+
+#if defined(ECONNREFUSED)
+#define UV__ECONNREFUSED (-ECONNREFUSED)
+#else
+#define UV__ECONNREFUSED (-4078)
+#endif
+
+#if defined(EINVAL)
+#define UV__EINVAL (-EINVAL)
+#else
+#define UV__EINVAL (-4071)
+#endif
+
+#if defined(ELOOP)
+#define UV__ELOOP (-ELOOP)
+#else
+#define UV__ELOOP (-4067)
+#endif
+
+#if defined(ENAMETOOLONG)
+#define UV__ENAMETOOLONG (-ENAMETOOLONG)
+#else
+#define UV__ENAMETOOLONG (-4064)
+#endif
+
+#if defined(ENOBUFS)
+#define UV__ENOBUFS (-ENOBUFS)
+#else
+#define UV__ENOBUFS (-4060)
+#endif
+
+#if defined(ENOENT)
+#define UV__ENOENT (-ENOENT)
+#else
+#define UV__ENOENT (-4058)
+#endif
+
+#if defined(ENOMEM)
+#define UV__ENOMEM (-ENOMEM)
+#else
+#define UV__ENOMEM (-4057)
+#endif
+
+#if defined(ENOSPC)
+#define UV__ENOSPC (-ENOSPC)
+#else
+#define UV__ENOSPC (-4055)
+#endif
+
+#if defined(ENOTDIR)
+#define UV__ENOTDIR (-ENOTDIR)
+#else
+#define UV__ENOTDIR (-4052)
+#endif
+
+#if defined(EPERM)
+#define UV__EPERM (-EPERM)
+#else
+#define UV__EPERM (-4048)
+#endif
+
+#if defined(ETIMEDOUT)
+#define UV__ETIMEDOUT (-ETIMEDOUT)
+#else
+#define UV__ETIMEDOUT (-4039)
+#endif
+
+//-----------------------------------------------------------------------------
+// errno map < uv.h
+
+#define UV_ERRNO_MAP(XX) \
+ XX(EAFNOSUPPORT, "address family not supported") \
+ XX(EAI_ADDRFAMILY, "address family not supported") \
+ XX(EAI_AGAIN, "temporary failure") \
+ XX(EAI_BADFLAGS, "bad ai_flags value") \
+ XX(EAI_BADHINTS, "invalid value for hints") \
+ XX(EAI_CANCELED, "request canceled") \
+ XX(EAI_FAIL, "permanent failure") \
+ XX(EAI_FAMILY, "ai_family not supported") \
+ XX(EAI_MEMORY, "out of memory") \
+ XX(EAI_NODATA, "no address") \
+ XX(EAI_NONAME, "unknown node or service") \
+ XX(EAI_OVERFLOW, "argument buffer overflow") \
+ XX(EAI_PROTOCOL, "resolved protocol is unknown") \
+ XX(EAI_SERVICE, "service not available for socket type") \
+ XX(EAI_SOCKTYPE, "socket type not supported") \
+ XX(EAGAIN, "resource temporarily unavailable") \
+ XX(EBUSY, "resource busy or locked") \
+ XX(ECANCELED, "operation canceled") \
+ XX(ECONNREFUSED, "connection refused") \
+ XX(EINVAL, "invalid argument") \
+ XX(ELOOP, "too many symbolic links encountered") \
+ XX(ENAMETOOLONG, "name too long") \
+ 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(ENOTDIR, "not a directory") \
+ XX(EPERM, "operation not permitted") \
+ XX(ETIMEDOUT, "connection timed out") \
+ XX(EOF, "end of file") \
+
+//-----------------------------------------------------------------------------
+#define XX(code, _) UV_ ## code = UV__ ## code,
+
+typedef enum {
+ UV_ERRNO_MAP(XX)
+ UV_ERRNO_MAX = UV__EOF - 1
+} uv_errno_t;
+
+#undef XX
+
+const char *uv_strerror(int err);
+const char *uv_err_name(int err);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__error_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__fd_header__
+#define __uv__fd_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct msghdr;
+
+int uv__nonblock(int fd, int set);
+int uv__close(int fd);
+int uv__socket(int domain, int type, int protocol);
+
+int uv__make_pipe(int fds[2], int flags);
+
+int uv__open_cloexec(const char *path, int flags);
+int uv__cloexec(int fd, int set);
+
+ssize_t uv__recvmsg(int fd, struct msghdr *msg, int flags);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__fd_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__fs_header__
+#define __uv__fs_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//-----------------------------------------------------------------------------
+
+struct uv_buf_s {
+ char *base;
+ size_t len;
+};
+
+//-----------------------------------------------------------------------------
+
+typedef enum {
+ UV_FS_UNKNOWN = -1,
+ UV_FS_CUSTOM,
+ UV_FS_OPEN,
+ UV_FS_CLOSE,
+ UV_FS_READ,
+ UV_FS_WRITE,
+ UV_FS_SENDFILE,
+ UV_FS_STAT,
+ UV_FS_LSTAT,
+ UV_FS_FSTAT,
+ UV_FS_FTRUNCATE,
+ UV_FS_UTIME,
+ UV_FS_FUTIME,
+ UV_FS_ACCESS,
+ UV_FS_CHMOD,
+ UV_FS_FCHMOD,
+ UV_FS_FSYNC,
+ UV_FS_FDATASYNC,
+ UV_FS_UNLINK,
+ UV_FS_RMDIR,
+ UV_FS_MKDIR,
+ UV_FS_MKDTEMP,
+ UV_FS_RENAME,
+ UV_FS_SCANDIR,
+ UV_FS_LINK,
+ UV_FS_SYMLINK,
+ UV_FS_READLINK,
+ UV_FS_CHOWN,
+ UV_FS_FCHOWN
+} uv_fs_type;
+
+/* uv_fs_t is a subclass of uv_req_t. */
+struct uv_fs_s {
+ UV_REQ_FIELDS uv_fs_type fs_type;
+ uv_loop_t *loop;
+ uv_fs_cb cb;
+ ssize_t result;
+ void *ptr;
+ const char *path;
+ uv_stat_t statbuf; /* Stores the result of uv_fs_stat() and uv_fs_fstat(). */
+ UV_FS_PRIVATE_FIELDS
+};
+
+void uv_fs_req_cleanup(uv_fs_t *req);
+int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb);
+int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb);
+int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb);
+int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb);
+
+int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb);
+int uv_fs_fstat(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb);
+
+int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb);
+int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb);
+
+int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb);
+int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file file, int64_t offset, uv_fs_cb cb);
+int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, uv_fs_cb cb);
+
+int uv_fs_futime(uv_loop_t *loop, uv_fs_t *req, uv_file file, double atime, double mtime, uv_fs_cb cb);
+int uv_fs_utime(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__fs_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__getaddrinfo_header__
+#define __uv__getaddrinfo_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * uv_getaddrinfo_t is a subclass of uv_req_t.
+ *
+ * Request object for uv_getaddrinfo.
+ */
+struct uv_getaddrinfo_s {
+ UV_REQ_FIELDS
+ /* read-only */
+ uv_loop_t *loop;
+ /* struct addrinfo* addrinfo is marked as private, but it really isn't. */
+ UV_GETADDRINFO_PRIVATE_FIELDS
+};
+
+int uv_getaddrinfo(uv_loop_t *loop, uv_getaddrinfo_t *req, uv_getaddrinfo_cb getaddrinfo_cb, const char *node, const char *service, const struct addrinfo *hints);
+void uv_freeaddrinfo(struct addrinfo *ai);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__getaddrinfo_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__handle_header__
+#define __uv__handle_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//-----------------------------------------------------------------------------
+// uv_handle_t
+
+#define UV_HANDLE_TYPE_MAP(XX) \
+ XX(ASYNC, async) \
+ XX(HANDLE, handle) \
+ XX(IDLE, idle) \
+ XX(NAMED_PIPE, pipe) \
+ XX(STREAM, stream) \
+ XX(TCP, tcp) \
+ XX(TIMER, timer) \
+ XX(TTY, tty) \
+ XX(POLL, poll) \
+
+/*
+ XX(ASYNC, async) \
+ XX(CHECK, check) \
+ XX(FS_EVENT, fs_event) \
+ XX(FS_POLL, fs_poll) \
+ XX(HANDLE, handle) \
+ XX(IDLE, idle) \
+ XX(NAMED_PIPE, pipe) \
+ XX(POLL, poll) \
+ XX(PREPARE, prepare) \
+ XX(PROCESS, process) \
+ XX(STREAM, stream) \
+ XX(TCP, tcp) \
+ XX(TIMER, timer) \
+ XX(TTY, tty) \
+ XX(UDP, udp) \
+ XX(SIGNAL, signal) \
+*/
+
+//-----------------------------------------------------------------------------
+#define XX(uc, lc) UV_##uc,
+
+typedef enum {
+ UV_UNKNOWN_HANDLE = 0,
+ UV_HANDLE_TYPE_MAP(XX)
+ UV_FILE,
+ UV_HANDLE_TYPE_MAX
+} uv_handle_type;
+
+#undef XX
+//-----------------------------------------------------------------------------
+
+/* Leans on the fact that, on Linux, POLLRDHUP == EPOLLRDHUP. */
+#ifdef POLLRDHUP
+#define UV__POLLRDHUP POLLRDHUP
+#else
+#define UV__POLLRDHUP 0x2000
+#endif
+
+/* handle flags */
+enum {
+ UV_CLOSING = 0x0001, /* uv_close() called but not finished. */
+ UV_CLOSED = 0x0002, /* close(2) finished. */
+ UV_STREAM_READING = 0x0004, /* uv_read_start() called. */
+ UV_STREAM_SHUTTING = 0x0008, /* uv_shutdown() called but not complete. */
+ UV_STREAM_SHUT = 0x0010, /* Write side closed. */
+ UV_STREAM_READABLE = 0x0020, /* The stream is readable */
+ UV_STREAM_WRITABLE = 0x0040, /* The stream is writable */
+ UV_STREAM_BLOCKING = 0x0080, /* Synchronous writes. */
+ UV_STREAM_READ_PARTIAL = 0x0100, /* read(2) read less than requested. */
+ UV_STREAM_READ_EOF = 0x0200, /* read(2) read EOF. */
+ UV_TCP_NODELAY = 0x0400, /* Disable Nagle. */
+ UV_TCP_KEEPALIVE = 0x0800, /* Turn on keep-alive. */
+ UV_TCP_SINGLE_ACCEPT = 0x1000, /* Only accept() when idle. */
+ UV_HANDLE_IPV6 = 0x10000 /* Handle is bound to a IPv6 socket. */
+};
+
+//-----------------------------------------------------------------------------
+
+#define UV_HANDLE_FIELDS \
+ /* public */ \
+ void* data; \
+ /* read-only */ \
+ uv_loop_t* loop; \
+ uv_handle_type type; \
+ /* private */ \
+ unsigned int flags; \
+ uv_close_cb close_cb; \
+ void* handle_queue[2]; \
+ UV_HANDLE_PRIVATE_FIELDS \
+
+// 'void* reserved[4]' came in to libuv @
+// 06f9e1438ed20dd83618bca78759ea8635a79289
+/*
+ union { \
+ int fd; \
+ void* reserved[4]; \
+ } u; \
+*/
+
+struct uv_handle_s {
+ UV_HANDLE_FIELDS
+};
+
+#define uv__active_handle_add(h) \
+ do { \
+ (h)->loop->active_handles++; \
+ } \
+ while (0)
+
+#define uv__active_handle_rm(h) \
+ do { \
+ (h)->loop->active_handles--; \
+ } \
+ while (0)
+
+#define uv__is_active(h) \
+ (((h)->flags & UV__HANDLE_ACTIVE) != 0)
+
+#define uv__is_closing(h) \
+ (((h)->flags & (UV_CLOSING | UV_CLOSED)) != 0)
+
+#define uv__handle_start(h) \
+ do { \
+ assert(((h)->flags & UV__HANDLE_CLOSING) == 0); \
+ if (((h)->flags & UV__HANDLE_ACTIVE) != 0) break; \
+ (h)->flags |= UV__HANDLE_ACTIVE; \
+ if (((h)->flags & UV__HANDLE_REF) != 0) uv__active_handle_add(h); \
+ } \
+ while (0)
+
+#define uv__handle_stop(h) \
+ do { \
+ assert(((h)->flags & UV__HANDLE_CLOSING) == 0); \
+ if (((h)->flags & UV__HANDLE_ACTIVE) == 0) break; \
+ (h)->flags &= ~UV__HANDLE_ACTIVE; \
+ if (((h)->flags & UV__HANDLE_REF) != 0) uv__active_handle_rm(h); \
+ } \
+ while (0)
+
+#define uv__handle_init(loop_, h, type_) \
+ do { \
+ (h)->loop = (loop_); \
+ (h)->type = (type_); \
+ (h)->flags = UV__HANDLE_REF; /* Ref the loop when active. */ \
+ QUEUE_INIT(&(h)->handle_queue); \
+ QUEUE_INSERT_TAIL(&(loop_)->handles_queue, &(h)->handle_queue); \
+ uv__handle_platform_init(h); \
+ } \
+ while (0)
+
+#define uv__handle_deinit(h) \
+ do { \
+ QUEUE_REMOVE(&(h)->handle_queue); \
+ QUEUE_INIT(&(h)->handle_queue); \
+ } \
+ while (0)
+
+#define uv__handle_ref(h) \
+ do { \
+ if (((h)->flags & UV__HANDLE_REF) != 0) break; \
+ (h)->flags |= UV__HANDLE_REF; \
+ if (((h)->flags & UV__HANDLE_CLOSING) != 0) break; \
+ if (((h)->flags & UV__HANDLE_ACTIVE) != 0) uv__active_handle_add(h); \
+ } \
+ while (0)
+
+#define uv__handle_unref(h) \
+ do { \
+ if (((h)->flags & UV__HANDLE_REF) == 0) break; \
+ (h)->flags &= ~UV__HANDLE_REF; \
+ if (((h)->flags & UV__HANDLE_CLOSING) != 0) break; \
+ if (((h)->flags & UV__HANDLE_ACTIVE) != 0) uv__active_handle_rm(h); \
+ } \
+ while (0)
+
+#define uv__has_ref(h) \
+ (((h)->flags & UV__HANDLE_REF) != 0)
+
+#define uv__has_active_handles(loop) \
+ ((loop)->active_handles > 0)
+
+//-----------------------------------------------------------------------------
+
+void uv_ref(uv_handle_t *);
+void uv_unref(uv_handle_t *);
+void uv_deinit(uv_loop_t *, uv_handle_t *);
+
+int uv_is_closing(const uv_handle_t *handle);
+int uv_is_active(const uv_handle_t *handle);
+
+void uv_close(uv_handle_t *handle, uv_close_cb close_cb);
+
+void uv_walk(uv_loop_t *loop, uv_walk_cb walk_cb, void *arg);
+
+#ifdef UV__O_NONBLOCK
+#define UV__F_NONBLOCK UV__O_NONBLOCK
+#else
+#define UV__F_NONBLOCK 1
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__handle_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__idle_header__
+#define __uv__idle_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//-----------------------------------------------------------------------------
+// uv_idle_t
+
+struct uv_idle_s {
+ UV_HANDLE_FIELDS UV_IDLE_PRIVATE_FIELDS
+};
+
+int uv_idle_init(uv_loop_t *loop, uv_idle_t *handle);
+int uv_idle_start(uv_idle_t *handle, uv_idle_cb cb);
+int uv_idle_stop(uv_idle_t *handle);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__idle_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__inet_header__
+#define __uv__inet_header__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb);
+int uv_accept(uv_stream_t *server, uv_stream_t *client);
+
+int uv_inet_ntop(int af, const void *src, char *dst, size_t size);
+int uv_inet_pton(int af, const char *src, void *dst);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__inet_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__loop_header__
+#define __uv__loop_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//-----------------------------------------------------------------------------
+// uv_loop_t
+
+struct uv_loop_s {
+ /* User data - use this for whatever. */
+ void *data;
+ /* Loop reference counting. */
+ uint32_t active_handles;
+ void *handles_queue[2];
+ void *active_reqs[2];
+ /* Internal flag to signal loop stop. */
+ uint32_t stop_flag;
+ /* platform dependent fields */
+ UV_LOOP_PRIVATE_FIELDS
+};
+
+typedef enum {
+ UV_RUN_DEFAULT = 0,
+ UV_RUN_ONCE,
+ UV_RUN_NOWAIT
+} uv_run_mode;
+
+//-----------------------------------------------------------------------------
+
+int uv_loop_init(uv_loop_t *loop);
+int uv_loop_close(uv_loop_t *loop);
+uv_loop_t *uv_default_loop(void);
+int uv_loop_alive(const uv_loop_t *loop);
+
+int uv_run(uv_loop_t *loop, uv_run_mode mode);
+
+void uv_update_time(uv_loop_t *);
+uint64_t uv_now(const uv_loop_t *);
+
+int uv__loop_alive(const uv_loop_t *loop);
+
+int uv__platform_loop_init(uv_loop_t *loop);
+void uv__platform_loop_delete(uv_loop_t *loop);
+void uv__platform_invalidate_fd(uv_loop_t *loop, int fd);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__loop_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__pipe_header__
+#define __uv__pipe_header__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct uv_pipe_s {
+ UV_HANDLE_FIELDS UV_STREAM_FIELDS int ipc; /* non-zero if this pipe is used for passing handles */
+ UV_PIPE_PRIVATE_FIELDS
+};
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__pipe_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__poll_header__
+#define __uv__poll_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//-----------------------------------------------------------------------------
+// uv_poll_t
+
+struct uv_poll_s {
+ UV_HANDLE_FIELDS uv_poll_cb poll_cb;
+ UV_POLL_PRIVATE_FIELDS
+};
+
+enum uv_poll_event {
+ UV_READABLE = 1,
+ UV_WRITABLE = 2,
+ UV_DISCONNECT = 4
+};
+
+int uv_poll_init(uv_loop_t *loop, uv_poll_t *handle, int fd);
+int uv_poll_init_socket(uv_loop_t *loop, uv_poll_t *handle, uv_os_sock_t socket);
+int uv_poll_start(uv_poll_t *handle, int events, uv_poll_cb cb);
+int uv_poll_stop(uv_poll_t *handle);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__poll_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__req_header__
+#define __uv__req_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//-----------------------------------------------------------------------------
+
+#define UV_REQ_TYPE_MAP(XX) \
+ XX(REQ, req) \
+ XX(CONNECT, connect) \
+ XX(WRITE, write) \
+ XX(SHUTDOWN, shutdown) \
+ XX(FS, fs) \
+ XX(WORK, work) \
+ XX(GETADDRINFO, getaddrinfo) \
+
+/*
+ XX(REQ, req) \
+ XX(CONNECT, connect) \
+ XX(WRITE, write) \
+ XX(SHUTDOWN, shutdown) \
+ XX(UDP_SEND, udp_send) \
+ XX(FS, fs) \
+ XX(WORK, work) \
+ XX(GETADDRINFO, getaddrinfo) \
+ XX(GETNAMEINFO, getnameinfo) \
+*/
+
+typedef enum {
+ UV_UNKNOWN_REQ = 0,
+#define XX(uc, lc) UV_##uc,
+ UV_REQ_TYPE_MAP(XX)
+#undef XX
+ UV_REQ_TYPE_PRIVATE UV_REQ_TYPE_MAX
+} uv_req_type;
+
+//-----------------------------------------------------------------------------
+// uv_req_t
+
+#define UV_REQ_FIELDS \
+ /* public */ \
+ void* data; \
+ /* read-only */ \
+ uv_req_type type; \
+ /* private */ \
+ void* active_queue[2]; \
+ void* reserved[4]; \
+ UV_REQ_PRIVATE_FIELDS \
+
+/* Abstract base class of all requests. */
+struct uv_req_s {
+ UV_REQ_FIELDS
+};
+
+#define uv__has_active_reqs(loop) \
+ (QUEUE_EMPTY(&(loop)->active_reqs) == 0)
+
+#define uv__req_register(loop, req) \
+ do { \
+ QUEUE_INSERT_TAIL(&(loop)->active_reqs, &(req)->active_queue); \
+ } \
+ while (0)
+
+#define uv__req_unregister(loop, req) \
+ do { \
+ assert(uv__has_active_reqs(loop)); \
+ QUEUE_REMOVE(&(req)->active_queue); \
+ } \
+ while (0)
+
+void uv__req_init_(uv_loop_t *loop, uv_req_t *req, uv_req_type type);
+
+#define uv__req_init(loop, req, type) \
+ uv__req_init_((loop), (uv_req_t*)(req), (type))
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__req_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__stream_header__
+#define __uv__stream_header__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define UV_STREAM_FIELDS \
+ /* number of bytes queued for writing */ \
+ size_t write_queue_size; \
+ uv_alloc_cb alloc_cb; \
+ uv_read_cb read_cb; \
+ /* private */ \
+ UV_STREAM_PRIVATE_FIELDS
+
+/*
+ * uv_stream_t is a subclass of uv_handle_t.
+ *
+ * uv_stream is an abstract class.
+ *
+ * uv_stream_t is the parent class of uv_tcp_t, uv_pipe_t and uv_tty_t.
+ */
+struct uv_stream_s {
+ UV_HANDLE_FIELDS UV_STREAM_FIELDS
+};
+
+typedef struct uv__stream_queued_fds_s uv__stream_queued_fds_t;
+
+struct uv__stream_queued_fds_s {
+ unsigned int size;
+ unsigned int offset;
+ int fds[1];
+};
+
+#define uv__stream_fd(handle) ((handle)->io_watcher.fd)
+
+void uv__stream_init(uv_loop_t *loop, uv_stream_t *stream, uv_handle_type type);
+int uv__stream_open(uv_stream_t *, int fd, int flags);
+void uv__stream_destroy(uv_stream_t *stream);
+void uv__stream_close(uv_stream_t *handle);
+
+void uv__server_io(uv_loop_t *loop, uv__io_t *w, unsigned int events);
+int uv__accept(int sockfd);
+
+int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb);
+int uv_read_stop(uv_stream_t *);
+
+int uv_write(uv_write_t *req, uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb);
+int uv_write2(uv_write_t *req, uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_stream_t *send_handle, uv_write_cb cb);
+int uv_try_write(uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__stream_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__tcp_header__
+#define __uv__tcp_header__
+
+#include <assert.h>
+#include <sys/socket.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr);
+int uv_ip4_name(const struct sockaddr_in *src, char *dst, size_t size);
+int uv_inet_ntop(int af, const void *src, char *dst, size_t size);
+
+/*
+ * uv_tcp_t is a subclass of uv_stream_t.
+ *
+ * Represents a TCP stream or TCP server.
+ */
+struct uv_tcp_s {
+ UV_HANDLE_FIELDS UV_STREAM_FIELDS UV_TCP_PRIVATE_FIELDS
+};
+
+int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle);
+int uv_tcp_open(uv_tcp_t *handle, uv_os_sock_t sock);
+int uv_tcp_keepalive(uv_tcp_t *handle, int enable, unsigned int delay);
+
+enum uv_tcp_flags {
+ /* Used with uv_tcp_bind, when an IPv6 address is used. */
+ UV_TCP_IPV6ONLY = 0x0001
+};
+
+int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags);
+int uv_tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, uv_connect_cb cb);
+
+int uv_tcp_listen(uv_tcp_t *tcp, int backlog, uv_connection_cb cb);
+
+int uv__tcp_bind(uv_tcp_t *tcp, const struct sockaddr *addr, unsigned int addrlen, unsigned int flags);
+int uv__tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, unsigned int addrlen, uv_connect_cb cb);
+
+int uv__tcp_nodelay(int fd, int on);
+int uv__tcp_keepalive(int fd, int on, unsigned int delay);
+void uv__tcp_close(uv_tcp_t *handle);
+
+/* uv_connect_t is a subclass of uv_req_t. */
+struct uv_connect_s {
+ UV_REQ_FIELDS uv_connect_cb cb;
+ uv_stream_t *handle;
+ UV_CONNECT_PRIVATE_FIELDS
+};
+
+/* uv_write_t is a subclass of uv_req_t. */
+struct uv_write_s {
+ UV_REQ_FIELDS uv_write_cb cb;
+ uv_stream_t *send_handle;
+ uv_stream_t *handle;
+ UV_WRITE_PRIVATE_FIELDS
+};
+
+struct uv_shutdown_s {
+ UV_REQ_FIELDS uv_stream_t *handle;
+ uv_shutdown_cb cb;
+ UV_SHUTDOWN_PRIVATE_FIELDS
+};
+
+int uv_shutdown(uv_shutdown_t *req, uv_stream_t *handle, uv_shutdown_cb cb);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__tcp_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__thread_header__
+#define __uv__thread_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//-----------------------------------------------------------------------------
+// uv_thread
+
+int uv_thread_create(uv_thread_t *tid, uv_thread_cb entry, void *arg);
+uv_thread_t uv_thread_self(void);
+int uv_thread_join(uv_thread_t *tid);
+int uv_thread_equal(const uv_thread_t *t1, const uv_thread_t *t2);
+
+//-----------------------------------------------------------------------------
+// uv_once
+
+void uv_once(uv_once_t *guard, void (*callback)(void));
+
+//-----------------------------------------------------------------------------
+// uv_mutex
+
+int uv_mutex_init(uv_mutex_t *handle);
+void uv_mutex_destroy(uv_mutex_t *handle);
+void uv_mutex_lock(uv_mutex_t *handle);
+int uv_mutex_trylock(uv_mutex_t *handle);
+void uv_mutex_unlock(uv_mutex_t *handle);
+
+//-----------------------------------------------------------------------------
+// uv_sem
+
+int uv_sem_init(uv_sem_t *sem, unsigned int value);
+void uv_sem_destroy(uv_sem_t *sem);
+void uv_sem_post(uv_sem_t *sem);
+void uv_sem_wait(uv_sem_t *sem);
+int uv_sem_trywait(uv_sem_t *sem);
+
+//-----------------------------------------------------------------------------
+// uv_cond
+
+int uv_cond_init(uv_cond_t *cond);
+void uv_cond_destroy(uv_cond_t *cond);
+void uv_cond_signal(uv_cond_t *cond);
+void uv_cond_broadcast(uv_cond_t *cond);
+
+void uv_cond_wait(uv_cond_t *cond, uv_mutex_t *mutex);
+int uv_cond_timedwait(uv_cond_t *cond, uv_mutex_t *mutex, uint64_t timeout);
+
+//-----------------------------------------------------------------------------
+// uv_rwlock
+
+int uv_rwlock_init(uv_rwlock_t *rwlock);
+void uv_rwlock_destroy(uv_rwlock_t *rwlock);
+void uv_rwlock_rdlock(uv_rwlock_t *rwlock);
+int uv_rwlock_tryrdlock(uv_rwlock_t *rwlock);
+void uv_rwlock_rdunlock(uv_rwlock_t *rwlock);
+void uv_rwlock_wrlock(uv_rwlock_t *rwlock);
+int uv_rwlock_trywrlock(uv_rwlock_t *rwlock);
+void uv_rwlock_wrunlock(uv_rwlock_t *rwlock);
+
+#ifdef __cplusplus
+}
+#endif
+#endif //__uv__thread_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__threadpool_header__
+#define __uv__threadpool_header__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void uv__work_submit(uv_loop_t *loop, struct uv__work *w, void (*work)(struct uv__work *w), void (*done)(struct uv__work *w, int status));
+
+void uv__work_done(uv_async_t *handle);
+
+/*
+ * uv_work_t is a subclass of uv_req_t.
+ */
+struct uv_work_s {
+ UV_REQ_FIELDS uv_loop_t *loop;
+ uv_work_cb work_cb;
+ uv_after_work_cb after_work_cb;
+ UV_WORK_PRIVATE_FIELDS
+};
+
+int uv_queue_work(uv_loop_t *loop, uv_work_t *req, uv_work_cb work_cb, uv_after_work_cb after_work_cb);
+
+int uv_cancel(uv_req_t *req);
+
+/*
+ * for embed systems that need cleanup before exit
+ */
+#if defined(__TINYARA__)
+void uv_cleanup(void);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* __uv__threadpool_header__ */
--- /dev/null
+/* 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.
+ */
+
+/* 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__timer_header__
+#define __uv__timer_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//-----------------------------------------------------------------------------
+// uv_timer_t
+
+struct uv_timer_s {
+ UV_HANDLE_FIELDS UV_TIMER_PRIVATE_FIELDS
+};
+
+int uv_timer_init(uv_loop_t *, uv_timer_t *handle);
+int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat);
+int uv_timer_stop(uv_timer_t *handle);
+int uv_timer_again(uv_timer_t *handle);
+void uv_timer_set_repeat(uv_timer_t *handle, uint64_t repeat);
+uint64_t uv_timer_get_repeat(const uv_timer_t *handle);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__timer_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__types_header__
+#define __uv__types_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//-----------------------------------------------------------------------------
+// strcture types
+
+// handle types
+typedef struct uv_handle_s uv_handle_t;
+typedef struct uv_loop_s uv_loop_t;
+typedef struct uv_timer_s uv_timer_t;
+typedef struct uv_idle_s uv_idle_t;
+typedef struct uv_async_s uv_async_t;
+typedef struct uv_stream_s uv_stream_t;
+typedef struct uv_tcp_s uv_tcp_t;
+typedef struct uv_pipe_s uv_pipe_t;
+typedef struct uv_poll_s uv_poll_t;
+
+// request types
+typedef struct uv_req_s uv_req_t;
+typedef struct uv_fs_s uv_fs_t;
+typedef struct uv_connect_s uv_connect_t;
+typedef struct uv_write_s uv_write_t;
+typedef struct uv_shutdown_s uv_shutdown_t;
+typedef struct uv_getaddrinfo_s uv_getaddrinfo_t;
+typedef struct uv_work_s uv_work_t;
+
+// ext types
+typedef struct uv__io_s uv__io_t;
+typedef struct uv_buf_s uv_buf_t;
+
+// structure
+struct uv__async;
+struct addrinfo;
+
+//-----------------------------------------------------------------------------
+// callback types
+
+typedef void (*uv_poll_cb)(uv_poll_t *handle, int status, int events);
+typedef void (*uv_timer_cb)(uv_timer_t *handle);
+typedef void (*uv_idle_cb)(uv_idle_t *handle);
+typedef void (*uv_close_cb)(uv_handle_t *handle);
+typedef void (*uv_async_cb)(uv_async_t *handle);
+
+typedef void (*uv_thread_cb)(void *arg);
+
+typedef void (*uv_fs_cb)(uv_fs_t *req);
+
+typedef void (*uv__io_cb)(struct uv_loop_s *loop, struct uv__io_s *w, unsigned int events);
+typedef void (*uv__async_cb)(struct uv_loop_s *loop, struct uv__async *w, unsigned int nevents);
+
+typedef void (*uv_alloc_cb)(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
+typedef void (*uv_read_cb)(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
+typedef void (*uv_write_cb)(uv_write_t *req, int status);
+typedef void (*uv_connect_cb)(uv_connect_t *req, int status);
+typedef void (*uv_shutdown_cb)(uv_shutdown_t *req, int status);
+typedef void (*uv_connection_cb)(uv_stream_t *server, int status);
+
+typedef void (*uv_walk_cb)(uv_handle_t *handle, void *arg);
+
+typedef void (*uv_work_cb)(uv_work_t *req);
+typedef void (*uv_after_work_cb)(uv_work_t *req, int status);
+
+typedef void (*uv_getaddrinfo_cb)(uv_getaddrinfo_t *req, int status, struct addrinfo *res);
+
+//-----------------------------------------------------------------------------
+
+typedef struct {
+ long tv_sec;
+ long tv_nsec;
+} uv_timespec_t;
+
+typedef struct {
+ uint64_t st_dev;
+ uint64_t st_mode;
+ uint64_t st_nlink;
+ uint64_t st_uid;
+ uint64_t st_gid;
+ uint64_t st_rdev;
+ uint64_t st_ino;
+ uint64_t st_size;
+ uint64_t st_blksize;
+ uint64_t st_blocks;
+ uint64_t st_flags;
+ uint64_t st_gen;
+ uv_timespec_t st_atim;
+ uv_timespec_t st_mtim;
+ uv_timespec_t st_ctim;
+ uv_timespec_t st_birthtim;
+} uv_stat_t;
+
+//-----------------------------------------------------------------------------
+// uv__work
+
+struct uv__work {
+ void (*work)(struct uv__work *w);
+ void (*done)(struct uv__work *w, int status);
+ struct uv_loop_s *loop;
+ void *wq[2];
+};
+
+//-----------------------------------------------------------------------------
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__types_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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_unix_extenstion_header__
+#define __uv_unix_extenstion_header__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define UV_HANDLE_PRIVATE_FIELDS \
+ uv_handle_t* next_closing; \
+
+#define UV_POLL_PRIVATE_FIELDS \
+ uv__io_t io_watcher;
+
+#define UV_IDLE_PRIVATE_FIELDS \
+ uv_idle_cb idle_cb; \
+ void* queue[2]; \
+
+
+#define UV_TIMER_PRIVATE_FIELDS \
+ uv_timer_cb timer_cb; \
+ void* heap_node[3]; \
+ uint64_t timeout; \
+ uint64_t repeat; \
+ uint64_t start_id;
+
+#define UV_REQ_TYPE_PRIVATE /* empty */
+
+#define UV_REQ_PRIVATE_FIELDS /* empty */
+
+#define UV_FS_PRIVATE_FIELDS \
+ const char *new_path; \
+ uv_file file; \
+ int flags; \
+ mode_t mode; \
+ unsigned int nbufs; \
+ uv_buf_t* bufs; \
+ off_t off; \
+ double atime; \
+ double mtime; \
+ struct uv__work work_req; \
+ uv_buf_t bufsml[4]; \
+ /*
+ const char *new_path; \
+ uv_file file; \
+ int flags; \
+ mode_t mode; \
+ unsigned int nbufs; \
+ uv_buf_t* bufs; \
+ off_t off; \
+ uv_uid_t uid; \
+ uv_gid_t gid; \
+ double atime; \
+ double mtime; \
+ struct uv__work work_req; \
+ uv_buf_t bufsml[4]; \
+ */
+
+#define UV_WORK_PRIVATE_FIELDS \
+ struct uv__work work_req;
+
+#define UV_ASYNC_PRIVATE_FIELDS \
+ uv_async_cb async_cb; \
+ void* queue[2]; \
+ int pending; \
+
+
+#define UV_IO_PRIVATE_PLATFORM_FIELDS /* empty */
+
+#define UV_TCP_PRIVATE_FIELDS /* empty */
+
+#define UV_CONNECT_PRIVATE_FIELDS \
+ void* queue[2]; \
+
+
+#define UV_WRITE_PRIVATE_FIELDS \
+ void* queue[2]; \
+ unsigned int write_index; \
+ uv_buf_t* bufs; \
+ unsigned int nbufs; \
+ int error; \
+ uv_buf_t bufsml[4]; \
+
+
+#define UV_SHUTDOWN_PRIVATE_FIELDS /* empty */
+
+//-----------------------------------------------------------------------------
+// as loop uses async, handle and async needs to be included here
+
+#include "uv__handle.h"
+#include "uv__async.h"
+
+//-----------------------------------------------------------------------------
+// loop, this needs to be at the bottom
+
+#define UV_LOOP_PRIVATE_FIELDS \
+ unsigned long flags; \
+ int backend_fd; \
+ void* pending_queue[2]; \
+ void* watcher_queue[2]; \
+ uv__io_t** watchers; \
+ unsigned int nwatchers; \
+ unsigned int nfds; \
+ void* wq[2]; \
+ uv_mutex_t wq_mutex; \
+ uv_async_t wq_async; \
+ uv_rwlock_t cloexec_lock; \
+ uv_handle_t* closing_handles; \
+ void* idle_handles[2]; \
+ void* async_handles[2]; \
+ struct uv__async async_watcher; \
+ struct { \
+ void* min; \
+ unsigned int nelts; \
+ } timer_heap; \
+ uint64_t timer_counter; \
+ uint64_t time; \
+ int emfile_fd; \
+ UV_PLATFORM_LOOP_FIELDS \
+
+// timer_heap should be same as 'struct heap' in heap-inl.h
+
+/*
+ unsigned long flags; \
+ int backend_fd; \
+ void* pending_queue[2]; \
+ void* watcher_queue[2]; \
+ uv__io_t** watchers; \
+ unsigned int nwatchers; \
+ unsigned int nfds; \
+ void* wq[2]; \
+ uv_mutex_t wq_mutex; \
+ uv_async_t wq_async; \
+ uv_rwlock_t cloexec_lock; \
+ uv_handle_t* closing_handles; \
+ void* process_handles[2]; \
+ void* prepare_handles[2]; \
+ void* check_handles[2]; \
+ void* idle_handles[2]; \
+ void* async_handles[2]; \
+ struct uv__async async_watcher; \
+ struct { \
+ void* min; \
+ unsigned int nelts; \
+ } timer_heap; \
+ uint64_t timer_counter; \
+ uint64_t time; \
+ int signal_pipefd[2]; \
+ uv__io_t signal_io_watcher; \
+ uv_signal_t child_watcher; \
+ int emfile_fd; \
+ UV_PLATFORM_LOOP_FIELDS \
+*/
+
+#define UV_PIPE_PRIVATE_FIELDS \
+ const char* pipe_fname; /* strdup'ed */
+
+#define UV_STREAM_PRIVATE_FIELDS \
+ uv_connect_t *connect_req; \
+ uv_shutdown_t *shutdown_req; \
+ uv__io_t io_watcher; \
+ void* write_queue[2]; \
+ void* write_completed_queue[2]; \
+ uv_connection_cb connection_cb; \
+ int delayed_error; \
+ int accepted_fd; \
+ void* queued_fds; \
+ UV_STREAM_PRIVATE_PLATFORM_FIELDS
+
+//-----------------------------------------------------------------------------
+// should be here for its used inside extension
+
+enum {
+ UV__HANDLE_INTERNAL = 0x8000,
+ UV__HANDLE_ACTIVE = 0x4000,
+ UV__HANDLE_REF = 0x2000,
+ UV__HANDLE_CLOSING = 0 /* no-op on unix */
+};
+
+//-----------------------------------------------------------------------------
+// getaddrinfo
+
+#define UV_GETADDRINFO_PRIVATE_FIELDS \
+ struct uv__work work_req; \
+ uv_getaddrinfo_cb cb; \
+ struct addrinfo* hints; \
+ char* hostname; \
+ char* service; \
+ struct addrinfo* addrinfo; \
+ int retcode;
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv_unix_extenstion_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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_unix_platform_header__
+#define __uv_unix_platform_header__
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//-----------------------------------------------------------------------------
+// fs
+
+typedef struct dirent uv__dirent_t;
+typedef int uv_file;
+typedef int uv_os_sock_t;
+typedef int uv_os_fd_t;
+
+//-----------------------------------------------------------------------------
+// platform init functions
+
+void uv__handle_platform_init(uv_handle_t *handle);
+void uv__idle_platform_init(uv_idle_t *handle);
+void uv__async_platform_init(uv_async_t *handle);
+void uv__timer_platform_init(uv_timer_t *handle);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv_unix_platform_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__util_header__
+#define __uv__util_header__
+
+#ifndef __uv_header__
+#error Please include with uv.h
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//-----------------------------------------------------------------------------
+//
+
+uv_buf_t uv_buf_init(char *base, unsigned int len);
+
+size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs);
+
+//-----------------------------------------------------------------------------
+//
+#define debugf printf
+
+#ifdef __cplusplus
+}
+#endif
+#endif // __uv__util_header__
--- /dev/null
+############################################################################
+# libtuv/source/Make.defs
+#
+# Copyright (C) 2014 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <gnutt@nuttx.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name NuttX nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################################
+
+ifeq ($(CONFIG_LIBTUV),y)
+
+CSRCS += uv_handle.c
+CSRCS += uv_loop.c
+CSRCS += uv_poll.c
+CSRCS += uv_idle.c
+CSRCS += uv_run.c
+CSRCS += uv_timer.c
+CSRCS += uv_threadpool.c
+CSRCS += uv_req.c
+CSRCS += uv_fs.c
+CSRCS += uv_async.c
+CSRCS += uv_util.c
+CSRCS += tuv_debuglog.c
+CSRCS += uv_error.c
+CSRCS += uv_dir.c
+CSRCS += uv_inet.c
+
+CFLAGS += -D__TINYARA__
+CFLAGS += -I$(TOPDIR)/../external/libtuv/include
+CFLAGS += -I$(TOPDIR)/../external/libtuv/source
+
+DEPPATH += --dep-path libtuv/source
+VPATH += :libtuv/source
+
+endif
--- /dev/null
+/* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef __UV_SRC_HEAP_H__
+#define __UV_SRC_HEAP_H__
+
+#include <stddef.h> /* NULL */
+
+#if defined(__GNUC__)
+#define HEAP_EXPORT(declaration) __attribute__((unused)) static declaration
+#else
+#define HEAP_EXPORT(declaration) static declaration
+#endif
+
+struct heap_node {
+ struct heap_node *left;
+ struct heap_node *right;
+ struct heap_node *parent;
+};
+
+/* A binary min heap. The usual properties hold: the root is the lowest
+ * element in the set, the height of the tree is at most log2(nodes) and
+ * it's always a complete binary tree.
+ *
+ * The heap function try hard to detect corrupted tree nodes at the cost
+ * of a minor reduction in performance. Compile with -DNDEBUG to disable.
+ */
+struct heap {
+ struct heap_node *min;
+ unsigned int nelts;
+};
+
+/* Return non-zero if a < b. */
+typedef int (*heap_compare_fn)(const struct heap_node *a, const struct heap_node *b);
+
+/* Public functions. */
+HEAP_EXPORT(void heap_init(struct heap *heap));
+HEAP_EXPORT(struct heap_node *heap_min(const struct heap *heap));
+HEAP_EXPORT(void heap_insert(struct heap *heap, struct heap_node *newnode, heap_compare_fn less_than));
+HEAP_EXPORT(void heap_remove(struct heap *heap, struct heap_node *node, heap_compare_fn less_than));
+HEAP_EXPORT(void heap_dequeue(struct heap *heap, heap_compare_fn less_than));
+
+/* Implementation follows. */
+
+HEAP_EXPORT(void heap_init(struct heap *heap))
+{
+ heap->min = NULL;
+ heap->nelts = 0;
+}
+
+HEAP_EXPORT(struct heap_node *heap_min(const struct heap *heap))
+{
+ return heap->min;
+}
+
+/* Swap parent with child. Child moves closer to the root, parent moves away. */
+static void heap_node_swap(struct heap *heap, struct heap_node *parent, struct heap_node *child)
+{
+ struct heap_node *sibling;
+ struct heap_node t;
+
+ t = *parent;
+ *parent = *child;
+ *child = t;
+
+ parent->parent = child;
+ if (child->left == child) {
+ child->left = parent;
+ sibling = child->right;
+ } else {
+ child->right = parent;
+ sibling = child->left;
+ }
+ if (sibling != NULL) {
+ sibling->parent = child;
+ }
+
+ if (parent->left != NULL) {
+ parent->left->parent = parent;
+ }
+ if (parent->right != NULL) {
+ parent->right->parent = parent;
+ }
+
+ if (child->parent == NULL) {
+ heap->min = child;
+ } else if (child->parent->left == parent) {
+ child->parent->left = child;
+ } else {
+ child->parent->right = child;
+ }
+}
+
+HEAP_EXPORT(void heap_insert(struct heap *heap, struct heap_node *newnode, heap_compare_fn less_than))
+{
+ struct heap_node **parent;
+ struct heap_node **child;
+ unsigned int path;
+ unsigned int n;
+ unsigned int k;
+
+ newnode->left = NULL;
+ newnode->right = NULL;
+ newnode->parent = NULL;
+
+ /* Calculate the path from the root to the insertion point. This is a min
+ * heap so we always insert at the left-most free node of the bottom row.
+ */
+ path = 0;
+ for (k = 0, n = 1 + heap->nelts; n >= 2; k += 1, n /= 2) {
+ path = (path << 1) | (n & 1);
+ }
+
+ /* Now traverse the heap using the path we calculated in the previous step. */
+ parent = child = &heap->min;
+ while (k > 0) {
+ parent = child;
+ if (path & 1) {
+ child = &(*child)->right;
+ } else {
+ child = &(*child)->left;
+ }
+ path >>= 1;
+ k -= 1;
+ }
+
+ /* Insert the new node. */
+ newnode->parent = *parent;
+ *child = newnode;
+ heap->nelts += 1;
+
+ /* Walk up the tree and check at each node if the heap property holds.
+ * It's a min heap so parent < child must be true.
+ */
+ while (newnode->parent != NULL && less_than(newnode, newnode->parent)) {
+ heap_node_swap(heap, newnode->parent, newnode);
+ }
+}
+
+HEAP_EXPORT(void heap_remove(struct heap *heap, struct heap_node *node, heap_compare_fn less_than))
+{
+ struct heap_node *smallest;
+ struct heap_node **max;
+ struct heap_node *child;
+ unsigned int path;
+ unsigned int k;
+ unsigned int n;
+
+ if (heap->nelts == 0) {
+ return;
+ }
+
+ /* Calculate the path from the min (the root) to the max, the left-most node
+ * of the bottom row.
+ */
+ path = 0;
+ for (k = 0, n = heap->nelts; n >= 2; k += 1, n /= 2) {
+ path = (path << 1) | (n & 1);
+ }
+
+ /* Now traverse the heap using the path we calculated in the previous step. */
+ max = &heap->min;
+ while (k > 0) {
+ if (path & 1) {
+ max = &(*max)->right;
+ } else {
+ max = &(*max)->left;
+ }
+ path >>= 1;
+ k -= 1;
+ }
+
+ heap->nelts -= 1;
+
+ /* Unlink the max node. */
+ child = *max;
+ *max = NULL;
+
+ if (child == node) {
+ /* We're removing either the max or the last node in the tree. */
+ if (child == heap->min) {
+ heap->min = NULL;
+ }
+ return;
+ }
+
+ /* Replace the to be deleted node with the max node. */
+ child->left = node->left;
+ child->right = node->right;
+ child->parent = node->parent;
+
+ if (child->left != NULL) {
+ child->left->parent = child;
+ }
+
+ if (child->right != NULL) {
+ child->right->parent = child;
+ }
+
+ if (node->parent == NULL) {
+ heap->min = child;
+ } else if (node->parent->left == node) {
+ node->parent->left = child;
+ } else {
+ node->parent->right = child;
+ }
+
+ /* Walk down the subtree and check at each node if the heap property holds.
+ * It's a min heap so parent < child must be true. If the parent is bigger,
+ * swap it with the smallest child.
+ */
+ for (;;) {
+ smallest = child;
+ if (child->left != NULL && less_than(child->left, smallest)) {
+ smallest = child->left;
+ }
+ if (child->right != NULL && less_than(child->right, smallest)) {
+ smallest = child->right;
+ }
+ if (smallest == child) {
+ break;
+ }
+ heap_node_swap(heap, child, smallest);
+ }
+
+ /* Walk up the subtree and check that each parent is less than the node
+ * this is required, because `max` node is not guaranteed to be the
+ * actual maximum in tree
+ */
+ while (child->parent != NULL && less_than(child, child->parent)) {
+ heap_node_swap(heap, child->parent, child);
+ }
+}
+
+HEAP_EXPORT(void heap_dequeue(struct heap *heap, heap_compare_fn less_than))
+{
+ heap_remove(heap, heap->min, less_than);
+}
+
+#undef HEAP_EXPORT
+
+#endif /* __UV_SRC_HEAP_H__ */
--- /dev/null
+############################################################################
+# libtuv/source/tinyara/Make.defs
+#
+# Copyright (C) 2014 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <gnutt@nuttx.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name NuttX nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################################
+
+ifeq ($(CONFIG_LIBTUV),y)
+
+CSRCS += uv_tinyara.c
+CSRCS += uv_tinyara_clock.c
+CSRCS += uv_tinyara_io.c
+CSRCS += uv_tinyara_loop.c
+CSRCS += uv_tinyara_thread.c
+
+CFLAGS += -D__TINYARA__
+CFLAGS += -I$(TOPDIR)/../external/libtuv/include
+CFLAGS += -I$(TOPDIR)/../external/libtuv/source
+CFLAGS += -I$(TOPDIR)/../external/libtuv/source/tinyara
+
+DEPPATH += --dep-path libtuv/source/tinyara
+VPATH += :libtuv/source/tinyara
+
+endif
--- /dev/null
+/* 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.
+ */
+
+/* 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_extenstion_header__
+#define __uv_extenstion_header__
+
+#include "uv__unix_extension.h"
+
+//
+// structure extension for nuttx
+//
+
+#define UV_PLATFORM_LOOP_FIELDS \
+ struct pollfd pollfds[TUV_POLL_EVENTS_SIZE]; \
+ int npollfds; \
+
+
+#ifndef UV_STREAM_PRIVATE_PLATFORM_FIELDS
+#define UV_STREAM_PRIVATE_PLATFORM_FIELDS /* empty */
+#endif
+
+#endif // __uv_extenstion_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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__platform_tinyara_header__
+#define __uv__platform_tinyara_header__
+
+#include <pthread.h>
+#include <poll.h> // nuttx poll // kbuild
+#include <unistd.h>
+#include <errno.h>
+
+#include "uv__unix_platform.h"
+
+#include <netdb.h>
+
+/* for testing */
+#define TUV_POLL_EVENTS_SIZE 32
+
+#ifndef TUV_POLL_EVENTS_SIZE
+#define TUV_POLL_EVENTS_SIZE 1024
+#endif
+
+#define TUV_TINYARA_IOV_MAX TUV_POLL_EVENTS_SIZE /* check this */
+
+//-----------------------------------------------------------------------------
+
+#define ENOTSUP EOPNOTSUPP
+
+#define STDIN_FILNO 0
+#define STDOUT_FILNO 1
+#define STDERR_FILENO 2
+
+#ifndef SIGCHLD
+#define SIGCHLD 17
+#endif
+#define SIGPROF 27
+
+#ifndef CONFIG_NET_LWIP
+#define TCP_NODELAY 1
+#endif
+
+#define _SC_CLK_TCK 0x0006
+#define _SC_NPROCESSORS_ONLN 0x0061
+
+#define CLOCK_MONOTONIC 1
+
+//-----------------------------------------------------------------------------
+
+#define UV__POLLIN POLLIN /* 0x01 */
+#define UV__POLLOUT POLLOUT /* 0x02 */
+#define UV__POLLERR POLLERR /* 0x04 */
+#define UV__POLLHUP POLLHUP /* 0x08 */
+
+//-----------------------------------------------------------------------------
+
+#define SAVE_ERRNO(block) \
+ do { \
+ int _saved_errno = get_errno(); \
+ do { block; } while (0); \
+ set_errno(_saved_errno); \
+ } \
+ while (0)
+
+//-----------------------------------------------------------------------------
+// date time extension
+
+// in (os)/uv_clock.cpp
+uint64_t uv__hrtime(void);
+
+#define uv__update_time(loop) \
+ loop->time = uv__hrtime() / 1000000
+
+inline uint64_t uv__time_precise(void);
+
+//-----------------------------------------------------------------------------
+// thread and mutex
+
+#define UV_ONCE_INIT PTHREAD_ONCE_INIT
+
+typedef pthread_t uv_thread_t;
+typedef pthread_once_t uv_once_t;
+typedef pthread_mutex_t uv_mutex_t;
+typedef sem_t uv_sem_t;
+typedef pthread_cond_t uv_cond_t;
+typedef pthread_mutex_t uv_rwlock_t; // no rwlock for nuttx
+
+#ifndef CONFIG_ENABLE_IOTIVITY
+//-----------------------------------------------------------------------------
+// uio
+struct iovec {
+ void *iov_base;
+ size_t iov_len;
+};
+#endif
+ssize_t readv(int __fd, const struct iovec *__iovec, int __count);
+ssize_t writev(int __fd, const struct iovec *__iovec, int __count);
+
+//-----------------------------------------------------------------------------
+// etc
+int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
+
+/* Maximum queue length specifiable by listen. */
+#define SOMAXCONN 8
+
+#endif // __uv__platform_tinyara_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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 <string.h>
+
+#include <uv.h>
+
+//-----------------------------------------------------------------------------
+
+void uv__platform_invalidate_fd(uv_loop_t *loop, int fd)
+{
+ int i;
+ int nfd = loop->npollfds;
+ for (i = 0; i < nfd; ++i) {
+ struct pollfd *pfd = &loop->pollfds[i];
+ if (fd == pfd->fd) {
+ pfd->fd = -1;
+ }
+ }
+}
+
+int uv__nonblock(int fd, int set)
+{
+ int flags;
+ int r;
+
+ do {
+ r = fcntl(fd, F_GETFL);
+ } while (r == -1 && errno == EINTR);
+
+ if (r == -1) {
+ return -errno;
+ }
+
+ /* Bail out now if already set/clear. */
+ if (! !(r & O_NONBLOCK) == ! !set) {
+ return 0;
+ }
+
+ if (set) {
+ flags = r | O_NONBLOCK;
+ } else {
+ flags = r & ~O_NONBLOCK;
+ }
+
+ do {
+ r = fcntl(fd, F_SETFL, flags);
+ } while (r == -1 && errno == EINTR);
+
+ if (r) {
+ return -errno;
+ }
+
+ return 0;
+}
+
+int uv__close(int fd)
+{
+ int saved_errno;
+ int rc;
+
+ assert(fd > -1); /* Catch uninitialized io_watcher.fd bugs. */
+ assert(fd > STDERR_FILENO); /* Catch stdio close bugs. */
+
+ saved_errno = errno;
+ rc = close(fd);
+ if (rc == -1) {
+ rc = -errno;
+ if (rc == -EINTR) {
+ rc = -EINPROGRESS; /* For platform/libc consistency. */
+ }
+ set_errno(saved_errno);
+ }
+
+ return rc;
+}
+
+int uv__cloexec(int fd, int set)
+{
+ return 0;
+}
+
+ssize_t uv__recvmsg(int fd, struct msghdr *msg, int flags)
+{
+ return -1;
+}
+
+//-----------------------------------------------------------------------------
+
+void uv__handle_platform_init(uv_handle_t *handle)
+{
+ handle->next_closing = NULL;
+}
+
+/* should not clear handle structure with memset as
+ * *data can be set before calling init function */
+
+void uv__idle_platform_init(uv_idle_t *handle)
+{
+ QUEUE_INIT(&(handle->queue));
+}
+
+void uv__timer_platform_init(uv_timer_t *handle)
+{
+}
+
+void uv__async_platform_init(uv_async_t *handle)
+{
+ QUEUE_INIT(&(handle->queue));
+}
+
+//-----------------------------------------------------------------------------
+#if 0 // kbuild
+int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
+{
+ return 0;
+}
+#endif
+ssize_t readv(int fd, const struct iovec *iiovec, int count)
+{
+ ssize_t result = 0;
+ ssize_t total = 0;
+ int idx;
+
+ for (idx = 0; idx < count; ++idx) {
+ result = read(fd, iiovec[idx].iov_base, iiovec[idx].iov_len);
+ if (result < 0) {
+ return result;
+ } else {
+ total += result;
+ }
+ }
+ return total;
+}
+
+ssize_t writev(int fd, const struct iovec *iiovec, int count)
+{
+ ssize_t result = 0;
+ ssize_t total = 0;
+ int idx;
+
+ for (idx = 0; idx < count; ++idx) {
+ result = write(fd, iiovec[idx].iov_base, iiovec[idx].iov_len);
+ if (result < 0) {
+ return result;
+ } else {
+ total += result;
+ }
+ }
+ return total;
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdio.h>
+
+#include <uv.h>
+
+uint64_t uv__hrtime()
+{
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ uint64_t ret = (((uint64_t) ts.tv_sec) * ((uint64_t) 1e9) + (uint64_t) ts.tv_nsec);
+ return ret;
+}
+
+inline uint64_t uv__time_precise()
+{
+ return uv__hrtime();
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdlib.h>
+#include <limits.h>
+#include <signal.h>
+#include <stdio.h>
+#include <sys/select.h>
+
+#include <uv.h>
+
+static void uv__add_pollfd(uv_loop_t *loop, struct pollfd *pe)
+{
+ int i;
+ bool exist = false;
+ for (i = 0; i < loop->npollfds; ++i) {
+ struct pollfd *cur = &loop->pollfds[i];
+ if (cur->fd == pe->fd) {
+ cur->events = pe->events;
+ exist = true;
+ break;
+ }
+ }
+ if (!exist) {
+ struct pollfd *cur = &loop->pollfds[loop->npollfds++];
+ cur->fd = pe->fd;
+ cur->events = pe->events;
+ cur->revents = 0;
+ cur->sem = 0;
+ cur->priv = 0;
+ }
+}
+
+static void uv__rem_pollfd(uv_loop_t *loop, struct pollfd *pe)
+{
+ int i = 0;
+ while (i < loop->npollfds) {
+ struct pollfd *cur = &loop->pollfds[i];
+ if (cur->fd == pe->fd) {
+ *cur = loop->pollfds[--loop->npollfds];
+ } else {
+ ++i;
+ }
+ }
+}
+
+void uv__io_poll(uv_loop_t *loop, int timeout)
+{
+ struct pollfd pfd;
+ struct pollfd *pe;
+ QUEUE *q;
+ uv__io_t *w;
+ uint64_t base;
+ uint64_t diff;
+ int nevents;
+ int count;
+ int nfd;
+ int i;
+
+ if (loop->nfds == 0) {
+ assert(QUEUE_EMPTY(&loop->watcher_queue));
+ return;
+ }
+
+ while (!QUEUE_EMPTY(&loop->watcher_queue)) {
+ q = QUEUE_HEAD(&loop->watcher_queue);
+ QUEUE_REMOVE(q);
+ QUEUE_INIT(q);
+
+ w = QUEUE_DATA(q, uv__io_t, watcher_queue);
+ assert(w->pevents != 0);
+ assert(w->fd >= 0);
+ assert(w->fd < (int)loop->nwatchers);
+
+ pfd.fd = w->fd;
+ pfd.events = w->pevents;
+ uv__add_pollfd(loop, &pfd);
+
+ w->events = w->pevents;
+ }
+
+ assert(timeout >= -1);
+ base = loop->time;
+ count = 5;
+
+ int max_fd = 0;
+ fd_set readfds;
+ fd_set writefds;
+ FD_ZERO(&readfds);
+ FD_ZERO(&writefds);
+ for (i = 0; i < loop->npollfds; ++i) {
+ pe = &loop->pollfds[i];
+
+ if (pe->fd > max_fd) {
+ max_fd = pe->fd;
+ }
+
+ if (pe->fd >= 0) {
+ if (pe->events & UV__POLLIN) {
+ FD_SET(pe->fd, &readfds);
+ }
+ if (pe->events & UV__POLLOUT) {
+ FD_SET(pe->fd, &writefds);
+ }
+ }
+ }
+
+ struct timeval tv;
+ tv.tv_sec = timeout / 1000;
+ tv.tv_usec = timeout * 1000;
+ for (;;) {
+ // nfd = poll(loop->pollfds, loop->npollfds, timeout); // kbuild
+ if (timeout != -1) {
+ nfd = select(max_fd + 1, &readfds, &writefds, NULL, &tv);
+ } else {
+ nfd = select(max_fd + 1, &readfds, &writefds, NULL, NULL);
+ }
+
+ SAVE_ERRNO(uv__update_time(loop));
+
+ if (nfd == 0) {
+ assert(timeout != -1);
+ return;
+ }
+
+ if (nfd == -1) {
+ int err = get_errno();
+ if (err == EAGAIN) {
+ set_errno(0);
+ } else if (err != EINTR) {
+ TDLOG("uv__io_poll abort for errno(%d)", err);
+ ABORT();
+ }
+ if (timeout == -1) {
+ continue;
+ }
+ if (timeout == 0) {
+ return;
+ }
+ goto update_timeout;
+ }
+
+ nevents = 0;
+
+ for (i = 0; i < loop->npollfds; ++i) {
+ pe = &loop->pollfds[i];
+
+ if (pe->fd >= 0) {
+ if (FD_ISSET(pe->fd, &readfds)) {
+ w = loop->watchers[pe->fd];
+ if (w == NULL) {
+ uv__rem_pollfd(loop, pe);
+ } else {
+ w->cb(loop, w, UV__POLLIN);
+ ++nevents;
+ }
+ } else if (FD_ISSET(pe->fd, &writefds)) {
+ w = loop->watchers[pe->fd];
+ if (w == NULL) {
+ uv__rem_pollfd(loop, pe);
+ } else {
+ w->cb(loop, w, UV__POLLOUT);
+ ++nevents;
+ }
+ }
+ }
+ }
+
+ if (nevents != 0) {
+ if (--count != 0) {
+ timeout = 0;
+ continue;
+ }
+ return;
+ }
+ if (timeout == 0) {
+ return;
+ }
+ if (timeout == -1) {
+ continue;
+ }
+update_timeout:
+ assert(timeout > 0);
+
+ diff = loop->time - base;
+ if (diff >= (uint64_t) timeout) {
+ return;
+ }
+ timeout -= diff;
+ }
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <uv.h>
+
+int uv__platform_loop_init(uv_loop_t *loop)
+{
+ loop->npollfds = 0;
+ return 0;
+}
+
+void uv__platform_loop_delete(uv_loop_t *loop)
+{
+ loop->npollfds = 0;
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdlib.h> // malloc(), free()
+#include <stdio.h>
+
+#include <uv.h>
+
+//-----------------------------------------------------------------------------
+
+//
+// nuttx has no pthread_rwlock_t, use pthread_mutex_t
+//
+
+int uv_rwlock_init(uv_rwlock_t *rwlock)
+{
+ return uv_mutex_init(rwlock);
+}
+
+void uv_rwlock_destroy(uv_rwlock_t *rwlock)
+{
+ uv_mutex_destroy(rwlock);
+}
+
+void uv_rwlock_rdlock(uv_rwlock_t *rwlock)
+{
+ uv_mutex_lock(rwlock);
+}
+
+int uv_rwlock_tryrdlock(uv_rwlock_t *rwlock)
+{
+ return uv_mutex_trylock(rwlock);
+}
+
+void uv_rwlock_rdunlock(uv_rwlock_t *rwlock)
+{
+ uv_mutex_unlock(rwlock);
+}
+
+void uv_rwlock_wrlock(uv_rwlock_t *rwlock)
+{
+ uv_mutex_lock(rwlock);
+}
+
+int uv_rwlock_trywrlock(uv_rwlock_t *rwlock)
+{
+ return uv_mutex_trylock(rwlock);
+}
+
+void uv_rwlock_wrunlock(uv_rwlock_t *rwlock)
+{
+ uv_mutex_unlock(rwlock);
+}
--- /dev/null
+/* 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.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "tuv_debuglog.h"
+
+#ifdef ENABLE_DEBUG_LOG
+
+#if defined(__TINYARA__)
+int tuv_debug_level = TDBGLEV_INFO;
+#else
+int tuv_debug_level = TDBGLEV_ERR;
+#endif
+
+FILE *tuv_log_stream;
+const char *tuv_debug_prefix[4] = { "", "ERR", "WRN", "INF" };
+#endif // ENABLE_DEBUG_LOG
+
+void InitDebugSettings()
+{
+#ifdef ENABLE_DEBUG_LOG
+ const char *dbglevel = NULL;
+ const char *dbglogfile = NULL;
+
+ dbglevel = getenv("TUV_DEBUG_LEVEL");
+ dbglogfile = getenv("TUV_DEBUG_LOGFILE");
+
+ tuv_log_stream = stderr;
+
+ if (dbglevel) {
+ tuv_debug_level = atoi(dbglevel);
+ if (tuv_debug_level < 0) {
+ tuv_debug_level = 0;
+ }
+ if (tuv_debug_level > TDBGLEV_INFO) {
+ tuv_debug_level = TDBGLEV_INFO;
+ }
+ }
+ if (dbglogfile) {
+ FILE *logstream;
+ logstream = fopen(dbglogfile, "w+");
+ if (logstream != NULL) {
+ tuv_log_stream = logstream;
+ }
+ }
+ //fprintf(stderr, "DBG LEV = %d", tuv_debug_level);
+ //fprintf(stderr, "DBG OUT = %s", (dbglogfile==NULL?"(stderr)":dbglogfile));
+#endif // ENABLE_DEBUG_LOG
+}
+
+void ReleaseDebugSettings()
+{
+#ifdef ENABLE_DEBUG_LOG
+ if (tuv_log_stream != stderr || tuv_log_stream != stdout) {
+ fclose(tuv_log_stream);
+ }
+ // some embed systems(ex, nuttx) may need this
+ tuv_log_stream = stderr;
+ tuv_debug_level = TDBGLEV_ERR;
+#endif // ENABLE_DEBUG_LOG
+}
--- /dev/null
+############################################################################
+# libtuv/source/unix/Make.defs
+#
+# Copyright (C) 2014 Gregory Nutt. All rights reserved.
+# Author: Gregory Nutt <gnutt@nuttx.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# 3. Neither the name NuttX nor the names of its contributors may be
+# used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+############################################################################
+
+ifeq ($(CONFIG_LIBTUV),y)
+
+CSRCS += uv_unix.c
+CSRCS += uv_unix_async.c
+CSRCS += uv_unix_fs.c
+CSRCS += uv_unix_getaddrinfo.c
+CSRCS += uv_unix_io.c
+CSRCS += uv_unix_process.c
+CSRCS += uv_unix_stream.c
+CSRCS += uv_unix_tcp.c
+CSRCS += uv_unix_thread.c
+
+CFLAGS += -D__TINYARA__
+CFLAGS += -I$(TOPDIR)/../external/libtuv/include
+CFLAGS += -I$(TOPDIR)/../external/libtuv/source
+CFLAGS += -I$(TOPDIR)/../external/libtuv/source/unix
+
+DEPPATH += --dep-path libtuv/source/unix
+VPATH += :libtuv/source/unix
+
+endif
--- /dev/null
+/* 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.
+ */
+
+/* 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.
+ */
+
+// uv_unix.cpp: unix, bsd, osx, linux, nuttx,
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <assert.h>
+#include <string.h>
+
+#include <uv.h>
+
+int uv__open_cloexec(const char *path, int flags)
+{
+ int err;
+ int fd;
+
+#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD__ >= 9)
+ static int no_cloexec;
+
+ if (!no_cloexec) {
+ fd = open(path, flags | UV__O_CLOEXEC);
+ if (fd != -1) {
+ return fd;
+ }
+
+ if (errno != EINVAL) {
+ return -errno;
+ }
+
+ /* O_CLOEXEC not supported. */
+ no_cloexec = 1;
+ }
+#endif
+
+ fd = open(path, flags);
+ if (fd == -1) {
+ return -errno;
+ }
+
+ err = uv__cloexec(fd, 1);
+ if (err) {
+ uv__close(fd);
+ return err;
+ }
+
+ return fd;
+}
+
+/* Open a socket in non-blocking close-on-exec mode, atomically if possible. */
+int uv__socket(int domain, int type, int protocol)
+{
+ int sockfd;
+ int err;
+
+#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
+ sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);
+ if (sockfd != -1) {
+ return sockfd;
+ }
+
+ if (errno != EINVAL) {
+ return -errno;
+ }
+#endif
+
+ sockfd = socket(domain, type, protocol);
+ if (sockfd == -1) {
+ return -errno;
+ }
+
+ err = uv__nonblock(sockfd, 1);
+ if (err == 0) {
+ err = uv__cloexec(sockfd, 1);
+ }
+
+ if (err) {
+ uv__close(sockfd);
+ return err;
+ }
+#if defined(SO_NOSIGPIPE)
+ {
+ int on = 1;
+ setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
+ }
+#endif
+
+ return sockfd;
+}
+
+int uv__accept(int sockfd)
+{
+ int peerfd;
+ int err;
+
+ assert(sockfd >= 0);
+
+ while (1) {
+#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD__ >= 10)
+ static int no_accept4 = 0;
+
+ if (no_accept4) {
+ goto skip;
+ }
+
+ peerfd = uv__accept4(sockfd, NULL, NULL, UV__SOCK_NONBLOCK | UV__SOCK_CLOEXEC);
+ if (peerfd != -1) {
+ return peerfd;
+ }
+
+ if (errno == EINTR) {
+ continue;
+ }
+
+ if (errno != ENOSYS) {
+ return -errno;
+ }
+
+ no_accept4 = 1;
+skip:
+#endif
+
+ peerfd = accept(sockfd, NULL, NULL);
+ if (peerfd == -1) {
+ err = get_errno();
+ if (err == EINTR) {
+ continue;
+ }
+ return -err;
+ }
+
+ err = uv__cloexec(peerfd, 1);
+ if (err == 0) {
+ err = uv__nonblock(peerfd, 1);
+ }
+
+ if (err) {
+ uv__close(peerfd);
+ return err;
+ }
+
+ return peerfd;
+ }
+}
+
+int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
+{
+ memset(addr, 0, sizeof(*addr));
+ addr->sin_family = AF_INET;
+ addr->sin_port = htons(port);
+ return uv_inet_pton(AF_INET, ip, &(addr->sin_addr.s_addr));
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <assert.h>
+#include <string.h>
+
+#include <uv.h>
+
+int uv__async_make_pending(int *pending)
+{
+ /* Do a cheap read first. */
+ if (ACCESS_ONCE(int, *pending) != 0) {
+ return 1;
+ }
+
+ /* Micro-optimization: use atomic memory operations to detect if we've been
+ * preempted by another thread and don't have to make an expensive syscall.
+ * This speeds up the heavily contended case by about 1-2% and has little
+ * if any impact on the non-contended case.
+ *
+ * Use XCHG instead of the CMPXCHG that __sync_val_compare_and_swap() emits
+ * on x86, it's about 4x faster. It probably makes zero difference in the
+ * grand scheme of things but I'm OCD enough not to let this one pass.
+ */
+#if defined(__i386__) || defined(__x86_64__)
+ {
+ unsigned int val = 1;
+ __asm__ __volatile__("xchgl %0, %1":"+r"(val)
+ :"m"(*pending));
+ return val != 0;
+ }
+#elif defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 0)
+ return __sync_val_compare_and_swap(pending, 0, 1) != 0;
+#else
+ ACCESS_ONCE(int, *pending) = 1;
+ return 0;
+#endif
+}
+
+static int uv__async_eventfd(void)
+{
+#if defined(__linux__)
+ static int no_eventfd2;
+ static int no_eventfd;
+ int fd;
+
+ if (no_eventfd2) {
+ goto skip_eventfd2;
+ }
+
+ fd = uv__eventfd2(0, UV__EFD_CLOEXEC | UV__EFD_NONBLOCK);
+ if (fd != -1) {
+ return fd;
+ }
+
+ if (errno != ENOSYS) {
+ return -errno;
+ }
+
+ no_eventfd2 = 1;
+
+skip_eventfd2:
+
+ if (no_eventfd) {
+ goto skip_eventfd;
+ }
+
+ fd = uv__eventfd(0);
+ if (fd != -1) {
+ uv__cloexec(fd, 1);
+ uv__nonblock(fd, 1);
+ return fd;
+ }
+
+ if (errno != ENOSYS) {
+ return -errno;
+ }
+
+ no_eventfd = 1;
+
+skip_eventfd:
+
+#endif
+
+ return -ENOSYS;
+}
+
+static void uv__async_io(uv_loop_t *loop, uv__io_t *w, unsigned int events)
+{
+ struct uv__async *wa;
+ char buf[1024];
+ unsigned n;
+ ssize_t r;
+ int err;
+
+ n = 0;
+ for (;;) {
+ r = read(w->fd, buf, sizeof(buf));
+
+ if (r > 0) {
+ n += r;
+ }
+
+ if (r == sizeof(buf)) {
+ continue;
+ }
+
+ if (r != -1) {
+ break;
+ }
+
+ err = get_errno();
+ if (err == EAGAIN || err == EWOULDBLOCK) {
+ break;
+ }
+
+ if (err == EINTR) {
+ continue;
+ }
+
+ TDLOG("uv__async_io abort for errno(%d)", err);
+ ABORT();
+ }
+
+ wa = container_of(w, struct uv__async, io_watcher);
+
+#if defined(__linux__)
+ if (wa->wfd == -1) {
+ uint64_t val;
+ assert(n == sizeof(val));
+ memcpy(&val, buf, sizeof(val)); /* Avoid alignment issues. */
+ wa->cb(loop, wa, val);
+ return;
+ }
+#endif
+
+ wa->cb(loop, wa, n);
+}
+
+//-----------------------------------------------------------------------------
+//
+
+void uv__async_init(struct uv__async *wa)
+{
+ QUEUE_INIT(&(wa->io_watcher.pending_queue));
+ QUEUE_INIT(&(wa->io_watcher.watcher_queue));
+ wa->io_watcher.fd = -1;
+ wa->wfd = -1;
+}
+
+void uv__async_close(uv_async_t *handle)
+{
+ QUEUE_REMOVE(&handle->queue);
+ uv__handle_stop(handle);
+}
+
+void uv__async_stop(uv_loop_t *loop, struct uv__async *wa)
+{
+ if (wa->io_watcher.fd == -1) {
+ return;
+ }
+
+ if (wa->wfd != -1) {
+ if (wa->wfd != wa->io_watcher.fd) {
+ uv__close(wa->wfd);
+ }
+ wa->wfd = -1;
+ }
+
+ uv__io_stop(loop, &wa->io_watcher, UV__POLLIN);
+ uv__close(wa->io_watcher.fd);
+ wa->io_watcher.fd = -1;
+}
+
+int uv__async_start(uv_loop_t *loop, struct uv__async *wa, uv__async_cb cb)
+{
+ int pipefd[2];
+ int err;
+
+ if (wa->io_watcher.fd != -1) {
+ return 0;
+ }
+
+ err = uv__async_eventfd();
+ if (err >= 0) {
+ pipefd[0] = err;
+ pipefd[1] = -1;
+ } else if (err == -ENOSYS) {
+ err = uv__make_pipe(pipefd, UV__F_NONBLOCK);
+#if defined(__linux__)
+ /* Save a file descriptor by opening one of the pipe descriptors as
+ * read/write through the procfs. That file descriptor can then
+ * function as both ends of the pipe.
+ */
+ if (err == 0) {
+ char buf[32];
+ int fd;
+
+ snprintf(buf, sizeof(buf), "/proc/self/fd/%d", pipefd[0]);
+ fd = uv__open_cloexec(buf, O_RDWR);
+ if (fd >= 0) {
+ uv__close(pipefd[0]);
+ uv__close(pipefd[1]);
+ pipefd[0] = fd;
+ pipefd[1] = fd;
+ }
+ }
+#endif
+ }
+
+ if (err < 0) {
+ return err;
+ }
+
+ uv__io_init(&wa->io_watcher, uv__async_io, pipefd[0]);
+ uv__io_start(loop, &wa->io_watcher, UV__POLLIN);
+ wa->wfd = pipefd[1];
+ wa->cb = cb;
+
+ return 0;
+}
+
+void uv__async_send(struct uv__async *wa)
+{
+ const void *buf;
+ ssize_t len;
+ int fd;
+ int r;
+
+ buf = "";
+ len = 1;
+ fd = wa->wfd;
+
+#if defined(__linux__)
+ if (fd == -1) {
+ static const uint64_t val = 1;
+ buf = &val;
+ len = sizeof(val);
+ fd = wa->io_watcher.fd; /* eventfd */
+ }
+#endif
+
+ do {
+ r = write(fd, buf, len);
+ } while (r == -1 && errno == EINTR);
+
+ if (r == len) {
+ return;
+ }
+
+ if (r == -1) {
+ int err = get_errno();
+ if (err == EAGAIN || err == EWOULDBLOCK) {
+ return;
+ }
+ }
+
+ TDLOG("uv__async_send abort");
+ ABORT();
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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.
+ */
+
+/* Caveat emptor: this file deviates from the libuv convention of returning
+ * negated errno codes. Most uv_fs_*() functions map directly to the system
+ * call of the same name. For more complex wrappers, it's easier to just
+ * return -1 with errno set. The dispatcher in uv__fs_work() takes care of
+ * getting the errno to the right place (req->result or as the return value.)
+ */
+
+#include <unistd.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <utime.h>
+#include <sys/time.h>
+#ifndef __TINYARA__
+#include <sys/uio.h>
+#endif
+
+#include <uv.h>
+#include "uv_internal.h"
+
+#define HAVE_PREADV 0
+
+//-----------------------------------------------------------------------------
+
+#define INIT(type) \
+ do { \
+ uv__req_init((loop), (req), UV_FS); \
+ (req)->fs_type = UV_FS_ ## type; \
+ (req)->result = 0; \
+ (req)->ptr = NULL; \
+ (req)->loop = loop; \
+ (req)->path = NULL; \
+ (req)->new_path = NULL; \
+ (req)->cb = (cb); \
+ } \
+ while (0)
+
+#define PATH \
+ do { \
+ (req)->path = strdup(path); \
+ if ((req)->path == NULL) \
+ return -ENOMEM; \
+ } \
+ while (0)
+
+#define PATH2 \
+ do { \
+ size_t path_len; \
+ size_t new_path_len; \
+ path_len = strlen((path)) + 1; \
+ new_path_len = strlen((new_path)) + 1; \
+ (req)->path = (char*)malloc(path_len + new_path_len); \
+ if ((req)->path == NULL) \
+ return -ENOMEM; \
+ (req)->new_path = (req)->path + path_len; \
+ memcpy((void*) (req)->path, (path), path_len); \
+ memcpy((void*) (req)->new_path, (new_path), new_path_len); \
+ } \
+ while (0)
+
+#define POST \
+ do { \
+ if ((cb) != NULL) { \
+ uv__work_submit((loop), &(req)->work_req, uv__fs_work, uv__fs_done); \
+ return 0; \
+ } \
+ else { \
+ uv__fs_work(&(req)->work_req); \
+ uv__fs_done(&(req)->work_req, 0); \
+ return (req)->result; \
+ } \
+ } \
+ while (0)
+
+//-----------------------------------------------------------------------------
+//
+
+static ssize_t uv__fs_fdatasync(uv_fs_t *req)
+{
+#if defined(__linux__)
+ return fdatasync(req->file);
+#elif defined(__TINYARA__)
+ return fsync(req->file);
+#endif
+}
+
+static void uv__to_stat(struct stat *src, uv_stat_t *dst)
+{
+#if !defined(__TINYARA__)
+ dst->st_dev = src->st_dev;
+ dst->st_nlink = src->st_nlink;
+ dst->st_uid = src->st_uid;
+ dst->st_gid = src->st_gid;
+ dst->st_rdev = src->st_rdev;
+ dst->st_ino = src->st_ino;
+#endif
+ dst->st_mode = src->st_mode;
+ dst->st_size = src->st_size;
+ dst->st_blksize = src->st_blksize;
+ dst->st_blocks = src->st_blocks;
+#if defined (__linux__)
+ dst->st_atim.tv_sec = src->st_atim.tv_sec;
+ dst->st_atim.tv_nsec = src->st_atim.tv_nsec;
+ dst->st_mtim.tv_sec = src->st_mtim.tv_sec;
+ dst->st_mtim.tv_nsec = src->st_mtim.tv_nsec;
+ dst->st_ctim.tv_sec = src->st_ctim.tv_sec;
+ dst->st_ctim.tv_nsec = src->st_ctim.tv_nsec;
+
+ dst->st_birthtim.tv_sec = src->st_ctim.tv_sec;
+ dst->st_birthtim.tv_nsec = src->st_ctim.tv_nsec;
+ dst->st_flags = 0;
+ dst->st_gen = 0;
+#elif defined (__TINYARA__)
+ dst->st_atim.tv_sec = src->st_atime;
+ dst->st_atim.tv_nsec = 0;
+ dst->st_mtim.tv_sec = src->st_mtime;
+ dst->st_mtim.tv_nsec = 0;
+ dst->st_ctim.tv_sec = src->st_ctime;
+ dst->st_ctim.tv_nsec = 0;
+ dst->st_birthtim.tv_sec = src->st_ctime;
+ dst->st_birthtim.tv_nsec = 0;
+ dst->st_flags = 0;
+ dst->st_gen = 0;
+#endif
+}
+
+static int uv__fs_fstat(int fd, uv_stat_t *buf)
+{
+#if defined(__TINYARA__)
+ return -1;
+#else
+ struct stat pbuf;
+ int ret;
+ ret = fstat(fd, &pbuf);
+ uv__to_stat(&pbuf, buf);
+ return ret;
+#endif
+}
+
+static int uv__fs_stat(const char *path, uv_stat_t *buf)
+{
+ struct stat pbuf;
+ int ret;
+ ret = stat(path, &pbuf);
+ uv__to_stat(&pbuf, buf);
+ return ret;
+}
+
+static ssize_t uv__fs_read(uv_fs_t *req)
+{
+#if defined(__linux__)
+ static int no_preadv;
+#endif
+ ssize_t result;
+
+ if (req->off < 0) {
+ if (req->nbufs == 1) {
+ result = read(req->file, req->bufs[0].base, req->bufs[0].len);
+ }
+#if defined(__linux__)
+ else {
+ result = readv(req->file, (struct iovec *)req->bufs, req->nbufs);
+ }
+#endif
+ } else {
+ if (req->nbufs == 1) {
+ result = pread(req->file, req->bufs[0].base, req->bufs[0].len, req->off);
+ goto done;
+ }
+#if HAVE_PREADV
+ result = preadv(req->file, (struct iovec *)req->bufs, req->nbufs, req->off);
+#else
+#if defined(__linux__)
+ if (no_preadv)
+retry:
+#endif
+ {
+ off_t nread;
+ size_t index;
+
+ nread = 0;
+ index = 0;
+ result = 1;
+ do {
+ if (req->bufs[index].len > 0) {
+ result = pread(req->file, req->bufs[index].base, req->bufs[index].len, req->off + nread);
+ if (result > 0) {
+ nread += result;
+ }
+ }
+ index++;
+ } while (index < req->nbufs && result > 0);
+ if (nread > 0) {
+ result = nread;
+ }
+ }
+#if defined(__linux__)
+ else {
+ result = uv__preadv(req->file, (struct iovec *)req->bufs, req->nbufs, req->off);
+ if (result == -1 && errno == ENOSYS) {
+ no_preadv = 1;
+ goto retry;
+ }
+ }
+#endif
+#endif
+ }
+
+done:
+ if (req->bufs != req->bufsml) {
+ free(req->bufs);
+ }
+ return result;
+}
+
+static ssize_t uv__fs_write(uv_fs_t *req)
+{
+#if defined(__linux__)
+ static int no_pwritev;
+#endif
+ ssize_t r;
+
+ if (req->off < 0) {
+ if (req->nbufs == 1) {
+ r = write(req->file, req->bufs[0].base, req->bufs[0].len);
+ }
+#if defined(__linux__)
+ else {
+ r = writev(req->file, (struct iovec *)req->bufs, req->nbufs);
+ }
+#endif
+ } else {
+ if (req->nbufs == 1) {
+ r = pwrite(req->file, req->bufs[0].base, req->bufs[0].len, req->off);
+ goto done;
+ }
+#if HAVE_PREADV
+ r = pwritev(req->file, (struct iovec *)req->bufs, req->nbufs, req->off);
+#else
+#if defined(__linux__)
+ if (no_pwritev)
+retry:
+#endif
+ {
+ off_t written;
+ size_t index;
+
+ written = 0;
+ index = 0;
+ r = 0;
+ do {
+ if (req->bufs[index].len > 0) {
+ r = pwrite(req->file, req->bufs[index].base, req->bufs[index].len, req->off + written);
+ if (r > 0) {
+ written += r;
+ }
+ }
+ index++;
+ } while (index < req->nbufs && r >= 0);
+ if (written > 0) {
+ r = written;
+ }
+ }
+#if defined(__linux__)
+ else {
+ r = uv__pwritev(req->file, (struct iovec *)req->bufs, req->nbufs, req->off);
+ if (r == -1 && errno == ENOSYS) {
+ no_pwritev = 1;
+ goto retry;
+ }
+ }
+#endif
+#endif
+ }
+
+done:
+ if (req->bufs != req->bufsml) {
+ free(req->bufs);
+ }
+
+ return r;
+}
+
+static void uv__fs_done(struct uv__work *w, int status)
+{
+ uv_fs_t *req;
+
+ req = container_of(w, uv_fs_t, work_req);
+ uv__req_unregister(req->loop, req);
+
+ if (status == -ECANCELED) {
+ assert(req->result == 0);
+ req->result = -ECANCELED;
+ }
+
+ if (req->cb != NULL) {
+ req->cb(req);
+ }
+}
+
+static ssize_t uv__fs_futime(uv_fs_t *req)
+{
+#if defined(__linux__)
+ /* utimesat() has nanosecond resolution but we stick to microseconds
+ * for the sake of consistency with other platforms.
+ */
+ static int no_utimesat;
+ struct timespec ts[2];
+ struct timeval tv[2];
+ char path[sizeof("/proc/self/fd/") + 3 * sizeof(int)];
+ int r;
+
+ if (no_utimesat) {
+ goto skip;
+ }
+
+ ts[0].tv_sec = req->atime;
+ ts[0].tv_nsec = (unsigned long)(req->atime * 1000000) % 1000000 * 1000;
+ ts[1].tv_sec = req->mtime;
+ ts[1].tv_nsec = (unsigned long)(req->mtime * 1000000) % 1000000 * 1000;
+
+ r = uv__utimesat(req->file, NULL, ts, 0);
+ if (r == 0) {
+ return r;
+ }
+
+ if (errno != ENOSYS) {
+ return r;
+ }
+
+ no_utimesat = 1;
+
+skip:
+
+ tv[0].tv_sec = req->atime;
+ tv[0].tv_usec = (unsigned long)(req->atime * 1000000) % 1000000;
+ tv[1].tv_sec = req->mtime;
+ tv[1].tv_usec = (unsigned long)(req->mtime * 1000000) % 1000000;
+ snprintf(path, sizeof(path), "/proc/self/fd/%d", (int)req->file);
+
+ r = utimes(path, tv);
+ if (r == 0) {
+ return r;
+ }
+
+ switch (errno) {
+ case ENOENT:
+ if (fcntl(req->file, F_GETFL) == -1 && errno == EBADF) {
+ break;
+ }
+ /* Fall through. */
+
+ case EACCES:
+ case ENOTDIR:
+ errno = ENOSYS;
+ break;
+ }
+
+ return r;
+
+#else
+
+ set_errno(ENOSYS);
+ return -1;
+
+#endif
+}
+
+static ssize_t uv__fs_utime(uv_fs_t *req)
+{
+#if defined(__TINYARA__)
+ return -1;
+#else
+ struct utimbuf buf;
+ buf.actime = req->atime;
+ buf.modtime = req->mtime;
+ return utime(req->path, &buf); /* TODO use utimes() where available */
+#endif
+}
+
+//-----------------------------------------------------------------------------
+//
+
+static void uv__fs_work(struct uv__work *w)
+{
+ int retry_on_eintr;
+ uv_fs_t *req;
+ ssize_t r;
+#ifdef O_CLOEXEC
+ static int no_cloexec_support;
+#endif /* O_CLOEXEC */
+
+ req = container_of(w, uv_fs_t, work_req);
+ retry_on_eintr = !(req->fs_type == UV_FS_CLOSE);
+
+ do {
+ set_errno(0);
+
+#define X(type, action) \
+ case UV_FS_ ## type: \
+ r = action; \
+ break;
+
+ switch (req->fs_type) {
+#if !defined(__TINYARA__)
+ //X(CHMOD, chmod(req->path, req->mode));
+ //X(CHOWN, chown(req->path, req->uid, req->gid));
+ //X(FCHMOD, fchmod(req->file, req->mode));
+ //X(FCHOWN, fchown(req->file, req->uid, req->gid));
+ X(FTRUNCATE, ftruncate(req->file, req->off));
+ //X(LINK, link(req->path, req->new_path));
+ //X(SYMLINK, symlink(req->path, req->new_path));
+#endif
+ //X(ACCESS, access(req->path, req->flags));
+ X(CLOSE, close(req->file));
+ X(FDATASYNC, uv__fs_fdatasync(req));
+ X(FSTAT, uv__fs_fstat(req->file, &req->statbuf));
+ X(FSYNC, fsync(req->file));
+ X(FUTIME, uv__fs_futime(req));
+ //X(LSTAT, uv__fs_lstat(req->path, &req->statbuf));
+ //X(MKDIR, mkdir(req->path, req->mode));
+ //X(MKDTEMP, uv__fs_mkdtemp(req));
+ X(READ, uv__fs_read(req));
+ //X(SCANDIR, uv__fs_scandir(req));
+ //X(READLINK, uv__fs_readlink(req));
+ X(RENAME, rename(req->path, req->new_path));
+ //X(RMDIR, rmdir(req->path));
+ //X(SENDFILE, uv__fs_sendfile(req));
+ X(STAT, uv__fs_stat(req->path, &req->statbuf));
+ X(UNLINK, unlink(req->path));
+ X(UTIME, uv__fs_utime(req));
+ X(WRITE, uv__fs_write(req));
+
+ case UV_FS_OPEN:
+#ifdef O_CLOEXEC
+ /* Try O_CLOEXEC before entering locks */
+ if (!no_cloexec_support) {
+ r = open(req->path, req->flags | O_CLOEXEC, req->mode);
+ if (r >= 0) {
+ break;
+ }
+ if (errno != EINVAL) {
+ break;
+ }
+ no_cloexec_support = 1;
+ }
+#endif /* O_CLOEXEC */
+ if (req->cb != NULL) {
+ uv_rwlock_rdlock(&req->loop->cloexec_lock);
+ }
+
+ r = open(req->path, req->flags, req->mode);
+
+ /*
+ * In case of failure `uv__cloexec` will leave error in `errno`,
+ * so it is enough to just set `r` to `-1`.
+ */
+ if (r >= 0 && uv__cloexec(r, 1) != 0) {
+ r = uv__close(r);
+ if (r != 0 && r != -EINPROGRESS) {
+ ABORT();
+ }
+ r = -1;
+ }
+ if (req->cb != NULL) {
+ uv_rwlock_rdunlock(&req->loop->cloexec_lock);
+ }
+ break;
+
+ default:
+ TDLOG("!!! file operation(%d) does not exist !!!", req->fs_type);
+ ABORT();
+ }
+ r = -1;
+#undef X
+ } while (r == -1 && errno == EINTR && retry_on_eintr);
+
+ if (r == -1) {
+ req->result = -get_errno();
+ } else {
+ req->result = r;
+ }
+
+ if (r == 0 && (req->fs_type == UV_FS_STAT || req->fs_type == UV_FS_FSTAT || req->fs_type == UV_FS_LSTAT)) {
+ req->ptr = &req->statbuf;
+ }
+}
+
+//-----------------------------------------------------------------------------
+//
+
+int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb)
+{
+ INIT(OPEN);
+ PATH;
+ req->flags = flags;
+ req->mode = mode;
+ POST;
+}
+
+int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, uv_fs_cb cb)
+{
+ INIT(READ);
+ req->file = file;
+
+ req->nbufs = nbufs;
+ req->bufs = req->bufsml;
+ if (nbufs > ARRAY_SIZE(req->bufsml)) {
+ req->bufs = (uv_buf_t *)malloc(nbufs * sizeof(*bufs));
+ }
+
+ if (req->bufs == NULL) {
+ return -ENOMEM;
+ }
+
+ memcpy(req->bufs, bufs, nbufs * sizeof(*bufs));
+
+ req->off = off;
+ POST;
+}
+
+int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)
+{
+ INIT(STAT);
+ PATH;
+ POST;
+}
+
+int uv_fs_fstat(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)
+{
+ INIT(FSTAT);
+ req->file = file;
+ POST;
+}
+
+int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)
+{
+ INIT(FDATASYNC);
+ req->file = file;
+ POST;
+}
+
+int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)
+{
+ INIT(FSYNC);
+ req->file = file;
+ POST;
+}
+
+int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)
+{
+ INIT(CLOSE);
+ req->file = file;
+ POST;
+}
+
+int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, uv_fs_cb cb)
+{
+ INIT(WRITE);
+ req->file = file;
+
+ req->nbufs = nbufs;
+ req->bufs = req->bufsml;
+ if (nbufs > ARRAY_SIZE(req->bufsml)) {
+ req->bufs = (uv_buf_t *)malloc(nbufs * sizeof(*bufs));
+ }
+
+ if (req->bufs == NULL) {
+ return -ENOMEM;
+ }
+
+ memcpy(req->bufs, bufs, nbufs * sizeof(*bufs));
+
+ req->off = off;
+ POST;
+}
+
+void uv_fs_req_cleanup(uv_fs_t *req)
+{
+ free((void *)req->path);
+ req->path = NULL;
+ req->new_path = NULL;
+
+ if (req->fs_type == UV_FS_SCANDIR && req->ptr != NULL) {
+ uv__fs_scandir_cleanup(req);
+ }
+
+ if (req->ptr != &req->statbuf) {
+ free(req->ptr);
+ }
+ req->ptr = NULL;
+}
+
+int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)
+{
+ INIT(UNLINK);
+ PATH;
+ POST;
+}
+
+int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file file, int64_t off, uv_fs_cb cb)
+{
+ INIT(FTRUNCATE);
+ req->file = file;
+ req->off = off;
+ POST;
+}
+
+int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, uv_fs_cb cb)
+{
+ INIT(RENAME);
+ PATH2;
+ POST;
+}
+
+int uv_fs_futime(uv_loop_t *loop, uv_fs_t *req, uv_file file, double atime, double mtime, uv_fs_cb cb)
+{
+ INIT(FUTIME);
+ req->file = file;
+ req->atime = atime;
+ req->mtime = mtime;
+ POST;
+}
+
+int uv_fs_utime(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb)
+{
+ INIT(UTIME);
+ PATH;
+ req->atime = atime;
+ req->mtime = mtime;
+ POST;
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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.
+ */
+
+/* Expose glibc-specific EAI_* error codes. Needs to be defined before we
+ * include any headers.
+ */
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include <errno.h>
+#include <stddef.h> /* NULL */
+#include <stdlib.h>
+#include <string.h>
+
+#include <uv.h>
+
+/* EAI_* constants. */
+//#if !defined(__TINYARA__)
+#include <netdb.h>
+//#endif
+
+int uv__getaddrinfo_translate_error(int sys_err)
+{
+ switch (sys_err) {
+ case 0:
+ return 0;
+#if defined(EAI_ADDRFAMILY)
+ case EAI_ADDRFAMILY:
+ return UV_EAI_ADDRFAMILY;
+#endif
+#if defined(EAI_AGAIN)
+ case EAI_AGAIN:
+ return UV_EAI_AGAIN;
+#endif
+#if defined(EAI_BADFLAGS)
+ case EAI_BADFLAGS:
+ return UV_EAI_BADFLAGS;
+#endif
+#if defined(EAI_BADHINTS)
+ case EAI_BADHINTS:
+ return UV_EAI_BADHINTS;
+#endif
+#if defined(EAI_CANCELED)
+ case EAI_CANCELED:
+ return UV_EAI_CANCELED;
+#endif
+#if defined(EAI_FAIL)
+ case EAI_FAIL:
+ return UV_EAI_FAIL;
+#endif
+#if defined(EAI_FAMILY)
+ case EAI_FAMILY:
+ return UV_EAI_FAMILY;
+#endif
+#if defined(EAI_MEMORY)
+ case EAI_MEMORY:
+ return UV_EAI_MEMORY;
+#endif
+#if defined(EAI_NODATA)
+ case EAI_NODATA:
+ return UV_EAI_NODATA;
+#endif
+#if defined(EAI_NONAME)
+#if !defined(EAI_NODATA) || EAI_NODATA != EAI_NONAME
+ case EAI_NONAME:
+ return UV_EAI_NONAME;
+#endif
+#endif
+#if defined(EAI_OVERFLOW)
+ case EAI_OVERFLOW:
+ return UV_EAI_OVERFLOW;
+#endif
+#if defined(EAI_PROTOCOL)
+ case EAI_PROTOCOL:
+ return UV_EAI_PROTOCOL;
+#endif
+#if defined(EAI_SERVICE)
+ case EAI_SERVICE:
+ return UV_EAI_SERVICE;
+#endif
+#if defined(EAI_SOCKTYPE)
+ case EAI_SOCKTYPE:
+ return UV_EAI_SOCKTYPE;
+#endif
+#if defined(EAI_SYSTEM)
+ case EAI_SYSTEM:
+ return -errno;
+#endif
+ }
+ assert(!"unknown EAI_* error code");
+ ABORT();
+ return 0; /* Pacify compiler. */
+}
+
+static void uv__getaddrinfo_work(struct uv__work *w)
+{
+ uv_getaddrinfo_t *req;
+ int err;
+
+ req = container_of(w, uv_getaddrinfo_t, work_req);
+#if defined(__TINYARA__)
+ err = 0;
+#else
+ err = getaddrinfo(req->hostname, req->service, req->hints, &req->addrinfo);
+#endif
+ req->retcode = uv__getaddrinfo_translate_error(err);
+}
+
+static void uv__getaddrinfo_done(struct uv__work *w, int status)
+{
+ uv_getaddrinfo_t *req;
+
+ req = container_of(w, uv_getaddrinfo_t, work_req);
+ uv__req_unregister(req->loop, req);
+
+ /* See initialization in uv_getaddrinfo(). */
+ if (req->hints) {
+ free(req->hints);
+ } else if (req->service) {
+ free(req->service);
+ } else if (req->hostname) {
+ free(req->hostname);
+ } else {
+ assert(0);
+ }
+
+ req->hints = NULL;
+ req->service = NULL;
+ req->hostname = NULL;
+
+ if (status == -ECANCELED) {
+ assert(req->retcode == 0);
+ req->retcode = UV_EAI_CANCELED;
+ }
+
+ if (req->cb) {
+ req->cb(req, req->retcode, req->addrinfo);
+ }
+}
+
+int uv_getaddrinfo(uv_loop_t *loop, uv_getaddrinfo_t *req, uv_getaddrinfo_cb cb, const char *hostname, const char *service, const struct addrinfo *hints)
+{
+ size_t hostname_len;
+ size_t service_len;
+ size_t hints_len;
+ size_t len;
+ char *buf;
+
+ if (req == NULL || (hostname == NULL && service == NULL)) {
+ return -EINVAL;
+ }
+
+ hostname_len = hostname ? strlen(hostname) + 1 : 0;
+ service_len = service ? strlen(service) + 1 : 0;
+ hints_len = hints ? sizeof(*hints) : 0;
+ buf = (char *)malloc(hostname_len + service_len + hints_len);
+
+ if (buf == NULL) {
+ return -ENOMEM;
+ }
+
+ uv__req_init(loop, req, UV_GETADDRINFO);
+ req->loop = loop;
+ req->cb = cb;
+ req->addrinfo = NULL;
+ req->hints = NULL;
+ req->service = NULL;
+ req->hostname = NULL;
+ req->retcode = 0;
+
+ /* order matters, see uv_getaddrinfo_done() */
+ len = 0;
+
+ if (hints) {
+ req->hints = (struct addrinfo *)memcpy(buf + len, hints, sizeof(*hints));
+ len += sizeof(*hints);
+ }
+
+ if (service) {
+ req->service = (char *)memcpy(buf + len, service, service_len);
+ len += service_len;
+ }
+
+ if (hostname) {
+ req->hostname = (char *)memcpy(buf + len, hostname, hostname_len);
+ }
+
+ if (cb) {
+ uv__work_submit(loop, &req->work_req, uv__getaddrinfo_work, uv__getaddrinfo_done);
+ return 0;
+ } else {
+ uv__getaddrinfo_work(&req->work_req);
+ uv__getaddrinfo_done(&req->work_req, 0);
+ return req->retcode;
+ }
+}
+
+void uv_freeaddrinfo(struct addrinfo *ai)
+{
+ if (ai) {
+#if defined(__TINYARA__)
+
+#else
+ freeaddrinfo(ai);
+#endif
+ }
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdlib.h>
+#include <limits.h>
+#include <signal.h>
+#include <stdio.h>
+
+#include <uv.h>
+
+static unsigned int next_power_of_two(unsigned int val)
+{
+ val -= 1;
+ val |= val >> 1;
+ val |= val >> 2;
+ val |= val >> 4;
+ val |= val >> 8;
+ val |= val >> 16;
+ val += 1;
+ return val;
+}
+
+static void maybe_resize(uv_loop_t *loop, unsigned int len)
+{
+ uv__io_t **watchers;
+ void *fake_watcher_list;
+ void *fake_watcher_count;
+ unsigned int nwatchers;
+ unsigned int i;
+
+ if (len <= loop->nwatchers) {
+ return;
+ }
+
+ /* Preserve fake watcher list and count at the end of the watchers */
+ if (loop->watchers != NULL) {
+ fake_watcher_list = loop->watchers[loop->nwatchers];
+ fake_watcher_count = loop->watchers[loop->nwatchers + 1];
+ } else {
+ fake_watcher_list = NULL;
+ fake_watcher_count = NULL;
+ }
+
+ nwatchers = next_power_of_two(len + 2) - 2;
+ watchers = (uv__io_t **) realloc(loop->watchers, (nwatchers + 2) * sizeof(loop->watchers[0]));
+
+ if (watchers == NULL) {
+ TDLOG("maybe_resize watchers NULL abort");
+ ABORT();
+ }
+ for (i = loop->nwatchers; i < nwatchers; i++) {
+ watchers[i] = NULL;
+ }
+ watchers[nwatchers] = (uv__io_t *) fake_watcher_list;
+ watchers[nwatchers + 1] = (uv__io_t *) fake_watcher_count;
+
+ loop->watchers = watchers;
+ loop->nwatchers = nwatchers;
+}
+
+//-----------------------------------------------------------------------------
+
+void uv__io_init(uv__io_t *w, uv__io_cb cb, int fd)
+{
+ assert(cb != NULL);
+ assert(fd >= -1);
+ QUEUE_INIT(&w->pending_queue);
+ QUEUE_INIT(&w->watcher_queue);
+ w->cb = cb;
+ w->fd = fd;
+ w->events = 0;
+ w->pevents = 0;
+
+#if defined(UV_HAVE_KQUEUE)
+ w->rcount = 0;
+ w->wcount = 0;
+#endif /* defined(UV_HAVE_KQUEUE) */
+}
+
+void uv__io_start(uv_loop_t *loop, uv__io_t *w, unsigned int events)
+{
+ assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT | UV__POLLRDHUP)));
+ assert(0 != events);
+ assert(w->fd >= 0);
+ assert(w->fd < INT_MAX);
+
+ w->pevents |= events;
+ maybe_resize(loop, w->fd + 1);
+
+#if !defined(__sun)
+ /* The event ports backend needs to rearm all file descriptors on each and
+ * every tick of the event loop but the other backends allow us to
+ * short-circuit here if the event mask is unchanged.
+ */
+ if (w->events == w->pevents) {
+ if (w->events == 0 && !QUEUE_EMPTY(&w->watcher_queue)) {
+ QUEUE_REMOVE(&w->watcher_queue);
+ QUEUE_INIT(&w->watcher_queue);
+ }
+ return;
+ }
+#endif
+
+ if (QUEUE_EMPTY(&w->watcher_queue)) {
+ QUEUE_INSERT_TAIL(&loop->watcher_queue, &w->watcher_queue);
+ }
+
+ if (loop->watchers[w->fd] == NULL) {
+ loop->watchers[w->fd] = w;
+ loop->nfds++;
+ }
+}
+
+void uv__io_stop(uv_loop_t *loop, uv__io_t *w, unsigned int events)
+{
+ assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT | UV__POLLRDHUP)));
+ assert(0 != events);
+
+ if (w->fd == -1) {
+ return;
+ }
+
+ assert(w->fd >= 0);
+
+ /* Happens when uv__io_stop() is called on a handle that was never started. */
+ if ((unsigned)w->fd >= loop->nwatchers) {
+ return;
+ }
+
+ w->pevents &= ~events;
+
+ if (w->pevents == 0) {
+ QUEUE_REMOVE(&w->watcher_queue);
+ QUEUE_INIT(&w->watcher_queue);
+
+ if (loop->watchers[w->fd] != NULL) {
+ assert(loop->watchers[w->fd] == w);
+ assert(loop->nfds > 0);
+ loop->watchers[w->fd] = NULL;
+ loop->nfds--;
+ w->events = 0;
+ }
+ } else if (QUEUE_EMPTY(&w->watcher_queue)) {
+ QUEUE_INSERT_TAIL(&loop->watcher_queue, &w->watcher_queue);
+ }
+}
+
+void uv__io_close(uv_loop_t *loop, uv__io_t *w)
+{
+ uv__io_stop(loop, w, UV__POLLIN | UV__POLLOUT);
+ QUEUE_REMOVE(&w->pending_queue);
+
+ /* Remove stale events for this file descriptor */
+ uv__platform_invalidate_fd(loop, w->fd);
+}
+
+void uv__io_feed(uv_loop_t *loop, uv__io_t *w)
+{
+ if (QUEUE_EMPTY(&w->pending_queue)) {
+ QUEUE_INSERT_TAIL(&loop->pending_queue, &w->pending_queue);
+ }
+}
+
+int uv__io_active(const uv__io_t *w, unsigned int events)
+{
+ assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
+ assert(0 != events);
+
+ return 0 != (w->pevents & events);
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <unistd.h>
+
+#include <uv.h>
+
+//-----------------------------------------------------------------------------
+
+int uv__make_pipe(int fds[2], int flags)
+{
+#if defined(__linux__)
+ static int no_pipe2 = 0;
+
+ if (no_pipe2) {
+ goto skip;
+ }
+
+ if (uv__pipe2(fds, flags | UV__O_CLOEXEC) == 0) {
+ return 0;
+ }
+
+ if (errno != ENOSYS) {
+ return -errno;
+ }
+
+ no_pipe2 = 1;
+
+skip:
+#endif
+
+ if (pipe(fds)) {
+ return -errno;
+ }
+
+ uv__cloexec(fds[0], 1);
+ uv__cloexec(fds[1], 1);
+
+ if (flags & UV__F_NONBLOCK) {
+ uv__nonblock(fds[0], 1);
+ uv__nonblock(fds[1], 1);
+ }
+
+ return 0;
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#if !defined(__TINYARA__)
+#include <sys/uio.h>
+#endif
+#include <sys/un.h>
+#include <unistd.h>
+#include <limits.h> /* IOV_MAX */
+
+#include <uv.h>
+
+static void uv__stream_flush_write_queue(uv_stream_t *stream, int error);
+static void uv__stream_connect(uv_stream_t *);
+static void uv__write(uv_stream_t *stream);
+static void uv__read(uv_stream_t *stream);
+static void uv__stream_io(uv_loop_t *loop, uv__io_t *w, unsigned int events);
+static void uv__write_callbacks(uv_stream_t *stream);
+static void uv__drain(uv_stream_t *stream);
+static size_t uv__write_req_size(uv_write_t *req);
+
+//-----------------------------------------------------------------------------
+
+void uv__stream_init(uv_loop_t *loop, uv_stream_t *stream, uv_handle_type type)
+{
+ int err;
+
+ uv__handle_init(loop, (uv_handle_t *) stream, type);
+ stream->read_cb = NULL;
+ stream->alloc_cb = NULL;
+ stream->close_cb = NULL;
+ stream->connection_cb = NULL;
+ stream->connect_req = NULL;
+ stream->shutdown_req = NULL;
+ stream->accepted_fd = -1;
+ stream->queued_fds = NULL;
+ stream->delayed_error = 0;
+ QUEUE_INIT(&stream->write_queue);
+ QUEUE_INIT(&stream->write_completed_queue);
+ stream->write_queue_size = 0;
+
+ if (loop->emfile_fd == -1) {
+ err = uv__open_cloexec("/", O_RDONLY);
+ if (err >= 0) {
+ loop->emfile_fd = err;
+ }
+ }
+#if defined(__APPLE__)
+ stream->select = NULL;
+#endif /* defined(__APPLE_) */
+
+ uv__io_init(&stream->io_watcher, uv__stream_io, -1);
+}
+
+int uv__stream_open(uv_stream_t *stream, int fd, int flags)
+{
+ assert(fd >= 0);
+ stream->flags |= flags;
+
+ if (stream->type == UV_TCP) {
+ if ((stream->flags & UV_TCP_NODELAY) && uv__tcp_nodelay(fd, 1)) {
+ return -get_errno();
+ }
+
+ /* TODO Use delay the user passed in. */
+ if ((stream->flags & UV_TCP_KEEPALIVE) && uv__tcp_keepalive(fd, 1, 60)) {
+ return -get_errno();
+ }
+ }
+
+ stream->io_watcher.fd = fd;
+
+ return 0;
+}
+
+void uv__stream_destroy(uv_stream_t *stream)
+{
+ assert(!uv__io_active(&stream->io_watcher, UV__POLLIN | UV__POLLOUT));
+ assert(stream->flags & UV_CLOSED);
+
+ if (stream->connect_req) {
+ uv__req_unregister(stream->loop, stream->connect_req);
+ stream->connect_req->cb(stream->connect_req, -ECANCELED);
+ stream->connect_req = NULL;
+ }
+
+ uv__stream_flush_write_queue(stream, -ECANCELED);
+ uv__write_callbacks(stream);
+
+ if (stream->shutdown_req) {
+ /* The ECANCELED error code is a lie, the shutdown(2) syscall is a
+ * fait accompli at this point. Maybe we should revisit this in v0.11.
+ * A possible reason for leaving it unchanged is that it informs the
+ * callee that the handle has been destroyed.
+ */
+ uv__req_unregister(stream->loop, stream->shutdown_req);
+ stream->shutdown_req->cb(stream->shutdown_req, -ECANCELED);
+ stream->shutdown_req = NULL;
+ }
+
+ assert(stream->write_queue_size == 0);
+}
+
+/* Implements a best effort approach to mitigating accept() EMFILE errors.
+ * We have a spare file descriptor stashed away that we close to get below
+ * the EMFILE limit. Next, we accept all pending connections and close them
+ * immediately to signal the clients that we're overloaded - and we are, but
+ * we still keep on trucking.
+ *
+ * There is one caveat: it's not reliable in a multi-threaded environment.
+ * The file descriptor limit is per process. Our party trick fails if another
+ * thread opens a file or creates a socket in the time window between us
+ * calling close() and accept().
+ */
+static int uv__emfile_trick(uv_loop_t *loop, int accept_fd)
+{
+ int err;
+ int emfile_fd;
+
+ if (loop->emfile_fd == -1) {
+ return -EMFILE;
+ }
+
+ uv__close(loop->emfile_fd);
+ loop->emfile_fd = -1;
+
+ do {
+ err = uv__accept(accept_fd);
+ if (err >= 0) {
+ uv__close(err);
+ }
+ } while (err >= 0 || err == -EINTR);
+
+ emfile_fd = uv__open_cloexec("/", O_RDONLY);
+ if (emfile_fd >= 0) {
+ loop->emfile_fd = emfile_fd;
+ }
+
+ return err;
+}
+
+#if defined(UV_HAVE_KQUEUE)
+#define UV_DEC_BACKLOG(w) w->rcount--;
+#else
+#define UV_DEC_BACKLOG(w) /* no-op */
+#endif /* defined(UV_HAVE_KQUEUE) */
+
+void uv__server_io(uv_loop_t *loop, uv__io_t *w, unsigned int events)
+{
+ uv_stream_t *stream;
+ int err;
+
+ stream = container_of(w, uv_stream_t, io_watcher);
+ assert(events == UV__POLLIN);
+ assert(stream->accepted_fd == -1);
+ assert(!(stream->flags & UV_CLOSING));
+
+ uv__io_start(stream->loop, &stream->io_watcher, UV__POLLIN);
+
+ /* connection_cb can close the server socket while we're
+ * in the loop so check it on each iteration.
+ */
+ while (uv__stream_fd(stream) != -1) {
+ assert(stream->accepted_fd == -1);
+
+#if defined(UV_HAVE_KQUEUE)
+ if (w->rcount <= 0) {
+ return;
+ }
+#endif /* defined(UV_HAVE_KQUEUE) */
+
+ set_errno(0);
+ err = uv__accept(uv__stream_fd(stream));
+
+ if (err < 0) {
+ if (err == -EAGAIN || err == -EWOULDBLOCK) {
+ return; /* Not an error. */
+ }
+
+ if (err == -ECONNABORTED) {
+ continue; /* Ignore. Nothing we can do about that. */
+ }
+
+ if (err == -EMFILE || err == -ENFILE) {
+ err = uv__emfile_trick(loop, uv__stream_fd(stream));
+ if (err == -EAGAIN || err == -EWOULDBLOCK) {
+ break;
+ }
+ }
+
+ stream->connection_cb(stream, err);
+ continue;
+ }
+
+ UV_DEC_BACKLOG(w)
+ stream->accepted_fd = err;
+ stream->connection_cb(stream, 0);
+
+ if (stream->accepted_fd != -1) {
+ /* The user hasn't yet accepted called uv_accept() */
+ uv__io_stop(loop, &stream->io_watcher, UV__POLLIN);
+ return;
+ }
+
+ if (stream->type == UV_TCP && (stream->flags & UV_TCP_SINGLE_ACCEPT)) {
+ /* Give other processes a chance to accept connections. */
+ struct timespec timeout = { 0, 1 };
+ nanosleep(&timeout, NULL);
+ }
+ }
+}
+
+#undef UV_DEC_BACKLOG
+
+int uv_accept(uv_stream_t *server, uv_stream_t *client)
+{
+ int err;
+
+ /* TODO document this */
+ assert(server->loop == client->loop);
+
+ if (server->accepted_fd == -1) {
+ return -EAGAIN;
+ }
+
+ switch (client->type) {
+ case UV_NAMED_PIPE:
+ case UV_TCP:
+ err = uv__stream_open(client, server->accepted_fd, UV_STREAM_READABLE | UV_STREAM_WRITABLE);
+ if (err) {
+ /* TODO handle error */
+ uv__close(server->accepted_fd);
+ goto done;
+ }
+ break;
+ /*
+ case UV_UDP:
+ err = uv_udp_open((uv_udp_t*) client, server->accepted_fd);
+ if (err) {
+ uv__close(server->accepted_fd);
+ goto done;
+ }
+ break;
+ */
+ default:
+ return -EINVAL;
+ }
+
+done:
+ /* Process queued fds */
+ if (server->queued_fds != NULL) {
+ uv__stream_queued_fds_t *queued_fds;
+
+ queued_fds = (uv__stream_queued_fds_t *) server->queued_fds;
+
+ /* Read first */
+ server->accepted_fd = queued_fds->fds[0];
+
+ /* All read, free */
+ assert(queued_fds->offset > 0);
+ if (--queued_fds->offset == 0) {
+ free(queued_fds);
+ server->queued_fds = NULL;
+ } else {
+ /* Shift rest */
+ memmove(queued_fds->fds, queued_fds->fds + 1, queued_fds->offset * sizeof(*queued_fds->fds));
+ }
+ } else {
+ server->accepted_fd = -1;
+ if (err == 0) {
+ uv__io_start(server->loop, &server->io_watcher, UV__POLLIN);
+ }
+ }
+ return err;
+}
+
+int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
+{
+ int err;
+
+ switch (stream->type) {
+ case UV_TCP:
+ err = uv_tcp_listen((uv_tcp_t *) stream, backlog, cb);
+ break;
+ /*
+ case UV_NAMED_PIPE:
+ err = uv_pipe_listen((uv_pipe_t*)stream, backlog, cb);
+ break;
+ */
+ default:
+ err = -EINVAL;
+ }
+
+ if (err == 0) {
+ uv__handle_start(stream);
+ }
+
+ return err;
+}
+
+//-----------------------------------------------------------------------------
+
+static void uv__stream_flush_write_queue(uv_stream_t *stream, int error)
+{
+ uv_write_t *req;
+ QUEUE *q;
+ while (!QUEUE_EMPTY(&stream->write_queue)) {
+ q = QUEUE_HEAD(&stream->write_queue);
+ QUEUE_REMOVE(q);
+
+ req = QUEUE_DATA(q, uv_write_t, queue);
+ req->error = error;
+
+ QUEUE_INSERT_TAIL(&stream->write_completed_queue, &req->queue);
+ }
+}
+
+static void uv__write_callbacks(uv_stream_t *stream)
+{
+ uv_write_t *req;
+ QUEUE *q;
+
+ while (!QUEUE_EMPTY(&stream->write_completed_queue)) {
+ /* Pop a req off write_completed_queue. */
+ q = QUEUE_HEAD(&stream->write_completed_queue);
+ req = QUEUE_DATA(q, uv_write_t, queue);
+ QUEUE_REMOVE(q);
+ uv__req_unregister(stream->loop, req);
+
+ if (req->bufs != NULL) {
+ stream->write_queue_size -= uv__write_req_size(req);
+ if (req->bufs != req->bufsml) {
+ free(req->bufs);
+ }
+ req->bufs = NULL;
+ }
+
+ /* NOTE: call callback AFTER freeing the request data. */
+ if (req->cb) {
+ req->cb(req, req->error);
+ }
+ }
+
+ assert(QUEUE_EMPTY(&stream->write_completed_queue));
+}
+
+static void uv__drain(uv_stream_t *stream)
+{
+ uv_shutdown_t *req;
+ int err;
+
+ assert(QUEUE_EMPTY(&stream->write_queue));
+ uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLOUT);
+
+ /* Shutdown? */
+ if ((stream->flags & UV_STREAM_SHUTTING) && !(stream->flags & UV_CLOSING) && !(stream->flags & UV_STREAM_SHUT)) {
+ assert(stream->shutdown_req);
+
+ req = stream->shutdown_req;
+ stream->shutdown_req = NULL;
+ stream->flags &= ~UV_STREAM_SHUTTING;
+ uv__req_unregister(stream->loop, req);
+
+ err = 0;
+#if defined(__TINYARA__)
+ TDLOG("uv__drain, shutdown() is not supported in NuttX");
+ assert(false);
+#else
+ if (shutdown(uv__stream_fd(stream), SHUT_WR)) {
+ err = -errno;
+ }
+#endif
+
+ if (err == 0) {
+ stream->flags |= UV_STREAM_SHUT;
+ }
+
+ if (req->cb != NULL) {
+ req->cb(req, err);
+ }
+ }
+}
+
+static size_t uv__write_req_size(uv_write_t *req)
+{
+ size_t size;
+
+ assert(req->bufs != NULL);
+ size = uv__count_bufs(req->bufs + req->write_index, req->nbufs - req->write_index);
+ assert(req->handle->write_queue_size >= size);
+
+ return size;
+}
+
+static void uv__stream_eof(uv_stream_t *stream, const uv_buf_t *buf)
+{
+ stream->flags |= UV_STREAM_READ_EOF;
+ uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLIN);
+ if (!uv__io_active(&stream->io_watcher, UV__POLLOUT)) {
+ uv__handle_stop(stream);
+ }
+ stream->read_cb(stream, UV_EOF, buf);
+ stream->flags &= ~UV_STREAM_READING;
+}
+
+#if !defined(__TINYARA__)
+static int uv__stream_queue_fd(uv_stream_t *stream, int fd)
+{
+ uv__stream_queued_fds_t *queued_fds;
+ unsigned int queue_size;
+
+ queued_fds = (uv__stream_queued_fds_t *) stream->queued_fds;
+ if (queued_fds == NULL) {
+ queue_size = 8;
+ queued_fds = (uv__stream_queued_fds_t *)
+ malloc((queue_size - 1) * sizeof(*queued_fds->fds) + sizeof(*queued_fds));
+ if (queued_fds == NULL) {
+ return -ENOMEM;
+ }
+ queued_fds->size = queue_size;
+ queued_fds->offset = 0;
+ stream->queued_fds = queued_fds;
+
+ /* Grow */
+ } else if (queued_fds->size == queued_fds->offset) {
+ queue_size = queued_fds->size + 8;
+ queued_fds = (uv__stream_queued_fds_t *)
+ realloc(queued_fds, (queue_size - 1) * sizeof(*queued_fds->fds) + sizeof(*queued_fds));
+
+ /*
+ * Allocation failure, report back.
+ * NOTE: if it is fatal - sockets will be closed in uv__stream_close
+ */
+ if (queued_fds == NULL) {
+ return -ENOMEM;
+ }
+ queued_fds->size = queue_size;
+ stream->queued_fds = queued_fds;
+ }
+
+ /* Put fd in a queue */
+ queued_fds->fds[queued_fds->offset++] = fd;
+
+ return 0;
+}
+#endif
+
+//-----------------------------------------------------------------------------
+
+static void uv__stream_io(uv_loop_t *loop, uv__io_t *w, unsigned int events)
+{
+ uv_stream_t *stream;
+
+ stream = container_of(w, uv_stream_t, io_watcher);
+
+ assert(stream->type == UV_TCP || stream->type == UV_NAMED_PIPE || stream->type == UV_TTY);
+ assert(!(stream->flags & UV_CLOSING));
+
+ if (stream->connect_req) {
+ uv__stream_connect(stream);
+ return;
+ }
+
+ assert(uv__stream_fd(stream) >= 0);
+
+ /* Ignore POLLHUP here. Even it it's set, there may still be data to read. */
+ if (events & (UV__POLLIN | UV__POLLERR | UV__POLLHUP)) {
+ uv__read(stream);
+ }
+
+ if (uv__stream_fd(stream) == -1) {
+ return; /* read_cb closed stream. */
+ }
+
+ /* Short-circuit iff POLLHUP is set, the user is still interested in read
+ * events and uv__read() reported a partial read but not EOF. If the EOF
+ * flag is set, uv__read() called read_cb with err=UV_EOF and we don't
+ * have to do anything. If the partial read flag is not set, we can't
+ * report the EOF yet because there is still data to read.
+ */
+ if ((events & UV__POLLHUP) && (stream->flags & UV_STREAM_READING) && (stream->flags & UV_STREAM_READ_PARTIAL) && !(stream->flags & UV_STREAM_READ_EOF)) {
+ uv_buf_t buf = { NULL, 0 };
+ uv__stream_eof(stream, &buf);
+ }
+
+ if (uv__stream_fd(stream) == -1) {
+ return; /* read_cb closed stream. */
+ }
+
+ if (events & (UV__POLLOUT | UV__POLLERR | UV__POLLHUP)) {
+ uv__write(stream);
+ uv__write_callbacks(stream);
+
+ /* Write queue drained. */
+ if (QUEUE_EMPTY(&stream->write_queue)) {
+ uv__drain(stream);
+ }
+ }
+}
+
+static void uv__write_req_finish(uv_write_t *req)
+{
+ uv_stream_t *stream = req->handle;
+
+ /* Pop the req off tcp->write_queue. */
+ QUEUE_REMOVE(&req->queue);
+
+ /* Only free when there was no error. On error, we touch up write_queue_size
+ * right before making the callback. The reason we don't do that right away
+ * is that a write_queue_size > 0 is our only way to signal to the user that
+ * they should stop writing - which they should if we got an error. Something
+ * to revisit in future revisions of the libuv API.
+ */
+ if (req->error == 0) {
+ if (req->bufs != req->bufsml) {
+ free(req->bufs);
+ }
+ req->bufs = NULL;
+ }
+
+ /* Add it to the write_completed_queue where it will have its
+ * callback called in the near future.
+ */
+ QUEUE_INSERT_TAIL(&stream->write_completed_queue, &req->queue);
+ uv__io_feed(stream->loop, &stream->io_watcher);
+}
+
+static int uv__handle_fd(uv_handle_t *handle)
+{
+ switch (handle->type) {
+ case UV_NAMED_PIPE:
+ case UV_TCP:
+ return ((uv_stream_t *) handle)->io_watcher.fd;
+ /*
+ case UV_UDP:
+ return ((uv_udp_t*) handle)->io_watcher.fd;
+ */
+ default:
+ return -1;
+ }
+}
+
+static int uv__getiovmax(void)
+{
+#if defined(IOV_MAX)
+ return IOV_MAX;
+#elif defined(_SC_IOV_MAX)
+ static int iovmax = -1;
+ if (iovmax == -1) {
+ iovmax = sysconf(_SC_IOV_MAX);
+ }
+ return iovmax;
+#elif defined(TUV_TINYARA_IOV_MAX)
+ return TUV_TINYARA_IOV_MAX;
+#else
+ return 1024;
+#endif
+}
+
+static void uv__write(uv_stream_t *stream)
+{
+ struct iovec *iov;
+ QUEUE *q;
+ uv_write_t *req;
+ int iovmax;
+ int iovcnt;
+ ssize_t n;
+
+start:
+ assert(uv__stream_fd(stream) >= 0);
+
+ if (QUEUE_EMPTY(&stream->write_queue)) {
+ return;
+ }
+
+ q = QUEUE_HEAD(&stream->write_queue);
+ req = QUEUE_DATA(q, uv_write_t, queue);
+ assert(req->handle == stream);
+
+ /*
+ * Cast to iovec. We had to have our own uv_buf_t instead of iovec
+ * because Windows's WSABUF is not an iovec.
+ */
+ assert(sizeof(uv_buf_t) == sizeof(struct iovec));
+ iov = (struct iovec *)&(req->bufs[req->write_index]);
+ iovcnt = req->nbufs - req->write_index;
+
+ iovmax = uv__getiovmax();
+
+ /* Limit iov count to avoid EINVALs from writev() */
+ if (iovcnt > iovmax) {
+ iovcnt = iovmax;
+ }
+
+ /*
+ * Now do the actual writev. Note that we've been updating the pointers
+ * inside the iov each time we write. So there is no need to offset it.
+ */
+
+#if defined(__TINYARA__)
+ do {
+ if (iovcnt == 1) {
+ n = write(uv__stream_fd(stream), iov[0].iov_base, iov[0].iov_len);
+ } else {
+ n = writev(uv__stream_fd(stream), iov, iovcnt);
+ }
+ } while (n == -1 && errno == EINTR);
+#else
+ if (req->send_handle) {
+ struct msghdr msg;
+ char scratch[64];
+ struct cmsghdr *cmsg;
+ int fd_to_send = uv__handle_fd((uv_handle_t *) req->send_handle);
+
+ assert(fd_to_send >= 0);
+
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ msg.msg_iov = iov;
+ msg.msg_iovlen = iovcnt;
+ msg.msg_flags = 0;
+
+ msg.msg_control = (void *)scratch;
+ msg.msg_controllen = CMSG_SPACE(sizeof(fd_to_send));
+
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(fd_to_send));
+
+ /* silence aliasing warning */
+ {
+ void *pv = CMSG_DATA(cmsg);
+ int *pi = (int *)pv;
+ *pi = fd_to_send;
+ }
+
+ do {
+ n = sendmsg(uv__stream_fd(stream), &msg, 0);
+ } while (n == -1 && errno == EINTR);
+ } else {
+ do {
+ if (iovcnt == 1) {
+ n = write(uv__stream_fd(stream), iov[0].iov_base, iov[0].iov_len);
+ } else {
+ n = writev(uv__stream_fd(stream), iov, iovcnt);
+ }
+ } while (n == -1 && errno == EINTR);
+ }
+#endif
+
+ if (n < 0) {
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ /* Error */
+ req->error = -errno;
+ uv__write_req_finish(req);
+ uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLOUT);
+ if (!uv__io_active(&stream->io_watcher, UV__POLLIN)) {
+ uv__handle_stop(stream);
+ }
+ return;
+ } else if (stream->flags & UV_STREAM_BLOCKING) {
+ /* If this is a blocking stream, try again. */
+ goto start;
+ }
+ } else {
+ /* Successful write */
+
+ while (n >= 0) {
+ uv_buf_t *buf = &(req->bufs[req->write_index]);
+ size_t len = buf->len;
+
+ assert(req->write_index < req->nbufs);
+
+ if ((size_t) n < len) {
+ buf->base += n;
+ buf->len -= n;
+ stream->write_queue_size -= n;
+ n = 0;
+
+ /* There is more to write. */
+ if (stream->flags & UV_STREAM_BLOCKING) {
+ /*
+ * If we're blocking then we should not be enabling the write
+ * watcher - instead we need to try again.
+ */
+ goto start;
+ } else {
+ /* Break loop and ensure the watcher is pending. */
+ break;
+ }
+
+ } else {
+ /* Finished writing the buf at index req->write_index. */
+ req->write_index++;
+
+ assert((size_t) n >= len);
+ n -= len;
+
+ assert(stream->write_queue_size >= len);
+ stream->write_queue_size -= len;
+
+ if (req->write_index == req->nbufs) {
+ /* Then we're done! */
+ assert(n == 0);
+ uv__write_req_finish(req);
+ /* TODO: start trying to write the next request. */
+ return;
+ }
+ }
+ }
+ }
+
+ /* Either we've counted n down to zero or we've got EAGAIN. */
+ assert(n == 0 || n == -1);
+
+ /* Only non-blocking streams should use the write_watcher. */
+ assert(!(stream->flags & UV_STREAM_BLOCKING));
+
+ /* We're not done. */
+ uv__io_start(stream->loop, &stream->io_watcher, UV__POLLOUT);
+
+}
+
+#if !defined(__TINYARA__)
+static int uv__stream_recv_cmsg(uv_stream_t *stream, struct msghdr *msg)
+{
+ struct cmsghdr *cmsg;
+
+ for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg)) {
+ char *start;
+ char *end;
+ int err;
+ void *pv;
+ int *pi;
+ unsigned int i;
+ unsigned int count;
+
+ if (cmsg->cmsg_type != SCM_RIGHTS) {
+ fprintf(stderr, "ignoring non-SCM_RIGHTS ancillary data: %d\n", cmsg->cmsg_type);
+ continue;
+ }
+
+ /* silence aliasing warning */
+ pv = CMSG_DATA(cmsg);
+ pi = (int *)pv;
+
+ /* Count available fds */
+ start = (char *)cmsg;
+ end = (char *)cmsg + cmsg->cmsg_len;
+ count = 0;
+ while (start + CMSG_LEN(count * sizeof(*pi)) < end) {
+ count++;
+ }
+ assert(start + CMSG_LEN(count * sizeof(*pi)) == end);
+
+ for (i = 0; i < count; i++) {
+ /* Already has accepted fd, queue now */
+ if (stream->accepted_fd != -1) {
+ err = uv__stream_queue_fd(stream, pi[i]);
+ if (err != 0) {
+ /* Close rest */
+ for (; i < count; i++) {
+ uv__close(pi[i]);
+ }
+ return err;
+ }
+ } else {
+ stream->accepted_fd = pi[i];
+ }
+ }
+ }
+ return 0;
+}
+#endif
+
+#if defined(__TINYARA__)
+#define READ_ALLOC_CB_SIZE (2 * 1024)
+#else
+#define READ_ALLOC_CB_SIZE (64 * 1024)
+
+#define UV__CMSG_FD_COUNT 64
+#define UV__CMSG_FD_SIZE (UV__CMSG_FD_COUNT * sizeof(int))
+#endif
+
+static void uv__read(uv_stream_t *stream)
+{
+ uv_buf_t buf;
+ ssize_t nread;
+ int count;
+ int err;
+ int is_ipc;
+#if !defined(__TINYARA__)
+ struct msghdr msg;
+ char cmsg_space[CMSG_SPACE(UV__CMSG_FD_SIZE)];
+#endif
+
+ stream->flags &= ~UV_STREAM_READ_PARTIAL;
+
+ /* Prevent loop starvation when the data comes in as fast as (or faster than)
+ * we can read it. XXX Need to rearm fd if we switch to edge-triggered I/O.
+ */
+ count = 32;
+
+ is_ipc = stream->type == UV_NAMED_PIPE && ((uv_pipe_t *) stream)->ipc;
+#if defined(__TINYARA__)
+ /* IPC for nuttx is not supported for now */
+ assert(!is_ipc);
+#endif
+
+ /* XXX: Maybe instead of having UV_STREAM_READING we just test if
+ * tcp->read_cb is NULL or not?
+ */
+ while (stream->read_cb && (stream->flags & UV_STREAM_READING)
+ && (count-- > 0)) {
+ assert(stream->alloc_cb != NULL);
+
+ stream->alloc_cb((uv_handle_t *) stream, READ_ALLOC_CB_SIZE, &buf);
+ if (buf.len == 0) {
+ /* User indicates it can't or won't handle the read. */
+ stream->read_cb(stream, UV_ENOBUFS, &buf);
+ return;
+ }
+
+ assert(buf.base != NULL);
+ assert(uv__stream_fd(stream) >= 0);
+
+#if !defined(__TINYARA__)
+ if (!is_ipc) {
+ do {
+ nread = read(uv__stream_fd(stream), buf.base, buf.len);
+ } while (nread < 0 && errno == EINTR);
+ } else {
+ /* ipc uses recvmsg */
+ msg.msg_flags = 0;
+ msg.msg_iov = (struct iovec *)&buf;
+ msg.msg_iovlen = 1;
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ /* Set up to receive a descriptor even if one isn't in the message */
+ msg.msg_controllen = sizeof(cmsg_space);
+ msg.msg_control = cmsg_space;
+
+ do {
+ nread = uv__recvmsg(uv__stream_fd(stream), &msg, 0);
+ } while (nread < 0 && errno == EINTR);
+ }
+#else
+ do {
+ nread = read(uv__stream_fd(stream), buf.base, buf.len);
+ err = get_errno();
+ } while (nread < 0 && err == EINTR);
+#endif
+
+ if (nread < 0) {
+ err = get_errno();
+ /* Error */
+ if (err == EAGAIN || err == EWOULDBLOCK) {
+ /* Wait for the next one. */
+ if (stream->flags & UV_STREAM_READING) {
+ uv__io_start(stream->loop, &stream->io_watcher, UV__POLLIN);
+ }
+ stream->read_cb(stream, 0, &buf);
+ } else {
+ /* Error. User should call uv_close(). */
+ stream->read_cb(stream, -err, &buf);
+ if (stream->flags & UV_STREAM_READING) {
+ stream->flags &= ~UV_STREAM_READING;
+ uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLIN);
+ if (!uv__io_active(&stream->io_watcher, UV__POLLOUT)) {
+ uv__handle_stop(stream);
+ }
+ }
+ }
+ return;
+ } else if (nread == 0) {
+ uv__stream_eof(stream, &buf);
+ return;
+ } else {
+ /* Successful read */
+ ssize_t buflen = buf.len;
+
+#if !defined(__TINYARA__)
+ if (is_ipc) {
+ err = uv__stream_recv_cmsg(stream, &msg);
+ if (err != 0) {
+ stream->read_cb(stream, err, &buf);
+ return;
+ }
+ }
+#endif
+ stream->read_cb(stream, nread, &buf);
+
+ /* Return if we didn't fill the buffer, there is no more data to read. */
+ if (nread < buflen) {
+ stream->flags |= UV_STREAM_READ_PARTIAL;
+ return;
+ }
+ }
+ }
+}
+
+int uv_shutdown(uv_shutdown_t *req, uv_stream_t *stream, uv_shutdown_cb cb)
+{
+ assert((stream->type == UV_TCP || stream->type == UV_NAMED_PIPE) && "uv_shutdown (unix) only supports uv_handle_t right now");
+
+ if (!(stream->flags & UV_STREAM_WRITABLE) || stream->flags & UV_STREAM_SHUT || stream->flags & UV_STREAM_SHUTTING || stream->flags & UV_CLOSED || stream->flags & UV_CLOSING) {
+ return -ENOTCONN;
+ }
+
+ assert(uv__stream_fd(stream) >= 0);
+
+ /* Initialize request */
+ uv__req_init(stream->loop, req, UV_SHUTDOWN);
+ req->handle = stream;
+ req->cb = cb;
+ stream->shutdown_req = req;
+ stream->flags |= UV_STREAM_SHUTTING;
+
+ uv__io_start(stream->loop, &stream->io_watcher, UV__POLLOUT);
+
+ return 0;
+}
+
+/**
+ * We get called here from directly following a call to connect(2).
+ * In order to determine if we've errored out or succeeded must call
+ * getsockopt.
+ */
+static void uv__stream_connect(uv_stream_t *stream)
+{
+ int error;
+ uv_connect_t *req = stream->connect_req;
+ socklen_t errorsize = sizeof(int);
+
+ assert(stream->type == UV_TCP || stream->type == UV_NAMED_PIPE);
+ assert(req);
+
+ if (stream->delayed_error) {
+ /* To smooth over the differences between unixes errors that
+ * were reported synchronously on the first connect can be delayed
+ * until the next tick--which is now.
+ */
+ error = stream->delayed_error;
+ stream->delayed_error = 0;
+ } else {
+ /* Normal situation: we need to get the socket error from the kernel. */
+ assert(uv__stream_fd(stream) >= 0);
+ getsockopt(uv__stream_fd(stream), SOL_SOCKET, SO_ERROR, &error, &errorsize);
+ error = -error;
+ }
+
+ if (error == -EINPROGRESS) {
+ return;
+ }
+
+ stream->connect_req = NULL;
+ uv__req_unregister(stream->loop, req);
+
+ if (error < 0 || QUEUE_EMPTY(&stream->write_queue)) {
+ uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLOUT);
+ }
+
+ if (req->cb) {
+ req->cb(req, error);
+ }
+
+ if (uv__stream_fd(stream) == -1) {
+ return;
+ }
+
+ if (error < 0) {
+ uv__stream_flush_write_queue(stream, -ECANCELED);
+ uv__write_callbacks(stream);
+ }
+}
+
+//-----------------------------------------------------------------------------
+
+int uv_write2(uv_write_t *req, uv_stream_t *stream, const uv_buf_t bufs[], unsigned int nbufs, uv_stream_t *send_handle, uv_write_cb cb)
+{
+ int empty_queue;
+
+ assert(nbufs > 0);
+ assert((stream->type == UV_TCP || stream->type == UV_NAMED_PIPE || stream->type == UV_TTY) && "uv_write (unix) does not yet support other types of streams");
+
+ if (uv__stream_fd(stream) < 0) {
+ return -EBADF;
+ }
+
+ if (send_handle) {
+ if (stream->type != UV_NAMED_PIPE || !((uv_pipe_t *) stream)->ipc) {
+ return -EINVAL;
+ }
+
+ /* XXX We abuse uv_write2() to send over UDP handles to child processes.
+ * Don't call uv__stream_fd() on those handles, it's a macro that on OS X
+ * evaluates to a function that operates on a uv_stream_t with a couple of
+ * OS X specific fields. On other Unices it does (handle)->io_watcher.fd,
+ * which works but only by accident.
+ */
+ if (uv__handle_fd((uv_handle_t *) send_handle) < 0) {
+ return -EBADF;
+ }
+ }
+
+ /* It's legal for write_queue_size > 0 even when the write_queue is empty;
+ * it means there are error-state requests in the write_completed_queue that
+ * will touch up write_queue_size later, see also uv__write_req_finish().
+ * We could check that write_queue is empty instead but that implies making
+ * a write() syscall when we know that the handle is in error mode.
+ */
+ empty_queue = (stream->write_queue_size == 0);
+
+ /* Initialize the req */
+ uv__req_init(stream->loop, req, UV_WRITE);
+ req->cb = cb;
+ req->handle = stream;
+ req->error = 0;
+ req->send_handle = send_handle;
+ QUEUE_INIT(&req->queue);
+
+ req->bufs = req->bufsml;
+ if (nbufs > ARRAY_SIZE(req->bufsml)) {
+ req->bufs = (uv_buf_t *)malloc(nbufs * sizeof(bufs[0]));
+ }
+
+ if (req->bufs == NULL) {
+ return -ENOMEM;
+ }
+
+ memcpy(req->bufs, bufs, nbufs * sizeof(bufs[0]));
+ req->nbufs = nbufs;
+ req->write_index = 0;
+ stream->write_queue_size += uv__count_bufs(bufs, nbufs);
+
+ /* Append the request to write_queue. */
+ QUEUE_INSERT_TAIL(&stream->write_queue, &req->queue);
+
+ /* If the queue was empty when this function began, we should attempt to
+ * do the write immediately. Otherwise start the write_watcher and wait
+ * for the fd to become writable.
+ */
+ if (stream->connect_req) {
+ /* Still connecting, do nothing. */
+ } else if (empty_queue) {
+ uv__write(stream);
+ } else {
+ /*
+ * blocking streams should never have anything in the queue.
+ * if this assert fires then somehow the blocking stream isn't being
+ * sufficiently flushed in uv__write.
+ */
+ assert(!(stream->flags & UV_STREAM_BLOCKING));
+ uv__io_start(stream->loop, &stream->io_watcher, UV__POLLOUT);
+ }
+
+ return 0;
+}
+
+/* The buffers to be written must remain valid until the callback is called.
+ * This is not required for the uv_buf_t array.
+ */
+int uv_write(uv_write_t *req, uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb)
+{
+ return uv_write2(req, handle, bufs, nbufs, NULL, cb);
+}
+
+void uv_try_write_cb(uv_write_t *req, int status)
+{
+ /* Should not be called */
+ TDLOG("uv_try_write_cb abort");
+ ABORT();
+}
+
+int uv_try_write(uv_stream_t *stream, const uv_buf_t bufs[], unsigned int nbufs)
+{
+ int r;
+ int has_pollout;
+ size_t written;
+ size_t req_size;
+ uv_write_t req;
+
+ /* Connecting or already writing some data */
+ if (stream->connect_req != NULL || stream->write_queue_size != 0) {
+ return -EAGAIN;
+ }
+
+ has_pollout = uv__io_active(&stream->io_watcher, UV__POLLOUT);
+
+ r = uv_write(&req, stream, bufs, nbufs, uv_try_write_cb);
+ if (r != 0) {
+ return r;
+ }
+
+ /* Remove not written bytes from write queue size */
+ written = uv__count_bufs(bufs, nbufs);
+ if (req.bufs != NULL) {
+ req_size = uv__write_req_size(&req);
+ } else {
+ req_size = 0;
+ }
+ written -= req_size;
+ stream->write_queue_size -= req_size;
+
+ /* Unqueue request, regardless of immediateness */
+ QUEUE_REMOVE(&req.queue);
+ uv__req_unregister(stream->loop, &req);
+ if (req.bufs != req.bufsml) {
+ free(req.bufs);
+ }
+ req.bufs = NULL;
+
+ /* Do not poll for writable, if we wasn't before calling this */
+ if (!has_pollout) {
+ uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLOUT);
+ }
+
+ if (written == 0) {
+ return -EAGAIN;
+ } else {
+ return written;
+ }
+}
+
+int uv_read_start(uv_stream_t *stream, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
+{
+ assert(stream->type == UV_TCP || stream->type == UV_NAMED_PIPE || stream->type == UV_TTY);
+
+ if (stream->flags & UV_CLOSING) {
+ return -EINVAL;
+ }
+
+ /* The UV_STREAM_READING flag is irrelevant of the state of the tcp - it just
+ * expresses the desired state of the user.
+ */
+ stream->flags |= UV_STREAM_READING;
+
+ /* TODO: try to do the read inline? */
+ /* TODO: keep track of tcp state. If we've gotten a EOF then we should
+ * not start the IO watcher.
+ */
+ assert(uv__stream_fd(stream) >= 0);
+ assert(alloc_cb);
+
+ stream->read_cb = read_cb;
+ stream->alloc_cb = alloc_cb;
+
+ uv__io_start(stream->loop, &stream->io_watcher, UV__POLLIN);
+ uv__handle_start(stream);
+
+ return 0;
+}
+
+int uv_read_stop(uv_stream_t *stream)
+{
+ if (!(stream->flags & UV_STREAM_READING)) {
+ return 0;
+ }
+
+ stream->flags &= ~UV_STREAM_READING;
+ uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLIN);
+ if (!uv__io_active(&stream->io_watcher, UV__POLLOUT)) {
+ uv__handle_stop(stream);
+ }
+
+ stream->read_cb = NULL;
+ stream->alloc_cb = NULL;
+ return 0;
+}
+
+void uv__stream_close(uv_stream_t *handle)
+{
+ unsigned int i;
+ uv__stream_queued_fds_t *queued_fds;
+
+#if defined(__APPLE__)
+ /* Terminate select loop first */
+ if (handle->select != NULL) {
+ uv__stream_select_t *s;
+
+ s = handle->select;
+
+ uv_sem_post(&s->close_sem);
+ uv_sem_post(&s->async_sem);
+ uv_thread_join(&s->thread);
+ uv_sem_destroy(&s->close_sem);
+ uv_sem_destroy(&s->async_sem);
+ uv__close(s->fake_fd);
+ uv__close(s->int_fd);
+ uv_close((uv_handle_t *)&s->async, uv__stream_osx_cb_close);
+
+ handle->select = NULL;
+ }
+#endif /* defined(__APPLE__) */
+
+ uv__io_close(handle->loop, &handle->io_watcher);
+ uv_read_stop(handle);
+ uv__handle_stop(handle);
+
+ if (handle->io_watcher.fd != -1) {
+ /* Don't close stdio file descriptors. Nothing good comes from it. */
+ if (handle->io_watcher.fd > STDERR_FILENO) {
+ uv__close(handle->io_watcher.fd);
+ }
+ handle->io_watcher.fd = -1;
+ }
+
+ if (handle->accepted_fd != -1) {
+ uv__close(handle->accepted_fd);
+ handle->accepted_fd = -1;
+ }
+
+ /* Close all queued fds */
+ if (handle->queued_fds != NULL) {
+ queued_fds = (uv__stream_queued_fds_t *) handle->queued_fds;
+ for (i = 0; i < queued_fds->offset; i++) {
+ uv__close(queued_fds->fds[i]);
+ }
+ free(handle->queued_fds);
+ handle->queued_fds = NULL;
+ }
+
+ assert(!uv__io_active(&handle->io_watcher, UV__POLLIN | UV__POLLOUT));
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdlib.h>
+
+#include <uv.h>
+
+//-----------------------------------------------------------------------------
+
+static int maybe_new_socket(uv_tcp_t *handle, int domain, int flags)
+{
+ int sockfd;
+ int err;
+
+ if (uv__stream_fd(handle) != -1) {
+ return 0;
+ }
+
+ err = uv__socket(domain, SOCK_STREAM, 0);
+ if (err < 0) {
+ return err;
+ }
+ sockfd = err;
+
+ err = uv__stream_open((uv_stream_t *) handle, sockfd, flags);
+ if (err) {
+ uv__close(sockfd);
+ return err;
+ }
+
+ return 0;
+}
+
+//-----------------------------------------------------------------------------
+
+int uv__tcp_bind(uv_tcp_t *tcp, const struct sockaddr *addr, unsigned int addrlen, unsigned int flags)
+{
+ int err;
+ int on;
+
+ /* Cannot set IPv6-only mode on non-IPv6 socket. */
+ if ((flags & UV_TCP_IPV6ONLY) && addr->sa_family != AF_INET6) {
+ return -EINVAL;
+ }
+
+ err = maybe_new_socket(tcp, addr->sa_family, UV_STREAM_READABLE | UV_STREAM_WRITABLE);
+ if (err) {
+ return err;
+ }
+
+ on = 1;
+ if (setsockopt(tcp->io_watcher.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
+ return -get_errno();
+ }
+
+#ifdef IPV6_V6ONLY
+ if (addr->sa_family == AF_INET6) {
+ on = (flags & UV_TCP_IPV6ONLY) != 0;
+ if (setsockopt(tcp->io_watcher.fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof on) == -1) {
+ return -get_errno();
+ }
+ }
+#endif
+
+ set_errno(0);
+ if (bind(tcp->io_watcher.fd, addr, addrlen) && get_errno() != EADDRINUSE) {
+ return -get_errno();
+ }
+ tcp->delayed_error = -get_errno();
+ if (tcp->delayed_error) {
+ TDDLOG("uv__tcp_bind delayed_error(%d)", tcp->delayed_error);
+ }
+
+ if (addr->sa_family == AF_INET6) {
+ tcp->flags |= UV_HANDLE_IPV6;
+ }
+
+ return 0;
+}
+
+int uv__tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, unsigned int addrlen, uv_connect_cb cb)
+{
+ int err;
+ int r;
+
+ assert(handle->type == UV_TCP);
+
+ if (handle->connect_req != NULL) {
+ return -EALREADY; /* FIXME(bnoordhuis) -EINVAL or maybe -EBUSY. */
+ }
+
+ err = maybe_new_socket(handle, addr->sa_family, UV_STREAM_READABLE | UV_STREAM_WRITABLE);
+ if (err) {
+ return err;
+ }
+
+ handle->delayed_error = 0;
+
+ do {
+ set_errno(0);
+ r = connect(uv__stream_fd(handle), addr, addrlen);
+ } while (r == -1 && get_errno() == EINTR);
+
+ if (r == -1) {
+ if (get_errno() == EINPROGRESS) ; /* not an error */
+ else if (get_errno() == ECONNREFUSED)
+ /* If we get a ECONNREFUSED wait until the next tick to report the
+ * error. Solaris wants to report immediately--other unixes want to
+ * wait.
+ */
+ {
+ handle->delayed_error = -get_errno();
+ } else {
+ return -get_errno();
+ }
+ }
+
+ uv__req_init(handle->loop, req, UV_CONNECT);
+ req->cb = cb;
+ req->handle = (uv_stream_t *) handle;
+ QUEUE_INIT(&req->queue);
+ handle->connect_req = req;
+
+ uv__io_start(handle->loop, &handle->io_watcher, UV__POLLOUT);
+
+ if (handle->delayed_error) {
+ uv__io_feed(handle->loop, &handle->io_watcher);
+ }
+
+ return 0;
+}
+
+int uv__tcp_nodelay(int fd, int on)
+{
+ if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on))) {
+ return -get_errno();
+ }
+ return 0;
+}
+
+int uv__tcp_keepalive(int fd, int on, unsigned int delay)
+{
+ if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on))) {
+ return -get_errno();
+ }
+
+#ifdef TCP_KEEPIDLE
+ if (on && setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &delay, sizeof(delay))) {
+ return -get_errno();
+ }
+#endif
+
+ /* Solaris/SmartOS, if you don't support keep-alive,
+ * then don't advertise it in your system headers...
+ */
+ /* FIXME(bnoordhuis) That's possibly because sizeof(delay) should be 1. */
+#if defined(TCP_KEEPALIVE) && !defined(__sun)
+ if (on && setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay))) {
+ return -get_errno();
+ }
+#endif
+
+ return 0;
+}
+
+void uv__tcp_close(uv_tcp_t *handle)
+{
+ uv__stream_close((uv_stream_t *) handle);
+}
+
+//-----------------------------------------------------------------------------
+
+int uv_tcp_init(uv_loop_t *loop, uv_tcp_t *tcp)
+{
+ uv__stream_init(loop, (uv_stream_t *) tcp, UV_TCP);
+ return 0;
+}
+
+int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags)
+{
+ unsigned int addrlen;
+
+ if (handle->type != UV_TCP) {
+ return UV_EINVAL;
+ }
+
+ if (addr->sa_family == AF_INET) {
+ addrlen = sizeof(struct sockaddr_in);
+ }
+ /*
+ else if (addr->sa_family == AF_INET6)
+ addrlen = sizeof(struct sockaddr_in6);
+ */
+ else {
+ return UV_EINVAL;
+ }
+
+ return uv__tcp_bind(handle, addr, addrlen, flags);
+}
+
+int uv_tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, uv_connect_cb cb)
+{
+ unsigned int addrlen;
+
+ if (handle->type != UV_TCP) {
+ return UV_EINVAL;
+ }
+
+ if (addr->sa_family == AF_INET) {
+ addrlen = sizeof(struct sockaddr_in);
+ }
+ /*
+ else if (addr->sa_family == AF_INET6)
+ addrlen = sizeof(struct sockaddr_in6);
+ */
+ else {
+ return UV_EINVAL;
+ }
+
+ return uv__tcp_connect(req, handle, addr, addrlen, cb);
+}
+
+int uv_tcp_open(uv_tcp_t *handle, uv_os_sock_t sock)
+{
+ int err;
+
+ err = uv__nonblock(sock, 1);
+ if (err) {
+ return err;
+ }
+
+ return uv__stream_open((uv_stream_t *) handle, sock, UV_STREAM_READABLE | UV_STREAM_WRITABLE);
+}
+
+int uv_tcp_getsockname(const uv_tcp_t *handle, struct sockaddr *name, int *namelen)
+{
+ socklen_t socklen;
+
+ if (handle->delayed_error) {
+ return handle->delayed_error;
+ }
+
+ if (uv__stream_fd(handle) < 0) {
+ return -EINVAL; /* FIXME(bnoordhuis) -EBADF */
+ }
+
+ /* sizeof(socklen_t) != sizeof(int) on some systems. */
+ socklen = (socklen_t) * namelen;
+
+ if (getsockname(uv__stream_fd(handle), name, &socklen)) {
+ return -get_errno();
+ }
+
+ *namelen = (int)socklen;
+ return 0;
+}
+
+int uv_tcp_getpeername(const uv_tcp_t *handle, struct sockaddr *name, int *namelen)
+{
+ socklen_t socklen;
+
+ if (handle->delayed_error) {
+ return handle->delayed_error;
+ }
+
+ if (uv__stream_fd(handle) < 0) {
+ return -EINVAL; /* FIXME(bnoordhuis) -EBADF */
+ }
+
+ /* sizeof(socklen_t) != sizeof(int) on some systems. */
+ socklen = (socklen_t) * namelen;
+#ifndef CONFIG_LIBTUV
+ if (getpeername(uv__stream_fd(handle), name, &socklen)) {
+ return -get_errno();
+ }
+#endif
+ *namelen = (int)socklen;
+ return 0;
+}
+
+int uv_tcp_listen(uv_tcp_t *tcp, int backlog, uv_connection_cb cb)
+{
+ static int single_accept = -1;
+ int err;
+
+ if (tcp->delayed_error) {
+ return tcp->delayed_error;
+ }
+
+ if (single_accept == -1) {
+ const char *val = getenv("UV_TCP_SINGLE_ACCEPT");
+ single_accept = (val != NULL && atoi(val) != 0); /* Off by default. */
+ }
+
+ if (single_accept) {
+ tcp->flags |= UV_TCP_SINGLE_ACCEPT;
+ }
+
+ err = maybe_new_socket(tcp, AF_INET, UV_STREAM_READABLE);
+ if (err) {
+ return err;
+ }
+
+ if (listen(tcp->io_watcher.fd, backlog)) {
+ return -get_errno();
+ }
+
+ tcp->connection_cb = cb;
+
+ /* Start listening for connections. */
+ tcp->io_watcher.cb = uv__server_io;
+ uv__io_start(tcp->loop, &tcp->io_watcher, UV__POLLIN);
+
+ return 0;
+}
+
+int uv_tcp_nodelay(uv_tcp_t *handle, int on)
+{
+ int err;
+
+ if (uv__stream_fd(handle) != -1) {
+ err = uv__tcp_nodelay(uv__stream_fd(handle), on);
+ if (err) {
+ return err;
+ }
+ }
+
+ if (on) {
+ handle->flags |= UV_TCP_NODELAY;
+ } else {
+ handle->flags &= ~UV_TCP_NODELAY;
+ }
+
+ return 0;
+}
+
+int uv_tcp_keepalive(uv_tcp_t *handle, int on, unsigned int delay)
+{
+ int err;
+
+ if (uv__stream_fd(handle) != -1) {
+ err = uv__tcp_keepalive(uv__stream_fd(handle), on, delay);
+ if (err) {
+ return err;
+ }
+ }
+
+ if (on) {
+ handle->flags |= UV_TCP_KEEPALIVE;
+ } else {
+ handle->flags &= ~UV_TCP_KEEPALIVE;
+ }
+
+ /* TODO Store delay if uv__stream_fd(handle) == -1 but don't want to enlarge
+ * uv_tcp_t with an int that's almost never used...
+ */
+
+ return 0;
+}
+
+int uv_tcp_simultaneous_accepts(uv_tcp_t *handle, int enable)
+{
+ if (enable) {
+ handle->flags &= ~UV_TCP_SINGLE_ACCEPT;
+ } else {
+ handle->flags |= UV_TCP_SINGLE_ACCEPT;
+ }
+ return 0;
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdlib.h> // malloc(), free()
+#include <stdio.h>
+
+#include <uv.h>
+
+//-----------------------------------------------------------------------------
+// thread
+
+struct thread_ctx {
+ void (*entry)(void *arg);
+ void *arg;
+};
+
+static void *uv__thread_start(void *arg)
+{
+ struct thread_ctx *ctx_p;
+ struct thread_ctx ctx;
+
+ ctx_p = (struct thread_ctx *)arg;
+ ctx = *ctx_p;
+ free(ctx_p);
+
+ ctx.entry(ctx.arg);
+
+ return 0;
+}
+
+int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg)
+{
+ struct thread_ctx *ctx;
+ int err;
+
+ ctx = (struct thread_ctx *)malloc(sizeof(*ctx));
+ if (ctx == NULL) {
+ return UV_ENOMEM;
+ }
+
+ ctx->entry = entry;
+ ctx->arg = arg;
+
+ err = pthread_create(tid, NULL, uv__thread_start, ctx);
+
+ if (err) {
+ free(ctx);
+ }
+
+ return err ? -1 : 0;
+}
+
+uv_thread_t uv_thread_self(void)
+{
+ return pthread_self();
+}
+
+int uv_thread_join(uv_thread_t *tid)
+{
+ return -pthread_join(*tid, NULL);
+}
+
+int uv_thread_equal(const uv_thread_t *t1, const uv_thread_t *t2)
+{
+ return pthread_equal(*t1, *t2);
+}
+
+//-----------------------------------------------------------------------------
+// once
+
+void uv_once(uv_once_t *guard, void (*callback)(void))
+{
+ if (pthread_once(guard, callback)) {
+ TDLOG("uv_once abort");
+ ABORT();
+ }
+}
+
+//-----------------------------------------------------------------------------
+// mutex
+
+int uv_mutex_init(uv_mutex_t *mutex)
+{
+#if defined(NDEBUG) || !defined(PTHREAD_MUTEX_ERRORCHECK)
+ return -pthread_mutex_init(mutex, NULL);
+#else
+ pthread_mutexattr_t attr;
+ int err;
+
+ if (pthread_mutexattr_init(&attr)) {
+ TDLOG("uv_mutex_init init abort");
+ ABORT();
+ }
+
+ if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)) {
+ TDLOG("uv_mutex_init settype abort");
+ ABORT();
+ }
+
+ err = pthread_mutex_init(mutex, &attr);
+
+ if (pthread_mutexattr_destroy(&attr)) {
+ TDLOG("uv_mutex_init destroy abort");
+ ABORT();
+ }
+
+ return -err;
+#endif
+}
+
+void uv_mutex_destroy(uv_mutex_t *mutex)
+{
+ if (pthread_mutex_destroy(mutex)) {
+ TDLOG("uv_mutex_destroy abort");
+ ABORT();
+ }
+}
+
+void uv_mutex_lock(uv_mutex_t *mutex)
+{
+ if (pthread_mutex_lock(mutex)) {
+ TDLOG("uv_mutex_lock abort");
+ ABORT();
+ }
+}
+
+int uv_mutex_trylock(uv_mutex_t *mutex)
+{
+ int err;
+
+ /* FIXME(bnoordhuis) EAGAIN means recursive lock limit reached. Arguably
+ * a bug, should probably abort rather than return -EAGAIN.
+ */
+ err = pthread_mutex_trylock(mutex);
+ if (err && err != EBUSY && err != EAGAIN) {
+ TDLOG("uv_mutex_trylock abort");
+ ABORT();
+ }
+
+ return -err;
+}
+
+void uv_mutex_unlock(uv_mutex_t *mutex)
+{
+ if (pthread_mutex_unlock(mutex)) {
+ ABORT();
+ }
+}
+
+//-----------------------------------------------------------------------------
+// semaphore
+
+int uv_sem_init(uv_sem_t *sem, unsigned int value)
+{
+ if (sem_init(sem, 0, value)) {
+ return -errno;
+ }
+ return 0;
+}
+
+void uv_sem_destroy(uv_sem_t *sem)
+{
+ if (sem_destroy(sem)) {
+ TDLOG("uv_sem_destroy abort");
+ ABORT();
+ }
+}
+
+void uv_sem_post(uv_sem_t *sem)
+{
+ if (sem_post(sem)) {
+ TDLOG("uv_sem_post abort");
+ ABORT();
+ }
+}
+
+void uv_sem_wait(uv_sem_t *sem)
+{
+ int r;
+
+ do {
+ r = sem_wait(sem);
+ } while (r == -1 && errno == EINTR);
+
+ if (r) {
+ TDLOG("uv_sem_wait abort");
+ ABORT();
+ }
+}
+
+int uv_sem_trywait(uv_sem_t *sem)
+{
+ int r;
+
+ do {
+ r = sem_trywait(sem);
+ } while (r == -1 && errno == EINTR);
+
+ if (r) {
+ if (errno == EAGAIN) {
+ return -EAGAIN;
+ }
+ ABORT();
+ }
+
+ return 0;
+}
+
+//-----------------------------------------------------------------------------
+// condition
+
+int uv_cond_init(uv_cond_t *cond)
+{
+ pthread_condattr_t attr;
+ int err;
+
+ err = pthread_condattr_init(&attr);
+ if (err) {
+ return -err;
+ }
+
+#if !(defined(__ANDROID__) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)) && !defined(__TINYARA__)
+ err = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+ if (err) {
+ goto error2;
+ }
+#endif
+
+ err = pthread_cond_init(cond, &attr);
+ if (err) {
+ goto error2;
+ }
+
+ err = pthread_condattr_destroy(&attr);
+ if (err) {
+ goto error;
+ }
+
+ return 0;
+
+error:
+ pthread_cond_destroy(cond);
+error2:
+ pthread_condattr_destroy(&attr);
+ return -err;
+}
+
+void uv_cond_destroy(uv_cond_t *cond)
+{
+ if (pthread_cond_destroy(cond)) {
+ TDLOG("uv_cond_destroy abort");
+ ABORT();
+ }
+}
+
+void uv_cond_signal(uv_cond_t *cond)
+{
+ if (pthread_cond_signal(cond)) {
+ TDLOG("uv_cond_signal abort");
+ ABORT();
+ }
+}
+
+void uv_cond_broadcast(uv_cond_t *cond)
+{
+ if (pthread_cond_broadcast(cond)) {
+ TDLOG("uv_cond_broadcast abort");
+ ABORT();
+ }
+}
+
+void uv_cond_wait(uv_cond_t *cond, uv_mutex_t *mutex)
+{
+ if (pthread_cond_wait(cond, mutex)) {
+ TDLOG("uv_cond_wait abort");
+ ABORT();
+ }
+}
+
+#undef NANOSEC
+#define NANOSEC ((uint64_t) 1e9)
+
+int uv_cond_timedwait(uv_cond_t *cond, uv_mutex_t *mutex, uint64_t timeout)
+{
+ int r;
+ struct timespec ts;
+
+ timeout += uv__time_precise();
+ ts.tv_sec = timeout / NANOSEC;
+ ts.tv_nsec = timeout % NANOSEC;
+ r = pthread_cond_timedwait(cond, mutex, &ts);
+
+ if (r == 0) {
+ return 0;
+ }
+
+ if (r == ETIMEDOUT) {
+ return -ETIMEDOUT;
+ }
+
+ TDLOG("uv_cond_timedwait abort");
+ ABORT();
+ return -EINVAL; /* Satisfy the compiler. */
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdlib.h>
+#include <unistd.h>
+#include <assert.h>
+
+#include <uv.h>
+
+//-----------------------------------------------------------------------------
+//
+
+static void uv__async_event(uv_loop_t *loop, struct uv__async *w, unsigned int nevents)
+{
+ QUEUE *q;
+ uv_async_t *h;
+
+ QUEUE_FOREACH(q, &loop->async_handles) {
+ h = QUEUE_DATA(q, uv_async_t, queue);
+
+ if (h->pending == 0) {
+ continue;
+ }
+ h->pending = 0;
+
+ if (h->async_cb == NULL) {
+ continue;
+ }
+ h->async_cb(h);
+ }
+}
+
+//-----------------------------------------------------------------------------
+//
+
+int uv_async_init(uv_loop_t *loop, uv_async_t *handle, uv_async_cb async_cb)
+{
+ int err;
+
+ uv__async_platform_init(handle);
+ uv__handle_init(loop, (uv_handle_t *) handle, UV_ASYNC);
+ handle->async_cb = async_cb;
+ handle->pending = 0;
+
+ QUEUE_INSERT_TAIL(&loop->async_handles, &handle->queue);
+ uv__handle_start(handle);
+
+ err = uv__async_start(loop, &loop->async_watcher, uv__async_event);
+ if (err) {
+ return err;
+ }
+
+ return 0;
+}
+
+int uv_async_send(uv_async_t *handle)
+{
+ if (uv__async_make_pending(&handle->pending) == 0) {
+ uv__async_send(&handle->loop->async_watcher);
+ }
+
+ return 0;
+}
+
+int uv_async_deinit(uv_loop_t *loop, uv_async_t *handle)
+{
+ uv__async_stop(loop, &loop->async_watcher);
+ uv__handle_stop(handle);
+ QUEUE_REMOVE(&handle->queue);
+ QUEUE_INIT(&handle->queue);
+ uv__handle_deinit(handle);
+
+ return 0;
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <uv.h>
+
+#include <string.h>
+#include <unistd.h>
+
+int uv_cwd(char *buffer, size_t *size)
+{
+ if (buffer == NULL || size == NULL) {
+ return -EINVAL;
+ }
+
+ if (getcwd(buffer, *size) == NULL) {
+ return -errno;
+ }
+
+ *size = strlen(buffer);
+
+ if (*size > 1 && buffer[*size - 1] == '/') {
+ buffer[*size - 1] = '\0';
+ (*size)--;
+ }
+
+ return 0;
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <uv.h>
+
+#include <assert.h>
+
+#define UV_ERR_NAME_GEN(name, _) case UV_ ## name: return #name;
+const char *uv_err_name(int err)
+{
+ switch (err) {
+ UV_ERRNO_MAP(UV_ERR_NAME_GEN)
+ default:
+ assert(0);
+ return NULL;
+ }
+}
+
+#undef UV_ERR_NAME_GEN
+
+#define UV_STRERROR_GEN(name, msg) case UV_ ## name: return msg;
+const char *uv_strerror(int err)
+{
+ switch (err) {
+ UV_ERRNO_MAP(UV_STRERROR_GEN)
+ default:
+ return "Unknown error";
+ }
+}
+
+#undef UV_STRERROR_GEN
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdlib.h>
+
+#include <uv.h>
+
+void uv__fs_scandir_cleanup(uv_fs_t *req)
+{
+ uv__dirent_t **dents;
+
+ dents = (uv__dirent_t **) req->ptr;
+ if (req->nbufs > 0 && req->nbufs != (unsigned int)req->result) {
+ req->nbufs--;
+ }
+ for (; req->nbufs < (unsigned int)req->result; req->nbufs++) {
+ free(dents[req->nbufs]);
+ }
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <string.h>
+
+#include <uv.h>
+#include "uv_internal.h"
+
+//-----------------------------------------------------------------------------
+
+void uv__make_close_pending(uv_handle_t *handle)
+{
+ assert(handle->flags & UV_CLOSING);
+ assert(!(handle->flags & UV_CLOSED));
+ handle->next_closing = handle->loop->closing_handles;
+ handle->loop->closing_handles = handle;
+}
+
+//-----------------------------------------------------------------------------
+
+void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
+{
+ assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
+
+ handle->flags |= UV_CLOSING;
+ handle->close_cb = close_cb;
+
+ switch (handle->type) {
+ case UV_TTY:
+ uv__stream_close((uv_stream_t *) handle);
+ break;
+
+ case UV_TCP:
+ uv__tcp_close((uv_tcp_t *) handle);
+ break;
+
+ case UV_IDLE:
+ uv__idle_close((uv_idle_t *) handle);
+ break;
+
+ case UV_ASYNC:
+ uv__async_close((uv_async_t *) handle);
+ break;
+
+ case UV_TIMER:
+ uv__timer_close((uv_timer_t *) handle);
+ break;
+
+ case UV_POLL:
+ uv__poll_close((uv_poll_t *) handle);
+
+ default:
+ assert(0);
+ }
+
+ uv__make_close_pending(handle);
+}
+
+//-----------------------------------------------------------------------------
+
+int uv_is_closing(const uv_handle_t *handle)
+{
+ return uv__is_closing(handle);
+}
+
+int uv_is_active(const uv_handle_t *handle)
+{
+ return uv__is_active(handle);
+}
+
+void uv_ref(uv_handle_t *handle)
+{
+ uv__handle_ref(handle);
+}
+
+void uv_unref(uv_handle_t *handle)
+{
+ uv__handle_unref(handle);
+}
+
+//-----------------------------------------------------------------------------
+
+void uv_walk(uv_loop_t *loop, uv_walk_cb walk_cb, void *arg)
+{
+ QUEUE *q;
+ uv_handle_t *h;
+
+ QUEUE_FOREACH(q, &loop->handles_queue) {
+ h = QUEUE_DATA(q, uv_handle_t, handle_queue);
+ if (h->flags & UV__HANDLE_INTERNAL) {
+ continue;
+ }
+ walk_cb(h, arg);
+ }
+}
+
+//-----------------------------------------------------------------------------
+
+void uv_deinit(uv_loop_t *loop, uv_handle_t *handle)
+{
+ QUEUE *q;
+ uv_handle_t *h;
+
+ QUEUE_FOREACH(q, &loop->handles_queue) {
+ h = QUEUE_DATA(q, uv_handle_t, handle_queue);
+ if (h == handle) {
+ uv__handle_deinit(handle);
+ break;
+ }
+ }
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <errno.h>
+#include <assert.h>
+
+#include <uv.h>
+
+int uv_idle_init(uv_loop_t *loop, uv_idle_t *handle)
+{
+ uv__idle_platform_init(handle);
+ uv__handle_init(loop, (uv_handle_t *) handle, UV_IDLE);
+
+ return 0;
+}
+
+int uv_idle_start(uv_idle_t *handle, uv_idle_cb cb)
+{
+ if (uv__is_active(handle)) {
+ TDDLOG("uv_idle_start, handle(%p) is already active", handle);
+ return 0;
+ }
+ if (cb == NULL) {
+ return -EINVAL;
+ }
+ QUEUE_INSERT_HEAD(&handle->loop->idle_handles, &handle->queue);
+ handle->idle_cb = cb;
+ uv__handle_start(handle);
+
+ return 0;
+}
+
+int uv_idle_stop(uv_idle_t *handle)
+{
+ if (!uv__is_active(handle)) {
+ TDDLOG("uv_idle_stop, handle(%p) is not active, cancel", handle);
+ return 0;
+ }
+ QUEUE_REMOVE(&handle->queue);
+ QUEUE_INIT(&handle->queue);
+ uv__handle_stop(handle);
+
+ return 0;
+}
+
+//-----------------------------------------------------------------------------
+
+void uv__run_idle(uv_loop_t *loop)
+{
+ uv_idle_t *h;
+ QUEUE *q;
+
+ QUEUE_FOREACH(q, &loop->idle_handles) {
+ h = QUEUE_DATA(q, uv_idle_t, queue);
+ if (h) {
+ h->idle_cb(h);
+ }
+ }
+}
+
+void uv__idle_close(uv_idle_t *handle)
+{
+ uv_idle_stop(handle);
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <string.h>
+#include <stdio.h>
+
+#include <uv.h>
+
+#define UV__INET_ADDRSTRLEN 16
+
+//-----------------------------------------------------------------------------
+
+static int inet_ntop4(const unsigned char *src, char *dst, size_t size)
+{
+ static const char fmt[] = "%u.%u.%u.%u";
+ char tmp[UV__INET_ADDRSTRLEN];
+ int l;
+
+ l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
+ if (l <= 0 || (size_t) l >= size) {
+ return UV_ENOSPC;
+ }
+ strncpy(dst, tmp, size);
+ dst[size - 1] = '\0';
+ return 0;
+}
+
+static int inet_pton4(const char *src, unsigned char *dst)
+{
+ static const char digits[] = "0123456789";
+ int saw_digit, octets, ch;
+ unsigned char tmp[sizeof(struct in_addr)], *tp;
+
+ saw_digit = 0;
+ octets = 0;
+ *(tp = tmp) = 0;
+ while ((ch = *src++) != '\0') {
+ const char *pch;
+
+ if ((pch = strchr(digits, ch)) != NULL) {
+ unsigned int nw = *tp * 10 + (pch - digits);
+
+ if (saw_digit && *tp == 0) {
+ return UV_EINVAL;
+ }
+ if (nw > 255) {
+ return UV_EINVAL;
+ }
+ *tp = nw;
+ if (!saw_digit) {
+ if (++octets > 4) {
+ return UV_EINVAL;
+ }
+ saw_digit = 1;
+ }
+ } else if (ch == '.' && saw_digit) {
+ if (octets == 4) {
+ return UV_EINVAL;
+ }
+ *++tp = 0;
+ saw_digit = 0;
+ } else {
+ return UV_EINVAL;
+ }
+ }
+ if (octets < 4) {
+ return UV_EINVAL;
+ }
+ memcpy(dst, tmp, sizeof(struct in_addr));
+ return 0;
+}
+
+//-----------------------------------------------------------------------------
+
+int uv_inet_ntop(int af, const void *src, char *dst, size_t size)
+{
+ switch (af) {
+ case AF_INET:
+ return (inet_ntop4((const unsigned char *)src, dst, size));
+ /*
+ case AF_INET6:
+ return (inet_ntop6(src, dst, size));
+ */
+ default:
+ return UV_EAFNOSUPPORT;
+ }
+ /* NOTREACHED */
+}
+
+int uv_inet_pton(int af, const char *src, void *dst)
+{
+ if (src == NULL || dst == NULL) {
+ return UV_EINVAL;
+ }
+
+ switch (af) {
+ case AF_INET:
+ return (inet_pton4(src, (unsigned char *)dst));
+ /*
+ case AF_INET6: {
+ int len;
+ char tmp[UV__INET6_ADDRSTRLEN], *s, *p;
+ s = (char*) src;
+ p = strchr(src, '%');
+ if (p != NULL) {
+ s = tmp;
+ len = p - src;
+ if (len > UV__INET6_ADDRSTRLEN-1)
+ return UV_EINVAL;
+ memcpy(s, src, len);
+ s[len] = '\0';
+ }
+ return inet_pton6(s, dst);
+ }
+ */
+ default:
+ return UV_EAFNOSUPPORT;
+ }
+ /* NOTREACHED */
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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__internal_header__
+#define __uv__internal_header__
+
+//-----------------------------------------------------------------------------
+// common internal functions
+
+// in uv_idle.cpp
+void uv__run_idle(uv_loop_t *loop);
+void uv__idle_close(uv_idle_t *handle);
+
+// in <platform>/uv_timer.cpp
+void uv__run_timers(uv_loop_t *loop);
+void uv__timer_close(uv_timer_t *handle);
+int uv__next_timeout(const uv_loop_t *loop);
+
+// in uv_poll.c
+void uv__poll_close(uv_poll_t *handle);
+
+// in uv_run.cpp
+void uv__make_close_pending(uv_handle_t *handle);
+
+// in uv_threadpool.cpp
+void uv__work_submit(uv_loop_t *loop, struct uv__work *w, void (*work)(struct uv__work *w), void (*done)(struct uv__work *w, int status));
+
+// in uv_fs.cpp
+void uv__fs_scandir_cleanup(uv_fs_t *req);
+
+#endif // __uv__internal_header__
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include <uv.h>
+#include "heap-inl.h"
+
+static uv_loop_t default_loop_struct;
+static uv_loop_t *default_loop_ptr = NULL;
+
+int uv_loop_init(uv_loop_t *loop)
+{
+ int err;
+
+ memset(loop, 0, sizeof(uv_loop_t));
+ heap_init((struct heap *)&loop->timer_heap);
+
+ QUEUE_INIT(&loop->wq);
+ QUEUE_INIT(&loop->active_reqs);
+ QUEUE_INIT(&loop->idle_handles);
+ QUEUE_INIT(&loop->async_handles);
+ QUEUE_INIT(&loop->handles_queue);
+
+ loop->nfds = 0;
+ loop->watchers = NULL;
+ loop->nwatchers = 0;
+ QUEUE_INIT(&loop->pending_queue);
+ QUEUE_INIT(&loop->watcher_queue);
+
+ loop->closing_handles = NULL;
+ uv__update_time(loop);
+ uv__async_init(&loop->async_watcher);
+ loop->backend_fd = -1;
+ loop->emfile_fd = -1;
+
+ loop->timer_counter = 0;
+ loop->stop_flag = 0;
+
+ err = uv__platform_loop_init(loop);
+ if (err) {
+ return err;
+ }
+
+ if (uv_rwlock_init(&loop->cloexec_lock)) {
+ TDLOG("uv_loop_init rwlock abort");
+ ABORT();
+ }
+
+ if (uv_mutex_init(&loop->wq_mutex)) {
+ TDLOG("uv_loop_init mutex abort");
+ ABORT();
+ }
+
+ if (uv_async_init(loop, &loop->wq_async, uv__work_done)) {
+ TDLOG("uv_loop_init async abort");
+ ABORT();
+ }
+
+ uv__handle_unref(&loop->wq_async);
+ loop->wq_async.flags |= UV__HANDLE_INTERNAL;
+
+ // add update timer here for slow systems
+ uv__update_time(loop);
+
+ return 0;
+}
+
+void uv__loop_close(uv_loop_t *loop)
+{
+ uv__platform_loop_delete(loop);
+ uv__async_stop(loop, &loop->async_watcher);
+
+ if (loop->emfile_fd != -1) {
+ uv__close(loop->emfile_fd);
+ loop->emfile_fd = -1;
+ }
+
+ if (loop->backend_fd != -1) {
+ uv__close(loop->backend_fd);
+ loop->backend_fd = -1;
+ }
+
+ uv_mutex_lock(&loop->wq_mutex);
+ assert(QUEUE_EMPTY(&loop->wq) && "thread pool work queue not empty!");
+ assert(!uv__has_active_reqs(loop));
+ uv_mutex_unlock(&loop->wq_mutex);
+ uv_mutex_destroy(&loop->wq_mutex);
+
+ /*
+ * Note that all thread pool stuff is finished at this point and
+ * it is safe to just destroy rw lock
+ */
+ uv_rwlock_destroy(&loop->cloexec_lock);
+
+ free(loop->watchers);
+ loop->watchers = NULL;
+ loop->nwatchers = 0;
+}
+
+uv_loop_t *uv_default_loop(void)
+{
+ if (default_loop_ptr != NULL) {
+ return default_loop_ptr;
+ }
+
+ if (uv_loop_init(&default_loop_struct)) {
+ return NULL;
+ }
+
+ default_loop_ptr = &default_loop_struct;
+ return default_loop_ptr;
+}
+
+int uv_loop_close(uv_loop_t *loop)
+{
+ QUEUE *q;
+ uv_handle_t *h;
+
+ if (!QUEUE_EMPTY(&(loop)->active_reqs)) {
+ TDDLOG("uv_loop_close active_req exist, cancel");
+ return UV_EBUSY;
+ }
+
+ uv_async_deinit(loop, &loop->wq_async);
+
+ QUEUE_FOREACH(q, &loop->handles_queue) {
+ h = QUEUE_DATA(q, uv_handle_t, handle_queue);
+ if (!(h->flags & UV__HANDLE_INTERNAL)) {
+ TDDLOG("uv_loop_close handle(%p) exist, cancel", h);
+ return UV_EBUSY;
+ }
+ }
+
+ uv__loop_close(loop);
+
+ if (loop == default_loop_ptr) {
+ default_loop_ptr = NULL;
+ }
+
+ return 0;
+}
+
+void uv_update_time(uv_loop_t *loop)
+{
+ uv__update_time(loop);
+}
+
+uint64_t uv_now(const uv_loop_t *loop)
+{
+ return loop->time;
+}
+
+int uv__loop_alive(const uv_loop_t *loop)
+{
+ return uv__has_active_handles(loop) || uv__has_active_reqs(loop) || loop->closing_handles != NULL;
+}
+
+int uv_loop_alive(const uv_loop_t *loop)
+{
+ return uv__loop_alive(loop);
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <errno.h>
+#include <assert.h>
+
+#include <uv.h>
+
+static void uv__poll_io(uv_loop_t *loop, uv__io_t *w, unsigned int events)
+{
+ uv_poll_t *handle;
+ int pevents;
+
+ handle = container_of(w, uv_poll_t, io_watcher);
+
+ if (events & POLLERR) {
+ uv__io_stop(loop, w, POLLIN | POLLOUT | UV__POLLRDHUP);
+ uv__handle_stop(handle);
+ handle->poll_cb(handle, -EBADF, 0);
+ return;
+ }
+
+ pevents = 0;
+ if (events & POLLIN) {
+ pevents |= UV_READABLE;
+ }
+ if (events & POLLOUT) {
+ pevents |= UV_WRITABLE;
+ }
+ if (events & UV__POLLRDHUP) {
+ pevents |= UV_DISCONNECT;
+ }
+
+ handle->poll_cb(handle, 0, pevents);
+}
+
+int uv_poll_init(uv_loop_t *loop, uv_poll_t *handle, int fd)
+{
+ int err;
+
+ // err = uv__io_check_fd(loop, fd);
+ // if (err)
+ // return err;
+
+ /* If ioctl(FIONBIO) reports ENOTTY, try fcntl(F_GETFL) + fcntl(F_SETFL).
+ * Workaround for e.g. kqueue fds not supporting ioctls.
+ */
+ err = uv__nonblock(fd, 1);
+ // if (err == -ENOTTY)
+ // if (uv__nonblock == uv__nonblock_ioctl)
+ // err = uv__nonblock_fcntl(fd, 1);
+
+ if (err) {
+ return err;
+ }
+
+ uv__handle_init(loop, (uv_handle_t *) handle, UV_POLL);
+ uv__io_init(&handle->io_watcher, uv__poll_io, fd);
+ handle->poll_cb = NULL;
+ return 0;
+}
+
+int uv_poll_init_socket(uv_loop_t *loop, uv_poll_t *handle, uv_os_sock_t socket)
+{
+ return uv_poll_init(loop, handle, socket);
+}
+
+static void uv__poll_stop(uv_poll_t *handle)
+{
+ uv__io_stop(handle->loop, &handle->io_watcher, POLLIN | POLLOUT | UV__POLLRDHUP);
+ uv__handle_stop(handle);
+}
+
+int uv_poll_stop(uv_poll_t *handle)
+{
+ assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
+ uv__poll_stop(handle);
+ return 0;
+}
+
+int uv_poll_start(uv_poll_t *handle, int pevents, uv_poll_cb poll_cb)
+{
+ int events;
+
+ assert((pevents & ~(UV_READABLE | UV_WRITABLE | UV_DISCONNECT)) == 0);
+ assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
+
+ uv__poll_stop(handle);
+
+ if (pevents == 0) {
+ return 0;
+ }
+
+ events = 0;
+ if (pevents & UV_READABLE) {
+ events |= POLLIN;
+ }
+ if (pevents & UV_WRITABLE) {
+ events |= POLLOUT;
+ }
+ if (pevents & UV_DISCONNECT) {
+ events |= UV__POLLRDHUP;
+ }
+
+ uv__io_start(handle->loop, &handle->io_watcher, events);
+ uv__handle_start(handle);
+ handle->poll_cb = poll_cb;
+
+ return 0;
+}
+
+void uv__poll_close(uv_poll_t *handle)
+{
+ uv__poll_stop(handle);
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdlib.h>
+
+#include <uv.h>
+#include "uv_internal.h"
+
+void uv__req_init_(uv_loop_t *loop, uv_req_t *req, uv_req_type type)
+{
+ req->type = type;
+ uv__req_register(loop, req);
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdio.h>
+#include <assert.h>
+
+#include <uv.h>
+#include "uv_internal.h"
+
+static void uv__finish_close(uv_handle_t *handle)
+{
+ /* Note: while the handle is in the UV_CLOSING state now, it's still possible
+ * for it to be active in the sense that uv__is_active() returns true.
+ * A good example is when the user calls uv_shutdown(), immediately followed
+ * by uv_close(). The handle is considered active at this point because the
+ * completion of the shutdown req is still pending.
+ */
+ assert(handle->flags & UV_CLOSING);
+ assert(!(handle->flags & UV_CLOSED));
+ handle->flags |= UV_CLOSED;
+
+ switch (handle->type) {
+ case UV_IDLE:
+ case UV_ASYNC:
+ case UV_TIMER:
+ case UV_POLL:
+ break;
+
+ case UV_NAMED_PIPE:
+ case UV_TCP:
+ case UV_TTY:
+ uv__stream_destroy((uv_stream_t *) handle);
+ break;
+
+ default:
+ assert(0);
+ break;
+ }
+
+ uv__handle_unref(handle);
+ QUEUE_REMOVE(&handle->handle_queue);
+ QUEUE_INIT(&handle->handle_queue);
+
+ if (handle->close_cb) {
+ handle->close_cb(handle);
+ }
+}
+
+static void uv__run_closing_handles(uv_loop_t *loop)
+{
+ uv_handle_t *p;
+ uv_handle_t *q;
+
+ p = loop->closing_handles;
+ loop->closing_handles = NULL;
+
+ while (p) {
+ q = p->next_closing;
+ uv__finish_close(p);
+ p = q;
+ }
+}
+
+static int uv__run_pending(uv_loop_t *loop)
+{
+ QUEUE *q;
+ QUEUE pq;
+ uv__io_t *w;
+
+ if (QUEUE_EMPTY(&loop->pending_queue)) {
+ return 0;
+ }
+
+ QUEUE_INIT(&pq);
+ q = QUEUE_HEAD(&loop->pending_queue);
+ QUEUE_SPLIT(&loop->pending_queue, q, &pq);
+
+ while (!QUEUE_EMPTY(&pq)) {
+ q = QUEUE_HEAD(&pq);
+ QUEUE_REMOVE(q);
+ QUEUE_INIT(q);
+ w = QUEUE_DATA(q, uv__io_t, pending_queue);
+ w->cb(loop, w, UV__POLLOUT);
+ }
+
+ return 1;
+}
+
+//-----------------------------------------------------------------------------
+
+int uv_backend_timeout(const uv_loop_t *loop)
+{
+ if (loop->stop_flag != 0) {
+ return 0;
+ }
+
+ if (!uv__has_active_handles(loop) && !uv__has_active_reqs(loop)) {
+ return 0;
+ }
+
+ if (!QUEUE_EMPTY(&loop->idle_handles)) {
+ return 0;
+ }
+
+ if (loop->closing_handles) {
+ return 0;
+ }
+
+ return uv__next_timeout(loop);
+}
+
+int uv_run(uv_loop_t *loop, uv_run_mode mode)
+{
+ int timeout;
+ int r;
+ int ran_pending;
+
+ r = uv__loop_alive(loop);
+ if (!r) {
+ uv__update_time(loop);
+ }
+
+ while (r != 0 && loop->stop_flag == 0) {
+ uv__update_time(loop);
+ uv__run_timers(loop);
+ ran_pending = uv__run_pending(loop);
+ uv__run_idle(loop);
+
+ timeout = 0;
+ if ((mode == UV_RUN_ONCE && !ran_pending) || mode == UV_RUN_DEFAULT) {
+ timeout = uv_backend_timeout(loop);
+ }
+
+ uv__io_poll(loop, timeout);
+ uv__run_closing_handles(loop);
+
+ if (mode == UV_RUN_ONCE) {
+ // read libuv/src/core.c comments
+ uv__update_time(loop);
+ uv__run_timers(loop);
+ }
+
+ r = uv__loop_alive(loop);
+ if (mode == UV_RUN_ONCE || mode == UV_RUN_NOWAIT) {
+ break;
+ }
+ }
+ uv__run_closing_handles(loop);
+
+ if (loop->stop_flag != 0) {
+ loop->stop_flag = 0;
+ }
+
+ return r;
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdlib.h>
+#include <stdio.h>
+
+#include <uv.h>
+
+//-----------------------------------------------------------------------------
+#define MAX_THREADPOOL_SIZE 4
+
+static uv_once_t _once = UV_ONCE_INIT;
+static uv_cond_t _cond;
+static uv_mutex_t _mutex;
+static unsigned int _nthreads;
+static uv_thread_t *_threads;
+static uv_thread_t _default_threads[2];
+static QUEUE _exit_message;
+static QUEUE _wq;
+static volatile int _initialized = 0;
+
+//-----------------------------------------------------------------------------
+
+static void uv__cancelled(struct uv__work *w)
+{
+ TDLOG("uv__cancelled async");
+ ABORT();
+}
+
+/* To avoid deadlock with uv_cancel() it's crucial that the worker
+ * never holds the global _mutex and the loop-local mutex at the same time.
+ */
+static void worker(void *arg)
+{
+ struct uv__work *w;
+ QUEUE *q;
+
+ (void)arg;
+
+ for (;;) {
+ uv_mutex_lock(&_mutex);
+
+ while (QUEUE_EMPTY(&_wq)) {
+ uv_cond_wait(&_cond, &_mutex);
+ }
+
+ q = QUEUE_HEAD(&_wq);
+
+ if (q == &_exit_message) {
+ uv_cond_signal(&_cond);
+ } else {
+ QUEUE_REMOVE(q);
+ QUEUE_INIT(q); /* Signal uv_cancel() that the work req is
+ executing. */
+ }
+
+ uv_mutex_unlock(&_mutex);
+
+ if (q == &_exit_message) {
+ break;
+ }
+
+ w = QUEUE_DATA(q, struct uv__work, wq);
+ w->work(w);
+
+ uv_mutex_lock(&w->loop->wq_mutex);
+ w->work = NULL; /* Signal uv_cancel() that the work req is done
+ executing. */
+ QUEUE_INSERT_TAIL(&w->loop->wq, &w->wq);
+ uv_async_send(&w->loop->wq_async);
+ uv_mutex_unlock(&w->loop->wq_mutex);
+ }
+}
+
+static void post(QUEUE *q)
+{
+ uv_mutex_lock(&_mutex);
+ QUEUE_INSERT_TAIL(&_wq, q);
+ uv_mutex_unlock(&_mutex);
+
+ uv_cond_signal(&_cond);
+}
+
+#if defined(__TINYARA__)
+static void cleanup(void)
+{
+#else
+__attribute__((destructor))
+static void cleanup(void)
+{
+#endif
+ unsigned int i;
+
+ if (_initialized == 0) {
+ return;
+ }
+
+ post(&_exit_message);
+
+ for (i = 0; i < _nthreads; i++)
+ if (uv_thread_join(_threads + i)) {
+ ABORT();
+ }
+
+ if (_threads != _default_threads) {
+ free(_threads);
+ }
+
+ uv_mutex_destroy(&_mutex);
+ uv_cond_destroy(&_cond);
+
+ _threads = NULL;
+ _nthreads = 0;
+ _initialized = 0;
+ _once = UV_ONCE_INIT;
+}
+
+static void init_once(void)
+{
+ unsigned int i;
+ const char *val;
+
+ assert(_initialized == 0);
+
+ _nthreads = ARRAY_SIZE(_default_threads);
+ val = getenv("UV_THREADPOOL_SIZE");
+ if (val != NULL) {
+ _nthreads = atoi(val);
+ }
+ if (_nthreads == 0) {
+ _nthreads = 1;
+ }
+ if (_nthreads > MAX_THREADPOOL_SIZE) {
+ _nthreads = MAX_THREADPOOL_SIZE;
+ }
+
+ _threads = _default_threads;
+ if (_nthreads > ARRAY_SIZE(_default_threads)) {
+ _threads = (uv_thread_t *)malloc(_nthreads * sizeof(_threads[0]));
+ if (_threads == NULL) {
+ _nthreads = ARRAY_SIZE(_default_threads);
+ _threads = _default_threads;
+ }
+ }
+
+ if (uv_cond_init(&_cond)) {
+ TDLOG("init_once cond abort");
+ ABORT();
+ }
+
+ if (uv_mutex_init(&_mutex)) {
+ TDLOG("init_once mutex abort");
+ ABORT();
+ }
+
+ QUEUE_INIT(&_wq);
+
+ for (i = 0; i < _nthreads; i++) {
+ if (uv_thread_create(_threads + i, worker, NULL)) {
+ TDLOG("init_once thread %d abort", i);
+ ABORT();
+ }
+ }
+
+ _initialized = 1;
+}
+
+//-----------------------------------------------------------------------------
+
+void uv__work_submit(uv_loop_t *loop, struct uv__work *w, void (*work)(struct uv__work *w), void (*done)(struct uv__work *w, int status))
+{
+
+ uv_once(&_once, init_once);
+ w->loop = loop;
+ w->work = work;
+ w->done = done;
+ QUEUE_INIT(&w->wq);
+ post(&w->wq);
+}
+
+static int uv__work_cancel(uv_loop_t *loop, uv_req_t *req, struct uv__work *w)
+{
+ int cancelled;
+
+ uv_mutex_lock(&_mutex);
+ uv_mutex_lock(&w->loop->wq_mutex);
+
+ cancelled = !QUEUE_EMPTY(&w->wq) && w->work != NULL;
+ if (cancelled) {
+ QUEUE_REMOVE(&w->wq);
+ }
+
+ uv_mutex_unlock(&w->loop->wq_mutex);
+ uv_mutex_unlock(&_mutex);
+
+ if (!cancelled) {
+ return UV_EBUSY;
+ }
+
+ w->work = uv__cancelled;
+ uv_mutex_lock(&loop->wq_mutex);
+ QUEUE_INSERT_TAIL(&loop->wq, &w->wq);
+ uv_async_send(&loop->wq_async);
+ uv_mutex_unlock(&loop->wq_mutex);
+
+ return 0;
+}
+
+void uv__work_done(uv_async_t *handle)
+{
+ struct uv__work *w;
+ uv_loop_t *loop;
+ QUEUE *q;
+ QUEUE wq;
+ int err;
+
+ loop = container_of(handle, uv_loop_t, wq_async);
+ QUEUE_INIT(&wq);
+
+ uv_mutex_lock(&loop->wq_mutex);
+ if (!QUEUE_EMPTY(&loop->wq)) {
+ q = QUEUE_HEAD(&loop->wq);
+ QUEUE_SPLIT(&loop->wq, q, &wq);
+ }
+ uv_mutex_unlock(&loop->wq_mutex);
+
+ while (!QUEUE_EMPTY(&wq)) {
+ q = QUEUE_HEAD(&wq);
+ QUEUE_REMOVE(q);
+
+ w = container_of(q, struct uv__work, wq);
+ err = (w->work == uv__cancelled) ? UV_ECANCELED : 0;
+ w->done(w, err);
+ }
+}
+
+//-----------------------------------------------------------------------------
+
+static void uv__queue_work(struct uv__work *w)
+{
+ uv_work_t *req = container_of(w, uv_work_t, work_req);
+
+ req->work_cb(req);
+}
+
+static void uv__queue_done(struct uv__work *w, int err)
+{
+ uv_work_t *req;
+
+ req = container_of(w, uv_work_t, work_req);
+ uv__req_unregister(req->loop, req);
+
+ if (req->after_work_cb == NULL) {
+ return;
+ }
+
+ req->after_work_cb(req, err);
+}
+
+int uv_queue_work(uv_loop_t *loop, uv_work_t *req, uv_work_cb work_cb, uv_after_work_cb after_work_cb)
+{
+ if (work_cb == NULL) {
+ return UV_EINVAL;
+ }
+
+ uv__req_init(loop, req, UV_WORK);
+ req->loop = loop;
+ req->work_cb = work_cb;
+ req->after_work_cb = after_work_cb;
+ uv__work_submit(loop, &req->work_req, uv__queue_work, uv__queue_done);
+ return 0;
+}
+
+//-----------------------------------------------------------------------------
+
+int uv_cancel(uv_req_t *req)
+{
+ struct uv__work *wreq;
+ uv_loop_t *loop;
+
+ switch (req->type) {
+ case UV_FS:
+ loop = ((uv_fs_t *) req)->loop;
+ wreq = &((uv_fs_t *) req)->work_req;
+ break;
+ case UV_GETADDRINFO:
+ loop = ((uv_getaddrinfo_t *) req)->loop;
+ wreq = &((uv_getaddrinfo_t *) req)->work_req;
+ break;
+ /*
+ case UV_GETNAMEINFO:
+ loop = ((uv_getnameinfo_t*) req)->loop;
+ wreq = &((uv_getnameinfo_t*) req)->work_req;
+ break;
+ */
+ case UV_WORK:
+ loop = ((uv_work_t *) req)->loop;
+ wreq = &((uv_work_t *) req)->work_req;
+ break;
+ default:
+ return UV_EINVAL;
+ }
+
+ return uv__work_cancel(loop, req, wreq);
+}
+
+//-----------------------------------------------------------------------------
+#if defined(__TINYARA__)
+void uv_cleanup(void)
+{
+ cleanup();
+}
+#endif
--- /dev/null
+/* 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.
+ */
+
+/* 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 <stdio.h>
+#include <errno.h>
+#include <assert.h>
+#include <limits.h> // INT_MAX
+#include <string.h> // memset
+
+#include <uv.h>
+#include "heap-inl.h"
+
+static int timer_less_than(const struct heap_node *ha, const struct heap_node *hb)
+{
+ const uv_timer_t *a;
+ const uv_timer_t *b;
+
+ a = container_of(ha, const uv_timer_t, heap_node);
+ b = container_of(hb, const uv_timer_t, heap_node);
+
+ if (a->timeout < b->timeout) {
+ return 1;
+ }
+ if (b->timeout < a->timeout) {
+ return 0;
+ }
+
+ /* Compare start_id when both have the same timeout. start_id is
+ * allocated with loop->timer_counter in uv_timer_start().
+ */
+ if (a->start_id < b->start_id) {
+ return 1;
+ }
+ if (b->start_id < a->start_id) {
+ return 0;
+ }
+
+ return 0;
+}
+
+//-----------------------------------------------------------------------------
+
+int uv_timer_init(uv_loop_t *loop, uv_timer_t *handle)
+{
+ uv__timer_platform_init(handle);
+ uv__handle_init(loop, (uv_handle_t *) handle, UV_TIMER);
+ handle->timer_cb = NULL;
+ handle->repeat = 0;
+ return 0;
+}
+
+int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
+{
+ uint64_t clamped_timeout;
+
+ if (cb == NULL) {
+ return -EINVAL;
+ }
+
+ if (uv__is_active(handle)) {
+ uv_timer_stop(handle);
+ }
+
+ clamped_timeout = handle->loop->time + timeout;
+ if (clamped_timeout < timeout) {
+ clamped_timeout = (uint64_t)(-1);
+ }
+
+ handle->timer_cb = cb;
+ handle->timeout = clamped_timeout;
+ handle->repeat = repeat;
+ /* start_id is the second index to be compared in uv__timer_cmp() */
+ handle->start_id = handle->loop->timer_counter++;
+
+ heap_insert((struct heap *)(&handle->loop->timer_heap), (struct heap_node *)(&handle->heap_node), timer_less_than);
+ uv__handle_start(handle);
+
+ return 0;
+}
+
+int uv_timer_stop(uv_timer_t *handle)
+{
+ if (!uv__is_active(handle)) {
+ return 0;
+ }
+
+ heap_remove((struct heap *)(&handle->loop->timer_heap), (struct heap_node *)(&handle->heap_node), timer_less_than);
+ uv__handle_stop(handle);
+
+ return 0;
+}
+
+int uv_timer_again(uv_timer_t *handle)
+{
+ if (handle->timer_cb == NULL) {
+ return -EINVAL;
+ }
+
+ if (handle->repeat) {
+ uv_timer_stop(handle);
+ uv_timer_start(handle, handle->timer_cb, handle->repeat, handle->repeat);
+ }
+
+ return 0;
+}
+
+void uv_timer_set_repeat(uv_timer_t *handle, uint64_t repeat)
+{
+ handle->repeat = repeat;
+}
+
+uint64_t uv_timer_get_repeat(const uv_timer_t *handle)
+{
+ return handle->repeat;
+}
+
+//-----------------------------------------------------------------------------
+
+int uv__next_timeout(const uv_loop_t *loop)
+{
+ const struct heap_node *heap_node;
+ const uv_timer_t *handle;
+ uint64_t diff;
+
+ heap_node = heap_min((const struct heap *)&loop->timer_heap);
+ if (heap_node == NULL) {
+ return -1; /* block indefinitely */
+ }
+
+ handle = container_of(heap_node, const uv_timer_t, heap_node);
+ if (handle->timeout <= loop->time) {
+ return 0;
+ }
+
+ diff = handle->timeout - loop->time;
+ if (diff > INT_MAX) {
+ diff = INT_MAX;
+ }
+
+ return diff;
+}
+
+void uv__run_timers(uv_loop_t *loop)
+{
+ struct heap_node *heap_node;
+ uv_timer_t *handle;
+
+ for (;;) {
+ heap_node = heap_min((struct heap *)&loop->timer_heap);
+ if (heap_node == NULL) {
+ break;
+ }
+
+ handle = container_of(heap_node, uv_timer_t, heap_node);
+ if (handle->timeout > loop->time) {
+ break;
+ }
+ uv_timer_stop(handle);
+ uv_timer_again(handle);
+ handle->timer_cb(handle);
+ }
+}
+
+void uv__timer_close(uv_timer_t *handle)
+{
+ uv_timer_stop(handle);
+}
--- /dev/null
+/* 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.
+ */
+
+/* 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 <errno.h>
+#include <assert.h>
+
+#include <uv.h>
+
+uv_buf_t uv_buf_init(char *base, unsigned int len)
+{
+ uv_buf_t buf;
+ buf.base = base;
+ buf.len = len;
+ return buf;
+}
+
+size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs)
+{
+ unsigned int i;
+ size_t bytes;
+
+ bytes = 0;
+ for (i = 0; i < nbufs; i++) {
+ bytes += (size_t) bufs[i].len;
+ }
+
+ return bytes;
+}