aboutsummaryrefslogtreecommitdiffstats
path: root/src/adapt/nstack_share_res.c
blob: 9466145f04ad6f4ca69a65ffcea63436fea2df6f (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
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* 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 "nstack_share_res.h"
#include "nsfw_mem_api.h"
#include "types.h"
#include "nsfw_recycle_api.h"
#include "nstack_securec.h"
#include "nstack_log.h"
#include "nsfw_maintain_api.h"
#include "nstack_types.h"

#define NSTACK_SHARE_FORK_LOCK "share_fork_lock"

typedef struct
{
  common_mem_spinlock_t *fork_share_lock;
} nstack_share_res;

NSTACK_STATIC nstack_share_res g_nstack_share_res;

/** global timer tick */
nstack_tick_info_t g_nstack_timer_tick;

NSTACK_STATIC int
nstack_create_share_fork_lock ()
{
  mzone_handle zone;
  nsfw_mem_zone param;
  int ret;

  param.isocket_id = -1;
  param.length = sizeof (common_mem_spinlock_t);
  param.stname.entype = NSFW_SHMEM;

  ret =
    STRCPY_S (param.stname.aname, NSFW_MEM_NAME_LENGTH,
              NSTACK_SHARE_FORK_LOCK);
  if (EOK != ret)
    {
      NSSOC_LOGERR ("STRCPY_S failed]name=%s,ret=%d", NSTACK_SHARE_FORK_LOCK,
                    ret);
      return -1;
    }

  zone = nsfw_mem_zone_create (&param);
  if (!zone)
    {
      NSSOC_LOGERR ("nsfw_mem_zone_create failed]name=%s",
                    NSTACK_SHARE_FORK_LOCK);
      return -1;
    }

  g_nstack_share_res.fork_share_lock = (common_mem_spinlock_t *) zone;
  common_mem_spinlock_init (g_nstack_share_res.fork_share_lock);

  NSSOC_LOGDBG ("ok");
  return 0;
}

NSTACK_STATIC int
nstack_lookup_share_fork_lock ()
{
  mzone_handle zone;
  nsfw_mem_name param;

  param.entype = NSFW_SHMEM;
  param.enowner = NSFW_PROC_MAIN;
  if (STRCPY_S (param.aname, NSFW_MEM_NAME_LENGTH, NSTACK_SHARE_FORK_LOCK) !=
      0)
    {
      NSSOC_LOGERR ("STRCPY_S failed]name=%s", NSTACK_SHARE_FORK_LOCK);
      return -1;
    }

  zone = nsfw_mem_zone_lookup (&param);
  if (!zone)
    {
      NSSOC_LOGERR ("nsfw_mem_zone_lookup failed]name=%s",
                    NSTACK_SHARE_FORK_LOCK);
      return -1;
    }

  g_nstack_share_res.fork_share_lock = (common_mem_spinlock_t *) zone;

  NSSOC_LOGDBG ("ok");

  return 0;
}

NSTACK_STATIC int
nstack_lookup_share_global_tick ()
{
  int ret;
  nsfw_mem_name name = {.entype = NSFW_SHMEM,.enowner = NSFW_PROC_MAIN };

  ret = STRCPY_S (name.aname, NSFW_MEM_NAME_LENGTH, NSTACK_GLOBAL_TICK_SHM);
  if (EOK != ret)
    {
      NSSOC_LOGERR ("STRCPY_S failed]name=%s,ret=%d", NSTACK_GLOBAL_TICK_SHM,
                    ret);
      return -1;
    }

  g_nstack_timer_tick.tick_ptr = (u64_t *) nsfw_mem_zone_lookup (&name);
  if (NULL == g_nstack_timer_tick.tick_ptr)
    {
      NSPOL_LOGERR ("Failed to lookup global timer tick memory");
      return -1;
    }

  NSSOC_LOGDBG ("ok");
  return 0;
}

int
nstack_init_share_res ()
{
  if (nstack_create_share_fork_lock () != 0)
    {
      return -1;
    }

  return 0;
}

int
nstack_attach_share_res ()
{
  if (nstack_lookup_share_fork_lock () != 0)
    {
      return -1;
    }

  if (nstack_lookup_share_global_tick () != 0)
    {
      return -1;
    }

  return 0;
}

common_mem_spinlock_t *
nstack_get_fork_share_lock ()
{
  return g_nstack_share_res.fork_share_lock;
}

NSTACK_STATIC nsfw_rcc_stat
nstack_recycle_fork_share_lock (u32 exit_pid, void *pdata, u16 rec_type)
{
  NSSOC_LOGDBG ("recycle]pid=%u", exit_pid);

  if (g_nstack_share_res.fork_share_lock
      && (g_nstack_share_res.fork_share_lock->locked == exit_pid))
    {
      common_mem_spinlock_unlock (g_nstack_share_res.fork_share_lock);
    }

  return NSFW_RCC_CONTINUE;
}

REGIST_RECYCLE_LOCK_REL (nstack_recycle_fork_share_lock, NULL, NSFW_PROC_APP)