thread_ipc.c 5.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.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.openairinterface.org/?page_id=698
 *
 * 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.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

linhuang's avatar
linhuang committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include <pthread.h>
#include <sched.h>

#include "thread_ipc.h"

g_thread_ipc_t thread_ipc = {0};

void loop_buffer_reset(buffer_t *loop_buf)
{
36
  int i;
linhuang's avatar
linhuang committed
37

38 39 40
  for (i = 0; i < BUFFERMAX; i++) {
    loop_buf[i].subframe_num = -1;
  }
linhuang's avatar
linhuang committed
41

42
  return;
linhuang's avatar
linhuang committed
43 44 45 46
}

static void loop_buffer_init(loop_buffer_op_t *loop_buffer)
{
47 48 49
  loop_buffer->packet_num = 0;
  loop_buffer->isfull = 0;
  loop_buffer->isempty = 1;
linhuang's avatar
linhuang committed
50

51
  pthread_mutex_init(&loop_buffer->buffer_mutex, NULL);
linhuang's avatar
linhuang committed
52

53 54
  pthread_cond_init(&loop_buffer->full_cond, NULL);
  pthread_cond_init(&loop_buffer->empty_cond, NULL);
linhuang's avatar
linhuang committed
55

56
  loop_buffer_reset(loop_buffer->loop_buf);
linhuang's avatar
linhuang committed
57

58
  return;
linhuang's avatar
linhuang committed
59 60 61 62
}

static void sync_buffer_init(sync_buffer_t *sync_buffer)
{
63 64
  sync_buffer->decoding_subframe_num = 0;
  pthread_mutex_init(&sync_buffer->buffer_mutex, NULL);
linhuang's avatar
linhuang committed
65

66
  return;
linhuang's avatar
linhuang committed
67 68 69 70
}

int thread_ipc_init(void)
{
71 72 73 74 75
  //printf("recv %d\n", thread_ipc.sync_buffer.decoding_subframe_num);
  thread_ipc.ue_sync_state = 0;
  thread_ipc.rx_timestamp = 0;
  thread_ipc.tx_timestamp = 0;
  thread_ipc.current_subframe = 0;
linhuang's avatar
linhuang committed
76

77 78
  pthread_mutex_init(&thread_ipc.dl_decode_mutex, NULL);
  pthread_mutex_lock(&thread_ipc.dl_decode_mutex);
linhuang's avatar
linhuang committed
79

80 81
  pthread_mutex_init(&thread_ipc.ul_send_mutex, NULL);
  pthread_mutex_lock(&thread_ipc.ul_send_mutex);
linhuang's avatar
linhuang committed
82

83 84
  pthread_mutex_init(&thread_ipc.sync_mutex, NULL);
  pthread_mutex_lock(&thread_ipc.sync_mutex);
linhuang's avatar
linhuang committed
85

86 87
  loop_buffer_init(&thread_ipc.loop_buffer);
  sync_buffer_init(&thread_ipc.sync_buffer);
linhuang's avatar
linhuang committed
88

89
  return 0;
linhuang's avatar
linhuang committed
90 91 92 93
}

int thread_ipc_deinit(void)
{
94 95 96
  pthread_mutex_destroy(&thread_ipc.ul_send_mutex);
  pthread_mutex_destroy(&thread_ipc.sync_mutex);
  pthread_mutex_destroy(&thread_ipc.dl_decode_mutex);
linhuang's avatar
linhuang committed
97

98 99 100
  pthread_mutex_destroy(&thread_ipc.loop_buffer.buffer_mutex);
  pthread_cond_destroy(&thread_ipc.loop_buffer.full_cond);
  pthread_cond_destroy(&thread_ipc.loop_buffer.empty_cond);
linhuang's avatar
linhuang committed
101

102
  pthread_mutex_destroy(&thread_ipc.sync_buffer.buffer_mutex);
linhuang's avatar
linhuang committed
103

104
  return 0;
linhuang's avatar
linhuang committed
105 106 107 108
}

int set_thread_attr(pthread_attr_t *attr, int policy, int priority, int cpuid)
{
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
  struct sched_param param;
  cpu_set_t cpu_info;

  pthread_attr_init(attr);

  if (pthread_attr_setschedpolicy(attr, policy) != 0) {
    perror("pthread_attr_setschedpolicy");
    return -1;
  }

  param.sched_priority = priority;

  if (pthread_attr_setschedparam(attr, &param) != 0) {
    perror("pthread_attr_setschedparam");
    return -1;
  }

  CPU_ZERO(&cpu_info);
  CPU_SET(cpuid, &cpu_info);

  if (pthread_attr_setaffinity_np(attr,sizeof(cpu_set_t),&cpu_info)) {
    perror("pthread_attr_setaffinity_np");
    return -1;
  }

  if (pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED) != 0) {
    perror("pthread_attr_setinheritsched");
    return -1;
  }

  return 0;
linhuang's avatar
linhuang committed
140 141 142 143 144
}


int find_subframe_num(unsigned long long current_subframe_num, buffer_t *buf, int *flag)
{
145 146 147 148 149 150 151 152 153 154 155 156 157 158
  long long tmp;
  int i;

  tmp = current_subframe_num;

  for ( i = 0; i < HIGHBUFFER + 1; i++) {
    if(tmp == buf[i].subframe_num) {
      return i;
    } else if (tmp < buf[i].subframe_num) {
      *flag = 1;
    }
  }

  return -1;
linhuang's avatar
linhuang committed
159 160 161 162
}

int ue_unsync_thread_ipc_reset(void)
{
163 164 165 166 167 168 169
  thread_ipc.ue_sync_state = 0;

  pthread_mutex_lock(&thread_ipc.loop_buffer.buffer_mutex);

  if (thread_ipc.loop_buffer.isempty) {
    pthread_cond_signal(&thread_ipc.loop_buffer.empty_cond);
  }
linhuang's avatar
linhuang committed
170

171 172 173
  if (thread_ipc.loop_buffer.isfull) {
    pthread_cond_signal(&thread_ipc.loop_buffer.full_cond);
  }
linhuang's avatar
linhuang committed
174

175 176 177
  thread_ipc.loop_buffer.packet_num = 0;
  thread_ipc.loop_buffer.isfull = 0;
  thread_ipc.loop_buffer.isempty = 1;
linhuang's avatar
linhuang committed
178

179 180
  loop_buffer_reset(thread_ipc.loop_buffer.loop_buf);
  pthread_mutex_unlock(&thread_ipc.loop_buffer.buffer_mutex);
linhuang's avatar
linhuang committed
181

182
  thread_ipc.current_subframe = 0;
linhuang's avatar
linhuang committed
183

184
  return 0;
linhuang's avatar
linhuang committed
185 186 187
}
void bind_thread2kernel(int cpu_id)
{
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  cpu_set_t mask;
  cpu_set_t get;
  int i;
  int num = sysconf(_SC_NPROCESSORS_CONF);
  //printf("system has %d processor(s) by super\n", num);
  CPU_ZERO(&mask);
  CPU_SET(cpu_id, &mask);

  if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
    fprintf(stderr, "set thread affinity failed\n");
  }

  /*CPU_ZERO(&get);
  if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
          fprintf(stderr, "get thread affinity failed\n");
  }
  for (i = 0; i < num; i++) {
          if (CPU_ISSET(i, &get)) {
                  printf("thread %d is running in processor %d\n", (int)pthread_self(), i);
          }
  }
  if (CPU_ISSET(cpu_id, &get)) {
    printf("thread %d is running in processor %d by super\n", (int)pthread_self(), cpu_id);
    }*/
linhuang's avatar
linhuang committed
212 213 214
}
void get_thread2kernel(void)
{
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  cpu_set_t get;
  int i;
  int num = sysconf(_SC_NPROCESSORS_CONF);
  printf("system has %d processor(s) by super\n", num);
  CPU_ZERO(&get);

  if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
    fprintf(stderr, "get thread affinity failed\n");
  }

  for (i = 0; i < num; i++) {
    if (CPU_ISSET(i, &get)) {
      printf("The thread %d is running in processor %d by super\n", (int)pthread_self(), i);
    }
  }
linhuang's avatar
linhuang committed
230 231
}