update the latest source
[framework/system/smartcard-service.git] / common / Synchronous.cpp
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
18 /* standard library header */
19 #include <stdio.h>
20 #include <sys/time.h>
21
22 /* SLP library header */
23
24 /* local header */
25 #include "Synchronous.h"
26
27 namespace smartcard_service_api
28 {
29         Synchronous::Synchronous()
30         {
31                 pthread_mutex_init(&syncMutex, NULL);
32                 pthread_cond_init(&syncCondition, NULL);
33         }
34
35         void Synchronous::syncLock()
36         {
37                 pthread_mutex_lock(&syncMutex);
38         }
39
40         void Synchronous::syncUnlock()
41         {
42                 pthread_mutex_unlock(&syncMutex);
43         }
44
45         int Synchronous::waitTimedCondition(int second)
46         {
47                 struct timeval now;
48                 struct timespec ts;
49                 int result;
50
51                 if (second > 0)
52                 {
53                         gettimeofday(&now, NULL);
54                         ts.tv_sec = now.tv_sec + second;
55                         ts.tv_nsec = now.tv_usec * 1000;
56
57                         result = pthread_cond_timedwait(&syncCondition, &syncMutex, &ts);
58                 }
59                 else
60                 {
61                         result = pthread_cond_wait(&syncCondition, &syncMutex);
62                 }
63
64                 return result;
65         }
66
67         void Synchronous::signalCondition()
68         {
69                 pthread_cond_signal(&syncCondition);
70         }
71
72 } /* namespace smartcard_service_api */