#include "pal/environ.h"
#include "pal/init.h"
+#if defined(__NetBSD__) && !HAVE_PTHREAD_GETCPUCLOCKID
+#include <sys/cdefs.h>
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <kvm.h>
+#endif
+
#include <signal.h>
#include <pthread.h>
#if HAVE_PTHREAD_NP_H
__int64 calcTime;
BOOL retval = FALSE;
const __int64 SECS_TO_NS = 1000000000; /* 10^9 */
+ const __int64 USECS_TO_NS = 1000; /* 10^3 */
#if HAVE_MACH_THREADS
thread_basic_info resUsage;
IPalObject *pobjThread = NULL;
mach_msg_type_number_t resUsage_count = THREAD_BASIC_INFO_COUNT;
- const __int64 USECS_TO_NS = 1000; /* 10^3 */
-
pthrCurrent = InternalGetCurrentThread();
palError = InternalGetThreadDataFromHandle(
pthrCurrent,
goto GetThreadTimesInternalExit;
+#elif defined(__NetBSD__) && !HAVE_PTHREAD_GETCPUCLOCKID /* Currently unimplemented */
+
+ PAL_ERROR palError;
+ CPalThread *pThread;
+ CPalThread *pTargetThread;
+ IPalObject *pobjThread = NULL;
+ kvm_t *kd;
+ int cnt, nlwps;
+ struct kinfo_lwp *klwp;
+ int i;
+ bool found = false;
+
+ pThread = InternalGetCurrentThread();
+
+ palError = InternalGetThreadDataFromHandle(
+ pThread,
+ hThread,
+ 0, // THREAD_GET_CONTEXT
+ &pTargetThread,
+ &pobjThread
+ );
+ if (palError != NO_ERROR)
+ {
+ ASSERT("Unable to get thread data from handle %p"
+ "thread\n", hThread);
+ SetLastError(ERROR_INTERNAL_ERROR);
+ goto SetTimesToZero;
+ }
+
+ kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
+ if (kd == NULL)
+ {
+ ASSERT("kvm_open(3) error");
+ SetLastError(ERROR_INTERNAL_ERROR);
+ goto SetTimesToZero;
+ }
+
+ pTargetThread->Lock(pThread);
+
+ klwp = kvm_getlwps(kd, getpid(), 0, sizeof(struct kinfo_lwp), &nlwps);
+ if (klwp == NULL || nlwps < 1)
+ {
+ kvm_close(kd);
+ ASSERT("Unable to get clock from %p thread\n", hThread);
+ SetLastError(ERROR_INTERNAL_ERROR);
+ pTargetThread->Unlock(pThread);
+ goto SetTimesToZero;
+ }
+
+ for (i = 0; i < nlwps; i++)
+ {
+ if (klwp[i].l_lid == THREADSilentGetCurrentThreadId())
+ {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found)
+ {
+ kvm_close(kd);
+ ASSERT("Unable to get clock from %p thread\n", hThread);
+ SetLastError(ERROR_INTERNAL_ERROR);
+ pTargetThread->Unlock(pThread);
+ goto SetTimesToZero;
+ }
+
+ pTargetThread->Unlock(pThread);
+
+ kvm_close(kd);
+
+ calcTime = (__int64) klwp[i].l_rtime_sec * SECS_TO_NS;
+ calcTime += (__int64) klwp[i].l_rtime_usec * USECS_TO_NS;
+ lpUserTime->dwLowDateTime = (DWORD)calcTime;
+ lpUserTime->dwHighDateTime = (DWORD)(calcTime >> 32);
+
+ /* NetBSD as of (7.0) doesn't differentiate used time in user/kernel for lwp */
+ lpKernelTime->dwLowDateTime = 0;
+ lpKernelTime->dwHighDateTime = 0;
+
+ retval = TRUE;
+ goto GetThreadTimesInternalExit;
+
#else //HAVE_MACH_THREADS
PAL_ERROR palError;