blob: a6a952df8e63c5f9dcd96c7531b0f760dd90b8b9 (
plain)
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
|
/******************************************************************************
* 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>
/// handle index
static int gHandleIdx = 1;
static int gInitialized = 0;
/// open file descriptor handle array
int gOpenFdArray[MaxPersHandle] = {0};
/// persistence key handle array
PersistenceKeyHandle_s gKeyHandleArray[MaxPersHandle];
/// persistence key handle array
PersistenceFileHandle_s gFileHandleArray[MaxPersHandle];
/// free handle array
int gFreeHandleArray[MaxPersHandle] = {0};
int gFreeHandleIdxHead = 0;
pthread_mutex_t gMtx;
/// get persistence handle
int get_persistence_handle_idx()
{
int handle = 0;
if(gInitialized == 0)
{
gInitialized = 1;
pthread_mutex_init(&gMtx, 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 = -1;
DLT_LOG(gDLTContext, 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);
}
}
|