Home | History | Annotate | Download | only in pthread_attr_setscope
      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_setscope()
      9  *
     10  * Steps:
     11  * 1.  Initialize pthread_attr_t object (attr)
     12  * 2.  sets the contentionscope to attr
     13  * 3.  create a thread with the attr
     14  * 4.  Get the contentionscope value in the created thread
     15  */
     16 
     17 #include <pthread.h>
     18 #include <stdio.h>
     19 #include <string.h>
     20 #include <stdlib.h>
     21 #include <errno.h>
     22 #include "posixtest.h"
     23 
     24 #define TEST "1-1"
     25 #define FUNCTION "pthread_attr_setscope"
     26 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
     27 
     28 #define CONSCOPE PTHREAD_SCOPE_SYSTEM
     29 
     30 void *thread_func()
     31 {
     32 	pthread_exit(0);
     33 	return NULL;
     34 }
     35 
     36 int main(void)
     37 {
     38 	pthread_t new_th;
     39 	pthread_attr_t attr;
     40 	int cscope;
     41 	int rc;
     42 
     43 	/* Initialize attr */
     44 	rc = pthread_attr_init(&attr);
     45 	if (rc != 0) {
     46 		perror(ERROR_PREFIX "pthread_attr_init");
     47 		exit(PTS_UNRESOLVED);
     48 	}
     49 
     50 	rc = pthread_attr_setscope(&attr, CONSCOPE);
     51 	if (rc != 0) {
     52 		perror(ERROR_PREFIX "PTHREAD_SCOPE_SYSTEM is not supported");
     53 		exit(PTS_UNRESOLVED);
     54 	}
     55 
     56 	rc = pthread_create(&new_th, &attr, thread_func, NULL);
     57 	if (rc != 0) {
     58 		perror(ERROR_PREFIX "pthread_create");
     59 		exit(PTS_UNRESOLVED);
     60 	}
     61 
     62 	rc = pthread_attr_getscope(&attr, &cscope);
     63 	if (rc != 0) {
     64 		perror(ERROR_PREFIX "pthread_attr_getscope");
     65 		exit(PTS_UNRESOLVED);
     66 	}
     67 
     68 	if (cscope != CONSCOPE) {
     69 		fprintf(stderr, ERROR_PREFIX "The contentionscope is not "
     70 			"correct \n");
     71 		exit(PTS_FAIL);
     72 	}
     73 
     74 	rc = pthread_join(new_th, NULL);
     75 	if (rc != 0) {
     76 		perror(ERROR_PREFIX "pthread_join");
     77 		exit(PTS_UNRESOLVED);
     78 	}
     79 
     80 	rc = pthread_attr_destroy(&attr);
     81 	if (rc != 0) {
     82 		perror(ERROR_PREFIX "pthread_attr_destroy");
     83 		exit(PTS_UNRESOLVED);
     84 	}
     85 
     86 	printf("Test PASSED\n");
     87 	return PTS_PASS;
     88 }
     89