#include <list>
#include <pthread.h>
+#include <mutex>
#include <vector>
#include "ttsd_main.h"
static pthread_mutex_t g_speak_data_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t g_sound_data_mutex = PTHREAD_MUTEX_INITIALIZER;
-static pthread_mutex_t g_app_state_mutex = PTHREAD_MUTEX_INITIALIZER;
+static mutex g_app_state_mutex;
/* If engine is running */
static ttsd_synthesis_control_e g_synth_control;
-/*
-* functions for debug
-*/
-int __data_show_list()
+#ifdef DATA_DEBUG
+static void __data_show_list()
{
int vsize = g_app_list.size();
SLOG(LOG_DEBUG, tts_tag(), "----- client list -----");
- for (int i=0; i < vsize; i++) {
- SECURE_SLOG(LOG_DEBUG, tts_tag(), "[%dth] pid(%d), uid(%d), state(%d)", i, g_app_list[i].pid, g_app_list[i].uid, g_app_list[i].state);
- }
-
if (0 == vsize) {
SLOG(LOG_DEBUG, tts_tag(), "No Client");
+ return;
}
- SLOG(LOG_DEBUG, tts_tag(), "-----------------------");
+ lock_guard<mutex> lock(g_app_state_mutex);
+ for (int i = 0; i < vsize; i++) {
+ SECURE_SLOG(LOG_DEBUG, tts_tag(), "[%dth] pid(%d), uid(%d), state(%d)", i, g_app_list[i].pid, g_app_list[i].uid, g_app_list[i].state);
+ }
- return TTSD_ERROR_NONE;
+ SLOG(LOG_DEBUG, tts_tag(), "-----------------------");
}
+#endif
int __data_show_sound_list(int index)
{
int index = 0;
index = ttsd_data_is_client(uid);
- if (index < 0) {
+ if (index < 0) {
SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
return TTSD_ERROR_INVALID_PARAMETER;
}
- //TODO : lock more specific memory
- pthread_mutex_lock(&g_app_state_mutex);
+ lock_guard<mutex> lock(g_app_state_mutex);
*state = g_app_list[index].state;
- pthread_mutex_unlock(&g_app_state_mutex);
return TTSD_ERROR_NONE;
}
+static int __get_playing_app_uid()
+{
+ for (auto app : g_app_list) {
+ if (APP_STATE_PLAYING == app.state) {
+ return app.uid;
+ }
+ }
+
+ return -1;
+}
+
int ttsd_data_set_client_state(int uid, app_tts_state_e state)
{
int index = 0;
index = ttsd_data_is_client(uid);
- if (index < 0) {
+ if (index < 0) {
SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
return TTSD_ERROR_INVALID_PARAMETER;
}
- //TODO : lock more specific memory
- pthread_mutex_lock(&g_app_state_mutex);
+ lock_guard<mutex> lock(g_app_state_mutex);
+ if (g_app_list[index].state == state) {
+ SLOG(LOG_ERROR, tts_tag(), "[DATA] Already current state. (%d)", state);
+ return TTSD_ERROR_NONE;
+ }
+
/* The client of playing state of all clients is only one. need to check state. */
- if (APP_STATE_PLAYING == state) {
- int vsize = g_app_list.size();
- for (int i = 0; i < vsize; i++) {
- if(g_app_list[i].state == APP_STATE_PLAYING) {
- SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] A playing client has already existed.");
- pthread_mutex_unlock(&g_app_state_mutex);
- return -1;
- }
- }
+ int playing_uid = __get_playing_app_uid();
+ if (APP_STATE_PLAYING == state && 0 < playing_uid) {
+ SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] A playing client has already existed. playing app uid(%d)", playing_uid);
+ return TTSD_ERROR_OPERATION_FAILED;
}
g_app_list[index].state = state;
- pthread_mutex_unlock(&g_app_state_mutex);
return TTSD_ERROR_NONE;
}
int ttsd_data_get_current_playing()
{
- int vsize = g_app_list.size();
+ lock_guard<mutex> lock(g_app_state_mutex);
+ int uid = __get_playing_app_uid();
- for (int i = 0; i < vsize; i++) {
- if (APP_STATE_PLAYING == g_app_list[i].state) {
- SLOG(LOG_DEBUG, tts_tag(), "[DATA] uid(%d) is playing", g_app_list[i].uid);
- return g_app_list[i].uid;
- }
- }
-
- return -1;
+ return uid;
}
int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
vector<app_data_s> temp_app_list;
int vsize = g_app_list.size();
- int i = 0;
- for (i = 0;i < vsize;i++) {
+ g_app_state_mutex.lock();
+ for (int i = 0; i < vsize; i++) {
app_data_s app;
app.pid = g_app_list[i].pid;
app.uid = g_app_list[i].uid;
temp_app_list.insert(temp_app_list.end(), app);
}
+ g_app_state_mutex.unlock();
- for (i = 0;i < vsize;i++) {
+ for (int i = 0; i < vsize; i++) {
SECURE_SLOG(LOG_DEBUG, tts_tag(), "[%dth] pid(%d), uid(%d), state(%d)", i, temp_app_list[i].pid, temp_app_list[i].uid, temp_app_list[i].state);
if (false == callback(temp_app_list[i].pid, temp_app_list[i].uid, temp_app_list[i].state, user_data)) {
break;
}
}
- for (i = 0;i < vsize;i++) {
- temp_app_list.erase(temp_app_list.begin());
- }
-
return 0;
}
return true;
}
-int ttsd_data_is_current_playing()
-{
- int vsize = g_app_list.size();
-
- for (int i = 0; i < vsize; i++) {
- if(g_app_list[i].state == APP_STATE_PLAYING) {
- return g_app_list[i].uid;
- }
- }
-
- return -1;
-}
-
int ttsd_data_get_same_pid_client_count(int pid)
{
int vsize = g_app_list.size();