Project

General

Profile

Revision 509

locking around position map

View differences:

PositionMonitor.cpp
54 54

  
55 55
  map<int, VisionPosition> newPositionMap;
56 56

  
57
  pthread_mutex_lock(&position_map_lock);
58

  
57 59
  //TODO: also remove robots that might have disappeared
58 60
  int i;
59 61
  for (i = 0; i < numPositions; i++) {
......
64 66

  
65 67
      if (isProbablySameRobot(newPos, oldPos)) {
66 68
        //TODO: is this the right use of an iterator?
67
	newPositionMap.insert(make_pair(iter->first, newPos));
69
        newPositionMap.insert(make_pair(iter->first, newPos));
68 70
        break;
69 71
      }
70 72
    }
......
87 89
  for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
88 90
    printf("%d has position (%d, %d)\n", iter->first, iter->second.x, iter->second.y);
89 91
  }
90
  
91
  if (positions)
92

  
93
  pthread_mutex_unlock(&position_map_lock);
94

  
95
  if (positions) {
92 96
    free(positions);
97
  }
93 98

  
94 99
  return 0;
95 100
}
......
97 102
int PositionMonitor::assignRealId(int old_id, int real_id) {
98 103
  printf("assigning real_id %d to old_id %d\n", real_id, old_id);
99 104

  
105
  pthread_mutex_lock(&position_map_lock);
106

  
100 107
  map<int,VisionPosition>::iterator iter = positionMap.find(old_id);
101 108

  
102 109
  if (iter == positionMap.end()) {
......
107 114
  positionMap.insert(make_pair(real_id, iter->second));
108 115
  positionMap.erase(old_id);
109 116

  
117
  pthread_mutex_unlock(&position_map_lock);
118

  
110 119
  return 0;
111 120
}
112 121

  
113 122
map<int, VisionPosition> PositionMonitor::getAllRobotPositions() {
123
  // TODO return a copy instead of the actual map for synch purposes
114 124
  return positionMap;
115 125
}
116 126

  
117 127
int PositionMonitor::getRobotPosition(int robot_id, int* xbuf, int* ybuf) {
118 128
  //TODO: figure out what a map returns if the element doesn't exist
129
  
130
  pthread_mutex_lock(&position_map_lock);
131

  
132
  
119 133
  if (positionMap.find(robot_id) == positionMap.end()){
120 134
    return -1;
121 135
  }
122 136

  
123 137
  VisionPosition pos = positionMap[robot_id];
124 138

  
139
  pthread_mutex_unlock(&position_map_lock);
140

  
125 141
  *xbuf = pos.x;
126 142
  *ybuf = pos.y;
127 143

  

Also available in: Unified diff