Home | History | Annotate | Download | only in pthread_create
      1 /*
      2  * Copyright (c) 2004, Intel Corporation. All rights reserved.
      3  * This file is licensed under the GPL license.  For the full content
      4  * of this license, see the COPYING file at the top level of this
      5  * source tree.
      6  *
      7  * adam.li (at) intel.com
      8  *
      9  * if _POSIX_THREAD_CPUTIME is defined, the new thread shall have a CPU-time
     10  * clock accessible, and the initial value of this clock shall be set to 0.
     11  */
     12  /* Do some work in the main thread so that it's clock value is non zero.
     13   * Create a new thread and get the time of the thread CUP-time clock
     14   * using clock_gettime().
     15   * Note, the tv_nsec cannot be exactly 0 at the time of calling
     16   * clock_gettime() since the thread has executed some time.
     17   */
     18 
     19 #define _XOPEN_SOURCE 600
     20 #include <unistd.h>
     21 #include <pthread.h>
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <time.h>
     25 #include <string.h>
     26 #include <signal.h>
     27 #include <sys/time.h>
     28 #include "posixtest.h"
     29 
     30 static void *a_thread_func()
     31 {
     32 	struct timespec ts = {.tv_sec = 1,.tv_nsec = 1 };
     33 
     34 	clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
     35 
     36 	/* We expect the clock to be less than 0.1s */
     37 	if (ts.tv_sec != 0 || ts.tv_nsec >= 100000000) {
     38 		printf("FAIL: ts.tv_sec: %ld, ts.tv_nsec: %ld\n",
     39 		       ts.tv_sec, ts.tv_nsec);
     40 		exit(PTS_FAIL);
     41 	}
     42 
     43 	return NULL;
     44 }
     45 
     46 static volatile sig_atomic_t flag = 1;
     47 
     48 static void alarm_handler()
     49 {
     50 	flag = 0;
     51 }
     52 
     53 int main(void)
     54 {
     55 	int ret;
     56 	struct itimerval it = {.it_value = {.tv_sec = 0, .tv_usec = 100000}};
     57 
     58 #if _POSIX_THREAD_CPUTIME == -1
     59 	printf("_POSIX_THREAD_CPUTIME not supported\n");
     60 	return PTS_UNSUPPORTED;
     61 #endif
     62 	pthread_t new_th;
     63 
     64 	if (sysconf(_SC_THREAD_CPUTIME) == -1) {
     65 		printf("_POSIX_THREAD_CPUTIME not supported\n");
     66 		return PTS_UNSUPPORTED;
     67 	}
     68 
     69 	/* Let's do some work in the main thread so that it's cputime > 0 */
     70 	if (signal(SIGVTALRM, alarm_handler)) {
     71 		perror("signal()");
     72 		return PTS_UNRESOLVED;
     73 	}
     74 
     75 	if (setitimer(ITIMER_VIRTUAL, &it, NULL)) {
     76 		perror("setitimer(ITIMER_VIRTUAL, ...)");
     77 		return PTS_UNRESOLVED;
     78 	}
     79 
     80 	while (flag);
     81 
     82 	ret = pthread_create(&new_th, NULL, a_thread_func, NULL);
     83 	if (ret) {
     84 		fprintf(stderr, "pthread_create(): %s\n", strerror(ret));
     85 		return PTS_UNRESOLVED;
     86 	}
     87 
     88 	pthread_join(new_th, NULL);
     89 	printf("Test PASSED\n");
     90 	return PTS_PASS;
     91 }
     92