pdcp_thread.c 4.42 KB
Newer Older
1
/*******************************************************************************
gauthier's avatar
gauthier committed
2 3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
4

gauthier's avatar
gauthier committed
5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


gauthier's avatar
gauthier committed
11 12 13 14
    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
15

gauthier's avatar
gauthier committed
16 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
   see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
gauthier's avatar
gauthier committed
22 23 24 25 26
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.
27

gauthier's avatar
gauthier committed
28
 *******************************************************************************/
29 30

/*! \file pdcp_thread.c
gauthier's avatar
gauthier committed
31 32 33 34 35 36 37 38 39
 * \brief
 * \author F. Kaltenberger
 * \date 2013
 * \version 0.1
 * \company Eurecom
 * \email: florian.kaltenberger@eurecom.fr
 * \note
 * \warning
 */
40 41 42 43 44 45 46 47 48 49 50
#include <pthread.h>
//#include <inttypes.h>

#include "pdcp.h"
#include "PHY/extern.h" //for PHY_vars
#include "UTIL/LOG/log.h"
#include "UTIL/LOG/vcd_signal_dumper.h"

#define OPENAIR_THREAD_STACK_SIZE    8192
#define OPENAIR_THREAD_PRIORITY        255

gauthier's avatar
gauthier committed
51
extern int  oai_exit;
52
extern char UE_flag;
53

gauthier's avatar
gauthier committed
54 55
pthread_t       pdcp_thread;
pthread_attr_t  pdcp_thread_attr;
56
pthread_mutex_t pdcp_mutex;
gauthier's avatar
gauthier committed
57 58
pthread_cond_t  pdcp_cond;
int             pdcp_instance_cnt;
59

60 61
static void *pdcp_thread_main(void* param);

62
static void *pdcp_thread_main(void* param) {
gauthier's avatar
gauthier committed
63
  uint8_t eNB_flag = !UE_flag;
64 65

  LOG_I(PDCP,"This is pdcp_thread eNB_flag = %d\n",eNB_flag);
66 67 68

  while (!oai_exit) {

gauthier's avatar
gauthier committed
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
      if (pthread_mutex_lock(&pdcp_mutex) != 0) {
          LOG_E(PDCP,"Error locking mutex.\n");
      }
      else {
          while (pdcp_instance_cnt < 0) {
              pthread_cond_wait(&pdcp_cond,&pdcp_mutex);
          }
          if (pthread_mutex_unlock(&pdcp_mutex) != 0) {
              LOG_E(PDCP,"Error unlocking mutex.\n");
          }
      }

      if (oai_exit) break;

      if (eNB_flag) {
          pdcp_run(PHY_vars_eNB_g[0]->frame, eNB_flag, PHY_vars_eNB_g[0]->Mod_id, 0);
          LOG_D(PDCP,"Calling pdcp_run (eNB) for frame %d\n",PHY_vars_eNB_g[0]->frame);
      }
      else  {
          pdcp_run(PHY_vars_UE_g[0]->frame, eNB_flag, 0, PHY_vars_UE_g[0]->Mod_id);
          LOG_D(PDCP,"Calling pdcp_run (UE) for frame %d\n",PHY_vars_UE_g[0]->frame);
      }

      if (pthread_mutex_lock(&pdcp_mutex) != 0) {
          LOG_E(PDCP,"Error locking mutex.\n");
      }
      else {
          pdcp_instance_cnt--;
          if (pthread_mutex_unlock(&pdcp_mutex) != 0) {
              LOG_E(PDCP,"Error unlocking mutex.\n");
          }
      }
101 102 103 104 105
  }
  return(NULL);
}


106
int init_pdcp_thread(void) {
107

gauthier's avatar
gauthier committed
108 109 110 111 112 113
  int    error_code;
  struct sched_param p;

  pthread_attr_init (&pdcp_thread_attr);
  pthread_attr_setstacksize(&pdcp_thread_attr,OPENAIR_THREAD_STACK_SIZE);
  //attr_dlsch_threads.priority = 1;
114

gauthier's avatar
gauthier committed
115 116
  p.sched_priority = OPENAIR_THREAD_PRIORITY;
  pthread_attr_setschedparam  (&pdcp_thread_attr, &p);
117
#ifndef RTAI_ISNT_POSIX
gauthier's avatar
gauthier committed
118
  pthread_attr_setschedpolicy (&pdcp_thread_attr, SCHED_FIFO);
119
#endif
gauthier's avatar
gauthier committed
120 121 122 123 124 125 126 127 128 129 130
  pthread_mutex_init(&pdcp_mutex,NULL);
  pthread_cond_init(&pdcp_cond,NULL);

  pdcp_instance_cnt = -1;
  LOG_I(PDCP,"Allocating PDCP thread\n");
  error_code = pthread_create(&pdcp_thread,
      &pdcp_thread_attr,
      pdcp_thread_main,
      (void*)NULL);

  if (error_code!= 0) {
131 132
      LOG_I(PDCP,"Could not allocate PDCP thread, error %d\n",error_code);
      return(error_code);
gauthier's avatar
gauthier committed
133 134
  }
  else {
135
      LOG_I(PDCP,"Allocate PDCP thread successful\n");
gauthier's avatar
gauthier committed
136 137
  }
  return(0);
138 139
}

gauthier's avatar
gauthier committed
140

141
void cleanup_pdcp_thread(void) {
gauthier's avatar
gauthier committed
142
  void *status_p = NULL;
143 144

  LOG_I(PDCP,"Scheduling PDCP thread to exit\n");
gauthier's avatar
gauthier committed
145

146 147 148 149 150 151
  pdcp_instance_cnt = 0;
  if (pthread_cond_signal(&pdcp_cond) != 0)
    LOG_I(PDCP,"ERROR pthread_cond_signal\n");
  else
    LOG_I(PDCP,"Signalled PDCP thread to exit\n");

gauthier's avatar
gauthier committed
152
  pthread_join(pdcp_thread,&status_p);
153 154 155 156
  LOG_I(PDCP,"PDCP thread exited\n");
  pthread_cond_destroy(&pdcp_cond);
  pthread_mutex_destroy(&pdcp_mutex);
}