Processes raycast requests as ODE is in a state to be able to do them. This ensures that it's thread safe and there will be no conflicts. Requests get returned by a different thread then they were requested by.
        public override void PostInitialize(IConfigSource config)
        {
            m_rayCastManager = new ODERayCastRequestManager(this);
            m_config = config;
            PID_D = 2200.0f;
            PID_P = 900.0f;

            if (m_config != null)
            {
                IConfig physicsconfig = m_config.Configs["ODEPhysicsSettings"];
                if (physicsconfig != null)
                {
                    gravityx = physicsconfig.GetFloat("world_gravityx", 0f);
                    gravityy = physicsconfig.GetFloat("world_gravityy", 0f);
                    gravityz = physicsconfig.GetFloat("world_gravityz", -9.8f);
                    //Set the vectors as well
                    gravityVector = new Vector3(gravityx, gravityy, gravityz);
                    gravityVectorNormalized = gravityVector;
                    gravityVectorNormalized.Normalize();

                    AvDecayTime = physicsconfig.GetFloat("avDecayTime", AvDecayTime);

                    AllowUnderwaterPhysics = physicsconfig.GetBoolean("useUnderWaterPhysics", false);
                    AllowAvGravity = physicsconfig.GetBoolean("useAvGravity", true);
                    AvGravityHeight = physicsconfig.GetInt("avGravityHeight", 4096);
                    AllowAvsToEscapeGravity = physicsconfig.GetBoolean("aviesCanEscapeGravity", true);

                    m_AvFlySpeed = physicsconfig.GetFloat("AvFlySpeed", m_AvFlySpeed);
                    m_allowJump = physicsconfig.GetBoolean("AllowJump", m_allowJump);
                    m_usepreJump = physicsconfig.GetBoolean("UsePreJump", m_usepreJump);
                    m_preJumpTime = physicsconfig.GetInt("PreJumpTime", m_preJumpTime);
                    m_preJumpForceMultiplierX = physicsconfig.GetFloat("PreJumpMultiplierX", m_preJumpForceMultiplierX);
                    m_preJumpForceMultiplierY = physicsconfig.GetFloat("PreJumpMultiplierY", m_preJumpForceMultiplierY);
                    m_preJumpForceMultiplierZ = physicsconfig.GetFloat("PreJumpMultiplierZ", m_preJumpForceMultiplierZ);

                    contactsurfacelayer = physicsconfig.GetFloat("world_contact_surface_layer", 0.001f);

                    AvatarContactBounce = physicsconfig.GetFloat("AvatarContactBounce", AvatarContactBounce);
                    FrictionMovementMultiplier = physicsconfig.GetFloat("FrictionMovementMultiplier",
                                                                        FrictionMovementMultiplier);
                    FrictionScale = physicsconfig.GetFloat("FrictionMovementMultiplier", FrictionScale);

                    ODE_STEPSIZE = physicsconfig.GetFloat("world_stepsize", 0.020f);
                    m_physicsiterations = physicsconfig.GetInt("world_internal_steps_without_collisions", 10);

                    avDensity = physicsconfig.GetFloat("av_density", 80f);
                    avHeightFudgeFactor = physicsconfig.GetFloat("av_height_fudge_factor", 0.52f);
                    avMovementDivisorWalk = (physicsconfig.GetFloat("WalkSpeed", 1.3f)*2);
                    avMovementDivisorRun = (physicsconfig.GetFloat("RunSpeed", 0.8f)*2);
                    avCapRadius = physicsconfig.GetFloat("av_capsule_radius", 0.37f);

                    contactsPerCollision = physicsconfig.GetInt("contacts_per_collision", 80);

                    geomContactPointsStartthrottle = physicsconfig.GetInt("geom_contactpoints_start_throttling", 3);
                    geomCrossingFailuresBeforeOutofbounds =
                        physicsconfig.GetInt("geom_crossing_failures_before_outofbounds", 5);

                    bodyFramesAutoDisable = physicsconfig.GetInt("body_frames_auto_disable", 10);

                    forceSimplePrimMeshing = physicsconfig.GetBoolean("force_simple_prim_meshing",
                                                                      forceSimplePrimMeshing);
                    meshSculptedPrim = physicsconfig.GetBoolean("mesh_sculpted_prim", true);
                    meshSculptLOD = physicsconfig.GetFloat("mesh_lod", 32f);
                    MeshSculptphysicalLOD = physicsconfig.GetFloat("mesh_physical_lod", 16f);
                    m_filterCollisions = physicsconfig.GetBoolean("filter_collisions", false);

                    PID_D = physicsconfig.GetFloat("av_pid_derivative", PID_D);
                    PID_P = physicsconfig.GetFloat("av_pid_proportional", PID_P);

                    m_useFlightCeilingHeight = physicsconfig.GetBoolean("Use_Flight_Ceiling_Height_Max",
                                                                        m_useFlightCeilingHeight);
                    m_flightCeilingHeight = physicsconfig.GetFloat("Flight_Ceiling_Height_Max", m_flightCeilingHeight);
                    //Rex

                    minimumGroundFlightOffset = physicsconfig.GetFloat("minimum_ground_flight_offset", 6f);
                    maximumMassObject = physicsconfig.GetFloat("maximum_mass_object", 100000.01f);
                    DoPhyWind = physicsconfig.GetBoolean("do_physics_wind", false);
                }
            }

            // alloc unmanaged memory to receive information from colision contact joints              
            ContactgeomsArray = Marshal.AllocHGlobal(contactsPerCollision*d.ContactGeom.unmanagedSizeOf);

            // alloc unmanaged memory to pass information to colision contact joints              
            GlobalContactsArray = Marshal.AllocHGlobal(maxContactsbeforedeath*d.Contact.unmanagedSizeOf);

            //UnmanagedODE.UnmanagedODEPhysics.Initialize(world, m_currentmaxContactsbeforedeath, ContactgeomsArray, GlobalContactsArray, contactgroup);

            newGlobalcontact.surface.mode = CommumContactFlags;
            newGlobalcontact.surface.soft_cfm = 0.0001f;
            newGlobalcontact.surface.soft_erp = 0.6f;

            // Set the gravity,, don't disable things automatically (we set it explicitly on some things)
            d.WorldSetGravity(world, gravityx, gravityy, gravityz);
            d.WorldSetContactSurfaceLayer(world, contactsurfacelayer);

            d.WorldSetLinearDamping(world, 0.001f);
            d.WorldSetAngularDamping(world, 0.001f);
            d.WorldSetAngularDampingThreshold(world, 0f);
            d.WorldSetLinearDampingThreshold(world, 0f);
            d.WorldSetMaxAngularSpeed(world, 256f);

            d.WorldSetCFM(world, 1e-6f); // a bit harder than default
            d.WorldSetERP(world, 0.6f); // higher than original

            d.WorldSetContactMaxCorrectingVel(world, 30.0f);

            // Set how many steps we go without running collision testing
            // This is in addition to the step size.
            // Essentially Steps * m_physicsiterations
            d.WorldSetQuickStepNumIterations(world, m_physicsiterations);
            //d.WorldSetContactMaxCorrectingVel(world, 1000.0f);

            if (staticPrimspace != null)
                return; //Reloading config, don't mess with this stuff

            d.HashSpaceSetLevels(space, HashspaceLow, HashspaceHigh);

            //  spaces grid for static objects

            if (WorldExtents.X < WorldExtents.Y)
                // // constant is 1/log(2),  -3 for division by 8 plus 0.5 for rounding
                GridSpaceScaleBits = (int) (Math.Log(WorldExtents.X)*1.4426950f - 2.5f);
            else
                GridSpaceScaleBits = (int) (Math.Log(WorldExtents.Y)*1.4426950f - 2.5f);

            if (GridSpaceScaleBits < 4) // no less than 16m side
                GridSpaceScaleBits = 4;
            else if (GridSpaceScaleBits > 10)
                GridSpaceScaleBits = 10; // no more than 1Km side

            int nspacesPerSideX2 = (int) (WorldExtents.X) >> GridSpaceScaleBits;
            int nspacesPerSideY2 = (int) (WorldExtents.Y) >> GridSpaceScaleBits;

            if ((int) (WorldExtents.X) > nspacesPerSideX2 << GridSpaceScaleBits)
                nspacesPerSideX2++;
            if ((int) (WorldExtents.Y) > nspacesPerSideY2 << GridSpaceScaleBits)
                nspacesPerSideY2++;

            staticPrimspace = new IntPtr[nspacesPerSideX2,nspacesPerSideY2];

            IntPtr aSpace;

            for (int i = 0; i < nspacesPerSideX2; i++)
            {
                for (int j = 0; j < nspacesPerSideY2; j++)
                {
                    aSpace = d.HashSpaceCreate(space);
                    staticPrimspace[i, j] = aSpace;
                    d.GeomSetCategoryBits(aSpace, (int) CollisionCategories.Space);
                    d.HashSpaceSetLevels(aSpace, -2, 8);
                    d.SpaceSetSublevel(aSpace, 1);
                }
            }
        }
        public override void Dispose()
        {
            lock (_prims)
            {
                foreach (ODEPrim prm in _prims)
                {
                    RemovePrim(prm);
                }
            }

            //foreach (OdeCharacter act in _characters)
            //{
            //RemoveAvatar(act);
            //}

            if (ContactgeomsArray != IntPtr.Zero)
                Marshal.FreeHGlobal(ContactgeomsArray);
            if (GlobalContactsArray != IntPtr.Zero)
                Marshal.FreeHGlobal(GlobalContactsArray);

            d.WorldDestroy(world);
            //d.CloseODE();
            m_rayCastManager.Dispose();
            m_rayCastManager = null;
        }