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