winpthreads: mutex.c: move initialization logic to `pthread_mutex_init`

Move initialization logic to `pthread_mutex_init`, where it belongs,
and remove static function `mutex_impl_init`.

Update `mutex_impl` function to call `pthread_mutex_init` instead of removed
`mutex_impl_init`.

Before this change, initialization of `pthread_mutex_t` objects was handled
as follows:

1. Function `pthread_mutex_init` did not perform actual initialization,
  but simply stored a static initializer in the `pthread_mutex_t` object.

2. Later, when such statically initialized mutex was about to be used,
  `mutex_impl` would call `mutex_impl_init` to allocate `mutex_impl_t`
  structure and store its address in `pthread_mutex_t` object.

3. In `pthread_mutex_lock_intern`, which is the implementation for public
  `pthread_mutex_*lock` functions, it would create event object and store
  it in `mutex_impl_t` structure.

This means that even after successful call to `pthread_mutex_init`,
initialized mutex still may be unusable:

- Step 2 may fail to allocate `mutex_impl_t` structure due to out-of-memory
  conditions.
- Step 3 may fail to create event object.
- In step 3, multiple threads may create an event object at the same time,
  potentially exhausting resources available to the process.

After this change, function `pthread_mutex_init` creates fully initialized
mutex which does not require additional initialization steps later.

Signed-off-by: Kirill Makurin <maiddaisuki@outlook.com>
Signed-off-by: LIU Hao <lh_mouse@126.com>
1 file changed