1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
|
// Package : omnithread
// omnithread/nt.cc Created : 6/95 tjr
//
// Copyright (C) 1995-1999 AT&T Laboratories Cambridge
//
// This file is part of the omnithread library
//
// The omnithread library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA
//
//
// Implementation of OMNI thread abstraction for NT threads
//
#include <stdlib.h>
#include <errno.h>
#include <omnithread.h>
#include <process.h>
#define DB(x) // x
//#include <iostream.h> or #include <iostream> if DB is on.
static void get_time_now(unsigned long* abs_sec, unsigned long* abs_nsec);
///////////////////////////////////////////////////////////////////////////
//
// Mutex
//
///////////////////////////////////////////////////////////////////////////
omni_mutex::omni_mutex(void)
{
InitializeCriticalSection(&crit);
}
omni_mutex::~omni_mutex(void)
{
DeleteCriticalSection(&crit);
}
void
omni_mutex::lock(void)
{
EnterCriticalSection(&crit);
}
void
omni_mutex::unlock(void)
{
LeaveCriticalSection(&crit);
}
///////////////////////////////////////////////////////////////////////////
//
// Condition variable
//
///////////////////////////////////////////////////////////////////////////
//
// Condition variables are tricky to implement using NT synchronisation
// primitives, since none of them have the atomic "release mutex and wait to be
// signalled" which is central to the idea of a condition variable. To get
// around this the solution is to record which threads are waiting and
// explicitly wake up those threads.
//
// Here we implement a condition variable using a list of waiting threads
// (protected by a critical section), and a per-thread semaphore (which
// actually only needs to be a binary semaphore).
//
// To wait on the cv, a thread puts itself on the list of waiting threads for
// that cv, then releases the mutex and waits on its own personal semaphore. A
// signalling thread simply takes a thread from the head of the list and kicks
// that thread's semaphore. Broadcast is simply implemented by kicking the
// semaphore of each waiting thread.
//
// The only other tricky part comes when a thread gets a timeout from a timed
// wait on its semaphore. Between returning with a timeout from the wait and
// entering the critical section, a signalling thread could get in, kick the
// waiting thread's semaphore and remove it from the list. If this happens,
// the waiting thread's semaphore is now out of step so it needs resetting, and
// the thread should indicate that it was signalled rather than that it timed
// out.
//
// It is possible that the thread calling wait or timedwait is not a
// omni_thread. In this case we have to provide a temporary data structure,
// i.e. for the duration of the call, for the thread to link itself on the
// list of waiting threads. _internal_omni_thread_dummy provides such
// a data structure and _internal_omni_thread_helper is a helper class to
// deal with this special case for wait() and timedwait(). Once created,
// the _internal_omni_thread_dummy is cached for use by the next wait() or
// timedwait() call from a non-omni_thread. This is probably worth doing
// because creating a Semaphore is quite heavy weight.
class _internal_omni_thread_helper;
class _internal_omni_thread_dummy : public omni_thread {
public:
inline _internal_omni_thread_dummy() : next(0) { }
inline ~_internal_omni_thread_dummy() { }
friend class _internal_omni_thread_helper;
private:
_internal_omni_thread_dummy* next;
};
class _internal_omni_thread_helper {
public:
inline _internal_omni_thread_helper() {
d = 0;
t = omni_thread::self();
if (!t) {
omni_mutex_lock sync(cachelock);
if (cache) {
d = cache;
cache = cache->next;
}
else {
d = new _internal_omni_thread_dummy;
}
t = d;
}
}
inline ~_internal_omni_thread_helper() {
if (d) {
omni_mutex_lock sync(cachelock);
d->next = cache;
cache = d;
}
}
inline operator omni_thread* () { return t; }
inline omni_thread* operator->() { return t; }
static _internal_omni_thread_dummy* cache;
static omni_mutex cachelock;
private:
_internal_omni_thread_dummy* d;
omni_thread* t;
};
_internal_omni_thread_dummy* _internal_omni_thread_helper::cache = 0;
omni_mutex _internal_omni_thread_helper::cachelock;
omni_condition::omni_condition(omni_mutex* m) : mutex(m)
{
InitializeCriticalSection(&crit);
waiting_head = waiting_tail = NULL;
}
omni_condition::~omni_condition(void)
{
DeleteCriticalSection(&crit);
DB( if (waiting_head != NULL) {
cerr << "omni_condition::~omni_condition: list of waiting threads "
<< "is not empty\n";
} )
}
void
omni_condition::wait(void)
{
_internal_omni_thread_helper me;
EnterCriticalSection(&crit);
me->cond_next = NULL;
me->cond_prev = waiting_tail;
if (waiting_head == NULL)
waiting_head = me;
else
waiting_tail->cond_next = me;
waiting_tail = me;
me->cond_waiting = TRUE;
LeaveCriticalSection(&crit);
mutex->unlock();
DWORD result = WaitForSingleObject(me->cond_semaphore, INFINITE);
mutex->lock();
if (result != WAIT_OBJECT_0)
throw omni_thread_fatal(GetLastError());
}
int
omni_condition::timedwait(unsigned long abs_sec, unsigned long abs_nsec)
{
_internal_omni_thread_helper me;
EnterCriticalSection(&crit);
me->cond_next = NULL;
me->cond_prev = waiting_tail;
if (waiting_head == NULL)
waiting_head = me;
else
waiting_tail->cond_next = me;
waiting_tail = me;
me->cond_waiting = TRUE;
LeaveCriticalSection(&crit);
mutex->unlock();
unsigned long now_sec, now_nsec;
get_time_now(&now_sec, &now_nsec);
DWORD timeout;
if ((abs_sec <= now_sec) && ((abs_sec < now_sec) || (abs_nsec < now_nsec)))
timeout = 0;
else {
timeout = (abs_sec-now_sec) * 1000;
if( abs_nsec < now_nsec ) timeout -= (now_nsec-abs_nsec) / 1000000;
else timeout += (abs_nsec-now_nsec) / 1000000;
}
DWORD result = WaitForSingleObject(me->cond_semaphore, timeout);
if (result == WAIT_TIMEOUT) {
EnterCriticalSection(&crit);
if (me->cond_waiting) {
if (me->cond_prev != NULL)
me->cond_prev->cond_next = me->cond_next;
else
waiting_head = me->cond_next;
if (me->cond_next != NULL)
me->cond_next->cond_prev = me->cond_prev;
else
waiting_tail = me->cond_prev;
me->cond_waiting = FALSE;
LeaveCriticalSection(&crit);
mutex->lock();
return 0;
}
//
// We timed out but another thread still signalled us. Wait for
// the semaphore (it _must_ have been signalled) to decrement it
// again. Return that we were signalled, not that we timed out.
//
LeaveCriticalSection(&crit);
result = WaitForSingleObject(me->cond_semaphore, INFINITE);
}
if (result != WAIT_OBJECT_0)
throw omni_thread_fatal(GetLastError());
mutex->lock();
return 1;
}
void
omni_condition::signal(void)
{
EnterCriticalSection(&crit);
if (waiting_head != NULL) {
omni_thread* t = waiting_head;
waiting_head = t->cond_next;
if (waiting_head == NULL)
waiting_tail = NULL;
else
waiting_head->cond_prev = NULL;
t->cond_waiting = FALSE;
if (!ReleaseSemaphore(t->cond_semaphore, 1, NULL)) {
int rc = GetLastError();
LeaveCriticalSection(&crit);
throw omni_thread_fatal(rc);
}
}
LeaveCriticalSection(&crit);
}
void
omni_condition::broadcast(void)
{
EnterCriticalSection(&crit);
while (waiting_head != NULL) {
omni_thread* t = waiting_head;
waiting_head = t->cond_next;
if (waiting_head == NULL)
waiting_tail = NULL;
else
waiting_head->cond_prev = NULL;
t->cond_waiting = FALSE;
if (!ReleaseSemaphore(t->cond_semaphore, 1, NULL)) {
int rc = GetLastError();
LeaveCriticalSection(&crit);
throw omni_thread_fatal(rc);
}
}
LeaveCriticalSection(&crit);
}
///////////////////////////////////////////////////////////////////////////
//
// Counting semaphore
//
///////////////////////////////////////////////////////////////////////////
#define SEMAPHORE_MAX 0x7fffffff
omni_semaphore::omni_semaphore(unsigned int initial)
{
nt_sem = CreateSemaphore(NULL, initial, SEMAPHORE_MAX, NULL);
if (nt_sem == NULL) {
DB( cerr << "omni_semaphore::omni_semaphore: CreateSemaphore error "
<< GetLastError() << endl );
throw omni_thread_fatal(GetLastError());
}
}
omni_semaphore::~omni_semaphore(void)
{
if (!CloseHandle(nt_sem)) {
DB( cerr << "omni_semaphore::~omni_semaphore: CloseHandle error "
<< GetLastError() << endl );
throw omni_thread_fatal(GetLastError());
}
}
void
omni_semaphore::wait(void)
{
if (WaitForSingleObject(nt_sem, INFINITE) != WAIT_OBJECT_0)
throw omni_thread_fatal(GetLastError());
}
int
omni_semaphore::trywait(void)
{
switch (WaitForSingleObject(nt_sem, 0)) {
case WAIT_OBJECT_0:
return 1;
case WAIT_TIMEOUT:
return 0;
}
throw omni_thread_fatal(GetLastError());
return 0; /* keep msvc++ happy */
}
void
omni_semaphore::post(void)
{
if (!ReleaseSemaphore(nt_sem, 1, NULL))
throw omni_thread_fatal(GetLastError());
}
///////////////////////////////////////////////////////////////////////////
//
// Thread
//
///////////////////////////////////////////////////////////////////////////
//
// Static variables
//
int omni_thread::init_t::count = 0;
omni_thread* omni_thread::root_thread;
omni_mutex* omni_thread::next_id_mutex;
int omni_thread::next_id = 0;
static DWORD self_tls_index;
//
// Initialisation function (gets called before any user code).
//
omni_thread::init_t::init_t(void)
{
if (count++ != 0) // only do it once however many objects get created.
return;
DB(cerr << "omni_thread::init: NT implementation initialising\n");
self_tls_index = TlsAlloc();
if (self_tls_index == 0xffffffff)
throw omni_thread_fatal(GetLastError());
next_id_mutex = new omni_mutex;
//
// Create object for this (i.e. initial) thread.
//
root_thread = new omni_thread;
root_thread->_state = STATE_RUNNING;
if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
GetCurrentProcess(), &root_thread->handle,
0, FALSE, DUPLICATE_SAME_ACCESS))
throw omni_thread_fatal(GetLastError());
root_thread->nt_id = GetCurrentThreadId();
DB(cerr << "initial thread " << root_thread->id() << " NT thread id " << root_thread->nt_id
<< endl);
if (!TlsSetValue(self_tls_index, (LPVOID) root_thread))
throw omni_thread_fatal(GetLastError());
if (!SetThreadPriority(root_thread->handle, nt_priority(PRIORITY_NORMAL)))
throw omni_thread_fatal(GetLastError());
}
omni_thread::init_t::~init_t (void)
{
if (--count != 0) // only do it once however many objects get created.
return;
delete root_thread;
delete next_id_mutex;
}
//
// Wrapper for thread creation.
//
extern "C"
#ifndef __BCPLUSPLUS__
unsigned __stdcall
#else
void _USERENTRY
#endif
omni_thread_wrapper(void* ptr)
{
omni_thread* me = (omni_thread*)ptr;
DB(cerr << "omni_thread_wrapper: thread " << me->id()
<< " started\n");
if (!TlsSetValue(self_tls_index, (LPVOID)me))
throw omni_thread_fatal(GetLastError());
//
// Now invoke the thread function with the given argument.
//
if (me->fn_void != NULL) {
(*me->fn_void)(me->thread_arg);
omni_thread::exit();
}
if (me->fn_ret != NULL) {
void* return_value = (*me->fn_ret)(me->thread_arg);
omni_thread::exit(return_value);
}
if (me->detached) {
me->run(me->thread_arg);
omni_thread::exit();
} else {
void* return_value = me->run_undetached(me->thread_arg);
omni_thread::exit(return_value);
}
// should never get here.
#ifndef __BCPLUSPLUS__
return 0;
#endif
}
//
// Constructors for omni_thread - set up the thread object but don't
// start it running.
//
// construct a detached thread running a given function.
omni_thread::omni_thread(void (*fn)(void*), void* arg, priority_t pri)
{
common_constructor(arg, pri, 1);
fn_void = fn;
fn_ret = NULL;
}
// construct an undetached thread running a given function.
omni_thread::omni_thread(void* (*fn)(void*), void* arg, priority_t pri)
{
common_constructor(arg, pri, 0);
fn_void = NULL;
fn_ret = fn;
}
// construct a thread which will run either run() or run_undetached().
omni_thread::omni_thread(void* arg, priority_t pri)
{
common_constructor(arg, pri, 1);
fn_void = NULL;
fn_ret = NULL;
}
// common part of all constructors.
void
omni_thread::common_constructor(void* arg, priority_t pri, int det)
{
_state = STATE_NEW;
_priority = pri;
next_id_mutex->lock();
_id = next_id++;
next_id_mutex->unlock();
thread_arg = arg;
detached = det; // may be altered in start_undetached()
cond_semaphore = CreateSemaphore(NULL, 0, SEMAPHORE_MAX, NULL);
if (cond_semaphore == NULL)
throw omni_thread_fatal(GetLastError());
cond_next = cond_prev = NULL;
cond_waiting = FALSE;
handle = NULL;
}
//
// Destructor for omni_thread.
//
omni_thread::~omni_thread(void)
{
DB(cerr << "destructor called for thread " << id() << endl);
if (handle && !CloseHandle(handle))
throw omni_thread_fatal(GetLastError());
if (cond_semaphore && !CloseHandle(cond_semaphore))
throw omni_thread_fatal(GetLastError());
}
//
// Start the thread
//
void
omni_thread::start(void)
{
omni_mutex_lock l(mutex);
if (_state != STATE_NEW)
throw omni_thread_invalid();
#ifndef __BCPLUSPLUS__
// MSVC++ or compatiable
unsigned int t;
handle = (HANDLE)_beginthreadex(
NULL,
0,
omni_thread_wrapper,
(LPVOID)this,
CREATE_SUSPENDED,
&t);
nt_id = t;
if (handle == NULL)
throw omni_thread_fatal(GetLastError());
#else
// Borland C++
handle = (HANDLE)_beginthreadNT(omni_thread_wrapper,
0,
(void*)this,
NULL,
CREATE_SUSPENDED,
&nt_id);
if (handle == INVALID_HANDLE_VALUE)
throw omni_thread_fatal(errno);
#endif
if (!SetThreadPriority(handle, this->nt_priority (_priority)))
throw omni_thread_fatal(GetLastError());
if (ResumeThread(handle) == 0xffffffff)
throw omni_thread_fatal(GetLastError());
_state = STATE_RUNNING;
}
//
// Start a thread which will run the member function run_undetached().
//
void
omni_thread::start_undetached(void)
{
if ((fn_void != NULL) || (fn_ret != NULL))
throw omni_thread_invalid();
detached = 0;
start();
}
//
// join - simply check error conditions & call WaitForSingleObject.
//
void
omni_thread::join(void** status)
{
mutex.lock();
if ((_state != STATE_RUNNING) && (_state != STATE_TERMINATED)) {
mutex.unlock();
throw omni_thread_invalid();
}
mutex.unlock();
if (this == self())
throw omni_thread_invalid();
if (detached)
throw omni_thread_invalid();
DB(cerr << "omni_thread::join: doing WaitForSingleObject\n");
if (WaitForSingleObject(handle, INFINITE) != WAIT_OBJECT_0)
throw omni_thread_fatal(GetLastError());
DB(cerr << "omni_thread::join: WaitForSingleObject succeeded\n");
if (status)
*status = return_val;
delete this;
}
//
// Change this thread's priority.
//
void
omni_thread::set_priority(priority_t pri)
{
omni_mutex_lock l(mutex);
if (_state != STATE_RUNNING)
throw omni_thread_invalid();
_priority = pri;
if (!SetThreadPriority(handle, nt_priority(pri)))
throw omni_thread_fatal(GetLastError());
}
//
// create - construct a new thread object and start it running. Returns thread
// object if successful, null pointer if not.
//
// detached version
omni_thread*
omni_thread::create(void (*fn)(void*), void* arg, priority_t pri)
{
omni_thread* t = new omni_thread(fn, arg, pri);
t->start();
return t;
}
// undetached version
omni_thread*
omni_thread::create(void* (*fn)(void*), void* arg, priority_t pri)
{
omni_thread* t = new omni_thread(fn, arg, pri);
t->start();
return t;
}
//
// exit() _must_ lock the mutex even in the case of a detached thread. This is
// because a thread may run to completion before the thread that created it has
// had a chance to get out of start(). By locking the mutex we ensure that the
// creating thread must have reached the end of start() before we delete the
// thread object. Of course, once the call to start() returns, the user can
// still incorrectly refer to the thread object, but that's their problem.
//
void
omni_thread::exit(void* return_value)
{
omni_thread* me = self();
if (me)
{
me->mutex.lock();
me->_state = STATE_TERMINATED;
me->mutex.unlock();
DB(cerr << "omni_thread::exit: thread " << me->id() << " detached "
<< me->detached << " return value " << return_value << endl);
if (me->detached) {
delete me;
} else {
me->return_val = return_value;
}
}
else
{
DB(cerr << "omni_thread::exit: called with a non-omnithread. Exit quietly." << endl);
}
#ifndef __BCPLUSPLUS__
// MSVC++ or compatiable
// _endthreadex() does not automatically closes the thread handle.
// The omni_thread dtor closes the thread handle.
_endthreadex(0);
#else
// Borland C++
// _endthread() does not automatically closes the thread handle.
// _endthreadex() is only available if __MFC_COMPAT__ is defined and
// all it does is to call _endthread().
_endthread();
#endif
}
omni_thread*
omni_thread::self(void)
{
LPVOID me;
me = TlsGetValue(self_tls_index);
if (me == NULL) {
DB(cerr << "omni_thread::self: called with a non-ominthread. NULL is returned." << endl);
}
return (omni_thread*)me;
}
void
omni_thread::yield(void)
{
Sleep(0);
}
#define MAX_SLEEP_SECONDS (DWORD)4294966 // (2**32-2)/1000
void
omni_thread::sleep(unsigned long secs, unsigned long nanosecs)
{
if (secs <= MAX_SLEEP_SECONDS) {
Sleep(secs * 1000 + nanosecs / 1000000);
return;
}
DWORD no_of_max_sleeps = secs / MAX_SLEEP_SECONDS;
for (DWORD i = 0; i < no_of_max_sleeps; i++)
Sleep(MAX_SLEEP_SECONDS * 1000);
Sleep((secs % MAX_SLEEP_SECONDS) * 1000 + nanosecs / 1000000);
}
void
omni_thread::get_time(unsigned long* abs_sec, unsigned long* abs_nsec,
unsigned long rel_sec, unsigned long rel_nsec)
{
get_time_now(abs_sec, abs_nsec);
*abs_nsec += rel_nsec;
*abs_sec += rel_sec + *abs_nsec / 1000000000;
*abs_nsec = *abs_nsec % 1000000000;
}
int
omni_thread::nt_priority(priority_t pri)
{
switch (pri) {
case PRIORITY_LOW:
return THREAD_PRIORITY_LOWEST;
case PRIORITY_NORMAL:
return THREAD_PRIORITY_NORMAL;
case PRIORITY_HIGH:
return THREAD_PRIORITY_HIGHEST;
}
throw omni_thread_invalid();
return 0; /* keep msvc++ happy */
}
static void
get_time_now(unsigned long* abs_sec, unsigned long* abs_nsec)
{
static int days_in_preceding_months[12]
= { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
static int days_in_preceding_months_leap[12]
= { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
SYSTEMTIME st;
GetSystemTime(&st);
*abs_nsec = st.wMilliseconds * 1000000;
// this formula should work until 1st March 2100
DWORD days = ((st.wYear - 1970) * 365 + (st.wYear - 1969) / 4
+ ((st.wYear % 4)
? days_in_preceding_months[st.wMonth - 1]
: days_in_preceding_months_leap[st.wMonth - 1])
+ st.wDay - 1);
*abs_sec = st.wSecond + 60 * (st.wMinute + 60 * (st.wHour + 24 * days));
}
|