|
| 1 | +// Copyright 2009-2025 NTESS. Under the terms |
| 2 | +// of Contract DE-NA0003525 with NTESS, the U.S. |
| 3 | +// Government retains certain rights in this software. |
| 4 | +// |
| 5 | +// Copyright (c) 2009-2025, NTESS |
| 6 | +// All rights reserved. |
| 7 | +// |
| 8 | +// C-linkage wrappers for hgcc replacement headers (sleep, usleep, gettimeofday, |
| 9 | +// clock_gettime, nanosleep, getenv). These bridge to mercury's simulated time |
| 10 | +// and environment when running under SST. |
| 11 | +// Do not include skeleton.h here (it redefines main and breaks SST core headers). |
| 12 | + |
| 13 | +#include <mercury/common/timestamp.h> |
| 14 | +#include <mercury/operating_system/process/thread.h> |
| 15 | + |
| 16 | +#include <sys/time.h> |
| 17 | +#include <time.h> |
| 18 | +#include <cstdint> |
| 19 | + |
| 20 | +namespace SST::Hg { |
| 21 | +extern unsigned int ssthg_sleep(unsigned int secs); |
| 22 | +extern unsigned int ssthg_usleep(unsigned int usecs); |
| 23 | +extern unsigned int ssthg_nanosleep(unsigned int nsecs); |
| 24 | +} |
| 25 | + |
| 26 | +extern "C" char* ssthg_getenv(const char* name); |
| 27 | + |
| 28 | +extern "C" { |
| 29 | + |
| 30 | +unsigned int hgcc_sleep(unsigned int secs) |
| 31 | +{ |
| 32 | + return SST::Hg::ssthg_sleep(secs); |
| 33 | +} |
| 34 | + |
| 35 | +int hgcc_usleep(unsigned int usecs) |
| 36 | +{ |
| 37 | + SST::Hg::ssthg_usleep(usecs); |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | +char* hgcc_getenv(const char* name) |
| 42 | +{ |
| 43 | + return ssthg_getenv(name); |
| 44 | +} |
| 45 | + |
| 46 | +int SSTMAC_gettimeofday(struct timeval* tv, struct timezone* tz) |
| 47 | +{ |
| 48 | + if (!tv) return 0; |
| 49 | + SST::Hg::Thread* t = SST::Hg::Thread::current(); |
| 50 | + if (!t) return -1; |
| 51 | + SST::Hg::Timestamp now = t->now(); |
| 52 | + uint64_t usec = now.usecRounded(); |
| 53 | + tv->tv_sec = static_cast<time_t>(usec / 1000000ULL); |
| 54 | + tv->tv_usec = static_cast<suseconds_t>(usec % 1000000ULL); |
| 55 | + if (tz) { |
| 56 | + tz->tz_minuteswest = 0; |
| 57 | + tz->tz_dsttime = 0; |
| 58 | + } |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +int HGCC_clock_gettime(clockid_t /*id*/, struct timespec* ts) |
| 63 | +{ |
| 64 | + if (!ts) return 0; |
| 65 | + SST::Hg::Thread* t = SST::Hg::Thread::current(); |
| 66 | + if (!t) return -1; |
| 67 | + SST::Hg::Timestamp now = t->now(); |
| 68 | + uint64_t nsec = now.nsecRounded(); |
| 69 | + ts->tv_sec = static_cast<time_t>(nsec / 1000000000ULL); |
| 70 | + ts->tv_nsec = static_cast<long>(nsec % 1000000000ULL); |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +int hgcc_ts_nanosleep(const struct timespec* req, struct timespec* rem) |
| 75 | +{ |
| 76 | + if (!req) return -1; |
| 77 | + uint64_t total_nsec = static_cast<uint64_t>(req->tv_sec) * 1000000000ULL |
| 78 | + + static_cast<uint64_t>(req->tv_nsec); |
| 79 | + const unsigned int max_nsec = static_cast<unsigned int>(-1); |
| 80 | + unsigned int nsecs = (total_nsec > max_nsec) ? max_nsec : static_cast<unsigned int>(total_nsec); |
| 81 | + SST::Hg::ssthg_nanosleep(nsecs); |
| 82 | + if (rem) { |
| 83 | + rem->tv_sec = 0; |
| 84 | + rem->tv_nsec = 0; |
| 85 | + } |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +} // extern "C" |
0 commit comments