---
# System prepended metadata

title: <center><i class="fa fa-edit"></i> gNB initialization</center>

---

:::warning
# <center><i class="fa fa-edit"></i> gNB initialization</center>
:::



[TOC]

Before initialization of gNB, we have to prepare RU (radio units)

```c 
    printf("wait RUs\n");
    wait_RUs();
    printf("ALL RUs READY!\n");
    printf("RC.nb_RU:%d\n", RC.nb_RU);
```



| Variable/function | Column 2 |
| -------- | -------- | 
| RC.nb_RU |This varible gives us an infotmation about number of RUs instances in this node  |

### Once all RUs are ready initialize the rest of the gNBs ((dependence on final RU parameters after configuration)
```c 
if(IS_SOFTMODEM_DOSCOPE) {
    scopeParms_t p;
    p.argc=&argc
    p.argv=argv;
    p.gNB=RC.gNB[0];
    p.ru=RC.ru[0];
    load_softscope("nr",&p);
}

    if (NFAPI_MODE != NFAPI_MODE_PNF && NFAPI_MODE != NFAPI_MODE_VNF) {
      printf("Not NFAPI mode - call init_eNB_afterRU()\n");
      init_eNB_afterRU();
    } else {
      printf("NFAPI mode - DO NOT call init_gNB_afterRU()\n");
    }


    printf("ALL RUs ready - ALL gNBs ready\n");
    printf("Sending sync to all threads\n");
    pthread_mutex_lock(&sync_mutex);
    sync_var=0;
    pthread_cond_broadcast(&sync_cond);
    pthread_mutex_unlock(&sync_mutex);
  }
printf("About to call end_configmodule() from %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
```

#### IS_SOFTMODEM_DOSCOPE
```c
#define IS_SOFTMODEM_DOSCOPE         ( get_softmodem_optmask() & SOFTMODEM_DOSCOPE_BIT)
```

Where:
- **get_softmodem_optmask()** - temporary dummy implem of get_softmodem_optmask, till basic simulators implemented as device

```c
uint64_t get_softmodem_optmask(void) {
  return softmodem_params.optmask;
}
```
- Link on source code of [softmodem_params.optmask](https://hackmd.io/PQW7okndQVGU3r6hT55siA)

And this function will retun us uint64_t ( an unsigned long long integer which is guaranteed to be exactly 8 bytes in size. )

#### scopeParms_t

```c
  int *argc;
  char **argv;
  RU_t *ru;
  PHY_VARS_gNB *gNB;
```
Where: 
- **argc** - argument count;
- **argv** - argument values;
- **RU_t** - Radio unit task wich has [such parameters](https://hackmd.io/qF85AF1SQrCCNlFGIA2Eew)
- **PHY_VARS_gNB** - [PHY Data Structure for gNB](https://hackmd.io/OxoUYMn8QTS5dzMkaQm2ug)

#### pthread_mutex_lock(&sync_mutex)
Locks a mutex object, which identifies a mutex. If the mutex is already locked by another thread, the thread waits for the mutex to become available. The thread that has locked a mutex becomes its current owner and remains the owner until the same thread has unlocked it.

```c
#define RLC_UM_MUTEX_LOCK(mUTEX, cTXT, rLC) pthread_mutex_lock(mUTEX)
```
#### pthread_cond_broadcast
Restart the nr-softmodem after it has been soft-stopped with stop_L1L2()

#### pthread_mutex_unlock
Unlock a mutex object, which identifies a mutex
```c
#define RLC_UM_MUTEX_UNLOCK(mUTEX) pthread_mutex_unlock(mUTEX)
```
### At this step we are ending our gNB initialization

Firsly we are ending a config module
```c
 printf("Called end_configmodule() from %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
```

Than we have to press ctrl-c for terminating of our program

```c 
printf("TYPE <CTRL-C> TO TERMINATE\n");
``` 

After that we have to handle signals and wait for all threads to join when the process complete. 

```c 
printf("Entering ITTI signals handler\n");
itti_wait_tasks_end();
printf("Returned from ITTI signal handler\n");   
oai_exit=1;
printf("oai_exit=%d\n",oai_exit);
```
The last step is stoping of gNB

```c 
 stop_gNB(NB_gNB_INST);
```

#### Code of stop_gNB

```c 
void stop_gNB(int nb_inst) {
  for (int inst=0; inst<nb_inst; inst++) {
    LOG_I(PHY,"Killing gNB %d processing threads\n",inst);
    kill_gNB_proc(inst);
  }
}
```