1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28/* this is a small test for pthread_getcpuclockid() and clock_gettime() */
29
30#include <time.h>
31#include <pthread.h>
32#include <stdio.h>
33#include <errno.h>
34#include <string.h>
35
36static pthread_mutex_t  lock = PTHREAD_MUTEX_INITIALIZER;
37
38static void*
39thread_func( void*  arg )
40{
41    pthread_t         self = pthread_self();
42    struct timespec   ts;
43    clockid_t         clock;
44    int               e;
45
46    pthread_mutex_lock( &lock );
47
48    e = pthread_getcpuclockid( self, &clock );
49    if (e != 0) {
50        fprintf(stderr, "pthread_getcpuclockid(%08lx,) returned error %d: %s\n", self, e, strerror(e));
51        pthread_mutex_unlock( &lock );
52        return NULL;
53    }
54
55    ts.tv_sec  = 0;
56    ts.tv_nsec = 300000000 + ((int)arg)*50000000;
57    nanosleep( &ts, &ts );
58
59    clock_gettime( clock, &ts );
60    fprintf(stderr, "thread %08lx: clock_gettime() returned %g nsecs\n", self, ts.tv_sec*1e9 + ts.tv_nsec);
61
62    pthread_mutex_unlock( &lock );
63
64    return NULL;
65}
66
67#define  MAX_THREADS   16
68
69int  main( void )
70{
71    int             nn;
72    pthread_attr_t  attr;
73    pthread_t       threads[MAX_THREADS];
74
75    pthread_attr_init(&attr);
76    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
77
78    for (nn = 0; nn < MAX_THREADS; nn++) {
79        pthread_create( &threads[nn], &attr, thread_func, (void*)nn );
80    }
81    for (nn = 0; nn < MAX_THREADS; nn++) {
82        void*  dummy;
83        pthread_join( threads[nn], &dummy );
84    }
85    return 0;
86}
87