Home | History | Annotate | Download | only in speculative
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Created by:  julie.n.fleischer 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 that timer_create() returns a unique timer ID for the current
      9  * process.
     10  * For this test, signal SIGALRM will be used, clock CLOCK_REALTIME
     11  * will be used.
     12  */
     13 
     14 #include <errno.h>
     15 #include <time.h>
     16 #include <signal.h>
     17 #include <stdio.h>
     18 #include <unistd.h>
     19 #include <stdlib.h>
     20 #include <search.h>
     21 #include <limits.h>
     22 #include "posixtest.h"
     23 
     24 int compare(const void *key, const void *amemb)
     25 {
     26 	if (*(timer_t *) key == *(timer_t *) amemb) {
     27 		return 0;
     28 	} else {
     29 		return 1;
     30 	}
     31 }
     32 
     33 int main(void)
     34 {
     35 	struct sigevent ev;
     36 	timer_t tid;
     37 	timer_t *tids;
     38 	size_t i;
     39 
     40 	ev.sigev_notify = SIGEV_SIGNAL;
     41 	ev.sigev_signo = SIGALRM;
     42 
     43 #if defined DEBUG && defined TIMER_MAX
     44 	printf("Max timers is %ld\n", (long)TIMER_MAX);
     45 	int max = TIMER_MAX;
     46 #else
     47 	int max = 256;
     48 #endif
     49 	tids = malloc(max * sizeof(timer_t));
     50 	if (tids == NULL) {
     51 		perror("malloc failed\n");
     52 		return PTS_UNRESOLVED;
     53 	}
     54 
     55 	for (i = 0; i < max; i++) {
     56 		if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
     57 #ifndef TIMER_MAX
     58 			if (errno == EAGAIN)
     59 				break;
     60 #endif
     61 			perror("timer_create() did not return success\n");
     62 			return PTS_UNRESOLVED;
     63 		}
     64 
     65 		tids[i] = tid;
     66 		if (lfind(&tid, tids, &i, sizeof(timer_t), compare) != NULL) {
     67 			printf("Duplicate tid found %ld\n", (long)tid);
     68 			printf("Test FAILED\n");
     69 			return PTS_FAIL;
     70 		}
     71 	}
     72 
     73 	printf("No duplicate tids found\n");
     74 	printf("Test PASSED\n");
     75 	return PTS_PASS;
     76 }
     77