4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
28 #include <sys/types.h>
31 #include <semaphore.h>
49 #define AUDIO_ROUTE_POLICY_LOCK "audio_route_policy_lock"
50 #define LOCK_TIMEOUT_SEC 6
57 int err = MM_ERROR_NONE;
58 struct timespec wait_time;
60 sem = sem_open(AUDIO_ROUTE_POLICY_LOCK, O_CREAT, 0666, 1);
61 if (sem == SEM_FAILED)
63 debug_error("Semaphore open Fail! (name:%s, %s)\n", AUDIO_ROUTE_POLICY_LOCK, strerror(errno));
64 return MM_ERROR_SOUND_INTERNAL;
67 wait_time.tv_sec = (long int)(time(NULL)) + LOCK_TIMEOUT_SEC;
68 wait_time.tv_nsec = 0;
69 ret = sem_timedwait(sem, &wait_time);
75 debug_error("Lock RETRY LOCK\n");
79 debug_error("Invalid semaphore\n");
80 err = MM_ERROR_SOUND_INTERNAL;
83 debug_error("EAGAIN\n");
84 err = MM_ERROR_SOUND_INTERNAL;
87 debug_error("sem_wait leached %d seconds timeout.\n", LOCK_TIMEOUT_SEC);
89 //Recovery of sem_wait lock....in abnormal condition
91 if(0 == sem_getvalue(sem, &sem_value))
93 debug_error("%s sem value is %d\n",AUDIO_ROUTE_POLICY_LOCK, sem_value);
99 debug_error("sem_post error %s : %d\n", AUDIO_ROUTE_POLICY_LOCK, sem_value);
103 debug_error("lock recovery success...try lock again\n");
109 debug_error("sem value is not 0. but failed sem_timedwait so retry.. : %s\n",AUDIO_ROUTE_POLICY_LOCK);
116 debug_error("sem_getvalue failed : %s\n",AUDIO_ROUTE_POLICY_LOCK);
119 err = MM_ERROR_SOUND_INTERNAL;
128 int __mm_sound_unlock()
132 int err = MM_ERROR_NONE;
134 sem = sem_open(AUDIO_ROUTE_POLICY_LOCK, O_CREAT, 0666, 1);
135 if (sem == SEM_FAILED)
137 debug_error("Semaphore open Fail! (name:%s, errno %d)\n", AUDIO_ROUTE_POLICY_LOCK, errno);
138 return MM_ERROR_SOUND_INTERNAL;
144 debug_error("UNLOCK FAIL\n");
145 err = MM_ERROR_SOUND_INTERNAL;