summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/GetCurrentThread/test2/test2.cpp
blob: beeb5ec241f731fc1dcce8c1eefe3e84553eb882 (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*=============================================================================
**
** Source: test2.c
**
** Dependencies: PAL_Initialize
**               PAL_Terminate
**               Fail
**               CreateThread
**               SetThreadPriority
**               GetThreadPriority
**               ResumeThread
**               WaitForSingleObject
**               GetLastError
**
** Purpose:
**
** Test to ensure proper operation of the GetCurrentThread()
** API. The test launches a thread in suspended mode, and sets
** its priority to a non-default value using the handle returned
** by CreateThread(). The new thread calls GetCurrentThred() to
** retrieve a handle to itself, and calls GetThreadPriority()
** to verify that its priority matches what it was set to on
** the main execution thread.
**

**
**===========================================================================*/
#include <palsuite.h>


/* we store the return code from the child thread here because */
/* we're missing the GetExitCodeThread() API                   */

static int g_priority = 0;

/**
 * ThreadFunc
 *
 * Thread function that calls GetCurrentThread() to get a pseudo-handle
 * to itself, then checks its priority and exits with that value.
 */
DWORD PALAPI ThreadFunc( LPVOID param )
{
    int      priority;
    HANDLE   hThread;

    /* call GetCurrentThread() to get a pseudo-handle to */
    /* the current thread                                */
    hThread = GetCurrentThread();
    if( hThread == NULL )
    {
        Fail( "GetCurrentThread() call failed\n" );
    }


    /* get the current thread priority */
    priority = GetThreadPriority( hThread );
    if( priority == THREAD_PRIORITY_ERROR_RETURN )
    {
        /* GetThreadPriority call failed */
        Fail( "ERROR:%lu:GetThreadPriority() call failed\n", GetLastError() );
    }

    /* store this globally because we don't have GetExitCodeThread() */
    g_priority = priority;
    return (DWORD)priority;
}


/**
 * main
 *
 * executable entry point
 */
INT __cdecl main( INT argc, CHAR **argv )
{
    HANDLE   hThread = NULL;
    DWORD    IDThread;
    DWORD    dwRet;

    SIZE_T i = 0;

    /* PAL initialization */
    if( (PAL_Initialize(argc, argv)) != 0 )
    {
        return( FAIL );
    }

#if !HAVE_SCHED_OTHER_ASSIGNABLE
    /* Defining thread priority for SCHED_OTHER is implementation defined.
       Some platforms like NetBSD cannot reassign it as they are dynamic.
    */
    printf("paltest_getcurrentthread_test2 has been disabled on this platform\n");
#else
    /* Create multiple threads. */
    hThread = CreateThread(    NULL,         /* no security attributes    */
                               0,            /* use default stack size    */
      (LPTHREAD_START_ROUTINE) ThreadFunc,   /* thread function           */
                      (LPVOID) i,            /* pass thread index as      */
                                             /* function argument         */
                               CREATE_SUSPENDED, /* create suspended      */
                               &IDThread );  /* returns thread identifier */

    /* Check the return value for success. */
    if( hThread == NULL )
    {
        /* ERROR */
        Fail( "ERROR:%lu:CreateThread failed\n", GetLastError() );
    }

    /* set the thread priority of the new thread to the highest value */
    if( ! SetThreadPriority( hThread, THREAD_PRIORITY_TIME_CRITICAL) )
    {
        Fail( "ERROR:%lu:SetThreadPriority() call failed\n", GetLastError() );
    }

    /* let the child thread run now */
    ResumeThread( hThread );


    /* wait for the thread to finish */
    dwRet = WaitForSingleObject( hThread, INFINITE );
    if( dwRet == WAIT_FAILED )
    {
        /* ERROR */
        Fail( "ERROR:%lu:WaitForSingleObject call failed\n", GetLastError() );
    }

    /* validate the thread's exit code */
    if( g_priority != THREAD_PRIORITY_TIME_CRITICAL )
    {
        /* ERROR */
        Fail( "FAIL:Unexpected thread priority %d returned, expected %d\n",
                g_priority, THREAD_PRIORITY_TIME_CRITICAL );
    }
#endif

    PAL_Terminate();
    return PASS;
}