Files
swift-mirror/stdlib/public/stubs/LibcShims.cpp
Kevin Ballard ac1996000e [Stdlib] Remove all redundant stdout locking when printing
* Switch to calling `putchar_unlocked()` instead of `putchar()` for
  actual printing. We're already locking stdout with `flockfile()`, so
  there's no need for the redundant lock that `putchar()` uses.
* Add an explicit lock to the output stream in `dump()`. This means the
  entire dump is printed with the lock held, which will prevent the
  output of `dump()` from mixing with prints on other threads.
* Use `_debugPrint_unlocked()` instead of `debugPrint()` in
  `_adHocPrint()`. The output stream is already locked while this
  function is executing. Rename the function to `_adHocPrint_unlocked()`
  to make this explicit.
* Use `targetStream.write()` and `_print_unlocked()` instead of
  `print()` in `_dumpObject()`. This removes the redundant locking, and
  also eliminates the creation of intermediate strings. Rename the
  function to `_dumpObject_unlocked()` to make this explicit.
* Use `targetStream.write()`, `_print_unlocked()`, and
  `_debugPrint_unlocked()` in `_dumpSuperclass()`. This removes the
  redundant locking, and also eliminates the creation of intermediate
  strings. Rename the function to `_dumpSuperclass_unlocked()` to make
  this explicit.
* Use `_debugPrint_unlocked()` instead of `debugPrint()` in
  `String.init(reflecting:)`. This shouldn't really make much of a
  difference but it matches the usage of `_print_unlocked()` in
  `String.init(_:)`.

The net result is that all printing is still covered under locks like
before, but stdout is never recursively locked. This should result in
slightly faster printing. In addition, `dump()` is now covered under a
single lock so it can't mix its output with prints from other threads.
2016-02-02 13:40:55 -08:00

76 lines
2.2 KiB
C++

//===--- LibcShims.cpp ----------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include <type_traits>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../SwiftShims/LibcShims.h"
#if defined(__linux__)
#include <bsd/stdlib.h>
#endif
static_assert(std::is_same<ssize_t, swift::__swift_ssize_t>::value,
"__swift_ssize_t is wrong");
namespace swift {
void _swift_stdlib_free(void *ptr) { free(ptr); }
int _swift_stdlib_putchar_unlocked(int c) { return putchar_unlocked(c); }
__swift_size_t _swift_stdlib_strlen(const char *s) { return strlen(s); }
int _swift_stdlib_memcmp(const void *s1, const void *s2, __swift_size_t n) {
return memcmp(s1, s2, n);
}
__swift_ssize_t _swift_stdlib_read(int fd, void *buf, __swift_size_t nbyte) {
return read(fd, buf, nbyte);
}
__swift_ssize_t _swift_stdlib_write(int fd, const void *buf,
__swift_size_t nbyte) {
return write(fd, buf, nbyte);
}
int _swift_stdlib_close(int fd) { return close(fd); }
#if defined(__APPLE__)
#include <malloc/malloc.h>
size_t _swift_stdlib_malloc_size(const void *ptr) { return malloc_size(ptr); }
#elif defined(__GNU_LIBRARY__)
#include <malloc.h>
size_t _swift_stdlib_malloc_size(const void *ptr) {
return malloc_usable_size(const_cast<void *>(ptr));
}
#elif defined(__FreeBSD__)
#include <malloc_np.h>
size_t _swift_stdlib_malloc_size(const void *ptr) {
return malloc_usable_size(const_cast<void *>(ptr));
}
#else
#error No malloc_size analog known for this platform/libc.
#endif
__swift_uint32_t _swift_stdlib_arc4random(void) { return arc4random(); }
__swift_uint32_t
_swift_stdlib_arc4random_uniform(__swift_uint32_t upper_bound) {
return arc4random_uniform(upper_bound);
}
} // namespace swift