Beta8397 / virtual_robot

A 2D robot simulator to help beginners learn Java programming for FTC Robotics
118 stars 182 forks source link

Deadlock issue involving VirtualBot, BNO055IMUImpl, and GyroSensorImpl classes #59

Closed jkenney2 closed 3 years ago

jkenney2 commented 3 years ago

After INIT pressed, BNO055IMUImpl.initialize(), which is synchronized, gets called from the op mode thread. This method calls VirtualBot.getHeadingRadians(), which was also synchronized. So the op mode thread owns the BNO055IMUImpl lock, and must obtain the VirtualBot lock before it can proceed.

At the same time VirtualBot.updateStateAndSensors(), which is synchronized, is being called repeatedly by the game loop thread. This calls BNO055IMUImpl.updateHeadingRadians(), which is also synchronized. So the game loop thread owns the VirtualBot lock, and mus obtain the BNO055IMU lock before it can proceed.

The above can cause a deadlock.

Solution: make VirtualBot x, y, and headingRadians fields volatile, and remove the 'synchronized' keyword from their getter methods.