6aa39c75bbf14b953a96ecea9075adc0abd95b54
[o-du/l2.git] / src / rlog / rl_platform.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /********************************************************************20**
20
21      Name:     Radisys Logging Framework
22      Type:     C source file
23      Desc:     This file contains logging framework implementation.
24      File:     rl_rlog.c
25
26 *********************************************************************21*/
27 /**************************************************************************
28 @ description: This is source file which has implementaion of logging 
29 framework.
30 ****************************************************************************/
31
32 #include"stdint.h"
33 #include "rl_interface.h"
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/resource.h>
40 #include <fcntl.h>
41 #include <sys/time.h>
42 #include <time.h>
43 #include <stdarg.h>
44 #include <string.h>
45 #include <netinet/in.h>
46 #include <pthread.h>
47
48 #include "rl_rlog.h"
49 #include "rl_platform.h"
50
51 /*      VARIABLE DECLARATION SECTION */
52
53 /* Thread-specific data key visible to all threads */
54 static pthread_key_t    g_threadkey;
55
56 pthread_mutex_t g_logmutex;
57 THREAD_DATA* g_pCirList[RLOG_MAX_THREADS];
58 void* rlAlloc(size_t mem_size)
59 {
60         return malloc(mem_size);
61 }
62
63 void rlFree(void* pMem)
64 {
65         free(pMem);
66 }
67
68 void* rlCalloc(size_t mem_size)
69 {
70         return calloc(mem_size, 1);
71 }
72
73 void rlSetThreadSpecificData(const void *pThrData)
74 {
75         int retVal = pthread_setspecific(g_threadkey, pThrData);
76         
77         if( retVal!=0 ) {
78       fprintf(stderr, "Failed to associate the value with the key or invalid key");
79       _exit(0);
80    }
81 }
82
83 #ifdef RLOG_USE_CIRCULAR_BUFFER
84 /*******************************************************************************************
85 @param[in] pThreadData - Thread specific data
86 @brief This function is called whenever thread is being destroyed. This function will delete 
87 thread specific data allocated during thread registration.
88 ********************************************************************************************/
89 void deInitThread(void* pThreadData)
90 {
91
92         THREAD_DATA* pThrData = (THREAD_DATA*)(pThreadData);
93
94         if( pThreadData == NULL )
95                 return;
96
97         /* lock the mutex, to make sure no one is accessing this buffer */
98         pthread_mutex_lock(&g_logmutex);
99
100         g_pCirList[pThrData->listIndex]  = NULL;
101
102         if( pThrData->logBuff != NULL )
103                 rlFree(pThrData->logBuff);
104
105         rlFree(pThreadData);
106
107         /* unlock the mutex */
108         pthread_mutex_unlock(&g_logmutex);
109 }
110 #endif
111
112 void* rlGetThreadSpecData(void)
113 {
114         return (void*) pthread_getspecific(g_threadkey);
115 }
116
117 void rlInitPlatformSpecific(void)
118 {
119 #ifdef RLOG_USE_CIRCULAR_BUFFER
120         pthread_key_create(&g_threadkey, &deInitThread);
121 #endif
122 }
123
124 /**********************************************************************
125          End of file
126  **********************************************************************/