• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <pthread.h>
18 #include "wifi_hal.h"
19 #include "common.h"
20 
21 #ifndef __WIFI_HAL_SYNC_H__
22 #define __WIFI_HAL_SYNC_H__
23 
24 class Mutex
25 {
26 private:
27     pthread_mutex_t mMutex;
28 public:
Mutex()29     Mutex() {
30         pthread_mutex_init(&mMutex, NULL);
31     }
~Mutex()32     ~Mutex() {
33         pthread_mutex_destroy(&mMutex);
34     }
tryLock()35     int tryLock() {
36         return pthread_mutex_trylock(&mMutex);
37     }
lock()38     int lock() {
39         return pthread_mutex_lock(&mMutex);
40     }
unlock()41     void unlock() {
42         pthread_mutex_unlock(&mMutex);
43     }
44 };
45 
46 class Condition
47 {
48 private:
49     pthread_cond_t mCondition;
50     pthread_mutex_t mMutex;
51 
52 public:
Condition()53     Condition() {
54         pthread_mutex_init(&mMutex, NULL);
55         pthread_cond_init(&mCondition, NULL);
56     }
~Condition()57     ~Condition() {
58         pthread_cond_destroy(&mCondition);
59         pthread_mutex_destroy(&mMutex);
60     }
61 
wait()62     wifi_error wait() {
63         int status = pthread_cond_wait(&mCondition, &mMutex);
64         return mapKernelErrortoWifiHalError(status);
65     }
66 
wait(struct timespec abstime)67     wifi_error wait(struct timespec abstime)
68     {
69         struct timeval now;
70         int status;
71 
72         gettimeofday(&now,NULL);
73 
74         abstime.tv_sec += now.tv_sec;
75         if(((abstime.tv_nsec + now.tv_usec*1000) > 1000*1000*1000) || (abstime.tv_nsec + now.tv_usec*1000 < 0))
76         {
77            abstime.tv_sec += 1;
78            abstime.tv_nsec += now.tv_usec * 1000;
79            abstime.tv_nsec -= 1000*1000*1000;
80         }
81         else
82         {
83             abstime.tv_nsec += now.tv_usec * 1000;
84         }
85         status = pthread_cond_timedwait(&mCondition, &mMutex, &abstime);
86         return mapKernelErrortoWifiHalError(status);
87     }
88 
signal()89     void signal() {
90         pthread_cond_signal(&mCondition);
91     }
92 };
93 
94 #endif
95