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_INHERIT_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 pass.
20 */
21
22#include <pthread.h>
23#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include "posixtest.h"
27
28#define TEST "2-1"
29#define FUNCTION "pthread_attr_setinheritsched"
30#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
31
32const long int policy = SCHED_FIFO;
33void *thread_func()
34{
35	int rc;
36	int new_policy;
37	pthread_t self = pthread_self();
38
39	struct sched_param param;
40        memset(&param, 0, sizeof(param));
41
42	rc = pthread_getschedparam(self, &new_policy, &param);
43        if (rc != 0 ) {
44                perror(ERROR_PREFIX "pthread_getschedparam");
45                exit(PTS_UNRESOLVED);
46        }
47	if (new_policy == policy) {
48		fprintf(stderr, ERROR_PREFIX "The scheduling attribute is not "
49		       "inherited from creating thread \n");
50		exit(PTS_FAIL);
51	}
52	pthread_exit(0);
53	return NULL;
54}
55int main()
56{
57	pthread_t new_th;
58	pthread_attr_t attr;
59	int rc;
60
61	/* Initialize attr */
62	rc = pthread_attr_init(&attr);
63	if( rc != 0) {
64		perror(ERROR_PREFIX "pthread_attr_init");
65		exit(PTS_UNRESOLVED);
66	}
67
68	rc = pthread_attr_setschedpolicy(&attr, policy);
69	if (rc != 0 ) {
70		perror(ERROR_PREFIX "pthread_attr_setschedpolicy");
71		exit(PTS_UNRESOLVED);
72        }
73
74	int insched = PTHREAD_INHERIT_SCHED;
75	rc = pthread_attr_setinheritsched(&attr, insched);
76	if (rc != 0 ) {
77		perror(ERROR_PREFIX "pthread_attr_setinheritsched");
78		exit(PTS_UNRESOLVED);
79        }
80
81	rc = pthread_create(&new_th, &attr, thread_func, NULL);
82	if (rc !=0 ) {
83		perror(ERROR_PREFIX "pthread_create");
84                exit(PTS_UNRESOLVED);
85        }
86
87	rc = pthread_join(new_th, NULL);
88	if(rc != 0)
89        {
90                perror(ERROR_PREFIX "pthread_join");
91		exit(PTS_UNRESOLVED);
92        }
93	rc = pthread_attr_destroy(&attr);
94	if(rc != 0)
95        {
96                perror(ERROR_PREFIX "pthread_attr_destroy");
97		exit(PTS_UNRESOLVED);
98        }
99
100	printf("Test PASSED\n");
101	return PTS_PASS;
102}
103
104
105