kernel/mutex: Fix races, make unlock rescheduling
kernel/mutex: Fix races, make unlock rescheduling

The k_mutex is a priority-inheriting mutex, so on unlock it's possible
that a thread's priority will be lowered.  Make this a reschedule
point so that reasoning about thread priorities is easier (possibly at
the cost of performance): most users are going to expect that the
priority elevation stops at exactly the moment of unlock.

Note that this also reorders the code to fix what appear to be obvious
race conditions.  After the call to z_ready_thread(), that thread may
be run (e.g. by an interrupt preemption or on another SMP core), yet
the return value and mutex weren't correctly set yet.  The spinlock
was also prematurely released.

Fixes #20802

Signed-off-by: Andy Ross <c70f9a6bf6ee0cd69c8af4ce5e6b131945769315@intel.com>

Vudentz/zephyr,finikorg/zephyr,zephyrproject-rtos/zephyr,Vudentz/zephyr,galak/zephyr,finikorg/zephyr,finikorg/zephyr,zephyrproject-rtos/zephyr,nashif/zephyr,galak/zephyr,Vudentz/zephyr,nashif/zephyr,nashif/zephyr,zephyrproject-rtos/zephyr,nashif/zephyr,Vudentz/zephyr,galak/zephyr,zephyrproject-rtos/zephyr,galak/zephyr,nashif/zephyr,Vudentz/zephyr,zephyrproject-rtos/zephyr,finikorg/zephyr,Vudentz/zephyr,galak/zephyr,finikorg/zephyr