1/*
2 * Copyright (c) 2004, Intel Corporation. All rights reserved.
3 * Created by:  crystal.xiong REMOVE-THIS AT intel DOT com
4 * This file is licensed under the GPL license.  For the full content
5 * of this license, see the COPYING file at the top level of this
6 * source tree.
7
8 * Test pthread_attr_setinheritsched()
9 *
10 * Steps:
11 * 1.  Initialize pthread_attr_t object (attr)
12 * 2.  Set schedule policy (policy) in attr to SCHED_FIFO
13 * 3.  Set inheritsched to PTHREAD_EXPLICIT_SCHED in attr
14 * 4.  Call pthread_create with attr
15 * 5.  Call pthread_getschedparam in the created thread and get the
16 *     policy value(new_policy)
17 * 6.  Compare new_policy with SCHED_OTHER. SCHED_OTHER is the
18 *     default policy value in the creating thread. if new_policy is
19 *     equal to SCHED_OTHER, the case fails.
20 *
21 * - adam.li@intel.com: 2004-04-30
22 *  add code pthread_attr_setschedparam(). Otherwise pthread_create()
23 *  will fail with EINVAL. And, the perror() thing is not correct.
24 */
25
26#include <errno.h>
27#include <pthread.h>
28#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include "posixtest.h"
32
33#define TEST "2-2"
34#define FUNCTION "pthread_attr_setinheritsched"
35#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
36
37const long int policy = SCHED_FIFO;
38const long int old_policy = SCHED_OTHER;
39void *thread_func(void* arg)
40{
41	int rc;
42	int new_policy;
43	pthread_t self = pthread_self();
44
45	struct sched_param param;
46        memset(&param, 0, sizeof(param));
47
48	rc = pthread_getschedparam(self, &new_policy, &param);
49        if (rc != 0 ) {
50                perror(ERROR_PREFIX "pthread_getschedparam");
51                exit(PTS_UNRESOLVED);
52        }
53	if (new_policy == old_policy) {
54		fprintf(stderr, ERROR_PREFIX "The scheduling attribute should "
55                        "not be inherited from creating thread \n");
56		exit(PTS_FAIL);
57	}
58	pthread_exit(0);
59	return NULL;
60}
61int main()
62{
63	pthread_t new_th;
64	pthread_attr_t attr;
65	int rc;
66	struct sched_param sp;
67
68	/* Initialize attr */
69	rc = pthread_attr_init(&attr);
70	if( rc != 0) {
71		printf(ERROR_PREFIX "pthread_attr_init");
72		exit(PTS_UNRESOLVED);
73	}
74
75	rc = pthread_attr_setschedpolicy(&attr, policy);
76	if (rc != 0 ) {
77		printf(ERROR_PREFIX "pthread_attr_setschedpolicy");
78		exit(PTS_UNRESOLVED);
79        }
80
81	sp.sched_priority = 1;
82	rc = pthread_attr_setschedparam(&attr, &sp);
83	if (rc != 0 ) {
84		printf(ERROR_PREFIX "pthread_attr_setschedparam");
85		exit(PTS_UNRESOLVED);
86        }
87
88	int insched = PTHREAD_EXPLICIT_SCHED;
89	rc = pthread_attr_setinheritsched(&attr, insched);
90	if (rc != 0 ) {
91		printf(ERROR_PREFIX "pthread_attr_setinheritsched");
92		exit(PTS_UNRESOLVED);
93        }
94
95	rc = pthread_create(&new_th, &attr, thread_func, NULL);
96	if (rc !=0 ) {
97		printf("Error at pthread_create(): %s\n", strerror(rc));
98                exit(PTS_UNRESOLVED);
99        }
100
101	rc = pthread_join(new_th, NULL);
102	if(rc != 0)
103        {
104                printf(ERROR_PREFIX "pthread_join");
105		exit(PTS_UNRESOLVED);
106        }
107	rc = pthread_attr_destroy(&attr);
108	if(rc != 0)
109        {
110                printf(ERROR_PREFIX "pthread_attr_destroy");
111		exit(PTS_UNRESOLVED);
112	}
113	printf("Test PASSED\n");
114	return PTS_PASS;
115}
116
117
118