1/*
2 * Copyright (c) 2004, QUALCOMM Inc. All rights reserved.
3 * Created by:  abisain REMOVE-THIS AT qualcomm 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 * pthread_attr_setschedparam()
9
10 * 1. Create a pthread_attr object and initialize it
11 * 2. Set the policy and priority in that object
12 * 3. Create a thread with this object
13 * Test SCHED_FIFO
14 */
15
16#include <pthread.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <errno.h>
20#include "posixtest.h"
21
22#define TEST "1-1"
23#define FUNCTION "pthread_attr_setschedparam"
24#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
25
26#define FIFOPOLICY SCHED_FIFO
27
28volatile int thread_created = 0;
29
30void *thread_func()
31{
32	thread_created = 1;
33	pthread_exit(0);
34	return (void*)(0);
35}
36
37int main()
38{
39	pthread_t              thread;
40	pthread_attr_t         attr;
41 	void                   *status;
42 	int                    rc=0;
43	int                    policy = FIFOPOLICY;
44	struct sched_param     param;
45	int                    priority;
46
47	rc = pthread_attr_init(&attr);
48	if(rc != 0) {
49		printf(ERROR_PREFIX "pthread_attr_init\n");
50		exit(PTS_UNRESOLVED);
51	}
52
53	rc = pthread_attr_setschedpolicy(&attr, policy);
54	if(rc != 0) {
55		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
56		exit(PTS_FAIL);
57	}
58
59	priority = sched_get_priority_max(policy);
60	if(priority == -1) {
61		printf(ERROR_PREFIX "sched_priority_get_max\n");
62 		exit(PTS_FAIL);
63	}
64	param.sched_priority = priority;
65	rc = pthread_attr_setschedparam(&attr, &param);
66	if(rc != 0) {
67		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
68 		exit(PTS_FAIL);
69	}
70
71	rc = pthread_create(&thread, &attr, thread_func, NULL);
72	if(rc != 0) {
73		if (rc == EPERM) {
74			printf(ERROR_PREFIX "Permission Denied when creating thread with policy %d\n", policy);
75			exit(PTS_UNRESOLVED);
76		} else {
77			printf(ERROR_PREFIX "pthread_create()\n");
78			exit(PTS_FAIL);
79		}
80	}
81
82	pthread_join(thread, &status);
83	pthread_attr_destroy(&attr);
84
85	if(thread_created == 0 ) {
86		printf(ERROR_PREFIX "Thread was not created\n");
87		exit(PTS_FAIL);
88	}
89
90	printf("Test PASS\n");
91	exit(PTS_PASS);
92}
93