Skip to content

Commit

Permalink
decouple _Thrd_create and _(Mtx/Cnd)_(init/destroy)
Browse files Browse the repository at this point in the history
  • Loading branch information
achabense committed Aug 14, 2023
1 parent 5e2241e commit 0bd784a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
8 changes: 0 additions & 8 deletions stl/inc/xthreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ enum { // mutex types
_Mtx_recursive = 0x100
};

#ifdef _CRTBLD
_CRTIMP2_PURE _Thrd_result __cdecl _Mtx_init(_Mtx_t*, int);
_CRTIMP2_PURE void __cdecl _Mtx_destroy(_Mtx_t);
#endif // _CRTBLD
_CRTIMP2_PURE void __cdecl _Mtx_init_in_situ(_Mtx_t, int);
_CRTIMP2_PURE void __cdecl _Mtx_destroy_in_situ(_Mtx_t);
_CRTIMP2_PURE int __cdecl _Mtx_current_owns(_Mtx_t);
Expand All @@ -117,10 +113,6 @@ void __cdecl _Smtx_unlock_exclusive(_Smtx_t*);
void __cdecl _Smtx_unlock_shared(_Smtx_t*);

// condition variables
#ifdef _CRTBLD
_CRTIMP2_PURE _Thrd_result __cdecl _Cnd_init(_Cnd_t*);
_CRTIMP2_PURE void __cdecl _Cnd_destroy(_Cnd_t);
#endif // _CRTBLD
_CRTIMP2_PURE void __cdecl _Cnd_init_in_situ(_Cnd_t);
_CRTIMP2_PURE void __cdecl _Cnd_destroy_in_situ(_Cnd_t);
_CRTIMP2_PURE _Thrd_result __cdecl _Cnd_wait(_Cnd_t, _Mtx_t); // TRANSITION, ABI: Always succeeds
Expand Down
2 changes: 2 additions & 0 deletions stl/src/cond.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ _CRTIMP2_PURE void __cdecl _Cnd_init_in_situ(const _Cnd_t cond) { // initialize

_CRTIMP2_PURE void __cdecl _Cnd_destroy_in_situ(_Cnd_t) {} // destroy condition variable in situ

// TRANSITION, ABI: preserved for binary compatibility
_CRTIMP2_PURE _Thrd_result __cdecl _Cnd_init(_Cnd_t* const pcond) { // initialize
*pcond = nullptr;

Expand All @@ -41,6 +42,7 @@ _CRTIMP2_PURE _Thrd_result __cdecl _Cnd_init(_Cnd_t* const pcond) { // initializ
return _Thrd_result::_Success;
}

// TRANSITION, ABI: preserved for binary compatibility
_CRTIMP2_PURE void __cdecl _Cnd_destroy(const _Cnd_t cond) { // clean up
if (cond) { // something to do, do it
_Cnd_destroy_in_situ(cond);
Expand Down
16 changes: 10 additions & 6 deletions stl/src/cthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,14 @@ _CRTIMP2_PURE _Thrd_result __cdecl _Thrd_create(_Thrd_t* thr, _Thrd_start_t func
_Thrd_result res;
_Thrd_binder b;
int started = 0;
_Cnd_t cond;
_Mtx_t mtx;
_Cnd_init(&cond);
_Mtx_init(&mtx, _Mtx_plain);

std::_Aligned_storage_t<_Cnd_internal_imp_size, _Cnd_internal_imp_alignment> cond_storage;
_Mtx_internal_imp_t mtx_storage{};
_Cnd_init_in_situ(reinterpret_cast<_Cnd_t>(&cond_storage));
_Mtx_init_in_situ(&mtx_storage, _Mtx_plain);

_Cnd_t cond = reinterpret_cast<_Cnd_t>(&cond_storage);
_Mtx_t mtx = &mtx_storage;
b.func = func;
b.data = d;
b.cond = &cond;
Expand All @@ -130,8 +134,8 @@ _CRTIMP2_PURE _Thrd_result __cdecl _Thrd_create(_Thrd_t* thr, _Thrd_start_t func
}
}
_Mtx_unlock(mtx);
_Cnd_destroy(cond);
_Mtx_destroy(mtx);
_Cnd_destroy_in_situ(cond);
_Mtx_destroy_in_situ(mtx);
return res;
}

Expand Down
2 changes: 2 additions & 0 deletions stl/src/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ _CRTIMP2_PURE void __cdecl _Mtx_destroy_in_situ(_Mtx_t mtx) { // destroy mutex i
(void) mtx;
}

// TRANSITION, ABI: preserved for binary compatibility
_CRTIMP2_PURE _Thrd_result __cdecl _Mtx_init(_Mtx_t* mtx, int type) { // initialize mutex
*mtx = nullptr;

Expand All @@ -65,6 +66,7 @@ _CRTIMP2_PURE _Thrd_result __cdecl _Mtx_init(_Mtx_t* mtx, int type) { // initial
return _Thrd_result::_Success;
}

// TRANSITION, ABI: preserved for binary compatibility
_CRTIMP2_PURE void __cdecl _Mtx_destroy(_Mtx_t mtx) { // destroy mutex
if (mtx) { // something to do, do it
_Mtx_destroy_in_situ(mtx);
Expand Down

0 comments on commit 0bd784a

Please sign in to comment.