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#include <pthread.h>
29e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#include <semaphore.h>
30e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#include <errno.h>
31e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#include <stdio.h>
32e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#include <time.h>
33e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#include <string.h>
34e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#include <unistd.h>
35e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
36e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project/* a simple semaphore test, using three threads
37e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project *
38e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project *  a semaphore is initialized with a value of 1
39e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project *
40e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project *  Thread 1, 2 and 3 start at the same time
41e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project *
42e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project *  Thread 1 takes the semaphore, then sleeps for 2 seconds, then post the semaphore
43e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project *  Thread 2 sleeps for 1 second, then waits the semaphore, sleeps for 2 seconds, then post the semaphoe
44e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project *  Thread 3 sleeps 3 seconds, waits for the semaphore
45e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project */
46e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
47e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectstatic  sem_t   semaphore;
48e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
49e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectstatic void*
50e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project_thread1( void* unused )
51e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project{
52e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    printf( "thread 1: waiting for semaphore\n" );
53e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    if ( sem_wait( &semaphore ) < 0 ) {
54e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        printf( "thread 1: could not wait for semaphore: %s\n", strerror(errno) );
55e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        return NULL;
56e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
57e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    printf( "thread 1: got the semaphore ! sleeping for 2 seconds\n" );
58e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    sleep( 2 );
59e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    printf( "thread 1: awake !! posting semaphore\n" );
60e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    if ( sem_post( &semaphore ) < 0 ) {
61e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        printf( "thread 2: could not post semaphore: %s\n", strerror(errno) );
62e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
63e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    printf( "thread 1: quitting\n" );
64e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    return NULL;
65e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project}
66e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
67e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectstatic void*
68e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project_thread2( void* unused )
69e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project{
70e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    printf( "thread 2: sleeping for 1 second\n" );
71e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    sleep(1);
72e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    printf( "thread 2: awake !! waiting for semaphore\n" );
73e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    if ( sem_wait( &semaphore ) < 0 ) {
74e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        printf( "thread 2: could not wait for semaphore: %s\n", strerror(errno) );
75e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        return NULL;
76e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
77e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    printf( "thread 2: got the semaphore ! sleeping for 2 seconds\n" );
78e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    sleep( 2 );
79e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    printf( "thread 2: awake !! posting semaphore\n" );
80e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    if ( sem_post( &semaphore ) < 0 ) {
81e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        printf( "thread 2: could not post semaphore: %s\n", strerror(errno) );
82e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
83e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    printf( "thread 2: quitting\n" );
84e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    return NULL;
85e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project}
86e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
87e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
88e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectstatic void*
89e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project_thread3( void* unused )
90e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project{
91e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    printf( "thread 3: sleeping for 3 seconds\n" );
92e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    sleep(3);
93e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    printf( "thread 3: awake !! waiting for semaphore\n" );
94e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    if ( sem_wait( &semaphore ) < 0 ) {
95e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        printf( "thread 3: could not wait for semaphore: %s\n", strerror(errno) );
96e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        return NULL;
97e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
98e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    printf( "thread 3: got semaphore. quitting\n" );
99e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    return NULL;
100e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project}
101e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
102e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projecttypedef void*  (*thread_func)(void*);
103e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
104e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectstatic const  thread_func  thread_routines[] =
105e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project{
106e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    &_thread1,
107e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    &_thread2,
108e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    &_thread3
109e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project};
110e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
111e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectint  main( void )
112e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project{
113e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    pthread_t   t[3];
114e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    int         nn;
115e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
116e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    if ( sem_init( &semaphore, 0, 1 ) < 0 ) {
117e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        printf( "could not initialize semaphore: %s\n", strerror(errno) );
118e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        return -1;
119e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
120e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
121e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    for ( nn = 0; nn < 3; nn++ ) {
122e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        if ( pthread_create( &t[nn], NULL, thread_routines[nn], NULL ) < 0 ) {
123e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project            printf("could not create thread %d: %s\n", nn+1, strerror(errno) );
124e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project            return -2;
125e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        }
126e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
127e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    sleep( 5 );
128e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    return 0;
129e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project}
130