1 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef __QCAMERA_SEMAPHORE_H__
31 #define __QCAMERA_SEMAPHORE_H__
32 
33 // System dependencies
34 #include <pthread.h>
35 #include <errno.h>
36 #include "cam_cond.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /* Implement semaphore with mutex and conditional variable.
43  * Reason being, POSIX semaphore on Android are not used or
44  * well tested.
45  */
46 
47 typedef struct {
48     int val;
49     pthread_mutex_t mutex;
50     pthread_cond_t cond;
51 } cam_semaphore_t;
52 
cam_sem_init(cam_semaphore_t * s,int n)53 static inline void cam_sem_init(cam_semaphore_t *s, int n)
54 {
55     pthread_mutex_init(&(s->mutex), NULL);
56     PTHREAD_COND_INIT(&(s->cond));
57     s->val = n;
58 }
59 
cam_sem_post(cam_semaphore_t * s)60 static inline void cam_sem_post(cam_semaphore_t *s)
61 {
62     pthread_mutex_lock(&(s->mutex));
63     s->val++;
64     pthread_cond_signal(&(s->cond));
65     pthread_mutex_unlock(&(s->mutex));
66 }
67 
cam_sem_wait(cam_semaphore_t * s)68 static inline int cam_sem_wait(cam_semaphore_t *s)
69 {
70     int rc = 0;
71     pthread_mutex_lock(&(s->mutex));
72     while (s->val == 0)
73         rc = pthread_cond_wait(&(s->cond), &(s->mutex));
74     s->val--;
75     pthread_mutex_unlock(&(s->mutex));
76     return rc;
77 }
78 
cam_sem_timedwait(cam_semaphore_t * s,const struct timespec * abs_timeout)79 static inline int cam_sem_timedwait(cam_semaphore_t *s, const struct timespec *abs_timeout)
80 {
81     int rc = 0;
82     pthread_mutex_lock(&(s->mutex));
83     while (s->val == 0 && rc != ETIMEDOUT)
84         rc = pthread_cond_timedwait(&(s->cond), &(s->mutex), abs_timeout);
85 
86     if (s->val > 0)
87         s->val--;
88 
89     pthread_mutex_unlock(&(s->mutex));
90 
91     /* sem_timedwait returns -1 for failure case, and failure code is in errno
92      */
93     if (rc != 0) {
94         errno = rc;
95         rc = -1;
96     }
97     return rc;
98 }
99 
cam_sem_destroy(cam_semaphore_t * s)100 static inline void cam_sem_destroy(cam_semaphore_t *s)
101 {
102     pthread_mutex_destroy(&(s->mutex));
103     pthread_cond_destroy(&(s->cond));
104     s->val = 0;
105 }
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif /* __QCAMERA_SEMAPHORE_H__ */
112