summaryrefslogtreecommitdiffstats
path: root/nsframework/common_library/client/src/cl_lock.c
blob: 6add2a5a330eb5b5cc1d393fb6fd57e99a353a02 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// #include <stdio.h>
// #include <stdlib.h>
#include <errno.h>

#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

#include <native_service/cl_lock.h>
#include <native_service/cl_lockid.h>
#include "cl_lock_internal.h"
#include "cl_error.h"

static int      shm_id = -1;

/*
 * Lock file is consists of slots(4KB)
 * The slot layout is:
 *   0     ~   4Byte : field of PID
 *   4Byte ~  28Byte : field of pthread_mutex_t
 */


static int cl_LockfileInit(void *base) {
  int   i;
  /*
    int   j;
  */
  void *addr;
  pthread_mutexattr_t   attr;

  if (pthread_mutexattr_init(&attr) != 0) {  // LCOV_EXCL_BR_LINE 5:fail safe for libc pthread_mutexattr_init
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    return -1;  // LCOV_EXCL_LINE 5:fail safe for libc pthread_mutexattr_init
  }
  if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) != 0) {  // LCOV_EXCL_BR_LINE 5:fail safe for libc pthread_mutexattr_setpshared
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    return -1;  // LCOV_EXCL_LINE 5:fail safe for libc pthread_mutexattr_setpshared
  }
//  if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP) != 0) {
//    return -1;
//  }
  if (pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST) != 0) {
    return -1;
  }

  for (i = 0; i < LID_NUM; i++) {
    /*
        addr = SLOT_SIZE * 1 + base;
        for (j = 0; j < 1024; j++) {
          *(int*)(addr + j * sizeof(int)) = j;
        }
    */
    addr = SLOT_SIZE * i + (char *)base + sizeof(int);
    pthread_mutex_init((pthread_mutex_t *)addr, &attr);

  }
  if (pthread_mutexattr_destroy(&attr) != 0) { // LCOV_EXCL_BR_LINE 5:fail safe for libc pthread_mutexattr_destroy
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    return -1; // LCOV_EXCL_LINE 5:fail safe for libc pthread_mutexattr_destroy
  }
  return 0;
}

/*
 * Initialize in the system
 *   This function will generate the Lock file, and initialize pthread_mutex_t for all slots
 */
int CL_LockSystemInit(void) {
  int           fd = -1;
  int           ret = 0;
  void         *base = MAP_FAILED;

  fd = shm_open(LOCKFILE_NAME, O_CREAT | O_EXCL | O_RDWR, (S_IRWXG | S_IRWXO | S_IRWXU));
  if (fd < 0) {
    ret = -1;
    goto exit;
  }
  if (ftruncate(fd, LOCKFILE_SIZE) != 0) { // LCOV_EXCL_BR_LINE 5:fail safe for libc ftruncate
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    ret = -1;  // LCOV_EXCL_LINE 5:fail safe for libc ftruncate
    goto exit;  // LCOV_EXCL_LINE 5:fail safe for libc ftruncate
  }

  base = mmap(NULL, LOCKFILE_SIZE, (PROT_READ | PROT_WRITE), MAP_SHARED, fd, 0);
  if (base == MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5:fail safe for libc mmap
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    ret = -1;  // LCOV_EXCL_LINE 5:fail safe for libc mmap
    goto exit;  // LCOV_EXCL_LINE 5:fail safe for libc mmap
  }

  if (cl_LockfileInit(base) < 0) { // LCOV_EXCL_BR_LINE 11:out branch
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    ret = -1;  // LCOV_EXCL_LINE 5:fail safe for libc
    goto exit;  // LCOV_EXCL_LINE 5:fail safe for libc
  }

exit:
  if (fd >= 0) {
    close(fd);
  }
  if (base != MAP_FAILED) {
    munmap(base, LOCKFILE_SIZE);
  }
  return ret;
}

void CL_LockSystemFin_debug(void) {
  if (shm_id >= 0) {
    close(shm_id);
  }
  shm_unlink(LOCKFILE_NAME);
  shm_id = -1;
}

/*
 * Initialize in the process
 *   Open the Lock file
 */
int CL_LockProcessInit(void) {
  if (shm_id < 0) {
    shm_id = shm_open(LOCKFILE_NAME, O_RDWR, (S_IRWXG | S_IRWXO | S_IRWXU));
  }
  if (shm_id < 0) {  // LCOV_EXCL_BR_LINE 5: fail safe for glibc function shm_open
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    return -1;  // LCOV_EXCL_LINE 5: fail safe for glibc function shm_open
  }
  return 0;
}

void *CL_LockMap(int lid) {
  if ((lid < 0) || (lid >= LID_NUM)) {
    errno = EINVAL;
    return MAP_FAILED;
  }
  return mmap(NULL, SLOT_SIZE, (PROT_READ | PROT_WRITE), MAP_SHARED, shm_id, (off_t)(lid * SLOT_SIZE));
}

int CL_LockUnmap(void *addr) {
  return munmap(addr, SLOT_SIZE);
}

int CL_LockGet(void *addr) {
  int ret;

  if ((addr == NULL) || (addr == MAP_FAILED)) {
    ret = EINVAL;
  } else {
    CL_DBG_PRINT("@@@@@ %s Start: pid = %d\n", __func__, *(int *)addr);
    // LCOV_EXCL_BR_START 5:fail safe for libc pthread_mutex_lock
    if ((ret = pthread_mutex_lock((pthread_mutex_t*)((char *)addr + sizeof(int)))) == 0) {
      // LCOV_EXCL_BR_STOP
      *(int *)addr = (int)getpid();
    } else if (ret == EOWNERDEAD) {
      if ((ret = pthread_mutex_consistent((pthread_mutex_t *)((char *)addr + sizeof(int)))) == 0) {
        *(int *)addr = (int)getpid();
      }
    }
    CL_DBG_PRINT("@@@@@ %s End: pid = %d\n", __func__, *(int *)addr);
  }
  return ret;
}

int CL_LockNowait(void *addr) {
  int ret;

  if ((addr == NULL) || (addr == MAP_FAILED)) {
    ret = EINVAL;
  } else {
    CL_DBG_PRINT("@@@@@ %s Start: pid = %d\n", __func__, *(int *)addr);
    if ((ret = pthread_mutex_trylock((pthread_mutex_t*)((char *)addr + sizeof(int)))) == 0) {
      *(int *)addr = (int)getpid();
    } else if (ret == EOWNERDEAD) {
      if ((ret = pthread_mutex_consistent((pthread_mutex_t *)((char *)addr + sizeof(int)))) == 0) {
        *(int *)addr = (int)getpid();
      }
    }
    CL_DBG_PRINT("@@@@@ %s End: pid = %d\n", __func__, *(int *)addr);
  }
  return ret;
}


int CL_LockRelease(void *addr) {
  int ret;
  if ((addr == NULL) || (addr == MAP_FAILED)) {
    ret = EINVAL;
  } else {
    CL_DBG_PRINT("@@@@@ %s : pid = %d\n", __func__, *(int *)addr);
    *(int *)addr = (int)0;
    ret = pthread_mutex_unlock((pthread_mutex_t*)((char *)addr + sizeof(int)));
  }
  return ret;
}