1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/******************************************************************************
* Project Persistency
* (c) copyright 2012
* Company XS Embedded GmbH
*****************************************************************************/
/******************************************************************************
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
* with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
******************************************************************************/
/**
* @file persistence_client_library_handle.c
* @ingroup Persistence client library handle
* @author Ingo Huerner
* @brief Implementation of the persistence client library handle
* Library provides an API to access persistent data
* @see
*/
#include "persistence_client_library_handle.h"
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
/// handle index
static int gHandleIdx = 1;
/// open file descriptor handle array
int gOpenFdArray[MaxPersHandle] = {0};
/// handle array
int gOpenHandleArray[MaxPersHandle] = {0};
/// persistence key handle array
PersistenceKeyHandle_s gKeyHandleArray[MaxPersHandle];
/// persistence file handle array
PersistenceFileHandle_s gFileHandleArray[MaxPersHandle];
/// persistence handle array for OSS and third party handles
PersistenceFileHandle_s gOssHandleArray[MaxPersHandle];
/// free handle array
int gFreeHandleArray[MaxPersHandle] = {0};
int gFreeHandleIdxHead = 0;
pthread_mutex_t gMtx = PTHREAD_MUTEX_INITIALIZER;
/// get persistence handle
int get_persistence_handle_idx()
{
int handle = 0;
if(pthread_mutex_lock(&gMtx) == 0)
{
if(gFreeHandleIdxHead > 0) // check if we have a free spot in the array before the current max
{
handle = gFreeHandleArray[--gFreeHandleIdxHead];
}
else
{
if(gHandleIdx < MaxPersHandle-1)
{
handle = gHandleIdx++; // no free spot before current max, increment handle index
}
else
{
handle = EPERS_MAXHANDLE;
DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("get_persistence_handle_idx => Reached maximum of open handles: "), DLT_INT(MaxPersHandle));
}
}
pthread_mutex_unlock(&gMtx);
}
return handle;
}
/// close persistence handle
void set_persistence_handle_close_idx(int handle)
{
if(pthread_mutex_lock(&gMtx) == 0)
{
if(gFreeHandleIdxHead < MaxPersHandle)
{
gFreeHandleArray[gFreeHandleIdxHead++] = handle;
}
pthread_mutex_unlock(&gMtx);
}
}
void close_all_persistence_handle()
{
if(pthread_mutex_lock(&gMtx) == 0)
{
// "free" all handles
memset(gFreeHandleArray, 0, sizeof(gFreeHandleArray));
memset(gOpenHandleArray, 0, sizeof(gOpenHandleArray));
memset(gOpenFdArray, 0, sizeof(gOpenFdArray));
// reset variables
gHandleIdx = 1;
gFreeHandleIdxHead = 0;
pthread_mutex_unlock(&gMtx);
}
}
|