summaryrefslogtreecommitdiffstats
path: root/lib/libUPnP/Neptune/Source/System/WinRT/NptWinRtThreads.cpp
blob: 034871864cc7814d6dba1d354c056da9b40610c6 (plain)
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
/*****************************************************************
|
|   Neptune - Threads :: WinRT Implementation
|
|   (c) 2001-2012 Gilles Boccon-Gibod
|   Author: Gilles Boccon-Gibod (bok@bok.net)
|
 ****************************************************************/

/*----------------------------------------------------------------------
|   includes
+---------------------------------------------------------------------*/
#include "NptWinRtPch.h"

using namespace Platform;
using namespace Windows::System::Threading;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Concurrency;

#include "NptConfig.h"
#include "NptTypes.h"
#include "NptConstants.h"
#include "NptThreads.h"
#include "NptDebug.h"
#include "NptResults.h"
#include "NptWinRtThreads.h"
#include "NptTime.h"
#include "NptSystem.h"
#include "NptLogging.h"

/*----------------------------------------------------------------------
|   logging
+---------------------------------------------------------------------*/
NPT_SET_LOCAL_LOGGER("neptune.threads.winrt")

/*----------------------------------------------------------------------
|   NPT_WinRtMutex::NPT_WinRtMutex
+---------------------------------------------------------------------*/
NPT_WinRtMutex::NPT_WinRtMutex()
{
    m_Handle = CreateMutexExW(NULL, L"", FALSE, MUTEX_ALL_ACCESS);
}

/*----------------------------------------------------------------------
|   NPT_WinRtMutex::~NPT_WinRtMutex
+---------------------------------------------------------------------*/
NPT_WinRtMutex::~NPT_WinRtMutex()
{
    CloseHandle(m_Handle);
}

/*----------------------------------------------------------------------
|   NPT_WinRtMutex::Lock
+---------------------------------------------------------------------*/
NPT_Result
NPT_WinRtMutex::Lock()
{
    DWORD result = WaitForSingleObjectEx(m_Handle, INFINITE, FALSE);
    if (result == WAIT_OBJECT_0) {
        return NPT_SUCCESS;
    } else {
        return NPT_FAILURE;
    }
}

/*----------------------------------------------------------------------
|   NPT_WinRtMutex::Unlock
+---------------------------------------------------------------------*/
NPT_Result
NPT_WinRtMutex::Unlock()
{
    ReleaseMutex(m_Handle);
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_Mutex::NPT_Mutex
+---------------------------------------------------------------------*/
NPT_Mutex::NPT_Mutex()
{
    m_Delegate = new NPT_WinRtMutex();
}

/*----------------------------------------------------------------------
|   NPT_WinRtCriticalSection::NPT_WinRtCriticalSection
+---------------------------------------------------------------------*/
NPT_WinRtCriticalSection::NPT_WinRtCriticalSection()
{
    InitializeCriticalSectionEx(&m_CriticalSection, 0, 0);
}

/*----------------------------------------------------------------------
|   NPT_WinRtCriticalSection::~NPT_WinRtCriticalSection
+---------------------------------------------------------------------*/
NPT_WinRtCriticalSection::~NPT_WinRtCriticalSection()
{
    DeleteCriticalSection(&m_CriticalSection);
}

/*----------------------------------------------------------------------
|   NPT_WinRtCriticalSection::Lock
+---------------------------------------------------------------------*/
NPT_Result
NPT_WinRtCriticalSection::Lock()
{
    EnterCriticalSection(&m_CriticalSection);
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_WinRtCriticalSection::Unlock
+---------------------------------------------------------------------*/
NPT_Result
NPT_WinRtCriticalSection::Unlock()
{
    LeaveCriticalSection(&m_CriticalSection);
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_WinRtEvent::NPT_WinRtEvent
+---------------------------------------------------------------------*/
NPT_WinRtEvent::NPT_WinRtEvent(bool manual /* = false */, bool initial /* = false */)
{
	DWORD flags = 0;
	if (manual)  flags |= CREATE_EVENT_MANUAL_RESET;
	if (initial) flags |= CREATE_EVENT_INITIAL_SET;
    m_Event = CreateEventExW(NULL, L"", flags, EVENT_ALL_ACCESS);
}

/*----------------------------------------------------------------------
|   NPT_WinRtEvent::~NPT_WinRtEvent
+---------------------------------------------------------------------*/
NPT_WinRtEvent::~NPT_WinRtEvent()
{
    CloseHandle(m_Event);
}

/*----------------------------------------------------------------------
|   NPT_WinRtEvent::Wait
+---------------------------------------------------------------------*/
NPT_Result
NPT_WinRtEvent::Wait(NPT_Timeout timeout)
{
    if (m_Event) {
        DWORD result = WaitForSingleObjectEx(m_Event, timeout==NPT_TIMEOUT_INFINITE?INFINITE:timeout, FALSE);
        if (result == WAIT_TIMEOUT) {
            return NPT_ERROR_TIMEOUT;
        }
        if (result != WAIT_OBJECT_0 && result != WAIT_ABANDONED) {
            return NPT_FAILURE;
        }
    }
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_WinRtEvent::Signal
+---------------------------------------------------------------------*/
void
NPT_WinRtEvent::Signal()
{
    SetEvent(m_Event);
}

/*----------------------------------------------------------------------
|   NPT_WinRtEvent::Reset
+---------------------------------------------------------------------*/
void
NPT_WinRtEvent::Reset()
{
    ResetEvent(m_Event);
}

/*----------------------------------------------------------------------
|       NPT_WinRtSharedVariable
+---------------------------------------------------------------------*/
class NPT_WinRtSharedVariable : public NPT_SharedVariableInterface
{
public:
    // methods
               NPT_WinRtSharedVariable(int value);
              ~NPT_WinRtSharedVariable();
    void       SetValue(int value);
    int        GetValue();
    NPT_Result WaitUntilEquals(int value, NPT_Timeout timeout);
    NPT_Result WaitWhileEquals(int value, NPT_Timeout timeout);

 private:
    // members
    volatile int       m_Value;
    CRITICAL_SECTION   m_Mutex;
    CONDITION_VARIABLE m_Condition;
};

/*----------------------------------------------------------------------
|       NPT_WinRtSharedVariable::NPT_WinRtSharedVariable
+---------------------------------------------------------------------*/
NPT_WinRtSharedVariable::NPT_WinRtSharedVariable(int value) : 
    m_Value(value)
{
    InitializeCriticalSectionEx(&m_Mutex, 0, 0);
    InitializeConditionVariable(&m_Condition);
}

/*----------------------------------------------------------------------
|       NPT_WinRtSharedVariable::~NPT_WinRtSharedVariable
+---------------------------------------------------------------------*/
NPT_WinRtSharedVariable::~NPT_WinRtSharedVariable()
{
    DeleteCriticalSection(&m_Mutex);
}

/*----------------------------------------------------------------------
|       NPT_WinRtSharedVariable::SetValue
+---------------------------------------------------------------------*/
void
NPT_WinRtSharedVariable::SetValue(int value)
{
    EnterCriticalSection(&m_Mutex);
    m_Value = value;
    WakeAllConditionVariable(&m_Condition);
    LeaveCriticalSection(&m_Mutex);
}

/*----------------------------------------------------------------------
|       NPT_WinRtSharedVariable::GetValue
+---------------------------------------------------------------------*/
int
NPT_WinRtSharedVariable::GetValue()
{
    // we assume that int read/write are atomic on the platform
    return m_Value;
}

/*----------------------------------------------------------------------
|       NPT_WinRtSharedVariable::WaitUntilEquals
+---------------------------------------------------------------------*/
NPT_Result
NPT_WinRtSharedVariable::WaitUntilEquals(int value, NPT_Timeout timeout)
{
    NPT_Result result = NPT_SUCCESS;
    
    EnterCriticalSection(&m_Mutex);
    while (value != m_Value) {
        if (!SleepConditionVariableCS(&m_Condition, &m_Mutex, timeout==NPT_TIMEOUT_INFINITE?INFINITE:timeout)) {
            DWORD error = GetLastError();
            if (error == ERROR_TIMEOUT) {
                result = NPT_ERROR_TIMEOUT;
            } else {
                result = NPT_FAILURE;
            }
        }
    }
    LeaveCriticalSection(&m_Mutex);
    
    return result;
}

/*----------------------------------------------------------------------
|       NPT_WinRtSharedVariable::WaitWhileEquals
+---------------------------------------------------------------------*/
NPT_Result
NPT_WinRtSharedVariable::WaitWhileEquals(int value, NPT_Timeout timeout)
{
    NPT_Result result = NPT_SUCCESS;

    EnterCriticalSection(&m_Mutex);
    while (value == m_Value) {
        if (!SleepConditionVariableCS(&m_Condition, &m_Mutex, timeout==NPT_TIMEOUT_INFINITE?INFINITE:timeout)) {
            DWORD error = GetLastError();
            if (error == ERROR_TIMEOUT) {
                result = NPT_ERROR_TIMEOUT;
            } else {
                result = NPT_FAILURE;
            }
        }
    }
    LeaveCriticalSection(&m_Mutex);
    
    return result;
}

/*----------------------------------------------------------------------
|       NPT_SharedVariable::NPT_SharedVariable
+---------------------------------------------------------------------*/
NPT_SharedVariable::NPT_SharedVariable(int value)
{
    m_Delegate = new NPT_WinRtSharedVariable(value);
}

#if 0
/*----------------------------------------------------------------------
|   NPT_WinRtSharedVariable
+---------------------------------------------------------------------*/
class NPT_WinRtSharedVariable : public NPT_SharedVariableInterface
{
 public:
    // methods
               NPT_WinRtSharedVariable(int value);
              ~NPT_WinRtSharedVariable();
    void       SetValue(int value);
    int        GetValue();
    NPT_Result WaitUntilEquals(int value, NPT_Timeout timeout = NPT_TIMEOUT_INFINITE);
    NPT_Result WaitWhileEquals(int value, NPT_Timeout timeout = NPT_TIMEOUT_INFINITE);

 private:
    // members
    volatile int   m_Value;
    NPT_Mutex      m_Lock;
    NPT_WinRtEvent m_Event;
};

/*----------------------------------------------------------------------
|   NPT_WinRtSharedVariable::NPT_WinRtSharedVariable
+---------------------------------------------------------------------*/
NPT_WinRtSharedVariable::NPT_WinRtSharedVariable(int value) : 
    m_Value(value)
{
}

/*----------------------------------------------------------------------
|   NPT_WinRtSharedVariable::~NPT_WinRtSharedVariable
+---------------------------------------------------------------------*/
NPT_WinRtSharedVariable::~NPT_WinRtSharedVariable()
{
}

/*----------------------------------------------------------------------
|   NPT_WinRtSharedVariable::SetValue
+---------------------------------------------------------------------*/
void
NPT_WinRtSharedVariable::SetValue(int value)
{
    m_Lock.Lock();
    if (value != m_Value) {
        m_Value = value;
        m_Event.Signal();
    }
    m_Lock.Unlock();
}

/*----------------------------------------------------------------------
|   NPT_WinRtSharedVariable::GetValue
+---------------------------------------------------------------------*/
int
NPT_WinRtSharedVariable::GetValue()
{
    // reading an integer should be atomic on all WinRt platforms
    return m_Value;
}

/*----------------------------------------------------------------------
|   NPT_WinRtSharedVariable::WaitUntilEquals
+---------------------------------------------------------------------*/
NPT_Result
NPT_WinRtSharedVariable::WaitUntilEquals(int value, NPT_Timeout timeout)
{
    do {
        m_Lock.Lock();
        if (m_Value == value) {
            break;
        }
        m_Lock.Unlock();
        {
             NPT_Result result = m_Event.Wait(timeout);
             if (NPT_FAILED(result)) return result;
        }
    } while (1);

    m_Lock.Unlock();
    
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_WinRtSharedVariable::WaitWhileEquals
+---------------------------------------------------------------------*/
NPT_Result
NPT_WinRtSharedVariable::WaitWhileEquals(int value, NPT_Timeout timeout)
{
    do {
        m_Lock.Lock();
        if (m_Value != value) {
            break;
        }
        m_Lock.Unlock();
        {
             NPT_Result result = m_Event.Wait(timeout);
             if (NPT_FAILED(result)) return result;
        }
    } while (1);

    m_Lock.Unlock();
    
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_SharedVariable::NPT_SharedVariable
+---------------------------------------------------------------------*/
NPT_SharedVariable::NPT_SharedVariable(int value)
{
    m_Delegate = new NPT_WinRtSharedVariable(value);
}
#endif

/*----------------------------------------------------------------------
|   NPT_WinRtAtomicVariable
+---------------------------------------------------------------------*/
class NPT_WinRtAtomicVariable : public NPT_AtomicVariableInterface
{
 public:
    // methods
                NPT_WinRtAtomicVariable(int value);
               ~NPT_WinRtAtomicVariable();
    int  Increment(); 
    int  Decrement();
    void SetValue(int value);
    int  GetValue();

 private:
    // members
    volatile LONG m_Value;
};

/*----------------------------------------------------------------------
|   NPT_WinRtAtomicVariable::NPT_WinRtAtomicVariable
+---------------------------------------------------------------------*/
NPT_WinRtAtomicVariable::NPT_WinRtAtomicVariable(int value) : 
    m_Value(value)
{
}

/*----------------------------------------------------------------------
|   NPT_WinRtAtomicVariable::~NPT_WinRtAtomicVariable
+---------------------------------------------------------------------*/
NPT_WinRtAtomicVariable::~NPT_WinRtAtomicVariable()
{
}

/*----------------------------------------------------------------------
|   NPT_WinRtAtomicVariable::Increment
+---------------------------------------------------------------------*/
int
NPT_WinRtAtomicVariable::Increment()
{
    return InterlockedIncrement(const_cast<LONG*>(&m_Value));
}

/*----------------------------------------------------------------------
|   NPT_WinRtAtomicVariable::Decrement
+---------------------------------------------------------------------*/
int
NPT_WinRtAtomicVariable::Decrement()
{
    return InterlockedDecrement(const_cast<LONG*>(&m_Value));
}

/*----------------------------------------------------------------------
|   NPT_WinRtAtomicVariable::SetValue
+---------------------------------------------------------------------*/
void
NPT_WinRtAtomicVariable::SetValue(int value)
{
    m_Value = value;
}

/*----------------------------------------------------------------------
|   NPT_WinRtAtomicVariable::GetValue
+---------------------------------------------------------------------*/
int
NPT_WinRtAtomicVariable::GetValue()
{
    return m_Value;
}

/*----------------------------------------------------------------------
|   NPT_AtomicVariable::NPT_AtomicVariable
+---------------------------------------------------------------------*/
NPT_AtomicVariable::NPT_AtomicVariable(int value)
{
    m_Delegate = new NPT_WinRtAtomicVariable(value);
}

/*----------------------------------------------------------------------
|   NPT_WinRtThread
+---------------------------------------------------------------------*/
class NPT_WinRtThread : public NPT_ThreadInterface
{
 public:
	// class methods
	static NPT_Result SetThreadPriority(HANDLE thread, int priority);

    // methods
                NPT_WinRtThread(NPT_Thread*   delegator,
                                NPT_Runnable& target,
                                bool          detached);
               ~NPT_WinRtThread();
    NPT_Result  Start(); 
    NPT_Result  Wait(NPT_Timeout timeout = NPT_TIMEOUT_INFINITE);
	NPT_Result  SetPriority(int priority);

 private:
    // methods
    static unsigned int __stdcall EntryPoint(void* argument);

    // NPT_Runnable methods
    void Run();

    // NPT_Interruptible methods
    NPT_Result Interrupt() { return NPT_ERROR_NOT_IMPLEMENTED; } 

    // members
    NPT_Thread*   m_Delegator;
    NPT_Runnable& m_Target;
    bool          m_Detached;
    HANDLE        m_ThreadHandle;
};

/*----------------------------------------------------------------------
|   NPT_WinRtThread::NPT_WinRtThread
+---------------------------------------------------------------------*/
NPT_WinRtThread::NPT_WinRtThread(NPT_Thread*   delegator,
                                 NPT_Runnable& target,
                                 bool          detached) : 
    m_Delegator(delegator),
    m_Target(target),
    m_Detached(detached),
    m_ThreadHandle(0)
{
}

/*----------------------------------------------------------------------
|   NPT_WinRtThread::~NPT_WinRtThread
+---------------------------------------------------------------------*/
NPT_WinRtThread::~NPT_WinRtThread()
{
    if (!m_Detached) {
        // we're not detached, and not in the Run() method, so we need to 
        // wait until the thread is done
        Wait();
    }

    // close the thread handle
    if (m_ThreadHandle) {
        CloseHandle(m_ThreadHandle);
    }
}

/*----------------------------------------------------------------------
|   NPT_WinRtThread::SetThreadPriority
+---------------------------------------------------------------------*/
NPT_Result
NPT_WinRtThread::SetThreadPriority(HANDLE thread, int priority)
{
	int WinRt_priority;
	if (priority < NPT_THREAD_PRIORITY_LOWEST) {
		WinRt_priority = THREAD_PRIORITY_IDLE;
	} else if (priority < NPT_THREAD_PRIORITY_BELOW_NORMAL) {
		WinRt_priority = THREAD_PRIORITY_LOWEST;
	} else if (priority < NPT_THREAD_PRIORITY_NORMAL) {
		WinRt_priority = THREAD_PRIORITY_BELOW_NORMAL;
	} else if (priority < NPT_THREAD_PRIORITY_ABOVE_NORMAL) {
		WinRt_priority = THREAD_PRIORITY_NORMAL;
	} else if (priority < NPT_THREAD_PRIORITY_HIGHEST) {
		WinRt_priority = THREAD_PRIORITY_ABOVE_NORMAL;
	} else if (priority < NPT_THREAD_PRIORITY_TIME_CRITICAL) {
		WinRt_priority = THREAD_PRIORITY_HIGHEST;
	} else {
		WinRt_priority = THREAD_PRIORITY_TIME_CRITICAL;
	}
#if 0
	BOOL result = ::SetThreadPriority(thread, WinRt_priority);
	if (!result) {
		NPT_LOG_WARNING_1("SetThreadPriority failed (%x)", GetLastError());
		return NPT_FAILURE;
	}
#endif

	return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_WinRtThread::Start
+---------------------------------------------------------------------*/
NPT_Result
NPT_WinRtThread::Start()
{
	if (m_ThreadHandle > 0) {
        // failed
        NPT_LOG_WARNING("thread already started !");
        return NPT_ERROR_INVALID_STATE;
    }

    NPT_LOG_FINER("creating thread");

	// create a stack local variable 'detached', as this object
    // may already be deleted when the thread creation returns and
    // before we get to call detach on the given thread
    bool detached = m_Detached;

    HANDLE thread_handle = CreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);

    auto handler = ref new WorkItemHandler([=](IAsyncAction^)
    {
        // run the thread routine
        NPT_LOG_FINE("+++ thread routine start +++");
        try {
            Run();
        } catch(...) {
            NPT_LOG_FINE("*** exception caught during thread routine ***");
        }
        NPT_LOG_FINE("--- thread routine done +++");
        
        // signal that we're done
        SetEvent(thread_handle);

        // if the thread is detached, delete it
        if (detached) {
            delete m_Delegator;
            CloseHandle(thread_handle);
        }
    });

    // remember the handle unless we're detached
    if (!detached) {
        m_ThreadHandle = thread_handle;
    }

    // run the thread
    ThreadPool::RunAsync(handler, WorkItemPriority::Normal, WorkItemOptions::TimeSliced);

    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_WinRtThread::Run
+---------------------------------------------------------------------*/
void
NPT_WinRtThread::Run()
{
    m_Target.Run();
}

/*----------------------------------------------------------------------
|   NPT_WinRtThread::SetPriority
+---------------------------------------------------------------------*/
NPT_Result 
NPT_WinRtThread::SetPriority(int priority)
{
	if (m_ThreadHandle == 0) return NPT_ERROR_INVALID_STATE;
	return NPT_WinRtThread::SetThreadPriority(m_ThreadHandle, priority);
}

/*----------------------------------------------------------------------
|   NPT_WinRtThread::Wait
+---------------------------------------------------------------------*/
NPT_Result
NPT_WinRtThread::Wait(NPT_Timeout timeout /* = NPT_TIMEOUT_INFINITE */)
{
    // check that we're not detached
    if (m_ThreadHandle == 0 || m_Detached) {
        return NPT_FAILURE;
    }

    // wait for the thread to finish
    // Logging here will cause a crash on exit because LogManager may already be destroyed
    DWORD result = WaitForSingleObjectEx(m_ThreadHandle, 
                                         timeout==NPT_TIMEOUT_INFINITE?INFINITE:timeout,
										 FALSE);
    if (result != WAIT_OBJECT_0) {
        return NPT_ERROR_TIMEOUT;
    } else {
        return NPT_SUCCESS;
    }
}

/*----------------------------------------------------------------------
|   NPT_Thread::GetCurrentThreadId
+---------------------------------------------------------------------*/
NPT_Thread::ThreadId 
NPT_Thread::GetCurrentThreadId()
{
    return ::GetCurrentThreadId();
}

/*----------------------------------------------------------------------
|   NPT_Thread::SetCurrentThreadPriority
+---------------------------------------------------------------------*/
NPT_Result 
NPT_Thread::SetCurrentThreadPriority(int priority)
{
	return NPT_WinRtThread::SetThreadPriority(::GetCurrentThread(), priority);
}

/*----------------------------------------------------------------------
|   NPT_Thread::NPT_Thread
+---------------------------------------------------------------------*/
NPT_Thread::NPT_Thread(bool detached)
{
    m_Delegate = new NPT_WinRtThread(this, *this, detached);
}

/*----------------------------------------------------------------------
|   NPT_Thread::NPT_Thread
+---------------------------------------------------------------------*/
NPT_Thread::NPT_Thread(NPT_Runnable& target, bool detached)
{
    m_Delegate = new NPT_WinRtThread(this, target, detached);
}