2012-12-10 17:44:02 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-06-27 15:14:04 -05:00
|
|
|
/* Foreign builtins. */
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2012-01-06 14:06:35 -06:00
|
|
|
#include "rust_util.h"
|
2013-01-10 21:03:13 -06:00
|
|
|
#include "sync/rust_thread.h"
|
2013-08-01 01:12:20 -05:00
|
|
|
#include "sync/lock_and_signal.h"
|
|
|
|
#include "memory_region.h"
|
|
|
|
#include "boxed_region.h"
|
2013-06-18 21:52:05 -05:00
|
|
|
#include "vg/valgrind.h"
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2012-04-03 12:32:26 -05:00
|
|
|
#include <time.h>
|
|
|
|
|
2012-02-07 20:55:02 -06:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <crt_externs.h>
|
2013-08-23 00:29:04 -05:00
|
|
|
#include <mach/mach_time.h>
|
2012-02-07 20:55:02 -06:00
|
|
|
#endif
|
|
|
|
|
2011-04-29 13:54:06 -05:00
|
|
|
#if !defined(__WIN32__)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2012-02-09 00:08:24 -06:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
extern char **environ;
|
|
|
|
#endif
|
|
|
|
|
2012-11-29 18:21:49 -06:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
time_t
|
|
|
|
timegm(struct tm *tm)
|
|
|
|
{
|
|
|
|
time_t ret;
|
|
|
|
char *tz;
|
|
|
|
|
|
|
|
tz = getenv("TZ");
|
|
|
|
setenv("TZ", "", 1);
|
|
|
|
tzset();
|
|
|
|
ret = mktime(tm);
|
|
|
|
if (tz)
|
|
|
|
setenv("TZ", tz, 1);
|
|
|
|
else
|
|
|
|
unsetenv("TZ");
|
|
|
|
tzset();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-07 20:55:02 -06:00
|
|
|
#if defined(__WIN32__)
|
2013-02-15 00:16:53 -06:00
|
|
|
extern "C" CDECL char**
|
2012-02-07 20:55:02 -06:00
|
|
|
rust_env_pairs() {
|
2013-02-15 00:16:53 -06:00
|
|
|
return 0;
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
#else
|
2013-02-15 00:16:53 -06:00
|
|
|
extern "C" CDECL char**
|
2012-02-07 20:55:02 -06:00
|
|
|
rust_env_pairs() {
|
|
|
|
#ifdef __APPLE__
|
|
|
|
char **environ = *_NSGetEnviron();
|
|
|
|
#endif
|
2013-02-15 00:16:53 -06:00
|
|
|
return environ;
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-21 00:46:26 -06:00
|
|
|
extern "C" CDECL char*
|
2011-07-12 17:14:57 -05:00
|
|
|
#if defined(__WIN32__)
|
2013-02-21 00:46:26 -06:00
|
|
|
rust_list_dir_val(WIN32_FIND_DATA* entry_ptr) {
|
|
|
|
return entry_ptr->cFileName;
|
|
|
|
}
|
2011-07-12 17:14:57 -05:00
|
|
|
#else
|
2013-02-21 00:46:26 -06:00
|
|
|
rust_list_dir_val(dirent* entry_ptr) {
|
|
|
|
return entry_ptr->d_name;
|
|
|
|
}
|
2011-07-12 17:14:57 -05:00
|
|
|
#endif
|
2011-09-01 17:51:27 -05:00
|
|
|
|
2013-02-21 00:46:26 -06:00
|
|
|
extern "C" CDECL size_t
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
rust_list_dir_wfd_size() {
|
|
|
|
return sizeof(WIN32_FIND_DATAW);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
rust_list_dir_wfd_size() {
|
|
|
|
return 0;
|
2011-07-12 17:14:57 -05:00
|
|
|
}
|
2013-02-21 00:46:26 -06:00
|
|
|
#endif
|
2011-07-12 17:14:57 -05:00
|
|
|
|
2013-02-21 00:46:26 -06:00
|
|
|
extern "C" CDECL void*
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
rust_list_dir_wfd_fp_buf(WIN32_FIND_DATAW* wfd) {
|
|
|
|
if(wfd == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return wfd->cFileName;
|
|
|
|
}
|
2012-10-02 14:19:04 -05:00
|
|
|
}
|
2013-02-21 00:46:26 -06:00
|
|
|
#else
|
|
|
|
rust_list_dir_wfd_fp_buf(void* wfd) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2012-10-02 14:19:04 -05:00
|
|
|
|
2011-03-10 08:56:51 -06:00
|
|
|
extern "C" CDECL int
|
2013-10-20 01:02:03 -05:00
|
|
|
rust_path_is_dir(const char *path) {
|
2011-03-10 08:56:51 -06:00
|
|
|
struct stat buf;
|
2011-09-01 14:58:24 -05:00
|
|
|
if (stat(path, &buf)) {
|
2011-09-01 14:38:36 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2011-03-10 08:56:51 -06:00
|
|
|
return S_ISDIR(buf.st_mode);
|
|
|
|
}
|
2010-08-24 20:37:42 -05:00
|
|
|
|
2011-12-16 16:32:09 -06:00
|
|
|
extern "C" CDECL int
|
2013-10-20 01:02:03 -05:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
rust_path_is_dir_u16(const wchar_t *path) {
|
|
|
|
struct _stat buf;
|
|
|
|
// Don't use GetFileAttributesW, it cannot get attributes of
|
|
|
|
// some system files (e.g. pagefile.sys).
|
|
|
|
if (_wstat(path, &buf)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return S_ISDIR(buf.st_mode);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
rust_path_is_dir_u16(const void *path) {
|
|
|
|
// Wide version of function is only used on Windows.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern "C" CDECL int
|
|
|
|
rust_path_exists(const char *path) {
|
2011-12-16 16:32:09 -06:00
|
|
|
struct stat buf;
|
|
|
|
if (stat(path, &buf)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-20 01:02:03 -05:00
|
|
|
extern "C" CDECL int
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
rust_path_exists_u16(const wchar_t *path) {
|
|
|
|
struct _stat buf;
|
|
|
|
if (_wstat(path, &buf)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
rust_path_exists_u16(const void *path) {
|
|
|
|
// Wide version of function is only used on Windows.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-10 09:02:53 -06:00
|
|
|
extern "C" CDECL FILE* rust_get_stdin() {return stdin;}
|
|
|
|
extern "C" CDECL FILE* rust_get_stdout() {return stdout;}
|
2011-08-24 23:19:56 -05:00
|
|
|
extern "C" CDECL FILE* rust_get_stderr() {return stderr;}
|
2011-03-10 09:02:53 -06:00
|
|
|
|
2011-04-29 13:54:06 -05:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
extern "C" CDECL void
|
2012-04-02 23:41:24 -05:00
|
|
|
get_time(int64_t *sec, int32_t *nsec) {
|
2011-04-29 13:54:06 -05:00
|
|
|
FILETIME fileTime;
|
2012-02-18 03:21:26 -06:00
|
|
|
GetSystemTimeAsFileTime(&fileTime);
|
|
|
|
|
|
|
|
// A FILETIME contains a 64-bit value representing the number of
|
|
|
|
// hectonanosecond (100-nanosecond) intervals since 1601-01-01T00:00:00Z.
|
|
|
|
// http://support.microsoft.com/kb/167296/en-us
|
|
|
|
ULARGE_INTEGER ul;
|
|
|
|
ul.LowPart = fileTime.dwLowDateTime;
|
|
|
|
ul.HighPart = fileTime.dwHighDateTime;
|
|
|
|
uint64_t ns_since_1601 = ul.QuadPart / 10;
|
|
|
|
|
2013-03-13 17:06:33 -05:00
|
|
|
const uint64_t NANOSECONDS_FROM_1601_TO_1970 = 11644473600000000ull;
|
2012-02-18 03:21:26 -06:00
|
|
|
uint64_t ns_since_1970 = ns_since_1601 - NANOSECONDS_FROM_1601_TO_1970;
|
|
|
|
*sec = ns_since_1970 / 1000000;
|
2012-04-02 23:41:24 -05:00
|
|
|
*nsec = (ns_since_1970 % 1000000) * 1000;
|
2011-04-29 13:54:06 -05:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
extern "C" CDECL void
|
2012-04-02 23:41:24 -05:00
|
|
|
get_time(int64_t *sec, int32_t *nsec) {
|
|
|
|
#ifdef __APPLE__
|
2011-04-29 13:54:06 -05:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
*sec = tv.tv_sec;
|
2012-04-02 23:41:24 -05:00
|
|
|
*nsec = tv.tv_usec * 1000;
|
|
|
|
#else
|
|
|
|
timespec ts;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
*sec = ts.tv_sec;
|
|
|
|
*nsec = ts.tv_nsec;
|
|
|
|
#endif
|
2011-04-29 13:54:06 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-08-23 00:29:04 -05:00
|
|
|
const uint64_t ns_per_s = 1000000000LL;
|
|
|
|
|
2011-06-28 19:58:44 -05:00
|
|
|
extern "C" CDECL void
|
2012-02-18 03:39:42 -06:00
|
|
|
precise_time_ns(uint64_t *ns) {
|
2013-08-23 00:29:04 -05:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
uint64_t time = mach_absolute_time();
|
|
|
|
mach_timebase_info_data_t info = {0, 0};
|
|
|
|
if (info.denom == 0) {
|
|
|
|
mach_timebase_info(&info);
|
|
|
|
}
|
|
|
|
uint64_t time_nano = time * (info.numer / info.denom);
|
|
|
|
*ns = time_nano;
|
|
|
|
#elif __WIN32__
|
|
|
|
uint64_t ticks_per_s;
|
|
|
|
QueryPerformanceFrequency((LARGE_INTEGER *)&ticks_per_s);
|
|
|
|
if (ticks_per_s == 0LL) {
|
|
|
|
ticks_per_s = 1LL;
|
|
|
|
}
|
|
|
|
uint64_t ticks;
|
|
|
|
QueryPerformanceCounter((LARGE_INTEGER *)&ticks);
|
|
|
|
*ns = ((ticks * ns_per_s) / ticks_per_s);
|
|
|
|
#else
|
|
|
|
timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
*ns = (ts.tv_sec * ns_per_s + ts.tv_nsec);
|
|
|
|
#endif
|
2011-06-28 19:58:44 -05:00
|
|
|
}
|
|
|
|
|
2012-04-03 12:32:26 -05:00
|
|
|
struct rust_tm {
|
|
|
|
int32_t tm_sec;
|
|
|
|
int32_t tm_min;
|
|
|
|
int32_t tm_hour;
|
|
|
|
int32_t tm_mday;
|
|
|
|
int32_t tm_mon;
|
|
|
|
int32_t tm_year;
|
|
|
|
int32_t tm_wday;
|
|
|
|
int32_t tm_yday;
|
|
|
|
int32_t tm_isdst;
|
|
|
|
int32_t tm_gmtoff;
|
|
|
|
rust_str *tm_zone;
|
|
|
|
int32_t tm_nsec;
|
|
|
|
};
|
|
|
|
|
|
|
|
void rust_tm_to_tm(rust_tm* in_tm, tm* out_tm) {
|
|
|
|
memset(out_tm, 0, sizeof(tm));
|
|
|
|
out_tm->tm_sec = in_tm->tm_sec;
|
|
|
|
out_tm->tm_min = in_tm->tm_min;
|
|
|
|
out_tm->tm_hour = in_tm->tm_hour;
|
|
|
|
out_tm->tm_mday = in_tm->tm_mday;
|
|
|
|
out_tm->tm_mon = in_tm->tm_mon;
|
|
|
|
out_tm->tm_year = in_tm->tm_year;
|
|
|
|
out_tm->tm_wday = in_tm->tm_wday;
|
|
|
|
out_tm->tm_yday = in_tm->tm_yday;
|
|
|
|
out_tm->tm_isdst = in_tm->tm_isdst;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tm_to_rust_tm(tm* in_tm, rust_tm* out_tm, int32_t gmtoff,
|
|
|
|
const char *zone, int32_t nsec) {
|
|
|
|
out_tm->tm_sec = in_tm->tm_sec;
|
|
|
|
out_tm->tm_min = in_tm->tm_min;
|
|
|
|
out_tm->tm_hour = in_tm->tm_hour;
|
|
|
|
out_tm->tm_mday = in_tm->tm_mday;
|
|
|
|
out_tm->tm_mon = in_tm->tm_mon;
|
|
|
|
out_tm->tm_year = in_tm->tm_year;
|
|
|
|
out_tm->tm_wday = in_tm->tm_wday;
|
|
|
|
out_tm->tm_yday = in_tm->tm_yday;
|
|
|
|
out_tm->tm_isdst = in_tm->tm_isdst;
|
|
|
|
out_tm->tm_gmtoff = gmtoff;
|
|
|
|
out_tm->tm_nsec = nsec;
|
|
|
|
|
|
|
|
if (zone != NULL) {
|
|
|
|
size_t size = strlen(zone);
|
2013-08-23 00:11:30 -05:00
|
|
|
assert(out_tm->tm_zone->alloc >= size);
|
2013-07-15 13:23:42 -05:00
|
|
|
memcpy(out_tm->tm_zone->data, zone, size);
|
2013-08-04 15:22:56 -05:00
|
|
|
out_tm->tm_zone->fill = size;
|
2012-04-03 12:32:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
#define TZSET() _tzset()
|
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
|
|
|
|
#define GMTIME(clock, result) gmtime_s((result), (clock))
|
|
|
|
#define LOCALTIME(clock, result) localtime_s((result), (clock))
|
|
|
|
#define TIMEGM(result) _mkgmtime64(result)
|
|
|
|
#else
|
|
|
|
struct tm* GMTIME(const time_t *clock, tm *result) {
|
|
|
|
struct tm* t = gmtime(clock);
|
|
|
|
if (t == NULL || result == NULL) { return NULL; }
|
|
|
|
*result = *t;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
struct tm* LOCALTIME(const time_t *clock, tm *result) {
|
|
|
|
struct tm* t = localtime(clock);
|
|
|
|
if (t == NULL || result == NULL) { return NULL; }
|
|
|
|
*result = *t;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#define TIMEGM(result) mktime((result)) - _timezone
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define TZSET() tzset()
|
|
|
|
#define GMTIME(clock, result) gmtime_r((clock), (result))
|
|
|
|
#define LOCALTIME(clock, result) localtime_r((clock), (result))
|
|
|
|
#define TIMEGM(result) timegm(result)
|
|
|
|
#endif
|
|
|
|
|
2012-04-13 19:33:02 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
rust_tzset() {
|
|
|
|
TZSET();
|
|
|
|
}
|
|
|
|
|
2012-04-03 12:32:26 -05:00
|
|
|
extern "C" CDECL void
|
2013-03-08 19:44:37 -06:00
|
|
|
rust_gmtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
|
2012-04-03 12:32:26 -05:00
|
|
|
tm tm;
|
2013-03-08 19:44:37 -06:00
|
|
|
time_t s = sec;
|
2012-04-03 12:32:26 -05:00
|
|
|
GMTIME(&s, &tm);
|
|
|
|
|
2013-03-08 19:44:37 -06:00
|
|
|
tm_to_rust_tm(&tm, timeptr, 0, "UTC", nsec);
|
2012-04-03 12:32:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2013-03-08 19:44:37 -06:00
|
|
|
rust_localtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
|
2012-04-03 12:32:26 -05:00
|
|
|
tm tm;
|
2013-03-08 19:44:37 -06:00
|
|
|
time_t s = sec;
|
2012-04-03 12:32:26 -05:00
|
|
|
LOCALTIME(&s, &tm);
|
|
|
|
|
2013-09-01 06:57:29 -05:00
|
|
|
const char* zone = NULL;
|
2012-04-03 12:32:26 -05:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
int32_t gmtoff = -timezone;
|
2013-10-20 01:02:03 -05:00
|
|
|
wchar_t wbuffer[64];
|
|
|
|
char buffer[256];
|
|
|
|
// strftime("%Z") can contain non-UTF-8 characters on non-English locale (issue #9418),
|
|
|
|
// so time zone should be converted from UTF-16 string set by wcsftime.
|
|
|
|
if (wcsftime(wbuffer, sizeof(wbuffer) / sizeof(wchar_t), L"%Z", &tm) > 0) {
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, wbuffer, -1, buffer, sizeof(buffer), NULL, NULL);
|
2013-09-01 06:57:29 -05:00
|
|
|
zone = buffer;
|
|
|
|
}
|
2012-04-03 12:32:26 -05:00
|
|
|
#else
|
|
|
|
int32_t gmtoff = tm.tm_gmtoff;
|
2013-09-01 06:57:29 -05:00
|
|
|
zone = tm.tm_zone;
|
2012-04-03 12:32:26 -05:00
|
|
|
#endif
|
|
|
|
|
2013-03-08 19:44:37 -06:00
|
|
|
tm_to_rust_tm(&tm, timeptr, gmtoff, zone, nsec);
|
2012-04-03 12:32:26 -05:00
|
|
|
}
|
|
|
|
|
2013-05-27 11:52:56 -05:00
|
|
|
extern "C" CDECL int64_t
|
|
|
|
rust_timegm(rust_tm* timeptr) {
|
2012-04-03 12:32:26 -05:00
|
|
|
tm t;
|
|
|
|
rust_tm_to_tm(timeptr, &t);
|
2013-05-27 11:52:56 -05:00
|
|
|
return TIMEGM(&t);
|
2012-04-03 12:32:26 -05:00
|
|
|
}
|
|
|
|
|
2013-05-27 11:52:56 -05:00
|
|
|
extern "C" CDECL int64_t
|
|
|
|
rust_mktime(rust_tm* timeptr) {
|
2012-04-03 12:32:26 -05:00
|
|
|
tm t;
|
|
|
|
rust_tm_to_tm(timeptr, &t);
|
2013-05-27 11:52:56 -05:00
|
|
|
return mktime(&t);
|
2012-04-03 12:32:26 -05:00
|
|
|
}
|
|
|
|
|
2012-08-07 13:42:30 -05:00
|
|
|
extern "C" lock_and_signal*
|
|
|
|
rust_create_little_lock() {
|
|
|
|
return new lock_and_signal();
|
2012-06-06 17:06:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
2012-08-07 13:42:30 -05:00
|
|
|
rust_destroy_little_lock(lock_and_signal *lock) {
|
2012-06-06 17:06:24 -05:00
|
|
|
delete lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
2012-08-07 13:42:30 -05:00
|
|
|
rust_lock_little_lock(lock_and_signal *lock) {
|
|
|
|
lock->lock();
|
2012-06-06 17:06:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
2012-08-07 13:42:30 -05:00
|
|
|
rust_unlock_little_lock(lock_and_signal *lock) {
|
|
|
|
lock->unlock();
|
2012-06-06 17:06:24 -05:00
|
|
|
}
|
|
|
|
|
2013-10-17 03:40:33 -05:00
|
|
|
typedef void(startfn)(void*, void*);
|
|
|
|
|
2013-01-10 21:03:13 -06:00
|
|
|
class raw_thread: public rust_thread {
|
|
|
|
public:
|
2013-10-17 03:40:33 -05:00
|
|
|
startfn *raw_start;
|
|
|
|
void *rust_fn;
|
|
|
|
void *rust_env;
|
2013-01-10 21:03:13 -06:00
|
|
|
|
2013-10-17 03:40:33 -05:00
|
|
|
raw_thread(startfn *raw_start, void *rust_fn, void *rust_env)
|
|
|
|
: raw_start(raw_start), rust_fn(rust_fn), rust_env(rust_env) { }
|
2013-01-10 21:03:13 -06:00
|
|
|
|
|
|
|
virtual void run() {
|
2013-10-17 03:40:33 -05:00
|
|
|
raw_start(rust_fn, rust_env);
|
2013-01-10 21:03:13 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" raw_thread*
|
2013-10-17 03:40:33 -05:00
|
|
|
rust_raw_thread_start(startfn *raw_start, void *rust_start, void *rust_env) {
|
|
|
|
assert(raw_start && rust_start);
|
|
|
|
raw_thread *thread = new raw_thread(raw_start, rust_start, rust_env);
|
2013-01-10 21:03:13 -06:00
|
|
|
thread->start();
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
2013-07-27 14:05:15 -05:00
|
|
|
rust_raw_thread_join(raw_thread *thread) {
|
2013-01-10 21:03:13 -06:00
|
|
|
assert(thread);
|
|
|
|
thread->join();
|
2013-07-27 14:05:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
rust_raw_thread_delete(raw_thread *thread) {
|
|
|
|
assert(thread);
|
2013-01-10 21:03:13 -06:00
|
|
|
delete thread;
|
|
|
|
}
|
|
|
|
|
2013-03-12 22:06:20 -05:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
extern "C" DIR*
|
|
|
|
rust_opendir(char *dirname) {
|
|
|
|
return opendir(dirname);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" dirent*
|
|
|
|
rust_readdir(DIR *dirp) {
|
|
|
|
return readdir(dirp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
rust_opendir() {
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
rust_readdir() {
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-03-15 20:06:19 -05:00
|
|
|
#ifndef _WIN32
|
2013-08-17 04:12:08 -05:00
|
|
|
typedef pthread_key_t tls_key;
|
2013-03-15 20:06:19 -05:00
|
|
|
#else
|
2013-08-17 04:12:08 -05:00
|
|
|
typedef DWORD tls_key;
|
2013-03-15 20:06:19 -05:00
|
|
|
#endif
|
|
|
|
|
2013-05-19 16:24:36 -05:00
|
|
|
// Initialize the TLS key used by the new scheduler
|
2013-03-15 20:06:19 -05:00
|
|
|
extern "C" CDECL void
|
2013-08-17 04:12:08 -05:00
|
|
|
rust_initialize_rt_tls_key(tls_key *key) {
|
2013-03-15 20:06:19 -05:00
|
|
|
|
2013-03-15 20:35:33 -05:00
|
|
|
static lock_and_signal init_lock;
|
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
scoped_lock with(init_lock);
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
|
2013-03-15 20:06:19 -05:00
|
|
|
#ifndef _WIN32
|
2013-08-17 04:12:08 -05:00
|
|
|
assert(!pthread_key_create(key, NULL));
|
2013-03-15 20:06:19 -05:00
|
|
|
#else
|
2013-08-17 04:12:08 -05:00
|
|
|
*key = TlsAlloc();
|
|
|
|
assert(*key != TLS_OUT_OF_INDEXES);
|
2013-03-15 20:06:19 -05:00
|
|
|
#endif
|
|
|
|
|
2013-03-15 20:35:33 -05:00
|
|
|
initialized = true;
|
|
|
|
}
|
2013-03-15 20:06:19 -05:00
|
|
|
}
|
|
|
|
|
2013-04-21 21:03:52 -05:00
|
|
|
extern "C" CDECL memory_region*
|
2013-08-23 00:42:36 -05:00
|
|
|
rust_new_memory_region(uintptr_t detailed_leaks,
|
2013-04-21 21:03:52 -05:00
|
|
|
uintptr_t poison_on_free) {
|
2013-08-23 00:42:36 -05:00
|
|
|
return new memory_region((bool)detailed_leaks,
|
2013-04-21 21:03:52 -05:00
|
|
|
(bool)poison_on_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_delete_memory_region(memory_region *region) {
|
|
|
|
delete region;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL boxed_region*
|
|
|
|
rust_new_boxed_region(memory_region *region,
|
|
|
|
uintptr_t poison_on_free) {
|
|
|
|
return new boxed_region(region, poison_on_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_delete_boxed_region(boxed_region *region) {
|
|
|
|
delete region;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL rust_opaque_box*
|
|
|
|
rust_boxed_region_malloc(boxed_region *region, type_desc *td, size_t size) {
|
|
|
|
return region->malloc(td, size);
|
|
|
|
}
|
|
|
|
|
2013-06-21 21:40:00 -05:00
|
|
|
extern "C" CDECL rust_opaque_box*
|
|
|
|
rust_boxed_region_realloc(boxed_region *region, rust_opaque_box *ptr, size_t size) {
|
|
|
|
return region->realloc(ptr, size);
|
|
|
|
}
|
|
|
|
|
2013-04-21 21:03:52 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
rust_boxed_region_free(boxed_region *region, rust_opaque_box *box) {
|
|
|
|
region->free(box);
|
|
|
|
}
|
|
|
|
|
2013-04-22 19:15:31 -05:00
|
|
|
typedef void *(rust_try_fn)(void*, void*);
|
|
|
|
|
|
|
|
extern "C" CDECL uintptr_t
|
|
|
|
rust_try(rust_try_fn f, void *fptr, void *env) {
|
|
|
|
try {
|
|
|
|
f(fptr, env);
|
|
|
|
} catch (uintptr_t token) {
|
|
|
|
assert(token != 0);
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_begin_unwind(uintptr_t token) {
|
|
|
|
throw token;
|
|
|
|
}
|
|
|
|
|
2013-06-18 21:52:05 -05:00
|
|
|
extern "C" CDECL uintptr_t
|
|
|
|
rust_running_on_valgrind() {
|
|
|
|
return RUNNING_ON_VALGRIND;
|
|
|
|
}
|
|
|
|
|
2013-08-10 00:21:01 -05:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
int
|
|
|
|
get_num_cpus() {
|
|
|
|
SYSTEM_INFO sysinfo;
|
|
|
|
GetSystemInfo(&sysinfo);
|
|
|
|
|
|
|
|
return (int) sysinfo.dwNumberOfProcessors;
|
|
|
|
}
|
|
|
|
#elif defined(__BSD__)
|
|
|
|
int
|
|
|
|
get_num_cpus() {
|
|
|
|
/* swiped from http://stackoverflow.com/questions/150355/
|
|
|
|
programmatically-find-the-number-of-cores-on-a-machine */
|
|
|
|
|
|
|
|
unsigned int numCPU;
|
|
|
|
int mib[4];
|
|
|
|
size_t len = sizeof(numCPU);
|
|
|
|
|
|
|
|
/* set the mib for hw.ncpu */
|
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
|
|
|
|
|
|
|
|
/* get the number of CPUs from the system */
|
|
|
|
sysctl(mib, 2, &numCPU, &len, NULL, 0);
|
|
|
|
|
|
|
|
if( numCPU < 1 ) {
|
|
|
|
mib[1] = HW_NCPU;
|
|
|
|
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
|
|
|
|
|
|
|
|
if( numCPU < 1 ) {
|
|
|
|
numCPU = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return numCPU;
|
|
|
|
}
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
int
|
|
|
|
get_num_cpus() {
|
|
|
|
return sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
}
|
|
|
|
#endif
|
2013-06-06 00:34:35 -05:00
|
|
|
|
|
|
|
extern "C" CDECL uintptr_t
|
|
|
|
rust_get_num_cpus() {
|
|
|
|
return get_num_cpus();
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:28:23 -05:00
|
|
|
static lock_and_signal global_args_lock;
|
|
|
|
static uintptr_t global_args_ptr = 0;
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_take_global_args_lock() {
|
|
|
|
global_args_lock.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_drop_global_args_lock() {
|
|
|
|
global_args_lock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL uintptr_t*
|
|
|
|
rust_get_global_args_ptr() {
|
|
|
|
return &global_args_ptr;
|
|
|
|
}
|
|
|
|
|
2013-08-10 00:21:01 -05:00
|
|
|
static lock_and_signal env_lock;
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_take_env_lock() {
|
|
|
|
env_lock.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_drop_env_lock() {
|
|
|
|
env_lock.unlock();
|
|
|
|
}
|
|
|
|
|
2013-10-03 23:34:35 -05:00
|
|
|
static lock_and_signal dlerror_lock;
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_take_dlerror_lock() {
|
|
|
|
dlerror_lock.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_drop_dlerror_lock() {
|
|
|
|
dlerror_lock.unlock();
|
|
|
|
}
|
|
|
|
|
2013-08-10 01:35:51 -05:00
|
|
|
extern "C" CDECL unsigned int
|
|
|
|
rust_valgrind_stack_register(void *start, void *end) {
|
|
|
|
return VALGRIND_STACK_REGISTER(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_valgrind_stack_deregister(unsigned int id) {
|
|
|
|
VALGRIND_STACK_DEREGISTER(id);
|
|
|
|
}
|
|
|
|
|
2013-09-17 16:45:50 -05:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_unset_sigprocmask() {
|
|
|
|
// empty stub for windows to keep linker happy
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_unset_sigprocmask() {
|
|
|
|
// this can't be safely converted to rust code because the
|
|
|
|
// representation of sigset_t is platform-dependent
|
|
|
|
sigset_t sset;
|
|
|
|
sigemptyset(&sset);
|
|
|
|
sigprocmask(SIG_SETMASK, &sset, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-10-01 11:18:57 -05:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
void
|
|
|
|
win32_require(LPCTSTR fn, BOOL ok) {
|
|
|
|
if (!ok) {
|
|
|
|
LPTSTR buf;
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, err,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR) &buf, 0, NULL );
|
|
|
|
fprintf(stderr, "%s failed with error %ld: %s", fn, err, buf);
|
|
|
|
LocalFree((HLOCAL)buf);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_win32_rand_acquire(HCRYPTPROV* phProv) {
|
|
|
|
win32_require
|
|
|
|
(_T("CryptAcquireContext"),
|
|
|
|
CryptAcquireContext(phProv, NULL, NULL, PROV_RSA_FULL,
|
|
|
|
CRYPT_VERIFYCONTEXT|CRYPT_SILENT));
|
|
|
|
|
|
|
|
}
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_win32_rand_gen(HCRYPTPROV hProv, DWORD dwLen, BYTE* pbBuffer) {
|
|
|
|
win32_require
|
|
|
|
(_T("CryptGenRandom"), CryptGenRandom(hProv, dwLen, pbBuffer));
|
|
|
|
}
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_win32_rand_release(HCRYPTPROV hProv) {
|
|
|
|
win32_require
|
|
|
|
(_T("CryptReleaseContext"), CryptReleaseContext(hProv, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// these symbols are listed in rustrt.def.in, so they need to exist; but they
|
|
|
|
// should never be called.
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_win32_rand_acquire() {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_win32_rand_gen() {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_win32_rand_release() {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2010-06-23 23:03:09 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|