1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "param_osadp.h"
16
17 #include <errno.h>
18 #include <sys/mman.h>
19
20 #include "param_message.h"
21 #include "param_utils.h"
22
paramMutexEnvInit(void)23 INIT_LOCAL_API void paramMutexEnvInit(void)
24 {
25 }
26
ParamRWMutexCreate(ParamRWMutex * lock)27 INIT_LOCAL_API int ParamRWMutexCreate(ParamRWMutex *lock)
28 {
29 PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
30 pthread_rwlockattr_t rwlockatt;
31 pthread_rwlockattr_init(&rwlockatt);
32 pthread_rwlockattr_setpshared(&rwlockatt, PTHREAD_PROCESS_SHARED);
33 pthread_rwlock_init(&lock->rwlock, &rwlockatt);
34 return 0;
35 }
36
ParamRWMutexWRLock(ParamRWMutex * lock)37 INIT_LOCAL_API int ParamRWMutexWRLock(ParamRWMutex *lock)
38 {
39 PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
40 pthread_rwlock_wrlock(&lock->rwlock);
41 return 0;
42 }
ParamRWMutexRDLock(ParamRWMutex * lock)43 INIT_LOCAL_API int ParamRWMutexRDLock(ParamRWMutex *lock)
44 {
45 PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
46 pthread_rwlock_rdlock(&lock->rwlock);
47 return 0;
48 }
ParamRWMutexUnlock(ParamRWMutex * lock)49 INIT_LOCAL_API int ParamRWMutexUnlock(ParamRWMutex *lock)
50 {
51 PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
52 pthread_rwlock_unlock(&lock->rwlock);
53 return 0;
54 }
55
ParamRWMutexDelete(ParamRWMutex * lock)56 INIT_LOCAL_API int ParamRWMutexDelete(ParamRWMutex *lock)
57 {
58 PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
59 int ret = pthread_rwlock_destroy(&lock->rwlock);
60 PARAM_CHECK(ret == 0, return -1, "Failed to mutex lock ret %d", ret);
61 return 0;
62 }
63
ParamMutexCreate(ParamMutex * mutex)64 INIT_LOCAL_API int ParamMutexCreate(ParamMutex *mutex)
65 {
66 return 0;
67 }
ParamMutexPend(ParamMutex * mutex)68 INIT_LOCAL_API int ParamMutexPend(ParamMutex *mutex)
69 {
70 return 0;
71 }
ParamMutexPost(ParamMutex * mutex)72 INIT_LOCAL_API int ParamMutexPost(ParamMutex *mutex)
73 {
74 return 0;
75 }
ParamMutexDelete(ParamMutex * mutex)76 INIT_LOCAL_API int ParamMutexDelete(ParamMutex *mutex)
77 {
78 return 0;
79 }
80
GetSharedMem(const char * fileName,MemHandle * handle,uint32_t spaceSize,int readOnly)81 INIT_LOCAL_API void *GetSharedMem(const char *fileName, MemHandle *handle, uint32_t spaceSize, int readOnly)
82 {
83 PARAM_CHECK(fileName != NULL, return NULL, "Invalid filename or handle");
84 int mode = readOnly ? O_RDONLY : O_CREAT | O_RDWR | O_TRUNC;
85 int fd = open(fileName, mode, S_IRWXU | S_IRWXG | S_IROTH);
86 PARAM_ONLY_CHECK(fd >= 0, return NULL);
87
88 int prot = PROT_READ;
89 if (!readOnly) {
90 prot = PROT_READ | PROT_WRITE;
91 ftruncate(fd, spaceSize);
92 }
93 void *areaAddr = (void *)mmap(NULL, spaceSize, prot, MAP_SHARED, fd, 0);
94 PARAM_CHECK(areaAddr != MAP_FAILED && areaAddr != NULL, close(fd);
95 return NULL, "Failed to map memory error %d fileName %s ", errno, fileName);
96 close(fd);
97 return areaAddr;
98 }
99
FreeSharedMem(const MemHandle * handle,void * mem,uint32_t dataSize)100 INIT_LOCAL_API void FreeSharedMem(const MemHandle *handle, void *mem, uint32_t dataSize)
101 {
102 PARAM_CHECK(mem != NULL && handle != NULL, return, "Invalid mem or handle");
103 munmap((char *)mem, dataSize);
104 }
105